vvauth 2.4.1 → 2.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/publish-npm.yml +5 -0
- package/.github/workflows/release.yml +33 -0
- package/.github/workflows/test.yml +52 -0
- package/.goreleaser.yaml +31 -0
- package/API_and_tests.md +144 -0
- package/README.md +88 -17
- package/SECURITY_AUDIT_fable-5.1.md +88 -0
- package/SECURITY_AUDIT_gpt-6.1-sol.md +196 -0
- package/cmd/vauth/main.go +218 -0
- package/go.mod +18 -0
- package/go.sum +24 -0
- package/index.js +146 -345
- package/package.json +7 -7
- package/rc.go +320 -0
- package/test/token-cache.js +271 -135
- package/vault.go +46 -0
- package/vauth.go +399 -0
- package/vauth_test.go +868 -0
package/vauth_test.go
ADDED
|
@@ -0,0 +1,868 @@
|
|
|
1
|
+
package vvauth
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"bytes"
|
|
5
|
+
"crypto/sha256"
|
|
6
|
+
"encoding/hex"
|
|
7
|
+
"encoding/json"
|
|
8
|
+
"fmt"
|
|
9
|
+
"io"
|
|
10
|
+
"net/http"
|
|
11
|
+
"net/http/httptest"
|
|
12
|
+
"os"
|
|
13
|
+
"os/exec"
|
|
14
|
+
"path/filepath"
|
|
15
|
+
"regexp"
|
|
16
|
+
"strings"
|
|
17
|
+
"testing"
|
|
18
|
+
"time"
|
|
19
|
+
|
|
20
|
+
sshagentcrypt "github.com/131/ssh-agent-crypt"
|
|
21
|
+
vauthlogin "github.com/131/vauth-login"
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
const hour = 60 * 60
|
|
25
|
+
|
|
26
|
+
// agentSocket is a throwaway ssh-agent holding one ed25519 key.
|
|
27
|
+
var agentSocket string
|
|
28
|
+
|
|
29
|
+
func TestMain(m *testing.M) {
|
|
30
|
+
directory, err := os.MkdirTemp("", "vvauth-agent-")
|
|
31
|
+
if err != nil {
|
|
32
|
+
panic(err)
|
|
33
|
+
}
|
|
34
|
+
key := filepath.Join(directory, "key")
|
|
35
|
+
if out, err := exec.Command("ssh-keygen", "-q", "-t", "ed25519", "-N", "", "-f", key).CombinedOutput(); err != nil {
|
|
36
|
+
panic(fmt.Sprintf("ssh-keygen: %v %s", err, out))
|
|
37
|
+
}
|
|
38
|
+
out, err := exec.Command("ssh-agent", "-s").Output()
|
|
39
|
+
if err != nil {
|
|
40
|
+
panic(err)
|
|
41
|
+
}
|
|
42
|
+
agentSocket = regexp.MustCompile(`SSH_AUTH_SOCK=([^;]+)`).FindStringSubmatch(string(out))[1]
|
|
43
|
+
pid := regexp.MustCompile(`SSH_AGENT_PID=(\d+)`).FindStringSubmatch(string(out))[1]
|
|
44
|
+
add := exec.Command("ssh-add", key)
|
|
45
|
+
add.Env = append(os.Environ(), "SSH_AUTH_SOCK="+agentSocket)
|
|
46
|
+
if out, err := add.CombinedOutput(); err != nil {
|
|
47
|
+
panic(fmt.Sprintf("ssh-add: %v %s", err, out))
|
|
48
|
+
}
|
|
49
|
+
// ssh-agent-crypt falls back to these for key selection; the real user's
|
|
50
|
+
// identity is not in the throwaway agent.
|
|
51
|
+
for _, name := range []string{"SSH_AGENT_CRYPT_KEY", "VAUTH_USER_IDENTITY", "VAUTH_USER_MAIL"} {
|
|
52
|
+
os.Unsetenv(name)
|
|
53
|
+
}
|
|
54
|
+
code := m.Run()
|
|
55
|
+
exec.Command("kill", pid).Run()
|
|
56
|
+
os.RemoveAll(directory)
|
|
57
|
+
os.Exit(code)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
type request struct {
|
|
61
|
+
Path, Method, Token, Body string
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
type fixtureOptions struct {
|
|
65
|
+
ttl *float64
|
|
66
|
+
renewable *bool
|
|
67
|
+
creationTTL *int64
|
|
68
|
+
lookupStatus int
|
|
69
|
+
renewStatus int
|
|
70
|
+
metadata map[string]any
|
|
71
|
+
database map[string]any
|
|
72
|
+
secrets map[string]any
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
func seconds(value float64) *float64 { return &value }
|
|
76
|
+
func boolean(value bool) *bool { return &value }
|
|
77
|
+
|
|
78
|
+
type fixture struct {
|
|
79
|
+
t *testing.T
|
|
80
|
+
server *httptest.Server
|
|
81
|
+
dir string
|
|
82
|
+
requests []request
|
|
83
|
+
options fixtureOptions
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
func newFixture(t *testing.T, options fixtureOptions) *fixture {
|
|
87
|
+
f := &fixture{t: t, dir: t.TempDir(), options: options}
|
|
88
|
+
f.server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
89
|
+
body, _ := io.ReadAll(r.Body)
|
|
90
|
+
f.requests = append(f.requests, request{r.URL.Path, r.Method, r.Header.Get("X-Vault-Token"), string(body)})
|
|
91
|
+
w.Header().Set("Content-Type", "application/json")
|
|
92
|
+
status := func(code int) {
|
|
93
|
+
if code != 0 {
|
|
94
|
+
w.WriteHeader(code)
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
switch r.URL.Path {
|
|
98
|
+
case "/v1/auth/jwt/login":
|
|
99
|
+
fmt.Fprintf(w, `{"auth":{"client_token":"fresh-token","lease_duration":%d,"renewable":true}}`, 24*hour)
|
|
100
|
+
case "/v1/auth/token/lookup-self":
|
|
101
|
+
status(options.lookupStatus)
|
|
102
|
+
ttl, renewable, creation := float64(12*hour), true, int64(24*hour)
|
|
103
|
+
if options.ttl != nil {
|
|
104
|
+
ttl = *options.ttl
|
|
105
|
+
}
|
|
106
|
+
if options.renewable != nil {
|
|
107
|
+
renewable = *options.renewable
|
|
108
|
+
}
|
|
109
|
+
if options.creationTTL != nil {
|
|
110
|
+
creation = *options.creationTTL
|
|
111
|
+
}
|
|
112
|
+
fmt.Fprintf(w, `{"data":{"ttl":%v,"renewable":%v,"creation_ttl":%d,"entity_id":"test-entity"}}`, ttl, renewable, creation)
|
|
113
|
+
case "/v1/auth/token/renew-self":
|
|
114
|
+
status(options.renewStatus)
|
|
115
|
+
fmt.Fprintf(w, `{"auth":{"client_token":%q,"lease_duration":%d,"renewable":true}}`, r.Header.Get("X-Vault-Token"), 24*hour)
|
|
116
|
+
case "/v1/identity/entity/id/test-entity":
|
|
117
|
+
json.NewEncoder(w).Encode(map[string]any{"data": map[string]any{"metadata": options.metadata}})
|
|
118
|
+
case "/v1/private/alice/data/.vauth_database":
|
|
119
|
+
if r.Method == "POST" {
|
|
120
|
+
var payload struct {
|
|
121
|
+
Data map[string]any `json:"data"`
|
|
122
|
+
}
|
|
123
|
+
json.Unmarshal(body, &payload)
|
|
124
|
+
f.options.database = payload.Data
|
|
125
|
+
}
|
|
126
|
+
json.NewEncoder(w).Encode(map[string]any{"data": map[string]any{"data": f.options.database}})
|
|
127
|
+
case "/v1/secrets/data/app":
|
|
128
|
+
json.NewEncoder(w).Encode(map[string]any{"data": map[string]any{"data": options.secrets}})
|
|
129
|
+
default:
|
|
130
|
+
w.WriteHeader(404)
|
|
131
|
+
fmt.Fprint(w, `{}`)
|
|
132
|
+
}
|
|
133
|
+
}))
|
|
134
|
+
t.Cleanup(f.server.Close)
|
|
135
|
+
return f
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// configuredClient constructs through the public readers, using synthetic RC/env.
|
|
139
|
+
func configuredClient(t *testing.T, document map[string]any, overrides map[string]string) *Vauth {
|
|
140
|
+
t.Helper()
|
|
141
|
+
file := filepath.Join(t.TempDir(), ".vauthrc")
|
|
142
|
+
body, err := json.Marshal(document)
|
|
143
|
+
if err != nil {
|
|
144
|
+
t.Fatal(err)
|
|
145
|
+
}
|
|
146
|
+
if err := os.WriteFile(file, body, 0600); err != nil {
|
|
147
|
+
t.Fatal(err)
|
|
148
|
+
}
|
|
149
|
+
t.Setenv("VAUTHRC", file)
|
|
150
|
+
t.Setenv("VAULT_TOKEN", "")
|
|
151
|
+
t.Setenv("VAULT_ADDR", "")
|
|
152
|
+
for key, value := range overrides {
|
|
153
|
+
t.Setenv(key, value)
|
|
154
|
+
}
|
|
155
|
+
v, err := New()
|
|
156
|
+
if err != nil {
|
|
157
|
+
t.Fatal(err)
|
|
158
|
+
}
|
|
159
|
+
v.Stderr = io.Discard
|
|
160
|
+
return v
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
type clientOptions struct {
|
|
164
|
+
token string
|
|
165
|
+
rc map[string]any
|
|
166
|
+
noAuth bool
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// client avoids the real user's configuration and cache.
|
|
170
|
+
func (f *fixture) client(settings ...clientOptions) *Vauth {
|
|
171
|
+
options := clientOptions{}
|
|
172
|
+
if len(settings) != 0 {
|
|
173
|
+
options = settings[0]
|
|
174
|
+
}
|
|
175
|
+
document := map[string]any{"vault_addr": f.server.URL, "jwt_auth": map[string]any{"path": "jwt", "jwt": "test-jwt", "role": "test-role"}}
|
|
176
|
+
for key, value := range options.rc {
|
|
177
|
+
document[key] = value
|
|
178
|
+
}
|
|
179
|
+
if options.noAuth {
|
|
180
|
+
delete(document, "jwt_auth")
|
|
181
|
+
}
|
|
182
|
+
v := configuredClient(f.t, document, map[string]string{"VAULT_TOKEN": options.token})
|
|
183
|
+
v.Agent = agentSocket
|
|
184
|
+
v.TokenCache = true
|
|
185
|
+
v.CacheDir = filepath.Join(f.dir, ".vauth", "tokens")
|
|
186
|
+
return v
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Fixture data follows the shared cache format; the application owns no cache code.
|
|
190
|
+
type tokenCache struct {
|
|
191
|
+
Token string `json:"token"`
|
|
192
|
+
LeaseDuration int64 `json:"lease_duration"`
|
|
193
|
+
Renewable bool `json:"renewable"`
|
|
194
|
+
ExpiresAt *int64 `json:"expires_at"`
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
func testCacheKey(v *Vauth) string {
|
|
198
|
+
key := `{"vault_addr":` + string(jsonString(strings.Trim(v.VAULT_ADDR(), "/")))
|
|
199
|
+
body, err := json.Marshal(v.RC())
|
|
200
|
+
if err != nil {
|
|
201
|
+
panic(err)
|
|
202
|
+
}
|
|
203
|
+
rc, err := vauthlogin.ParseRC(body, "fixture RC", v.Environ)
|
|
204
|
+
if err != nil {
|
|
205
|
+
panic(err)
|
|
206
|
+
}
|
|
207
|
+
if auth := rc.AuthKey(); auth != "" {
|
|
208
|
+
key += "," + auth
|
|
209
|
+
}
|
|
210
|
+
return sha256Hex(key + "}")
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
func cacheFile(v *Vauth) string { return filepath.Join(v.CacheDir, testCacheKey(v)+".creds") }
|
|
214
|
+
|
|
215
|
+
func (f *fixture) paths() []string {
|
|
216
|
+
var paths []string
|
|
217
|
+
for _, r := range f.requests {
|
|
218
|
+
paths = append(paths, r.Path)
|
|
219
|
+
}
|
|
220
|
+
return paths
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
func (f *fixture) read(v *Vauth) tokenCache {
|
|
224
|
+
f.t.Helper()
|
|
225
|
+
armor, err := os.ReadFile(cacheFile(v))
|
|
226
|
+
if err != nil {
|
|
227
|
+
f.t.Fatal(err)
|
|
228
|
+
}
|
|
229
|
+
body, err := sshagentcrypt.Decrypt(armor, "", &sshagentcrypt.Options{AgentSocket: agentSocket})
|
|
230
|
+
if err != nil {
|
|
231
|
+
f.t.Fatal(err)
|
|
232
|
+
}
|
|
233
|
+
var cache tokenCache
|
|
234
|
+
if err := json.Unmarshal(body, &cache); err != nil {
|
|
235
|
+
f.t.Fatal(err)
|
|
236
|
+
}
|
|
237
|
+
return cache
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
func (f *fixture) write(v *Vauth, cache tokenCache) {
|
|
241
|
+
f.t.Helper()
|
|
242
|
+
body, err := json.Marshal(cache)
|
|
243
|
+
if err != nil {
|
|
244
|
+
f.t.Fatal(err)
|
|
245
|
+
}
|
|
246
|
+
armor, err := sshagentcrypt.Encrypt(body, "", &sshagentcrypt.Options{AgentSocket: agentSocket})
|
|
247
|
+
if err != nil {
|
|
248
|
+
f.t.Fatal(err)
|
|
249
|
+
}
|
|
250
|
+
if err := os.MkdirAll(v.CacheDir, 0700); err != nil {
|
|
251
|
+
f.t.Fatal(err)
|
|
252
|
+
}
|
|
253
|
+
if err := os.WriteFile(cacheFile(v), []byte(armor), 0600); err != nil {
|
|
254
|
+
f.t.Fatal(err)
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// remaining rewrites the cached expiry so that ttl seconds remain (0: unlimited).
|
|
259
|
+
func (f *fixture) remaining(v *Vauth, ttl float64) {
|
|
260
|
+
cache := f.read(v)
|
|
261
|
+
cache.ExpiresAt = nil
|
|
262
|
+
if ttl != 0 {
|
|
263
|
+
expires := time.Now().UnixMilli() + int64(ttl*1000)
|
|
264
|
+
cache.ExpiresAt = &expires
|
|
265
|
+
}
|
|
266
|
+
f.write(v, cache)
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
func (f *fixture) seed(v *Vauth, lease int64) {
|
|
270
|
+
renewable := f.options.renewable == nil || *f.options.renewable
|
|
271
|
+
expires := time.Now().UnixMilli() + lease*1000
|
|
272
|
+
f.write(v, tokenCache{Token: "cached-token", LeaseDuration: lease, Renewable: renewable, ExpiresAt: &expires})
|
|
273
|
+
if f.options.ttl != nil {
|
|
274
|
+
f.remaining(v, *f.options.ttl)
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
func connect(t *testing.T, v *Vauth, renew bool) string {
|
|
279
|
+
t.Helper()
|
|
280
|
+
token, err := v.Connect(renew)
|
|
281
|
+
if err != nil {
|
|
282
|
+
t.Fatal(err)
|
|
283
|
+
}
|
|
284
|
+
return token
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
func equal[T comparable](t *testing.T, got, want T) {
|
|
288
|
+
t.Helper()
|
|
289
|
+
if got != want {
|
|
290
|
+
t.Fatalf("got %v, want %v", got, want)
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
func equalPaths(t *testing.T, got []string, want ...string) {
|
|
295
|
+
t.Helper()
|
|
296
|
+
if strings.Join(got, " ") != strings.Join(want, " ") {
|
|
297
|
+
t.Fatalf("requests %v, want %v", got, want)
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
func TestLoginPersistsAndReuses(t *testing.T) {
|
|
302
|
+
f := newFixture(t, fixtureOptions{})
|
|
303
|
+
first := f.client()
|
|
304
|
+
equal(t, connect(t, first, false), "fresh-token")
|
|
305
|
+
cache := f.read(first)
|
|
306
|
+
equal(t, cache.Token, "fresh-token")
|
|
307
|
+
if cache.ExpiresAt == nil || *cache.ExpiresAt < time.Now().UnixMilli()+23*hour*1000 {
|
|
308
|
+
t.Fatalf("expires_at %v", cache.ExpiresAt)
|
|
309
|
+
}
|
|
310
|
+
info, _ := os.Stat(cacheFile(first))
|
|
311
|
+
equal(t, info.Mode().Perm(), 0o600)
|
|
312
|
+
info, _ = os.Stat(filepath.Dir(cacheFile(first)))
|
|
313
|
+
equal(t, info.Mode().Perm(), 0o700)
|
|
314
|
+
second := f.client()
|
|
315
|
+
equal(t, connect(t, second, false), "fresh-token")
|
|
316
|
+
equalPaths(t, f.paths(), "/v1/auth/jwt/login")
|
|
317
|
+
entries, _ := os.ReadDir(filepath.Dir(cacheFile(first)))
|
|
318
|
+
equal(t, len(entries), 1)
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
func TestLastThirdRenewsAndSaves(t *testing.T) {
|
|
322
|
+
f := newFixture(t, fixtureOptions{ttl: seconds(hour)})
|
|
323
|
+
v := f.client()
|
|
324
|
+
f.seed(v, 12*hour)
|
|
325
|
+
equal(t, connect(t, v, false), "cached-token")
|
|
326
|
+
equalPaths(t, f.paths(), "/v1/auth/token/renew-self")
|
|
327
|
+
equal(t, f.requests[0].Method, "POST")
|
|
328
|
+
equal(t, f.requests[0].Token, "cached-token")
|
|
329
|
+
if *f.read(v).ExpiresAt < time.Now().UnixMilli()+23*hour*1000 {
|
|
330
|
+
t.Fatal("renewed expiry not saved")
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
func TestMoreThanOneThirdNoRenewal(t *testing.T) {
|
|
335
|
+
f := newFixture(t, fixtureOptions{ttl: seconds(4*hour + 1)})
|
|
336
|
+
v := f.client()
|
|
337
|
+
f.seed(v, 12*hour)
|
|
338
|
+
equal(t, connect(t, v, false), "cached-token")
|
|
339
|
+
equal(t, len(f.requests), 0)
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
func TestExpiredCacheReplaced(t *testing.T) {
|
|
343
|
+
f := newFixture(t, fixtureOptions{})
|
|
344
|
+
v := f.client()
|
|
345
|
+
f.seed(v, 12*hour)
|
|
346
|
+
f.remaining(v, -1)
|
|
347
|
+
equal(t, connect(t, v, false), "fresh-token")
|
|
348
|
+
equal(t, f.read(v).Token, "fresh-token")
|
|
349
|
+
equalPaths(t, f.paths(), "/v1/auth/jwt/login")
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
func TestCachedTokensReusedWithoutRevocationCheck(t *testing.T) {
|
|
353
|
+
f := newFixture(t, fixtureOptions{lookupStatus: 403})
|
|
354
|
+
v := f.client()
|
|
355
|
+
f.seed(v, 12*hour)
|
|
356
|
+
equal(t, connect(t, v, false), "cached-token")
|
|
357
|
+
equal(t, len(f.requests), 0)
|
|
358
|
+
if _, err := v.LookupSelf(v.VAULT_TOKEN()); err == nil {
|
|
359
|
+
t.Fatal("lookup should fail")
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
func TestNonRenewableNearingExpiryReauthenticates(t *testing.T) {
|
|
364
|
+
f := newFixture(t, fixtureOptions{ttl: seconds(hour), renewable: boolean(false)})
|
|
365
|
+
v := f.client()
|
|
366
|
+
f.seed(v, 12*hour)
|
|
367
|
+
equal(t, connect(t, v, false), "fresh-token")
|
|
368
|
+
equalPaths(t, f.paths(), "/v1/auth/jwt/login")
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
func TestRejectedRenewalTriggersLogin(t *testing.T) {
|
|
372
|
+
for _, status := range []int{400, 403} {
|
|
373
|
+
f := newFixture(t, fixtureOptions{ttl: seconds(hour), renewStatus: status})
|
|
374
|
+
v := f.client()
|
|
375
|
+
f.seed(v, 12*hour)
|
|
376
|
+
equal(t, connect(t, v, false), "fresh-token")
|
|
377
|
+
equal(t, f.read(v).Token, "fresh-token")
|
|
378
|
+
equalPaths(t, f.paths(), "/v1/auth/token/renew-self", "/v1/auth/jwt/login")
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
func TestServerFailuresDoNotGenerateTokens(t *testing.T) {
|
|
383
|
+
f := newFixture(t, fixtureOptions{lookupStatus: 500})
|
|
384
|
+
v := f.client(clientOptions{token: "uncached-token"})
|
|
385
|
+
f.seed(v, 12*hour)
|
|
386
|
+
for i := 0; i < 2; i++ {
|
|
387
|
+
if _, err := v.LookupSelf(v.VAULT_TOKEN()); err == nil {
|
|
388
|
+
t.Fatal("lookup should fail")
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
equalPaths(t, f.paths(), "/v1/auth/token/lookup-self", "/v1/auth/token/lookup-self")
|
|
392
|
+
|
|
393
|
+
f = newFixture(t, fixtureOptions{ttl: seconds(hour), renewStatus: 500})
|
|
394
|
+
v = f.client()
|
|
395
|
+
f.seed(v, 12*hour)
|
|
396
|
+
if _, err := v.Connect(false); err == nil {
|
|
397
|
+
t.Fatal("connect should fail")
|
|
398
|
+
}
|
|
399
|
+
equalPaths(t, f.paths(), "/v1/auth/token/renew-self")
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
func TestInheritedTokenIsOfflineAndNotCached(t *testing.T) {
|
|
403
|
+
f := newFixture(t, fixtureOptions{})
|
|
404
|
+
v := f.client(clientOptions{token: "environment-token"})
|
|
405
|
+
f.seed(v, 12*hour)
|
|
406
|
+
equal(t, connect(t, v, false), "environment-token")
|
|
407
|
+
equal(t, f.read(v).Token, "cached-token")
|
|
408
|
+
equal(t, len(f.requests), 0)
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
func TestNonExpiringTokensReused(t *testing.T) {
|
|
412
|
+
f := newFixture(t, fixtureOptions{ttl: seconds(0)})
|
|
413
|
+
v := f.client()
|
|
414
|
+
f.seed(v, 0)
|
|
415
|
+
equal(t, connect(t, v, false), "cached-token")
|
|
416
|
+
if f.read(v).ExpiresAt != nil {
|
|
417
|
+
t.Fatal("expires_at should be null")
|
|
418
|
+
}
|
|
419
|
+
equal(t, len(f.requests), 0)
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
func TestCorruptCacheReplaced(t *testing.T) {
|
|
423
|
+
f := newFixture(t, fixtureOptions{})
|
|
424
|
+
v := f.client()
|
|
425
|
+
os.MkdirAll(filepath.Dir(cacheFile(v)), 0o700)
|
|
426
|
+
os.WriteFile(cacheFile(v), []byte("{broken"), 0o600)
|
|
427
|
+
equal(t, connect(t, v, false), "fresh-token")
|
|
428
|
+
equal(t, f.read(v).Token, "fresh-token")
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
func TestPlaintextCacheNotAccepted(t *testing.T) {
|
|
432
|
+
f := newFixture(t, fixtureOptions{})
|
|
433
|
+
v := f.client()
|
|
434
|
+
os.MkdirAll(filepath.Dir(cacheFile(v)), 0o700)
|
|
435
|
+
os.WriteFile(cacheFile(v), []byte(`{"token":"plain-token","lease_duration":43200,"renewable":true,"expires_at":null}`), 0o600)
|
|
436
|
+
equal(t, connect(t, v, false), "fresh-token")
|
|
437
|
+
equalPaths(t, f.paths(), "/v1/auth/jwt/login")
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
func TestCacheIsolation(t *testing.T) {
|
|
441
|
+
f := newFixture(t, fixtureOptions{})
|
|
442
|
+
first := f.client()
|
|
443
|
+
f.seed(first, 12*hour)
|
|
444
|
+
second := f.client(clientOptions{rc: map[string]any{"jwt_auth": map[string]any{"path": "jwt", "jwt": "test-jwt", "role": "other-role"}}})
|
|
445
|
+
equal(t, connect(t, second, false), "fresh-token")
|
|
446
|
+
if cacheFile(first) == cacheFile(second) {
|
|
447
|
+
t.Fatal("cache files should differ")
|
|
448
|
+
}
|
|
449
|
+
equal(t, f.read(first).Token, "cached-token")
|
|
450
|
+
equal(t, f.read(second).Token, "fresh-token")
|
|
451
|
+
third := f.client(clientOptions{rc: map[string]any{"vault_addr": "http://other-vault.test"}})
|
|
452
|
+
if testCacheKey(third) == testCacheKey(first) {
|
|
453
|
+
t.Fatal("cache keys should differ")
|
|
454
|
+
}
|
|
455
|
+
entries, _ := os.ReadDir(filepath.Dir(cacheFile(first)))
|
|
456
|
+
equal(t, len(entries), 2)
|
|
457
|
+
armor, _ := os.ReadFile(cacheFile(first))
|
|
458
|
+
if bytes.Contains(armor, []byte("test-jwt")) || bytes.Contains(armor, []byte("cached-token")) {
|
|
459
|
+
t.Fatal("cache leaks secrets")
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
func TestTokenCacheKeyMatchesNode(t *testing.T) {
|
|
464
|
+
v := configuredClient(t, map[string]any{"vault_addr": "https://vault.example.test/", "ssh_auth": map[string]any{"role": "alice"}}, nil)
|
|
465
|
+
equal(t, testCacheKey(v), sha256Hex(`{"vault_addr":"https://vault.example.test","ssh_auth":{"role":"alice"}}`))
|
|
466
|
+
v = configuredClient(t, map[string]any{"vault_addr": "https://vault.example.test", "jwt_auth": map[string]any{"path": "jwt", "jwt": "x", "role": "r"}}, nil)
|
|
467
|
+
equal(t, testCacheKey(v), sha256Hex(`{"vault_addr":"https://vault.example.test","jwt_auth":{"jwt":"x","path":"jwt","role":"r"}}`))
|
|
468
|
+
v = configuredClient(t, map[string]any{"vault_addr": "https://vault.example.test", "ssh_auth": map[string]any{"role": "$${env.USER}"}}, map[string]string{"USER": "bob"})
|
|
469
|
+
equal(t, testCacheKey(v), sha256Hex(`{"vault_addr":"https://vault.example.test","ssh_auth":{"role":"bob"}}`))
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
func TestRepeatedConnectReusesConnection(t *testing.T) {
|
|
473
|
+
f := newFixture(t, fixtureOptions{})
|
|
474
|
+
v := f.client()
|
|
475
|
+
equal(t, connect(t, v, false), "fresh-token")
|
|
476
|
+
equal(t, connect(t, v, false), "fresh-token")
|
|
477
|
+
equal(t, len(f.requests), 1)
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
func TestUnwritableCacheDoesNotPreventLogin(t *testing.T) {
|
|
481
|
+
f := newFixture(t, fixtureOptions{})
|
|
482
|
+
v := f.client()
|
|
483
|
+
os.MkdirAll(filepath.Dir(filepath.Dir(cacheFile(v))), 0o700)
|
|
484
|
+
os.WriteFile(filepath.Dir(cacheFile(v)), []byte("not a directory"), 0o600)
|
|
485
|
+
var stderr bytes.Buffer
|
|
486
|
+
v.Stderr = &stderr
|
|
487
|
+
equal(t, connect(t, v, false), "fresh-token")
|
|
488
|
+
if !strings.Contains(stderr.String(), "token not persisted") {
|
|
489
|
+
t.Fatalf("stderr %q", stderr.String())
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
func TestWithoutAuthMethodInheritanceStillWorks(t *testing.T) {
|
|
494
|
+
f := newFixture(t, fixtureOptions{lookupStatus: 403})
|
|
495
|
+
v := f.client(clientOptions{token: "uncached-token", noAuth: true})
|
|
496
|
+
f.seed(v, 12*hour)
|
|
497
|
+
equal(t, connect(t, v, false), "uncached-token")
|
|
498
|
+
equal(t, v.VAULT_TOKEN(), "uncached-token")
|
|
499
|
+
equal(t, connect(t, v, true), "")
|
|
500
|
+
equal(t, len(f.requests), 0)
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
func TestRenewIgnoresTokensAndSaves(t *testing.T) {
|
|
504
|
+
f := newFixture(t, fixtureOptions{})
|
|
505
|
+
v := f.client(clientOptions{token: "environment-token"})
|
|
506
|
+
f.seed(v, 12*hour)
|
|
507
|
+
equal(t, connect(t, v, true), "fresh-token")
|
|
508
|
+
equalPaths(t, f.paths(), "/v1/auth/jwt/login")
|
|
509
|
+
equal(t, f.read(v).Token, "fresh-token")
|
|
510
|
+
|
|
511
|
+
f = newFixture(t, fixtureOptions{})
|
|
512
|
+
v = f.client()
|
|
513
|
+
f.seed(v, 12*hour)
|
|
514
|
+
connect(t, v, false)
|
|
515
|
+
equal(t, connect(t, v, true), "fresh-token")
|
|
516
|
+
equal(t, connect(t, v, true), "fresh-token")
|
|
517
|
+
equalPaths(t, f.paths(), "/v1/auth/jwt/login", "/v1/auth/jwt/login")
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
func TestProportionalThreshold(t *testing.T) {
|
|
521
|
+
for _, c := range []struct {
|
|
522
|
+
lease int64
|
|
523
|
+
remaining float64
|
|
524
|
+
renew bool
|
|
525
|
+
}{
|
|
526
|
+
{24 * hour, 8 * hour, true}, {24 * hour, 8*hour + 1, false},
|
|
527
|
+
{hour, 20 * 60, true}, {hour, 20*60 + 1, false},
|
|
528
|
+
{60, 20, true}, {60, 21, false},
|
|
529
|
+
} {
|
|
530
|
+
f := newFixture(t, fixtureOptions{ttl: seconds(c.remaining)})
|
|
531
|
+
v := f.client()
|
|
532
|
+
f.seed(v, c.lease)
|
|
533
|
+
connect(t, v, false)
|
|
534
|
+
renewed := strings.Contains(strings.Join(f.paths(), " "), "/renew-self")
|
|
535
|
+
if renewed != c.renew {
|
|
536
|
+
t.Fatalf("lease=%d remaining=%v renewed=%v", c.lease, c.remaining, renewed)
|
|
537
|
+
}
|
|
538
|
+
want := c.lease
|
|
539
|
+
if c.renew {
|
|
540
|
+
want = 24 * hour
|
|
541
|
+
}
|
|
542
|
+
equal(t, f.read(v).LeaseDuration, want)
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
func TestLookupsNeverResetLease(t *testing.T) {
|
|
547
|
+
f := newFixture(t, fixtureOptions{ttl: seconds(20 * hour)})
|
|
548
|
+
v := f.client()
|
|
549
|
+
f.seed(v, 24*hour)
|
|
550
|
+
connect(t, v, false)
|
|
551
|
+
equal(t, f.read(v).LeaseDuration, 24*hour)
|
|
552
|
+
f.remaining(v, 12*hour)
|
|
553
|
+
connect(t, f.client(), false)
|
|
554
|
+
equal(t, f.read(v).LeaseDuration, 24*hour)
|
|
555
|
+
f.remaining(v, 8*hour)
|
|
556
|
+
connect(t, f.client(), false)
|
|
557
|
+
equal(t, strings.Count(strings.Join(f.paths(), " "), "/renew-self"), 1)
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
func TestRenewalUpdatesLeaseBaseline(t *testing.T) {
|
|
561
|
+
f := newFixture(t, fixtureOptions{ttl: seconds(20 * 60)})
|
|
562
|
+
v := f.client()
|
|
563
|
+
f.seed(v, hour)
|
|
564
|
+
connect(t, v, false)
|
|
565
|
+
equal(t, f.read(v).LeaseDuration, 24*hour)
|
|
566
|
+
f.remaining(v, 8*hour)
|
|
567
|
+
connect(t, f.client(), false)
|
|
568
|
+
equal(t, strings.Count(strings.Join(f.paths(), " "), "/renew-self"), 2)
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
func TestNormalReuseNeverWrites(t *testing.T) {
|
|
572
|
+
f := newFixture(t, fixtureOptions{})
|
|
573
|
+
v := f.client(clientOptions{token: "cached-token"})
|
|
574
|
+
f.seed(v, 12*hour)
|
|
575
|
+
before, _ := os.ReadFile(cacheFile(v))
|
|
576
|
+
stat, _ := os.Stat(cacheFile(v))
|
|
577
|
+
equal(t, connect(t, v, false), "cached-token")
|
|
578
|
+
after, _ := os.ReadFile(cacheFile(v))
|
|
579
|
+
if !bytes.Equal(before, after) {
|
|
580
|
+
t.Fatal("cache rewritten")
|
|
581
|
+
}
|
|
582
|
+
again, _ := os.Stat(cacheFile(v))
|
|
583
|
+
equal(t, again.ModTime(), stat.ModTime())
|
|
584
|
+
equal(t, len(f.requests), 0)
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
func TestLookupReturnsLease(t *testing.T) {
|
|
588
|
+
f := newFixture(t, fixtureOptions{ttl: seconds(hour)})
|
|
589
|
+
v := f.client()
|
|
590
|
+
info, err := v.LookupSelf("environment-token")
|
|
591
|
+
if err != nil {
|
|
592
|
+
t.Fatal(err)
|
|
593
|
+
}
|
|
594
|
+
equal(t, info.LeaseDuration, 24*hour)
|
|
595
|
+
equal(t, info.TTL, float64(hour))
|
|
596
|
+
equal(t, len(f.requests), 1)
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
func TestTokenCacheDisabled(t *testing.T) {
|
|
600
|
+
f := newFixture(t, fixtureOptions{})
|
|
601
|
+
v := f.client()
|
|
602
|
+
v.TokenCache = false
|
|
603
|
+
equal(t, connect(t, v, false), "fresh-token")
|
|
604
|
+
if _, err := os.Stat(cacheFile(v)); err == nil {
|
|
605
|
+
t.Fatal("cache written")
|
|
606
|
+
}
|
|
607
|
+
equalPaths(t, f.paths(), "/v1/auth/jwt/login")
|
|
608
|
+
|
|
609
|
+
f = newFixture(t, fixtureOptions{})
|
|
610
|
+
v = f.client()
|
|
611
|
+
f.seed(v, 12*hour)
|
|
612
|
+
before, _ := os.ReadFile(cacheFile(v))
|
|
613
|
+
v.TokenCache = false
|
|
614
|
+
equal(t, connect(t, v, false), "fresh-token")
|
|
615
|
+
after, _ := os.ReadFile(cacheFile(v))
|
|
616
|
+
if !bytes.Equal(before, after) {
|
|
617
|
+
t.Fatal("disabled cache touched disk")
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
f = newFixture(t, fixtureOptions{})
|
|
621
|
+
v = f.client(clientOptions{token: "environment-token"})
|
|
622
|
+
v.TokenCache = false
|
|
623
|
+
equal(t, connect(t, v, false), "environment-token")
|
|
624
|
+
equal(t, len(f.requests), 0)
|
|
625
|
+
if _, err := os.Stat(cacheFile(v)); err == nil {
|
|
626
|
+
t.Fatal("cache written")
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
func TestTokenCacheFalseFromRC(t *testing.T) {
|
|
631
|
+
rc, err := vauthlogin.ParseRC([]byte("vault_addr: https://vault.example.test\ntoken_cache: false\n"), ".vauthrc", map[string]string{})
|
|
632
|
+
if err != nil {
|
|
633
|
+
t.Fatal(err)
|
|
634
|
+
}
|
|
635
|
+
if rc.TokenCache == nil || *rc.TokenCache {
|
|
636
|
+
t.Fatal("token_cache false not parsed")
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
func TestCacheIsArmorOnly(t *testing.T) {
|
|
641
|
+
f := newFixture(t, fixtureOptions{})
|
|
642
|
+
v := f.client()
|
|
643
|
+
connect(t, v, false)
|
|
644
|
+
armor, _ := os.ReadFile(cacheFile(v))
|
|
645
|
+
if !bytes.HasPrefix(armor, []byte("ssh-agent-crypt:")) || bytes.Contains(armor, []byte("fresh-token")) {
|
|
646
|
+
t.Fatalf("not armored: %q", armor[:40])
|
|
647
|
+
}
|
|
648
|
+
// The ssh-agent-crypt CLI (Go) decrypts the same armor.
|
|
649
|
+
cli := exec.Command("go", "run", "github.com/131/ssh-agent-crypt/cmd/ssh-agent-crypt", "-decrypt")
|
|
650
|
+
cli.Stdin = bytes.NewReader(armor)
|
|
651
|
+
cli.Env = append(os.Environ(), "SSH_AUTH_SOCK="+agentSocket)
|
|
652
|
+
out, err := cli.Output()
|
|
653
|
+
if err != nil {
|
|
654
|
+
t.Skipf("ssh-agent-crypt CLI unavailable: %v", err)
|
|
655
|
+
}
|
|
656
|
+
var cache tokenCache
|
|
657
|
+
if err := json.Unmarshal(out, &cache); err != nil || cache.Token != "fresh-token" {
|
|
658
|
+
t.Fatalf("CLI output %q", out)
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
func TestWithoutAgentCacheSkipped(t *testing.T) {
|
|
663
|
+
f := newFixture(t, fixtureOptions{})
|
|
664
|
+
v := f.client()
|
|
665
|
+
f.seed(v, 12*hour)
|
|
666
|
+
before, _ := os.ReadFile(cacheFile(v))
|
|
667
|
+
v.Agent = ""
|
|
668
|
+
v.TokenCache = false // as New() does when no agent is configured
|
|
669
|
+
equal(t, connect(t, v, false), "fresh-token")
|
|
670
|
+
after, _ := os.ReadFile(cacheFile(v))
|
|
671
|
+
if !bytes.Equal(before, after) {
|
|
672
|
+
t.Fatal("existing armor touched")
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
func TestOneStatusPerConnection(t *testing.T) {
|
|
677
|
+
for _, c := range []struct {
|
|
678
|
+
options fixtureOptions
|
|
679
|
+
operation string
|
|
680
|
+
duration string
|
|
681
|
+
}{
|
|
682
|
+
{fixtureOptions{ttl: seconds(12 * hour)}, "reused", "12h"},
|
|
683
|
+
{fixtureOptions{ttl: seconds(hour)}, "renewed", "1d"},
|
|
684
|
+
{fixtureOptions{ttl: seconds(hour), renewStatus: 400}, "generated", "1d"},
|
|
685
|
+
{fixtureOptions{ttl: seconds(hour), renewStatus: 403}, "generated", "1d"},
|
|
686
|
+
} {
|
|
687
|
+
f := newFixture(t, c.options)
|
|
688
|
+
v := f.client()
|
|
689
|
+
f.seed(v, 12*hour)
|
|
690
|
+
var stderr bytes.Buffer
|
|
691
|
+
v.Stderr = &stderr
|
|
692
|
+
connect(t, v, false)
|
|
693
|
+
equal(t, stderr.String(), fmt.Sprintf("Vauth token: %s · %s\n", c.operation, c.duration))
|
|
694
|
+
}
|
|
695
|
+
f := newFixture(t, fixtureOptions{ttl: seconds(hour), renewStatus: 500})
|
|
696
|
+
v := f.client()
|
|
697
|
+
f.seed(v, 12*hour)
|
|
698
|
+
var stderr bytes.Buffer
|
|
699
|
+
v.Stderr = &stderr
|
|
700
|
+
if _, err := v.Connect(false); err == nil {
|
|
701
|
+
t.Fatal("connect should fail")
|
|
702
|
+
}
|
|
703
|
+
equal(t, stderr.String(), "")
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
func TestReplaceEnv(t *testing.T) {
|
|
707
|
+
dict := Lookup{"env": map[string]string{"USER": "alice"}, "profile": map[string]any{"TOKEN": "secret", "N": 3}}
|
|
708
|
+
equal(t, ReplaceEnv("$${env.USER}", dict), "alice")
|
|
709
|
+
equal(t, ReplaceEnv("x-$${profile.TOKEN}-$${profile.N}", dict), "x-secret-3")
|
|
710
|
+
equal(t, ReplaceEnv("$${profile.MISSING}", dict), "$${profile.MISSING}") // unresolved macros are kept, like nyks
|
|
711
|
+
equal(t, ReplaceEnv("plain", dict), "plain")
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
func sha256Hex(value string) string {
|
|
715
|
+
digest := sha256.Sum256([]byte(value))
|
|
716
|
+
return hex.EncodeToString(digest[:])
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
func TestEnvMapKeysMustBeVariableNames(t *testing.T) {
|
|
720
|
+
f := newFixture(t, fixtureOptions{})
|
|
721
|
+
v := f.client()
|
|
722
|
+
v.Config.Env = &EnvConfig{}
|
|
723
|
+
v.Config.Env.Map.Set("SAFE; printf INJECTED >&2; #", "dummy")
|
|
724
|
+
if _, err := v.Env(false); err == nil || !strings.Contains(err.Error(), "Invalid environment variable name") {
|
|
725
|
+
t.Fatalf("hostile env.map key accepted: %v", err)
|
|
726
|
+
}
|
|
727
|
+
v.Config.Env = &EnvConfig{}
|
|
728
|
+
v.Config.Env.Map.Set("TF_HTTP_USERNAME", "$${profile.VAUTH_USER_LOGIN}")
|
|
729
|
+
if _, err := v.Env(false); err != nil {
|
|
730
|
+
t.Fatal(err)
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
func TestCheckProjectVersion(t *testing.T) {
|
|
735
|
+
dir := t.TempDir()
|
|
736
|
+
manifest := filepath.Join(dir, "package.json")
|
|
737
|
+
write := func(body string) { os.WriteFile(manifest, []byte(body), 0o644) }
|
|
738
|
+
write(`{"dependencies":{"vvauth":"^2.5.0","yaml":"^2"}}`)
|
|
739
|
+
if err := CheckProjectVersion(manifest, "v2.7.1"); err != nil {
|
|
740
|
+
t.Fatal(err)
|
|
741
|
+
}
|
|
742
|
+
if err := CheckProjectVersion(manifest, "v3.0.0"); err == nil || !strings.Contains(err.Error(), "project requires ^2.5.0") {
|
|
743
|
+
t.Fatalf("v3 accepted by ^2.5.0: %v", err)
|
|
744
|
+
}
|
|
745
|
+
if err := CheckProjectVersion(manifest, "(devel)"); err != nil {
|
|
746
|
+
t.Fatal("devel builds must not be checked:", err)
|
|
747
|
+
}
|
|
748
|
+
write(`{"dependencies":{"yaml":"^2"}}`)
|
|
749
|
+
if err := CheckProjectVersion(manifest, "v0.0.1"); err != nil {
|
|
750
|
+
t.Fatal("unrelated dependencies must be ignored:", err)
|
|
751
|
+
}
|
|
752
|
+
if err := CheckProjectVersion(filepath.Join(dir, "missing.json"), "v1.0.0"); err != nil {
|
|
753
|
+
t.Fatal(err)
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
func TestProfileKVAndEnvironmentUseLoginTransport(t *testing.T) {
|
|
758
|
+
f := newFixture(t, fixtureOptions{
|
|
759
|
+
metadata: map[string]any{"VAUTH_USER_LOGIN": "alice", "VAUTH_USER_NAME": "Alice", "VAUTH_USER_MAIL": "alice@example.test"},
|
|
760
|
+
database: map[string]any{"TEAM": "ops"}, secrets: map[string]any{"API_KEY": "fixture-secret"},
|
|
761
|
+
})
|
|
762
|
+
v := f.client()
|
|
763
|
+
v.Config.Env = &EnvConfig{Git: true, Paths: []string{"app"}}
|
|
764
|
+
v.Config.Env.Map.Set("LOGIN", "$${profile.VAUTH_USER_LOGIN}")
|
|
765
|
+
v.Config.Env.Map.Set("TEAM", "$${profile.TEAM}")
|
|
766
|
+
v.Config.Env.Map.Set("API_KEY", "$${secrets.API_KEY}")
|
|
767
|
+
env, err := v.Env(true)
|
|
768
|
+
if err != nil {
|
|
769
|
+
t.Fatal(err)
|
|
770
|
+
}
|
|
771
|
+
for key, want := range map[string]string{"VAULT_TOKEN": "fresh-token", "LOGIN": "alice", "TEAM": "ops", "API_KEY": "fixture-secret", "GIT_AUTHOR_NAME": "Alice"} {
|
|
772
|
+
value, ok := env.Get(key)
|
|
773
|
+
if !ok {
|
|
774
|
+
t.Fatalf("missing %s", key)
|
|
775
|
+
}
|
|
776
|
+
equal(t, value, want)
|
|
777
|
+
}
|
|
778
|
+
equalPaths(t, f.paths(), "/v1/auth/jwt/login", "/v1/auth/token/lookup-self", "/v1/identity/entity/id/test-entity", "/v1/private/alice/data/.vauth_database", "/v1/secrets/data/app")
|
|
779
|
+
for _, r := range f.requests[1:] {
|
|
780
|
+
equal(t, r.Token, "fresh-token")
|
|
781
|
+
}
|
|
782
|
+
if err := v.Set("feature", "enabled"); err != nil {
|
|
783
|
+
t.Fatal(err)
|
|
784
|
+
}
|
|
785
|
+
equal(t, f.options.database["FEATURE"], "enabled")
|
|
786
|
+
if err := v.Unset("feature"); err != nil {
|
|
787
|
+
t.Fatal(err)
|
|
788
|
+
}
|
|
789
|
+
if _, ok := f.options.database["FEATURE"]; ok {
|
|
790
|
+
t.Fatal("unset key retained")
|
|
791
|
+
}
|
|
792
|
+
logins := 0
|
|
793
|
+
for _, r := range f.requests {
|
|
794
|
+
if strings.HasSuffix(r.Path, "/login") {
|
|
795
|
+
logins++
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
equal(t, logins, 1)
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
func TestEnvironmentConfigIsSeparateFromAuthentication(t *testing.T) {
|
|
802
|
+
config, err := ParseConfig([]byte(`jwt_auth: ignored
|
|
803
|
+
env: {map: {LOGIN: '$${env.LOGIN}'}}
|
|
804
|
+
ssh-agent-crypt: {path: '$${env.FILE}'}
|
|
805
|
+
`), ".vauthrc", map[string]string{"LOGIN": "alice", "FILE": "fixture.creds"})
|
|
806
|
+
if err != nil {
|
|
807
|
+
t.Fatal(err)
|
|
808
|
+
}
|
|
809
|
+
value, _ := config.Env.Map.Get("LOGIN")
|
|
810
|
+
equal(t, value, "alice")
|
|
811
|
+
equal(t, config.AgentCrypt.Path, "fixture.creds")
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
func TestNewConsumesCompleteRCSnapshot(t *testing.T) {
|
|
815
|
+
document := map[string]any{
|
|
816
|
+
"env": map[string]any{"map": map[string]any{"VALUE": "$${env.LOCAL_VALUE}", "USER": "$${profile.USER}"}},
|
|
817
|
+
"extra": map[string]any{"nested": []any{"retained"}},
|
|
818
|
+
}
|
|
819
|
+
v := configuredClient(t, document, map[string]string{"LOCAL_VALUE": "local-value", "VAULT_ADDR": "https://vault.example.test"})
|
|
820
|
+
value, _ := v.Config.Env.Map.Get("VALUE")
|
|
821
|
+
equal(t, value, "local-value")
|
|
822
|
+
value, _ = v.Config.Env.Map.Get("USER")
|
|
823
|
+
equal(t, value, "$${profile.USER}")
|
|
824
|
+
v.Config.Env.Map.Set("VALUE", "consumer-value")
|
|
825
|
+
equal(t, v.RC()["env"].(map[string]any)["map"].(map[string]any)["VALUE"], "$${env.LOCAL_VALUE}")
|
|
826
|
+
if _, ok := v.RC()["extra"]; !ok {
|
|
827
|
+
t.Fatal("complete RC section lost")
|
|
828
|
+
}
|
|
829
|
+
if err := os.Remove(os.Getenv("VAUTHRC")); err != nil {
|
|
830
|
+
t.Fatal(err)
|
|
831
|
+
}
|
|
832
|
+
config, err := configFromRC(v.RC(), ".", v.Environ)
|
|
833
|
+
if err != nil {
|
|
834
|
+
t.Fatal(err)
|
|
835
|
+
}
|
|
836
|
+
value, _ = config.Env.Map.Get("VALUE")
|
|
837
|
+
equal(t, value, "local-value")
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
func TestGitLabUsesWorkingProjectWithExternalRC(t *testing.T) {
|
|
841
|
+
project := t.TempDir()
|
|
842
|
+
for _, args := range [][]string{{"init", "-q", project}, {"-C", project, "remote", "add", "origin", "git@git.example.test:Team/app.git"}} {
|
|
843
|
+
if out, err := exec.Command("git", args...).CombinedOutput(); err != nil {
|
|
844
|
+
t.Fatalf("git: %v %s", err, out)
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
cwd, err := os.Getwd()
|
|
848
|
+
if err != nil {
|
|
849
|
+
t.Fatal(err)
|
|
850
|
+
}
|
|
851
|
+
if err := os.Chdir(project); err != nil {
|
|
852
|
+
t.Fatal(err)
|
|
853
|
+
}
|
|
854
|
+
t.Cleanup(func() {
|
|
855
|
+
if err := os.Chdir(cwd); err != nil {
|
|
856
|
+
t.Error(err)
|
|
857
|
+
}
|
|
858
|
+
})
|
|
859
|
+
for _, ci := range []string{"", "1"} {
|
|
860
|
+
v := configuredClient(t, map[string]any{"env": map[string]any{"gitlab": true, "map": map[string]any{"PROJECT": "$${env.CI_PROJECT_PATH}"}}}, map[string]string{"CI": ci, "CI_PROJECT_PATH": "pipeline/project"})
|
|
861
|
+
want := "Team/app"
|
|
862
|
+
if ci != "" {
|
|
863
|
+
want = "pipeline/project"
|
|
864
|
+
}
|
|
865
|
+
value, _ := v.Config.Env.Map.Get("PROJECT")
|
|
866
|
+
equal(t, value, want)
|
|
867
|
+
}
|
|
868
|
+
}
|