-
Notifications
You must be signed in to change notification settings - Fork 1
/
vault.go
173 lines (138 loc) · 3.59 KB
/
vault.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"fmt"
"log"
"time"
"github.com/hashicorp/vault/api"
)
type SecretMsgStorer interface {
Store(msg string) (token string, err error)
Get(token string) (msg string, err error)
}
type vault struct {
address string
prefix string
token string
}
// NewVault creates a vault client to talk with underline vault server
func newVault(address string, prefix string, token string) vault {
v := vault{address, prefix, token}
go v.newVaultClientToRenewToken()
return v
}
func (v vault) Store(msg string) (token string, err error) {
var ttl string = "168h" // 7 days
// Verify duration
d, err := time.ParseDuration(ttl)
if err != nil {
return "", fmt.Errorf("cannot parse duration %v", err)
}
// validate duration length
if d > 168*time.Hour || d == 0*time.Hour {
return "", fmt.Errorf("cannot set ttl to infinte or more than 7 days %v", err)
}
t, err := v.createOneTimeToken(ttl)
if err != nil {
return "", err
}
if v.writeMsgToVault(t, msg) != nil {
return "", err
}
return t, nil
}
func (v vault) createOneTimeToken(ttl string) (string, error) {
fmt.Println("Info: creating message with ttl: ", ttl)
c, err := v.newVaultClient()
if err != nil {
return "", err
}
t := c.Auth().Token()
var notRenewable bool
s, err := t.Create(&api.TokenCreateRequest{
Metadata: map[string]string{"name": "placeholder"},
ExplicitMaxTTL: ttl,
NumUses: 2, //1 to create 2 to get
Renewable: ¬Renewable,
})
if err != nil {
return "", err
}
return s.Auth.ClientToken, nil
}
func (v vault) newVaultClient() (*api.Client, error) {
c, err := api.NewClient(api.DefaultConfig())
if err != nil {
return nil, err
}
if v.token != "" {
c.SetToken(v.token)
}
if v.address == "" {
return c, nil
}
err = c.SetAddress(v.address)
if err != nil {
return nil, err
}
return c, nil
}
func (v vault) writeMsgToVault(token, msg string) error {
c, err := v.newVaultClientWithToken(token)
if err != nil {
return err
}
raw := map[string]interface{}{"msg": msg}
_, err = c.Logical().Write("/"+v.prefix+token, raw)
return err
}
func (v vault) Get(token string) (msg string, err error) {
c, err := v.newVaultClientWithToken(token)
if err != nil {
return "", err
}
r, err := c.Logical().Read(v.prefix + token)
if err != nil {
return "", err
}
return r.Data["msg"].(string), nil
}
func (v vault) newVaultClientWithToken(token string) (*api.Client, error) {
c, err := v.newVaultClient()
if err != nil {
return nil, err
}
c.SetToken(token)
return c, nil
}
func (v vault) newVaultClientToRenewToken() {
c, err := v.newVaultClient()
if err != nil {
log.Println(err)
}
client_auth_token := &api.Secret{Auth: &api.SecretAuth{ClientToken: c.Token(), Renewable: true}}
/* */
log.Println("renew cycle: begin")
defer log.Println("renew cycle: end")
// auth token
authTokenWatcher, err := c.NewLifetimeWatcher(&api.LifetimeWatcherInput{
Secret: client_auth_token,
})
if err != nil {
err := fmt.Errorf("unable to initialize auth token lifetime watcher: %w", err)
fmt.Println(err.Error())
}
go authTokenWatcher.Start()
defer authTokenWatcher.Stop()
// monitor events from both watchers
for {
select {
case err := <-authTokenWatcher.DoneCh():
// Leases created by a token get revoked when the token is revoked.
fmt.Println("Error is :", err)
// RenewCh is a channel that receives a message when a successful
// renewal takes place and includes metadata about the renewal.
case info := <-authTokenWatcher.RenewCh():
log.Printf("auth token: successfully renewed; remaining duration: %ds", info.Secret.Auth.LeaseDuration)
}
}
}