Skip to content

Commit 2e20213

Browse files
committed
added caller identity check
added search small refactor
1 parent 9cb1371 commit 2e20213

File tree

4 files changed

+47
-26
lines changed

4 files changed

+47
-26
lines changed

biscuit/biscuit.go

+21-19
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,43 @@ package biscuit
22

33
import (
44
"bytes"
5-
"log"
6-
"os"
75
"os/exec"
6+
"sort"
87
"strings"
8+
9+
"github.com/pkg/errors"
910
)
1011

11-
type Client struct {
12-
filename string
12+
func KmsCallerIdentity() error {
13+
cmd := exec.Command("biscuit", "kms", "get-caller-identity")
14+
_, err := handleCommand(cmd)
15+
return err
1316
}
1417

15-
func NewClient(filename string) (*Client, error) {
16-
if _, err := os.Open(filename); err != nil {
18+
func List(filename string) ([]string, error) {
19+
out, err := handleCommand(exec.Command("biscuit", "list", "-f", filename))
20+
if err != nil {
1721
return nil, err
1822
}
19-
return &Client{filename: filename}, nil
20-
}
2123

22-
func (c *Client) List() ([]string, error) {
23-
out, err := exec.Command("biscuit", "list", "-f", c.filename).Output()
24-
if err != nil {
25-
log.Fatal(err)
26-
}
24+
secrets := strings.Split(out, "\n")
25+
sort.Strings(secrets)
26+
27+
return secrets, nil
28+
}
2729

28-
return strings.Split(strings.TrimSpace(string(out)), "\n"), nil
30+
func Get(filename, secret string) (string, error) {
31+
cmd := exec.Command("biscuit", "get", "-f", filename, secret)
32+
return handleCommand(cmd)
2933
}
3034

31-
func (c *Client) Get(secret string) (string, error) {
32-
cmd := exec.Command("biscuit", "get", "-f", c.filename, secret)
35+
func handleCommand(cmd *exec.Cmd) (string, error) {
3336
var stderr bytes.Buffer
3437
cmd.Stderr = &stderr
3538

3639
out, err := cmd.Output()
3740
if err != nil {
38-
return stderr.String(), err
41+
return "", errors.New(stderr.String())
3942
}
40-
41-
return string(out), nil
43+
return strings.TrimSpace(string(out)), nil
4244
}

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ go 1.15
55
require (
66
github.com/manifoldco/promptui v0.8.0
77
github.com/mitchellh/gox v1.0.1 // indirect
8+
github.com/pkg/errors v0.9.1
89
)

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@ github.com/mitchellh/gox v1.0.1 h1:x0jD3dcHk9a9xPSDN6YEL4xL6Qz0dvNYm8yZqui5chI=
2121
github.com/mitchellh/gox v1.0.1/go.mod h1:ED6BioOGXMswlXa2zxfh/xdd5QhwYliBFn9V18Ap4z4=
2222
github.com/mitchellh/iochan v1.0.0 h1:C+X3KsSTLFVBr/tK1eYN/vs4rJcvsiLU338UhYPJWeY=
2323
github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY=
24+
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
25+
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
2426
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
2527
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

main.go

+23-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"flag"
66
"fmt"
77
"os"
8+
"strings"
89

910
"github.com/enrichman/ringo/biscuit"
1011
"github.com/manifoldco/promptui"
@@ -16,34 +17,49 @@ func main() {
1617
flag.StringVar(&secretsFilename, "filename", "config/secrets.yml", "the file containing the secrets to decrypt")
1718
flag.Parse()
1819

19-
biscuit, err := biscuit.NewClient(secretsFilename)
20-
if err != nil {
20+
if _, err := os.Open(secretsFilename); err != nil {
2121
if errors.Is(err, os.ErrNotExist) {
2222
fmt.Println("'config/secrets.yml' file not found!\nYou can use the -filename flag for a different path")
2323
return
2424
}
2525
panic(err)
2626
}
2727

28-
list, err := biscuit.List()
28+
if err := biscuit.KmsCallerIdentity(); err != nil {
29+
fmt.Print(err)
30+
os.Exit(1)
31+
}
32+
33+
list, err := biscuit.List(secretsFilename)
2934
if err != nil {
30-
panic(err)
35+
fmt.Print(err)
36+
os.Exit(1)
3137
}
3238

3339
prompt := promptui.Select{
3440
Label: "Select a secret to decrypt",
3541
Items: list,
42+
Size: 10,
43+
Searcher: func(input string, index int) bool {
44+
name := strings.Replace(strings.ToLower(list[index]), " ", "", -1)
45+
input = strings.Replace(strings.ToLower(input), " ", "", -1)
46+
return strings.Contains(name, input)
47+
},
3648
}
49+
3750
_, result, err := prompt.Run()
3851
if err != nil {
52+
// this happens when you ^C the prompt
3953
return
4054
}
4155

4256
fmt.Printf("Decrypting %q\n", result)
43-
res, err := biscuit.Get(result)
57+
58+
res, err := biscuit.Get(secretsFilename, result)
4459
if err != nil {
45-
fmt.Println(res)
46-
return
60+
fmt.Print(err)
61+
os.Exit(1)
4762
}
63+
4864
fmt.Println(res)
4965
}

0 commit comments

Comments
 (0)