-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
this command is basically `vault kv patch` but it's nice to have inside vsh when in interactive mode. you can simply add a key/value to a path. this can save you time if you're doing other operations inside vsh. i was also able to eliminate another duplication when creating new commands by making fetching the Command object from the Commands struct dynamic.
- Loading branch information
Matt Kulka
committed
Feb 21, 2021
1 parent
af2a284
commit 9e4a1ed
Showing
8 changed files
with
195 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/fishi0x01/vsh/client" | ||
"github.com/fishi0x01/vsh/log" | ||
) | ||
|
||
// AddCommand container for all 'append' parameters | ||
type AddCommand struct { | ||
name string | ||
args *AddCommandArgs | ||
|
||
client *client.Client | ||
} | ||
|
||
// AddCommandArgs provides a struct for go-arg parsing | ||
type AddCommandArgs struct { | ||
Key string `arg:"positional,required"` | ||
Value string `arg:"positional,required"` | ||
Path string `arg:"positional,required"` | ||
Force bool `arg:"-f,--force" help:"Overwrite key if exists"` | ||
} | ||
|
||
// Description provides detail on what the command does | ||
func (AddCommandArgs) Description() string { | ||
return "adds a key with value to a path" | ||
} | ||
|
||
// NewAddCommand creates a new AddCommand parameter container | ||
func NewAddCommand(c *client.Client) *AddCommand { | ||
return &AddCommand{ | ||
name: "add", | ||
client: c, | ||
args: &AddCommandArgs{}, | ||
} | ||
} | ||
|
||
// GetName returns the AddCommand's name identifier | ||
func (cmd *AddCommand) GetName() string { | ||
return cmd.name | ||
} | ||
|
||
// GetArgs provides the struct holding arguments for the command | ||
func (cmd *AddCommand) GetArgs() interface{} { | ||
return cmd.args | ||
} | ||
|
||
// IsSane returns true if command is sane | ||
func (cmd *AddCommand) IsSane() bool { | ||
return cmd.args.Key != "" && cmd.args.Value != "" && cmd.args.Path != "" | ||
} | ||
|
||
// PrintUsage print command usage | ||
func (cmd *AddCommand) PrintUsage() { | ||
fmt.Println(Help(cmd)) | ||
} | ||
|
||
// Parse parses the arguments into the Command and Args structs | ||
func (cmd *AddCommand) Parse(args []string) error { | ||
_, err := parseCommandArgs(args, cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Run executes 'add' with given AddCommand's parameters | ||
func (cmd *AddCommand) Run() int { | ||
path := cmdPath(cmd.client.Pwd, cmd.args.Path) | ||
|
||
pathType := cmd.client.GetType(path) | ||
if pathType != client.LEAF { | ||
log.UserError("Not a valid path for operation: %s", path) | ||
return 1 | ||
} | ||
|
||
err := cmd.addKeyValue(cmd.args.Path, cmd.args.Key, cmd.args.Value) | ||
if err != nil { | ||
log.UserError("Add failed: " + err.Error()) | ||
return 1 | ||
} | ||
|
||
return 0 | ||
} | ||
|
||
func (cmd *AddCommand) addKeyValue(path string, key string, value string) error { | ||
secret, err := cmd.client.Read(path) | ||
if err != nil { | ||
return fmt.Errorf("Read failed: %s", err) | ||
} | ||
data := secret.GetData() | ||
if _, ok := data[key]; ok && !cmd.args.Force { | ||
return fmt.Errorf("Key already exists at path: %s", path) | ||
} | ||
data[key] = value | ||
secret.SetData(data) | ||
return cmd.client.Write(path, secret) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# add | ||
|
||
```text | ||
add [-f|--force] KEY VALUE PATH | ||
``` | ||
|
||
Add operation adds or overwrites a single key at path. | ||
|
||
By default, it will add the key if it does not already exist. To overwrite the key if it exists, use the `--force` flag. | ||
|
||
```bash | ||
> cat secret/path | ||
|
||
value = 1 | ||
other = thing | ||
|
||
> add fizz buzz secret/path | ||
> cat /secret/to | ||
|
||
value = 1 | ||
fizz = buzz | ||
other = thing | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
load ../../util/common | ||
load ../../util/standard-setup | ||
load ../../bin/plugins/bats-support/load | ||
load ../../bin/plugins/bats-assert/load | ||
|
||
@test "vault-${VAULT_VERSION} ${KV_BACKEND} 'add'" { | ||
####################################### | ||
echo "==== case: add value to non existing path ====" | ||
run ${APP_BIN} -c "add test value ${KV_BACKEND}/fake/path" | ||
assert_failure | ||
|
||
####################################### | ||
echo "==== case: add key to existing path ====" | ||
run ${APP_BIN} -c "add test value ${KV_BACKEND}/src/a/foo" | ||
assert_success | ||
|
||
echo "ensure the key was written to destination" | ||
run get_vault_value "test" "${KV_BACKEND}/src/a/foo" | ||
assert_success | ||
assert_output "value" | ||
|
||
####################################### | ||
echo "==== case: add existing key to existing path ====" | ||
run ${APP_BIN} -c "add value another ${KV_BACKEND}/src/a/foo" | ||
assert_failure | ||
assert_output --partial "Key already exists at path: ${KV_BACKEND}/src/a/foo" | ||
|
||
####################################### | ||
echo "==== case: overwrite existing key to existing path ====" | ||
run ${APP_BIN} -c "add -f value another ${KV_BACKEND}/src/a/foo" | ||
assert_success | ||
|
||
echo "ensure the key was written to destination" | ||
run get_vault_value "value" "${KV_BACKEND}/src/a/foo" | ||
assert_success | ||
assert_output "another" | ||
} |