-
Notifications
You must be signed in to change notification settings - Fork 0
/
secenv.go
269 lines (219 loc) · 5.82 KB
/
secenv.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package secenv
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
)
const (
prefix = "VAULT:"
authKubernetes = "kubernetes"
kubernetesTokenPath = "/var/run/secrets/kubernetes.io/serviceaccount/token"
)
var (
// Package level structure with simple access via secenv.Get() function
global *SecEnv
defaultConfig Config
)
func init() {
defaultConfig = Config{
service: os.Getenv("SERVICE_NAME"),
addr: os.Getenv("VAULT_ADDR"),
token: os.Getenv("VAULT_TOKEN"),
auth: &Auth{
mu: &sync.Mutex{},
method: os.Getenv("VAULT_AUTH"),
tokenPath: "",
expireAt: time.Now(),
},
client: &http.Client{
Timeout: 250 * time.Millisecond,
},
}
if defaultConfig.auth.method == authKubernetes {
defaultConfig.auth.tokenPath = kubernetesTokenPath
}
global = NewSecEnv(&defaultConfig)
}
type Secret struct {
// Data is the actual contents of the secret. The format of the data
// is arbitrary and up to the secret backend.
Data map[string]map[string]any `json:"data"`
// Auth, if non-nil, means that there was authentication information
// attached to this response.
Auth *SecretAuth `json:"auth,omitempty"`
}
// SecretAuth is the structure containing auth information if we have it.
type SecretAuth struct {
ClientToken string `json:"client_token"`
LeaseDuration int `json:"lease_duration"`
}
type SecEnv struct {
cfg Config
}
type Config struct {
service string
addr string
token string
auth *Auth
client *http.Client
}
type Auth struct {
mu *sync.Mutex
method string
tokenPath string
expireAt time.Time
}
type LoginRequest struct {
Role string `json:"role"`
JWT string `json:"jwt"`
}
// SecEnv constructor
func NewSecEnv(cfg *Config) *SecEnv {
if cfg == nil {
cfg = &Config{}
}
if cfg.service == "" {
cfg.service = defaultConfig.service
}
if cfg.addr == "" {
cfg.addr = defaultConfig.addr
}
if cfg.token == "" {
cfg.token = defaultConfig.token
}
if cfg.auth == nil {
cfg.auth = defaultConfig.auth
}
if cfg.client == nil {
cfg.client = defaultConfig.client
}
return &SecEnv{cfg: *cfg}
}
// Get secret from vault with settings based on environment variables
func Get(name string) (string, error) {
return global.Get(name)
}
// Get secret from vault
func (se *SecEnv) Get(name string) (string, error) {
value := os.Getenv(name)
if !strings.HasPrefix(value, prefix) {
return value, nil
}
if se.cfg.auth.method == authKubernetes {
err := se.auth()
if err != nil {
return "", fmt.Errorf("secenv(%s): %w", name, err)
}
}
if se.cfg.token == "" {
return "", fmt.Errorf("no token provided")
}
value = strings.TrimPrefix(value, prefix)
path, field := splitName(value)
response, err := se.request("GET", filepath.Join("/v1/secret/data", path), nil)
if err != nil {
return "", fmt.Errorf("secenv(%s): %w", name, err)
}
var secret Secret
if err = se.parse(response, &secret); err != nil {
return "", fmt.Errorf("secenv(%s): %w", name, err)
}
var innerData map[string]any
var ok bool
if innerData, ok = secret.Data["data"]; !ok {
return "", fmt.Errorf("no second level data was found")
}
if secret, ok := innerData[field]; ok {
switch secret.(type) {
case string:
return secret.(string), nil
case int:
return strconv.FormatInt(int64(secret.(int)), 10), nil
default:
return "", fmt.Errorf("secenv(%s): %s", name, "wrong value type")
}
} else {
return "", fmt.Errorf("secenv(%s): %s", name, "no data returned")
}
}
// Requests to vault via http.Client
func (se *SecEnv) request(verb, path string, body io.Reader) (*http.Response, error) {
request, err := http.NewRequest(verb, se.cfg.addr+path, body)
if err != nil {
return nil, fmt.Errorf("building request error: %w", err)
}
if strings.HasPrefix(path, "/v1/secret") {
request.Header.Set("X-Vault-token", se.cfg.token)
}
response, err := se.cfg.client.Do(request)
if err != nil {
return nil, fmt.Errorf("doing remote request error: %w", err)
}
return response, nil
}
// Authentication with Kubernetes token
func (se *SecEnv) auth() error {
se.cfg.auth.mu.Lock()
defer se.cfg.auth.mu.Unlock()
now := time.Now()
if se.cfg.auth.expireAt.Before(now) || se.cfg.auth.expireAt.Equal(now) {
jwt, err := os.ReadFile(se.cfg.auth.tokenPath)
if err != nil {
return fmt.Errorf("read token file error: %w", err)
}
login := LoginRequest{
Role: se.cfg.service,
JWT: string(jwt),
}
loginJson, err := json.Marshal(login)
if err != nil {
return fmt.Errorf("marshalling json error: %w", err)
}
response, err := se.request("PUT", "/v1/auth/kubernetes/login", bytes.NewReader(loginJson))
if err != nil {
return fmt.Errorf("kubernetes auth request error: %w", err)
}
var secret Secret
err = se.parse(response, &secret)
if err != nil {
return fmt.Errorf("parsing kubernetes auth response error: %w", err)
}
se.cfg.token = secret.Auth.ClientToken
se.cfg.auth.expireAt = now.Add(time.Duration(secret.Auth.LeaseDuration) * time.Second)
}
return nil
}
// Response parsing into Secret structure
func (se *SecEnv) parse(response *http.Response, secret *Secret) error {
body, err := io.ReadAll(response.Body)
if err != nil {
return fmt.Errorf("reading body error: %w", err)
}
defer response.Body.Close()
if err = json.Unmarshal(body, secret); err != nil {
return fmt.Errorf("unmarshalling error: %w", err)
}
return nil
}
// Split full vault path into two pieces. First piece is path to the secret "path/to/test" and second piece is key name
// "something" of secret. One secret may contain many keys and values. Default key name is "value".
// VAULT:path/to/test.something.
func splitName(value string) (path, field string) {
switch v := strings.SplitN(value, ".", 2); len(v) {
case 2:
path = v[0]
field = v[1]
default:
path = v[0]
field = "value"
}
return
}