diff --git a/command/kv_put.go b/command/kv_put.go index f5e1fa5de2..98c70b00c5 100644 --- a/command/kv_put.go +++ b/command/kv_put.go @@ -2,6 +2,7 @@ package command import ( "bytes" + "encoding/base64" "flag" "fmt" "io" @@ -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: @@ -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. @@ -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, "") @@ -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)") @@ -137,7 +153,7 @@ func (c *KVPutCommand) Run(args []string) int { Key: key, ModifyIndex: *modifyIndex, Flags: *flags, - Value: []byte(data), + Value: dataBytes, Session: *session, } diff --git a/command/kv_put_test.go b/command/kv_put_test.go index 5b84a68b38..3735652c80 100644 --- a/command/kv_put_test.go +++ b/command/kv_put_test.go @@ -2,6 +2,7 @@ package command import ( "bytes" + "encoding/base64" "io" "io/ioutil" "os" @@ -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() diff --git a/website/source/docs/commands/kv/put.html.markdown.erb b/website/source/docs/commands/kv/put.html.markdown.erb index 5719f15677..562f75835a 100644 --- a/website/source/docs/commands/kv/put.html.markdown.erb +++ b/website/source/docs/commands/kv/put.html.markdown.erb @@ -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. @@ -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.