-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
79 lines (66 loc) · 1.62 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"errors"
"flag"
"fmt"
"os"
"strings"
"github.com/enrichman/ringo/biscuit"
"github.com/manifoldco/promptui"
)
var (
Version = "v0.0.0-dev"
secretsFilename string
)
func main() {
showVersion := flag.Bool("version", false, "show ringo version")
flag.StringVar(&secretsFilename, "filename", "config/secrets.yml", "the file containing the secrets to decrypt")
flag.Parse()
if *showVersion {
fmt.Print(Version)
os.Exit(0)
}
if _, err := os.Open(secretsFilename); err != nil {
if errors.Is(err, os.ErrNotExist) {
fmt.Println("'config/secrets.yml' file not found!\nYou can use the -filename flag for a different path")
return
}
fmt.Print(err)
os.Exit(1)
}
if err := biscuit.KmsCallerIdentity(); err != nil {
fmt.Print(err)
os.Exit(1)
}
secrets, err := biscuit.List(secretsFilename)
if err != nil {
fmt.Print(err)
os.Exit(1)
}
selectedSecret, err := loadSecretsSelection(secrets)
if err != nil {
// this happens when you ^C the prompt
return
}
fmt.Printf("Decrypting %q\n", selectedSecret)
res, err := biscuit.Get(secretsFilename, selectedSecret)
if err != nil {
fmt.Print(err)
os.Exit(1)
}
fmt.Println(res)
}
func loadSecretsSelection(secrets []string) (string, error) {
prompt := promptui.Select{
Label: "Select a secret to decrypt",
Items: secrets,
Size: 10,
Searcher: func(input string, index int) bool {
name := strings.Replace(strings.ToLower(secrets[index]), " ", "", -1)
input = strings.Replace(strings.ToLower(input), " ", "", -1)
return strings.Contains(name, input)
},
}
_, result, err := prompt.Run()
return result, err
}