Skip to content

Commit

Permalink
Add proper error return codes
Browse files Browse the repository at this point in the history
  • Loading branch information
fishi0x01 committed Oct 2, 2020
1 parent 2579ad8 commit 9d2c750
Show file tree
Hide file tree
Showing 23 changed files with 164 additions and 124 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v0.7.1 (TBD)

BUG FIXES:

* Proper return codes [#51](https://github.com/fishi0x01/vsh/pull/51)

## v0.7.0 (September 26, 2020)

ENHANCEMENTS:
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ cross-compile: clean
ls build/

compile: clean
go build -ldflags "-X main.vshVersion=$(VERSION)" -o build/${APP_NAME}_linux_amd64
go build -ldflags "-X main.vshVersion=$(VERSION)" -o build/${APP_NAME}_$(shell uname | tr '[:upper:]' '[:lower:]')_amd64

get-bats:
rm -rf test/bin/
Expand Down
26 changes: 12 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ vegetable=tomato
fruit=pear
tree=oak

> append /secret/from /secret/to
> append /secret/from /secret/to -s

> cat /secret/to

Expand All @@ -74,7 +74,7 @@ vegetable=tomato
fruit=pear
tree=oak

> append /secret/from /secret/to
> append /secret/from /secret/to -f

> cat /secret/to

Expand All @@ -96,7 +96,7 @@ vegetable=tomato
fruit=pear
tree=oak

> append /secret/from /secret/to
> append /secret/from /secret/to -r

> cat /secret/to

Expand All @@ -108,8 +108,7 @@ tree=oak

### grep

`grep` recursively searches the given term in key and value pairs and does not support regex.
[SSOT](https://en.wikipedia.org/wiki/Single_source_of_truth) is very much desired, however, in praxis it is probably not always applied consistently.
`grep` recursively searches the given term in key and value pairs. It does not support regex.
If you are looking for copies or just trying to find the path to a certain term, this command might come in handy.

## Setting the vault token
Expand All @@ -123,7 +122,7 @@ That means `vsh` supports setting vault tokens via `~/.vault-token`, `VAULT_TOKE
Ideally, the vault token used by `vsh` has `list` permissions on `sys/mount`.
If this is not the case, then `vsh` does not know the available backends beforehand.
That means initially there won't be path auto-completion on the top (backend) level.
However, `vsh` will try with best effort to reliably determine the kv version of every entered path.
However, `vsh` will try with best-effort strategy to reliably determine the kv version of every entered path.

## Interactive mode

Expand Down Expand Up @@ -174,10 +173,13 @@ Further, it is needed to gather auto-completion data.

For operations like `cp` or `mv`, `vsh` additionally requires `Read` and `Write` permissions on the operated paths.

## Stability
## Quality

Every command has integration tests against KV1 and KV2.
Every test is run against vault `1.0.0` and `1.5.3`, i.e., versions in between should also be compatible.
Working on vault secrets can be critical, making quality and correct behavior a first class citizen for `vsh`.
That being said, `vsh` is still a small open source project, meaning we cannot make any guarantees.
However, we put strong emphasis on [TDD](https://en.wikipedia.org/wiki/Test-driven_development).
Every PR is tested with an extensive [suite](test/suites) of integration tests.
Most tests run on KV1 and KV2 and every test runs against vault `1.0.0` and `1.5.3`, i.e., versions in between should also be compatible.

## Local Development

Expand All @@ -188,10 +190,6 @@ Requirements:

```
make compile
make get-bats
make integration-tests
```

## Potential TODOs

- `tree` command
- currently `mv` and `cp` behave a little different from UNIX. `mv /secret/source/a /secret/target/` should yield `/secret/target/a`
14 changes: 8 additions & 6 deletions cli/append.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,29 +109,31 @@ func (cmd *AppendCommand) tryParse(args []string) (success bool) {
}

// Parse parses the arguments and returns true on success; otherwise it prints usage and returns false
func (cmd *AppendCommand) Parse(args []string) (success bool) {
success = cmd.tryParse(args)
func (cmd *AppendCommand) Parse(args []string) error {
success := cmd.tryParse(args)
if !success {
printUsage()
return fmt.Errorf("cannot parse arguments")
}
return success
return nil
}

// Run executes 'append' with given AppendCommand's parameters
func (cmd *AppendCommand) Run() {
func (cmd *AppendCommand) Run() int {
newSrcPwd := cmdPath(cmd.client.Pwd, cmd.Source)
newTargetPwd := cmdPath(cmd.client.Pwd, cmd.Target)

src := cmd.client.GetType(newSrcPwd)
if src != client.LEAF {
log.NotAValidPath(newSrcPwd)
return
return 1
}

if err := cmd.mergeSecrets(newSrcPwd, newTargetPwd); err != nil {
log.Error("Append failed: " + err.Error())
return 1
}
return
return 0
}

func (cmd *AppendCommand) createDummySecret(target string) error {
Expand Down
18 changes: 9 additions & 9 deletions cli/cat.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,24 @@ func (cmd *CatCommand) IsSane() bool {
}

// Parse given arguments and return status
func (cmd *CatCommand) Parse(args []string) (success bool) {
if len(args) == 2 {
cmd.Path = args[1]
success = true
} else {
func (cmd *CatCommand) Parse(args []string) error {
if len(args) != 2 {
fmt.Println("Usage:\ncat <secret>")
return fmt.Errorf("cannot parse arguments")
}
return success
cmd.Path = args[1]
return nil
}

// Run executes 'cat' with given CatCommand's parameters
func (cmd *CatCommand) Run() {
func (cmd *CatCommand) Run() int {
absPath := cmdPath(cmd.client.Pwd, cmd.Path)
t := cmd.client.GetType(absPath)

if t == client.LEAF {
secret, err := cmd.client.Read(absPath)
if err != nil {
return
return 1
}

for k, v := range secret.Data {
Expand All @@ -73,6 +72,7 @@ func (cmd *CatCommand) Run() {
}
} else {
log.NotAValidPath(absPath)
return 1
}
return
return 0
}
19 changes: 9 additions & 10 deletions cli/cd.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,35 +40,34 @@ func (cmd *CdCommand) IsSane() bool {
}

// Parse given arguments and return status
func (cmd *CdCommand) Parse(args []string) (success bool) {
if len(args) == 2 {
cmd.Path = args[1]
success = true
} else {
func (cmd *CdCommand) Parse(args []string) error {
if len(args) != 2 {
fmt.Println("Usage:\ncd <path>")
return fmt.Errorf("cannot parse arguments")
}
return success
cmd.Path = args[1]
return nil
}

// Run executes 'cd' with given CdCommand's parameters
func (cmd *CdCommand) Run() {
func (cmd *CdCommand) Run() int {
newPwd := cmdPath(cmd.client.Pwd, cmd.Path)

t := cmd.client.GetType(newPwd)

if t == client.NONE {
log.NotAValidPath(newPwd)
return
return 1
}

if t == client.LEAF {
log.NotAValidPath(newPwd)
return
return 1
}

if !strings.HasSuffix(newPwd, "/") {
newPwd = newPwd + "/"
}
cmd.client.Pwd = newPwd
return
return 0
}
4 changes: 2 additions & 2 deletions cli/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (

// Command interface to describe a command structure
type Command interface {
Run()
Run() int
GetName() string
IsSane() bool
Parse(args []string) bool
Parse(args []string) error
}

func cmdPath(pwd string, arg string) (result string) {
Expand Down
18 changes: 10 additions & 8 deletions cli/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,18 @@ func (cmd *CopyCommand) IsSane() bool {
}

// Parse given arguments and return status
func (cmd *CopyCommand) Parse(args []string) (success bool) {
if len(args) == 3 {
cmd.Source = args[1]
cmd.Target = args[2]
success = true
} else {
func (cmd *CopyCommand) Parse(args []string) error {
if len(args) != 3 {
fmt.Println("Usage:\ncp <from> <to>")
return fmt.Errorf("cannot parse arguments")
}
return success
cmd.Source = args[1]
cmd.Target = args[2]
return nil
}

// Run executes 'cp' with given CopyCommand's parameters
func (cmd *CopyCommand) Run() {
func (cmd *CopyCommand) Run() int {
newSrcPwd := cmdPath(cmd.client.Pwd, cmd.Source)
newTargetPwd := cmdPath(cmd.client.Pwd, cmd.Target)

Expand All @@ -64,7 +63,10 @@ func (cmd *CopyCommand) Run() {
runCommandWithTraverseTwoPaths(cmd.client, newSrcPwd, newTargetPwd, cmd.copySecret)
default:
log.NotAValidPath(newSrcPwd)
return 1
}

return 0
}

func (cmd *CopyCommand) copySecret(source string, target string) error {
Expand Down
21 changes: 10 additions & 11 deletions cli/grep.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,18 @@ func (cmd *GrepCommand) IsSane() bool {
}

// Parse given arguments and return status
func (cmd *GrepCommand) Parse(args []string) (success bool) {
if len(args) == 3 {
cmd.Search = args[1]
cmd.Path = args[2]
success = true
} else {
func (cmd *GrepCommand) Parse(args []string) error {
if len(args) != 3 {
fmt.Println("Usage:\ngrep <term-string> <path>")
return fmt.Errorf("cannot parse arguments")
}
return success
cmd.Search = args[1]
cmd.Path = args[2]
return nil
}

// Run executes 'grep' with given RemoveCommand's parameters
func (cmd *GrepCommand) Run() {
func (cmd *GrepCommand) Run() int {
path := cmdPath(cmd.client.Pwd, cmd.Path)
filePaths := []string{}

Expand All @@ -80,19 +79,19 @@ func (cmd *GrepCommand) Run() {
}
default:
log.NotAValidPath(path)
return
return 1
}

for _, curPath := range filePaths {
matches, err := cmd.grepFile(cmd.Search, curPath)
if err != nil {
return
return 1
}
for _, match := range matches {
match.print(cmd.stdout)
}
}
return
return 0
}

func (cmd *GrepCommand) grepFile(search string, path string) (matches []*Match, err error) {
Expand Down
16 changes: 7 additions & 9 deletions cli/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,27 @@ func (cmd *ListCommand) IsSane() bool {
}

// Parse given arguments and return status
func (cmd *ListCommand) Parse(args []string) (success bool) {
func (cmd *ListCommand) Parse(args []string) error {
if len(args) == 2 {
cmd.Path = args[1]
success = true
} else if len(args) == 1 {
cmd.Path = cmd.client.Pwd
success = true
} else {
fmt.Println("Usage:\nls <path // optional>")
return fmt.Errorf("cannot parse arguments")
}
return success
return nil
}

// Run executes 'ls' with given ListCommand's parameters
func (cmd *ListCommand) Run() {
func (cmd *ListCommand) Run() int {
newPwd := cmdPath(cmd.client.Pwd, cmd.Path)
result, err := cmd.client.List(newPwd)

if err != nil {
log.NotAValidPath(newPwd)
} else {
fmt.Fprintln(cmd.stdout, strings.Join(result, "\n"))
return 1
}

return
fmt.Fprintln(cmd.stdout, strings.Join(result, "\n"))
return 0
}
19 changes: 10 additions & 9 deletions cli/mv.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,18 @@ func (cmd *MoveCommand) IsSane() bool {
}

// Parse given arguments and return status
func (cmd *MoveCommand) Parse(args []string) (success bool) {
if len(args) == 3 {
cmd.Source = args[1]
cmd.Target = args[2]
success = true
} else {
func (cmd *MoveCommand) Parse(args []string) error {
if len(args) != 3 {
fmt.Println("Usage:\nmv <from> <to>")
return fmt.Errorf("cannot parse arguments")
}
return success
cmd.Source = args[1]
cmd.Target = args[2]
return nil
}

// Run executes 'mv' with given MoveCommand's parameters
func (cmd *MoveCommand) Run() {
func (cmd *MoveCommand) Run() int {
newSrcPwd := cmdPath(cmd.client.Pwd, cmd.Source)
newTargetPwd := cmdPath(cmd.client.Pwd, cmd.Target)

Expand All @@ -64,8 +63,10 @@ func (cmd *MoveCommand) Run() {
runCommandWithTraverseTwoPaths(cmd.client, newSrcPwd, newTargetPwd, cmd.moveSecret)
default:
log.NotAValidPath(newSrcPwd)
return 1
}
return

return 0
}

func (cmd *MoveCommand) moveSecret(source string, target string) error {
Expand Down
Loading

0 comments on commit 9d2c750

Please sign in to comment.