-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
140 lines (125 loc) · 2.87 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"bufio"
"crypto/sha256"
"fmt"
"log"
"net/url"
"os"
"os/user"
"path/filepath"
"github.com/krysopath/vtoh/backends"
"gopkg.in/yaml.v2"
)
func getUser() *user.User {
usr, err := user.Current()
if err != nil {
log.Fatal(err)
}
return usr
}
func hash(input string) string {
hash := sha256.New()
hash.Write([]byte(input))
return fmt.Sprintf("%x", hash.Sum(nil))
}
var (
User = getUser()
StoreFilePath string = filepath.Join(
User.HomeDir, ".vault-tokens",
)
s3Object string = fmt.Sprintf(
"%s/%s",
"tf-stage-internal-vault-storage",
".vault-tokens",
)
VAULT_ADDR string = os.Getenv("VAULT_ADDR")
VAULT_TOKEN_SRC string = os.Getenv("VAULT_TOKEN_SRC")
hashedAddr string = hash(VAULT_ADDR)
gitRef string = ""
gitTag string = ""
)
type Backend interface {
Load() ([]byte, error)
Save(interface{}) (bool, error)
}
type TokenStore struct {
Backend Backend `yaml:"backend"`
DataSourceURI string `yaml:"source_uri"`
Data map[string]interface{} `yaml:"data"`
}
func (ts *TokenStore) Init() {
source, err := url.Parse(ts.DataSourceURI)
if err != nil {
panic(err)
}
var backend Backend
switch source.Scheme {
case "file":
backend = backends.FileBackend{FilePath: source.Path}
case "gpg":
q := source.Query()
backend = backends.GpgBackend{
FilePath: source.Path,
Recipients: q["recipients"],
//KeyRingHome: "/home/gve/src/token-helper/gnupg"}
KeyRingHome: filepath.Join(User.HomeDir, ".token-helper")}
case "s3":
backend = backends.S3Backend{
Bucket: source.Host,
//Region: "eu-central-1",
Path: source.Path}
default:
}
ts.Backend = backend
content, _ := ts.Backend.Load()
yaml.Unmarshal(content, &ts.Data)
}
func config(data interface{}) {
configBytes, _ := yaml.Marshal(data)
fmt.Fprintf(os.Stderr, `%s`, configBytes)
}
func usage() {
fmt.Fprintf(os.Stderr,
`%s %s-%s
important: this token helper is not meant to be executed directly
supported commands: get, store, erase & config
where only config is intended to be invoked interactively
`, os.Args[0], gitTag, gitRef,
)
}
func main() {
if len(VAULT_ADDR) == 0 {
fmt.Fprintln(os.Stderr, "err: VAULT_ADDR is unset")
os.Exit(100)
}
if len(VAULT_TOKEN_SRC) == 0 {
fmt.Fprintln(os.Stderr, "err: VAULT_TOKEN_SRC is unset")
os.Exit(101)
}
if len(os.Args) >= 2 {
store := &TokenStore{
DataSourceURI: VAULT_TOKEN_SRC,
}
store.Init()
switch os.Args[1] {
case "get":
token, _ := store.Data[hashedAddr].(string)
fmt.Fprintf(os.Stdout, "%s", token)
case "store":
reader := bufio.NewReader(os.Stdin)
token, _ := reader.ReadString('\n')
store.Data[hashedAddr] = token
case "erase":
delete(store.Data, hashedAddr)
case "config":
config(store)
default:
usage()
os.Exit(1)
}
store.Backend.Save(store.Data)
} else {
usage()
}
}