Skip to content

Commit

Permalink
cli: Add -base64 option to consul kv put
Browse files Browse the repository at this point in the history
This commit adds a -base64 option to the consul kv put command, which
base 64 decodeds the data prior to writing it. This can be used in
conjunction with `consul kv get -base64 key`.
  • Loading branch information
jen20 committed Jan 4, 2017
1 parent 5334649 commit 2e8f440
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
18 changes: 17 additions & 1 deletion command/kv_put.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package command

import (
"bytes"
"encoding/base64"
"flag"
"fmt"
"io"
Expand Down Expand Up @@ -45,6 +46,9 @@ Usage: consul kv put [options] KEY [DATA]
$ consul kv put webapp/beta/active
If the -base64 flag is specified, the data will be treated as base 64
encoded.
To perform a Check-And-Set operation, specify the -cas flag with the
appropriate -modify-index flag corresponding to the key you want to perform
the CAS operation on:
Expand All @@ -62,6 +66,9 @@ KV Put Options:
lock. The session must already exist and be specified
via the -session flag. The default value is false.
-base64 Treat the data as base 64 encoded. The default value
is false.
-cas Perform a Check-And-Set operation. Specifying this
value also requires the -modify-index flag to be set.
The default value is false.
Expand Down Expand Up @@ -95,6 +102,7 @@ func (c *KVPutCommand) Run(args []string) int {
token := cmdFlags.String("token", "", "")
cas := cmdFlags.Bool("cas", false, "")
flags := cmdFlags.Uint64("flags", 0, "")
base64encoded := cmdFlags.Bool("base64", false, "")
modifyIndex := cmdFlags.Uint64("modify-index", 0, "")
session := cmdFlags.String("session", "", "")
acquire := cmdFlags.Bool("acquire", false, "")
Expand All @@ -111,6 +119,14 @@ func (c *KVPutCommand) Run(args []string) int {
return 1
}

dataBytes := []byte(data)
if *base64encoded {
dataBytes, err = base64.StdEncoding.DecodeString(data)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error! Cannot base 64 decode data: %s", err))
}
}

// Session is reauired for release or acquire
if (*release || *acquire) && *session == "" {
c.Ui.Error("Error! Missing -session (required with -acquire and -release)")
Expand All @@ -137,7 +153,7 @@ func (c *KVPutCommand) Run(args []string) int {
Key: key,
ModifyIndex: *modifyIndex,
Flags: *flags,
Value: []byte(data),
Value: dataBytes,
Session: *session,
}

Expand Down
37 changes: 37 additions & 0 deletions command/kv_put_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package command

import (
"bytes"
"encoding/base64"
"io"
"io/ioutil"
"os"
Expand Down Expand Up @@ -100,6 +101,42 @@ func TestKVPutCommand_Run(t *testing.T) {
}
}

func TestKVPutCommand_RunBase64(t *testing.T) {
srv, client := testAgentWithAPIClient(t)
defer srv.Shutdown()
waitForLeader(t, srv.httpAddr)

ui := new(cli.MockUi)
c := &KVPutCommand{Ui: ui}

const encodedString = "aGVsbG8gd29ybGQK"

args := []string{
"-http-addr=" + srv.httpAddr,
"-base64",
"foo", encodedString,
}

code := c.Run(args)
if code != 0 {
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
}

data, _, err := client.KV().Get("foo", nil)
if err != nil {
t.Fatal(err)
}

expected, err := base64.StdEncoding.DecodeString(encodedString)
if err != nil {
t.Fatal(err)
}

if !bytes.Equal(data.Value, []byte(expected)) {
t.Errorf("bad: %#v, %s", data.Value, data.Value)
}
}

func TestKVPutCommand_File(t *testing.T) {
srv, client := testAgentWithAPIClient(t)
defer srv.Shutdown()
Expand Down
9 changes: 9 additions & 0 deletions website/source/docs/commands/kv/put.html.markdown.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Usage: `consul kv put [options] KEY [DATA]`
operation will create the key and obtain the lock. The session must already
exist and be specified via the -session flag. The default value is false.

* `-base64` - Treat the data as base 64 encoded. The default value is false.

* `-cas` - Perform a Check-And-Set operation. Specifying this value also
requires the -modify-index flag to be set. The default value is false.

Expand Down Expand Up @@ -60,6 +62,13 @@ $ consul kv put redis/config/connections
Success! Data written to: redis/config/connections
```

If the `-base64` flag is set, the data will be decoded before writing:

```
$ consul kv put -base64 foo/encoded aGVsbG8gd29ybGQK
Success! Data written to: foo/encoded
```

!> **Be careful when overwriting data!** The above operation would overwrite
the value at the key to the empty value.

Expand Down

0 comments on commit 2e8f440

Please sign in to comment.