vvauth 2.5.0 → 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/test.yml +14 -0
- package/API_and_tests.md +144 -0
- package/README.md +87 -33
- package/SECURITY_AUDIT_fable-5.1.md +88 -0
- package/SECURITY_AUDIT_gpt-6.1-sol.md +196 -0
- package/cmd/vauth/main.go +19 -3
- package/go.mod +5 -3
- package/go.sum +12 -2
- package/index.js +146 -345
- package/package.json +7 -7
- package/rc.go +41 -148
- package/test/token-cache.js +271 -135
- package/vault.go +5 -197
- package/vauth.go +109 -228
- package/vauth_test.go +315 -111
- package/cache.go +0 -101
- package/humandiff.go +0 -50
package/test/token-cache.js
CHANGED
|
@@ -18,10 +18,12 @@ let agentDirectory, agentPid;
|
|
|
18
18
|
const previousSocket = process.env.SSH_AUTH_SOCK;
|
|
19
19
|
const previousIdentity = process.env.VAUTH_USER_IDENTITY;
|
|
20
20
|
const previousMail = process.env.VAUTH_USER_MAIL;
|
|
21
|
+
const previousCryptKey = process.env.SSH_AGENT_CRYPT_KEY;
|
|
21
22
|
|
|
22
23
|
before(() => {
|
|
23
24
|
delete process.env.VAUTH_USER_IDENTITY;
|
|
24
25
|
delete process.env.VAUTH_USER_MAIL;
|
|
26
|
+
delete process.env.SSH_AGENT_CRYPT_KEY;
|
|
25
27
|
agentDirectory = fs.mkdtempSync(path.join(os.tmpdir(), 'vvauth-agent-'));
|
|
26
28
|
const key = path.join(agentDirectory, 'key');
|
|
27
29
|
assert.equal(spawnSync('ssh-keygen', ['-q', '-t', 'ed25519', '-N', '', '-f', key]).status, 0);
|
|
@@ -33,7 +35,7 @@ before(() => {
|
|
|
33
35
|
});
|
|
34
36
|
|
|
35
37
|
after(() => {
|
|
36
|
-
for(const [name, value] of [['VAUTH_USER_IDENTITY', previousIdentity], ['VAUTH_USER_MAIL', previousMail]]) {
|
|
38
|
+
for(const [name, value] of [['VAUTH_USER_IDENTITY', previousIdentity], ['VAUTH_USER_MAIL', previousMail], ['SSH_AGENT_CRYPT_KEY', previousCryptKey]]) {
|
|
37
39
|
if(value === undefined)
|
|
38
40
|
delete process.env[name];
|
|
39
41
|
else
|
|
@@ -50,10 +52,32 @@ after(() => {
|
|
|
50
52
|
|
|
51
53
|
const HOUR = 60 * 60;
|
|
52
54
|
|
|
55
|
+
// Exercise real construction under an isolated RC, without changing private data.
|
|
56
|
+
function configuredClient(t, document, overrides = {}) {
|
|
57
|
+
const home = fs.mkdtempSync(path.join(os.tmpdir(), 'vvauth-config-'));
|
|
58
|
+
t.after(() => fs.rmSync(home, {recursive : true, force : true}));
|
|
59
|
+
const file = path.join(home, '.vauthrc');
|
|
60
|
+
fs.writeFileSync(file, JSON.stringify(document));
|
|
61
|
+
const env = {HOME : home, USERPROFILE : home, VAUTHRC : file, VAULT_TOKEN : '', VAULT_ADDR : '', ...overrides};
|
|
62
|
+
const previous = {};
|
|
63
|
+
for(const key of Object.keys(env)) { previous[key] = process.env[key]; process.env[key] = env[key]; }
|
|
64
|
+
const modules = [require.resolve('../index'), require.resolve('vauth-login')];
|
|
65
|
+
const cached = modules.map(file => require.cache[file]);
|
|
66
|
+
for(const file of modules) delete require.cache[file];
|
|
67
|
+
try { return new (require('../index'))(); } finally {
|
|
68
|
+
modules.forEach((file, i) => { require.cache[file] = cached[i]; });
|
|
69
|
+
for(const key of Object.keys(env)) {
|
|
70
|
+
if(previous[key] === undefined) delete process.env[key];
|
|
71
|
+
else process.env[key] = previous[key];
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
53
76
|
async function fixture(t, options = {}) {
|
|
54
77
|
const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'vvauth-test-'));
|
|
55
78
|
let file;
|
|
56
79
|
const requests = [];
|
|
80
|
+
let database = options.database || {};
|
|
57
81
|
const server = http.createServer(async (req, res) => {
|
|
58
82
|
let body = '';
|
|
59
83
|
for await(const chunk of req)
|
|
@@ -69,7 +93,12 @@ async function fixture(t, options = {}) {
|
|
|
69
93
|
res.statusCode = options.renew_status || 200;
|
|
70
94
|
res.end(JSON.stringify({auth : {client_token : req.headers['x-vault-token'], lease_duration : 24 * HOUR, renewable : true}}));
|
|
71
95
|
} else if(req.url === '/v1/identity/entity/id/test-entity') {
|
|
72
|
-
res.end(JSON.stringify({data : {metadata : {}}}));
|
|
96
|
+
res.end(JSON.stringify({data : {metadata : options.metadata || {}}}));
|
|
97
|
+
} else if(req.url === '/v1/private/alice/data/.vauth_database') {
|
|
98
|
+
if(req.method === 'POST') database = JSON.parse(body).data;
|
|
99
|
+
res.end(JSON.stringify({data : {data : database}}));
|
|
100
|
+
} else if(req.url === '/v1/secrets/data/app') {
|
|
101
|
+
res.end(JSON.stringify({data : {data : options.secrets || {}}}));
|
|
73
102
|
} else {
|
|
74
103
|
res.statusCode = 404;
|
|
75
104
|
res.end('{}');
|
|
@@ -80,11 +109,10 @@ async function fixture(t, options = {}) {
|
|
|
80
109
|
await new Promise(resolve => server.close(resolve));
|
|
81
110
|
fs.rmSync(directory, {recursive : true, force : true});
|
|
82
111
|
});
|
|
83
|
-
const client = () => {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
instance
|
|
87
|
-
instance.VAULT_ADDR = `http://127.0.0.1:${server.address().port}`;
|
|
112
|
+
const client = (settings = {}) => {
|
|
113
|
+
const rc = {vault_addr : `http://127.0.0.1:${server.address().port}`, jwt_auth : {path : 'jwt', jwt : 'test-jwt', role : 'test-role'}, ...settings.rc};
|
|
114
|
+
if(settings.noAuth) delete rc.jwt_auth;
|
|
115
|
+
const instance = configuredClient(t, rc, {VAULT_TOKEN : settings.token || '', ...settings.env});
|
|
88
116
|
instance._token_cache_file = () => path.join(directory, '.vauth', 'tokens', instance._token_cache_key() + '.creds');
|
|
89
117
|
return instance;
|
|
90
118
|
};
|
|
@@ -101,19 +129,19 @@ async function fixture(t, options = {}) {
|
|
|
101
129
|
await remaining(instance, options.ttl);
|
|
102
130
|
};
|
|
103
131
|
const read = async instance => readCache(instance._token_cache_file());
|
|
104
|
-
return {client, seed, read, requests, file, remaining};
|
|
132
|
+
return {client, seed, read, requests, file, remaining, database : () => database};
|
|
105
133
|
}
|
|
106
134
|
|
|
107
135
|
test('login persists a token; a new instance reuses it without logging in', async t => {
|
|
108
136
|
const {client, read, requests, file} = await fixture(t);
|
|
109
137
|
const first = client();
|
|
110
|
-
assert.equal(await first.
|
|
138
|
+
assert.equal(await first.Connect(), 'fresh-token');
|
|
111
139
|
assert.equal((await read(first)).token, 'fresh-token');
|
|
112
140
|
assert.ok((await read(first)).expires_at > Date.now() + 23 * HOUR * 1000);
|
|
113
141
|
assert.equal(fs.statSync(file).mode & 0o777, 0o600);
|
|
114
142
|
assert.equal(fs.statSync(path.dirname(file)).mode & 0o777, 0o700);
|
|
115
143
|
const second = client();
|
|
116
|
-
assert.equal(await second.
|
|
144
|
+
assert.equal(await second.Connect(), 'fresh-token');
|
|
117
145
|
assert.deepEqual(requests.map(req => req.path), ['/v1/auth/jwt/login']);
|
|
118
146
|
assert.deepEqual(fs.readdirSync(path.dirname(file)), [path.basename(file)]);
|
|
119
147
|
});
|
|
@@ -122,7 +150,7 @@ test('a token in the last third of its lease is renewed and saved', async t => {
|
|
|
122
150
|
const {client, seed, read, requests} = await fixture(t, {ttl : HOUR});
|
|
123
151
|
const instance = client();
|
|
124
152
|
await seed(instance, 12 * HOUR);
|
|
125
|
-
assert.equal(await instance.
|
|
153
|
+
assert.equal(await instance.Connect(), 'cached-token');
|
|
126
154
|
assert.deepEqual(requests.map(req => req.path), ['/v1/auth/token/renew-self']);
|
|
127
155
|
assert.equal(requests[0].method, 'POST');
|
|
128
156
|
assert.equal(requests[0].token, 'cached-token');
|
|
@@ -133,7 +161,7 @@ test('more than one third remaining does not trigger renewal', async t => {
|
|
|
133
161
|
const {client, seed, requests} = await fixture(t, {ttl : 4 * HOUR + 1});
|
|
134
162
|
const instance = client();
|
|
135
163
|
await seed(instance);
|
|
136
|
-
assert.equal(await instance.
|
|
164
|
+
assert.equal(await instance.Connect(), 'cached-token');
|
|
137
165
|
assert.equal(requests.length, 0);
|
|
138
166
|
});
|
|
139
167
|
|
|
@@ -144,7 +172,7 @@ test('expired cached tokens are replaced with a new persisted login', async t =>
|
|
|
144
172
|
const cache = (await readCache(file));
|
|
145
173
|
cache.expires_at = Date.now() - 1000;
|
|
146
174
|
await writeCache(file, cache);
|
|
147
|
-
assert.equal(await instance.
|
|
175
|
+
assert.equal(await instance.Connect(), 'fresh-token');
|
|
148
176
|
assert.equal((await read(instance)).token, 'fresh-token');
|
|
149
177
|
assert.deepEqual(requests.map(req => req.path), ['/v1/auth/jwt/login']);
|
|
150
178
|
});
|
|
@@ -153,16 +181,16 @@ test('cached tokens are reused without checking revocation', async t => {
|
|
|
153
181
|
const {client, seed, requests} = await fixture(t, {lookup_status : 403});
|
|
154
182
|
const instance = client();
|
|
155
183
|
await seed(instance);
|
|
156
|
-
assert.equal(await instance.
|
|
184
|
+
assert.equal(await instance.Connect(), 'cached-token');
|
|
157
185
|
assert.deepEqual(requests, []);
|
|
158
|
-
await assert.rejects(instance.
|
|
186
|
+
await assert.rejects(instance.LookupSelf(instance.VAULT_TOKEN));
|
|
159
187
|
});
|
|
160
188
|
|
|
161
189
|
test('nonrenewable tokens nearing expiry trigger reauthentication', async t => {
|
|
162
190
|
const {client, seed, requests} = await fixture(t, {ttl : HOUR, renewable : false});
|
|
163
191
|
const instance = client();
|
|
164
192
|
await seed(instance);
|
|
165
|
-
assert.equal(await instance.
|
|
193
|
+
assert.equal(await instance.Connect(), 'fresh-token');
|
|
166
194
|
assert.deepEqual(requests.map(req => req.path), ['/v1/auth/jwt/login']);
|
|
167
195
|
});
|
|
168
196
|
|
|
@@ -171,7 +199,7 @@ for(const status of [400, 403]) {
|
|
|
171
199
|
const {client, seed, read, requests} = await fixture(t, {ttl : HOUR, renew_status : status});
|
|
172
200
|
const instance = client();
|
|
173
201
|
await seed(instance);
|
|
174
|
-
assert.equal(await instance.
|
|
202
|
+
assert.equal(await instance.Connect(), 'fresh-token');
|
|
175
203
|
assert.equal((await read(instance)).token, 'fresh-token');
|
|
176
204
|
assert.deepEqual(requests.map(req => req.path), ['/v1/auth/token/renew-self', '/v1/auth/jwt/login']);
|
|
177
205
|
});
|
|
@@ -179,11 +207,11 @@ for(const status of [400, 403]) {
|
|
|
179
207
|
|
|
180
208
|
test('server failures do not generate extra tokens, and connect can be retried', async t => {
|
|
181
209
|
const {client, seed, requests} = await fixture(t, {lookup_status : 500});
|
|
182
|
-
const instance = client();
|
|
210
|
+
const instance = client({token : 'uncached-token'});
|
|
183
211
|
await seed(instance);
|
|
184
|
-
instance.
|
|
185
|
-
await assert.rejects(instance.
|
|
186
|
-
await assert.rejects(instance.
|
|
212
|
+
assert.equal(await instance.Connect(), 'uncached-token');
|
|
213
|
+
await assert.rejects(instance.LookupSelf());
|
|
214
|
+
await assert.rejects(instance.LookupSelf());
|
|
187
215
|
assert.deepEqual(requests.map(req => req.path), ['/v1/auth/token/lookup-self', '/v1/auth/token/lookup-self']);
|
|
188
216
|
});
|
|
189
217
|
|
|
@@ -191,26 +219,24 @@ test('renewal server failures do not generate extra tokens', async t => {
|
|
|
191
219
|
const {client, seed, requests} = await fixture(t, {ttl : HOUR, renew_status : 500});
|
|
192
220
|
const instance = client();
|
|
193
221
|
await seed(instance);
|
|
194
|
-
await assert.rejects(instance.
|
|
222
|
+
await assert.rejects(instance.Connect());
|
|
195
223
|
assert.deepEqual(requests.map(req => req.path), ['/v1/auth/token/renew-self']);
|
|
196
224
|
});
|
|
197
225
|
|
|
198
|
-
test('
|
|
226
|
+
test('inherited tokens are returned offline without changing the cache', async t => {
|
|
199
227
|
const {client, seed, read, requests} = await fixture(t);
|
|
200
|
-
const instance = client();
|
|
228
|
+
const instance = client({token : 'environment-token'});
|
|
201
229
|
await seed(instance);
|
|
202
|
-
instance.
|
|
203
|
-
assert.equal(await instance.
|
|
204
|
-
assert.
|
|
205
|
-
assert.equal(requests[0].token, 'environment-token');
|
|
206
|
-
assert.equal(requests.length, 1);
|
|
230
|
+
assert.equal(await instance.Connect(), 'environment-token');
|
|
231
|
+
assert.equal((await read(instance)).token, 'cached-token');
|
|
232
|
+
assert.deepEqual(requests, []);
|
|
207
233
|
});
|
|
208
234
|
|
|
209
235
|
test('nonexpiring tokens are reused without renewal', async t => {
|
|
210
236
|
const {client, seed, read, requests} = await fixture(t, {ttl : 0});
|
|
211
237
|
const instance = client();
|
|
212
238
|
await seed(instance, 0);
|
|
213
|
-
assert.equal(await instance.
|
|
239
|
+
assert.equal(await instance.Connect(), 'cached-token');
|
|
214
240
|
assert.equal((await read(instance)).expires_at, null);
|
|
215
241
|
assert.equal(requests.length, 0);
|
|
216
242
|
});
|
|
@@ -220,7 +246,7 @@ test('corrupt caches are replaced with a valid cache', async t => {
|
|
|
220
246
|
fs.mkdirSync(path.dirname(file), {recursive : true});
|
|
221
247
|
fs.writeFileSync(file, '{broken');
|
|
222
248
|
const instance = client();
|
|
223
|
-
assert.equal(await instance.
|
|
249
|
+
assert.equal(await instance.Connect(), 'fresh-token');
|
|
224
250
|
assert.equal((await read(instance)).token, 'fresh-token');
|
|
225
251
|
});
|
|
226
252
|
|
|
@@ -228,14 +254,12 @@ test('cache entries are isolated by Vault address and authentication configurati
|
|
|
228
254
|
const {client, seed, read, file} = await fixture(t);
|
|
229
255
|
const first = client();
|
|
230
256
|
await seed(first);
|
|
231
|
-
const second = client();
|
|
232
|
-
second.
|
|
233
|
-
assert.equal(await second.connect(), 'fresh-token');
|
|
257
|
+
const second = client({rc : {jwt_auth : {path : 'jwt', jwt : 'test-jwt', role : 'other-role'}}});
|
|
258
|
+
assert.equal(await second.Connect(), 'fresh-token');
|
|
234
259
|
assert.notEqual(first._token_cache_file(), second._token_cache_file());
|
|
235
260
|
assert.equal((await read(first)).token, 'cached-token');
|
|
236
261
|
assert.equal((await read(second)).token, 'fresh-token');
|
|
237
|
-
const third = client();
|
|
238
|
-
third.VAULT_ADDR = 'http://other-vault.test';
|
|
262
|
+
const third = client({rc : {vault_addr : 'http://other-vault.test'}});
|
|
239
263
|
assert.notEqual(third._token_cache_key(), first._token_cache_key());
|
|
240
264
|
assert.equal(fs.readdirSync(path.dirname(file)).length, 2);
|
|
241
265
|
assert.ok(!fs.readFileSync(file, 'utf8').includes('test-jwt'));
|
|
@@ -244,8 +268,8 @@ test('cache entries are isolated by Vault address and authentication configurati
|
|
|
244
268
|
test('repeated connect calls reuse the established connection', async t => {
|
|
245
269
|
const {client, requests} = await fixture(t);
|
|
246
270
|
const instance = client();
|
|
247
|
-
assert.equal(await instance.
|
|
248
|
-
assert.equal(await instance.
|
|
271
|
+
assert.equal(await instance.Connect(), 'fresh-token');
|
|
272
|
+
assert.equal(await instance.Connect(), 'fresh-token');
|
|
249
273
|
assert.equal(requests.length, 1);
|
|
250
274
|
});
|
|
251
275
|
|
|
@@ -253,26 +277,23 @@ test('an unwritable cache does not prevent login', async t => {
|
|
|
253
277
|
const {client, file} = await fixture(t);
|
|
254
278
|
fs.mkdirSync(path.dirname(path.dirname(file)), {recursive : true});
|
|
255
279
|
fs.writeFileSync(path.dirname(file), 'not a directory');
|
|
256
|
-
assert.equal(await client().
|
|
280
|
+
assert.equal(await client().Connect(), 'fresh-token');
|
|
257
281
|
});
|
|
258
282
|
|
|
259
|
-
test('without an auth method inherited tokens
|
|
283
|
+
test('without an auth method inherited tokens are returned but caches are ignored', async t => {
|
|
260
284
|
const {client, seed} = await fixture(t, {lookup_status : 403});
|
|
261
|
-
const instance = client();
|
|
262
|
-
instance.rc = {};
|
|
285
|
+
const instance = client({token : 'uncached-token', noAuth : true});
|
|
263
286
|
await seed(instance);
|
|
264
|
-
instance.VAULT_TOKEN = 'uncached-token';
|
|
265
287
|
instance._read_token_cache = async () => { throw new Error('No auth: no cache lookup'); };
|
|
266
|
-
assert.equal(await instance.
|
|
267
|
-
assert.equal(instance.VAULT_TOKEN,
|
|
288
|
+
assert.equal(await instance.Connect(), 'uncached-token');
|
|
289
|
+
assert.equal(instance.VAULT_TOKEN, 'uncached-token');
|
|
268
290
|
});
|
|
269
291
|
|
|
270
292
|
test('--renew ignores both environment and cached tokens and saves a new token', async t => {
|
|
271
293
|
const {client, seed, read, requests} = await fixture(t);
|
|
272
|
-
const instance = client();
|
|
294
|
+
const instance = client({token : 'environment-token'});
|
|
273
295
|
await seed(instance);
|
|
274
|
-
instance.
|
|
275
|
-
assert.equal(await instance.connect(true), 'fresh-token');
|
|
296
|
+
assert.equal(await instance.Connect(true), 'fresh-token');
|
|
276
297
|
assert.deepEqual(requests.map(req => req.path), ['/v1/auth/jwt/login']);
|
|
277
298
|
assert.equal((await read(instance)).token, 'fresh-token');
|
|
278
299
|
assert.ok((await read(instance)).expires_at > Date.now() + 23 * HOUR * 1000);
|
|
@@ -282,8 +303,8 @@ test('--renew replaces the token of a previously connected instance', async t =>
|
|
|
282
303
|
const {client, seed, read, requests} = await fixture(t);
|
|
283
304
|
const instance = client();
|
|
284
305
|
await seed(instance);
|
|
285
|
-
await instance.
|
|
286
|
-
assert.equal(await instance.
|
|
306
|
+
await instance.Connect();
|
|
307
|
+
assert.equal(await instance.Connect(true), 'fresh-token');
|
|
287
308
|
assert.deepEqual(requests.map(req => req.path), ['/v1/auth/jwt/login']);
|
|
288
309
|
assert.equal((await read(instance)).token, 'fresh-token');
|
|
289
310
|
});
|
|
@@ -291,16 +312,15 @@ test('--renew replaces the token of a previously connected instance', async t =>
|
|
|
291
312
|
test('each successive --renew call forces a new login', async t => {
|
|
292
313
|
const {client, requests} = await fixture(t);
|
|
293
314
|
const instance = client();
|
|
294
|
-
assert.equal(await instance.
|
|
295
|
-
assert.equal(await instance.
|
|
315
|
+
assert.equal(await instance.Connect(true), 'fresh-token');
|
|
316
|
+
assert.equal(await instance.Connect(true), 'fresh-token');
|
|
296
317
|
assert.equal(requests.length, 2);
|
|
297
318
|
});
|
|
298
319
|
|
|
299
320
|
test('--renew without an authentication method remains a no-op', async t => {
|
|
300
321
|
const {client} = await fixture(t);
|
|
301
|
-
const instance = client();
|
|
302
|
-
instance.
|
|
303
|
-
assert.equal(await instance.connect(true), undefined);
|
|
322
|
+
const instance = client({noAuth : true});
|
|
323
|
+
assert.equal(await instance.Connect(true), undefined);
|
|
304
324
|
});
|
|
305
325
|
|
|
306
326
|
for(const flag of ['renew']) {
|
|
@@ -308,8 +328,8 @@ for(const flag of ['renew']) {
|
|
|
308
328
|
const {client} = await fixture(t);
|
|
309
329
|
const instance = client();
|
|
310
330
|
let forced;
|
|
311
|
-
instance.
|
|
312
|
-
assert.deepEqual(await instance.
|
|
331
|
+
instance.Connect = async renew => { forced = renew; };
|
|
332
|
+
assert.deepEqual(await instance.Env(false, true), {VAULT_ADDR : instance.VAULT_ADDR});
|
|
313
333
|
assert.equal(forced, true);
|
|
314
334
|
});
|
|
315
335
|
|
|
@@ -320,7 +340,7 @@ for(const flag of ['renew']) {
|
|
|
320
340
|
await seed(instance);
|
|
321
341
|
const home = path.dirname(path.dirname(path.dirname(file)));
|
|
322
342
|
const rc = path.join(home, '.vauthrc');
|
|
323
|
-
fs.writeFileSync(rc, JSON.stringify({vault_addr : instance.VAULT_ADDR, ...instance.
|
|
343
|
+
fs.writeFileSync(rc, JSON.stringify({vault_addr : instance.VAULT_ADDR, ...instance.RC}));
|
|
324
344
|
const options = {
|
|
325
345
|
cwd : home,
|
|
326
346
|
env : {...process.env, HOME : home, VAUTHRC : rc, VAULT_TOKEN : 'environment-token', SSH_AUTH_SOCK : process.env.SSH_AUTH_SOCK},
|
|
@@ -329,7 +349,7 @@ for(const flag of ['renew']) {
|
|
|
329
349
|
const output = await exec(process.execPath, [cli, command, '--source', `--${flag}`], options);
|
|
330
350
|
assert.ok(output.stdout.includes("export VAULT_TOKEN='fresh-token'"));
|
|
331
351
|
assert.ok(!output.stdout.includes('Vauth token: '));
|
|
332
|
-
assert.equal(output.stderr.split('\n').filter(line => line === `Vauth token: generated ·
|
|
352
|
+
assert.equal(output.stderr.split('\n').filter(line => line === `Vauth token: generated · 1d`).length, 1);
|
|
333
353
|
assert.ok(!output.stderr.includes('Vauth token: renewed'));
|
|
334
354
|
assert.ok(!output.stderr.includes('fresh-token'));
|
|
335
355
|
assert.ok(!output.stderr.includes('environment-token'));
|
|
@@ -343,7 +363,7 @@ for(const flag of ['renew']) {
|
|
|
343
363
|
});
|
|
344
364
|
assert.ok(reused.stdout.includes("export VAULT_TOKEN='fresh-token'"));
|
|
345
365
|
assert.equal(reused.stderr.split('\n').filter(line => line.startsWith('Vauth token: ')).length, 1);
|
|
346
|
-
assert.ok(reused.stderr.includes(`Vauth token: reused ·
|
|
366
|
+
assert.ok(reused.stderr.includes(`Vauth token: reused · 1d`));
|
|
347
367
|
assert.ok(!requests.some(req => req.path.endsWith('/login') || req.path.endsWith('/renew-self')));
|
|
348
368
|
});
|
|
349
369
|
}
|
|
@@ -358,7 +378,7 @@ test('installed venv forwards --renew through the real CLI', async t => {
|
|
|
358
378
|
instance._function_exists = async () => 1;
|
|
359
379
|
try {
|
|
360
380
|
os.homedir = () => home;
|
|
361
|
-
await instance.
|
|
381
|
+
await instance.Install();
|
|
362
382
|
} finally {
|
|
363
383
|
os.homedir = original_home;
|
|
364
384
|
}
|
|
@@ -368,7 +388,7 @@ test('installed venv forwards --renew through the real CLI', async t => {
|
|
|
368
388
|
fs.mkdirSync(bin);
|
|
369
389
|
fs.symlinkSync(path.resolve(__dirname, '..', 'index.js'), path.join(bin, 'vauth'));
|
|
370
390
|
const rc = path.join(home, '.vauthrc');
|
|
371
|
-
fs.writeFileSync(rc, JSON.stringify({vault_addr : instance.VAULT_ADDR, ...instance.
|
|
391
|
+
fs.writeFileSync(rc, JSON.stringify({vault_addr : instance.VAULT_ADDR, ...instance.RC}));
|
|
372
392
|
const options = {
|
|
373
393
|
cwd : home,
|
|
374
394
|
env : {...process.env, HOME : home, VAUTHRC : rc, VAULT_TOKEN : '', SSH_AUTH_SOCK : process.env.SSH_AUTH_SOCK, PATH : `${bin}:${process.env.PATH}`},
|
|
@@ -376,12 +396,12 @@ test('installed venv forwards --renew through the real CLI', async t => {
|
|
|
376
396
|
const initial = await exec('bash', ['-c', `${declaration}\nvenv; printf '%s' "$VAULT_TOKEN"`], options);
|
|
377
397
|
assert.equal(initial.stdout, 'cached-token');
|
|
378
398
|
assert.equal(initial.stderr.split('\n').filter(line => line.startsWith('Vauth token: ')).length, 1);
|
|
379
|
-
assert.ok(initial.stderr.includes(`Vauth token: reused ·
|
|
399
|
+
assert.ok(initial.stderr.includes(`Vauth token: reused · 12h`));
|
|
380
400
|
assert.ok(!requests.some(req => req.path.endsWith('/login') || req.path.endsWith('/renew-self')));
|
|
381
401
|
for(const flag of ['--renew']) {
|
|
382
402
|
const output = await exec('bash', ['-c', `${declaration}\nvenv ${flag}; printf '%s' "$VAULT_TOKEN"`], options);
|
|
383
403
|
assert.equal(output.stdout, 'fresh-token');
|
|
384
|
-
assert.equal(output.stderr.split('\n').filter(line => line === `Vauth token: generated ·
|
|
404
|
+
assert.equal(output.stderr.split('\n').filter(line => line === `Vauth token: generated · 1d`).length, 1);
|
|
385
405
|
assert.ok(!output.stderr.includes('Vauth token: renewed'));
|
|
386
406
|
assert.ok(!output.stderr.includes('fresh-token'));
|
|
387
407
|
assert.equal((await read(instance)).token, 'fresh-token');
|
|
@@ -401,7 +421,7 @@ for(const answer of ['yes', 'no', '']) {
|
|
|
401
421
|
const script = `const Vauth = require(${JSON.stringify(module_path)});
|
|
402
422
|
const instance = Object.create(Vauth.prototype);
|
|
403
423
|
instance._function_exists = async () => 0;
|
|
404
|
-
instance.
|
|
424
|
+
instance.Install().catch(() => process.exit(1));`;
|
|
405
425
|
const child = spawn(process.execPath, ['-e', script], {cwd : home, env : {...process.env, HOME : home}});
|
|
406
426
|
let output = '', prompted = false;
|
|
407
427
|
child.stdout.on('data', chunk => {
|
|
@@ -437,7 +457,6 @@ function captureStatus(callback) {
|
|
|
437
457
|
|
|
438
458
|
test('token status uses humanDiff with two significant units', () => {
|
|
439
459
|
const instance = Object.create(Vauth.prototype);
|
|
440
|
-
instance.VAULT_ADDR = 'https://vault.example.test';
|
|
441
460
|
const cases = [
|
|
442
461
|
[2 * 86400 + 3 * HOUR + 20 * 60 + 5, '2d 3h'],
|
|
443
462
|
[12 * HOUR + 30 * 60 + 5, '12h 30m'],
|
|
@@ -452,14 +471,14 @@ test('token status uses humanDiff with two significant units', () => {
|
|
|
452
471
|
];
|
|
453
472
|
for(const [ttl, duration] of cases) {
|
|
454
473
|
assert.deepEqual(captureStatus(() => instance._token_status('reused', {ttl})), [
|
|
455
|
-
`Vauth token: reused · ${
|
|
474
|
+
`Vauth token: reused · ${duration}`,
|
|
456
475
|
]);
|
|
457
476
|
}
|
|
458
477
|
assert.deepEqual(captureStatus(() => instance._token_status('renewed', {lease_duration : HOUR + 60})), [
|
|
459
|
-
`Vauth token: renewed ·
|
|
478
|
+
`Vauth token: renewed · 1h 1m`,
|
|
460
479
|
]);
|
|
461
480
|
assert.deepEqual(captureStatus(() => instance._token_status('generated', {lease_duration : 0})), [
|
|
462
|
-
`Vauth token: generated ·
|
|
481
|
+
`Vauth token: generated · valid indefinitely`,
|
|
463
482
|
]);
|
|
464
483
|
});
|
|
465
484
|
|
|
@@ -478,21 +497,20 @@ for(const options of [
|
|
|
478
497
|
instance._token_status = (operation, metadata) => {
|
|
479
498
|
calls.push(...captureStatus(() => Vauth.prototype._token_status.call(instance, operation, metadata)));
|
|
480
499
|
};
|
|
481
|
-
await instance.
|
|
482
|
-
await instance.
|
|
500
|
+
await instance.Connect();
|
|
501
|
+
await instance.Connect();
|
|
483
502
|
const validity = options.duration ? `valid for ${options.duration}` : 'valid indefinitely';
|
|
484
|
-
assert.deepEqual(calls, [`Vauth token: ${options.operation} · ${
|
|
503
|
+
assert.deepEqual(calls, [`Vauth token: ${options.operation} · ${validity.replace(/^valid for /, '')}`]);
|
|
485
504
|
});
|
|
486
505
|
}
|
|
487
506
|
|
|
488
507
|
test('failed connections never announce successful token status', async t => {
|
|
489
|
-
const {client, seed} = await fixture(t, {
|
|
508
|
+
const {client, seed} = await fixture(t, {ttl : HOUR, renew_status : 500});
|
|
490
509
|
const instance = client();
|
|
491
510
|
await seed(instance);
|
|
492
|
-
instance.VAULT_TOKEN = 'uncached-token';
|
|
493
511
|
const calls = [];
|
|
494
512
|
instance._token_status = operation => calls.push(operation);
|
|
495
|
-
await assert.rejects(instance.
|
|
513
|
+
await assert.rejects(instance.Connect());
|
|
496
514
|
assert.deepEqual(calls, []);
|
|
497
515
|
});
|
|
498
516
|
|
|
@@ -508,7 +526,7 @@ for(const [lease, remaining, renew] of [
|
|
|
508
526
|
const {client, seed, read, requests} = await fixture(t, {ttl : remaining});
|
|
509
527
|
const instance = client();
|
|
510
528
|
await seed(instance, lease);
|
|
511
|
-
await instance.
|
|
529
|
+
await instance.Connect();
|
|
512
530
|
assert.equal(requests.some(req => req.path.endsWith('/renew-self')), renew);
|
|
513
531
|
assert.equal((await read(instance)).lease_duration, renew ? 24 * HOUR : lease);
|
|
514
532
|
});
|
|
@@ -519,13 +537,13 @@ test('lookups never reset the original lease duration', async t => {
|
|
|
519
537
|
const {client, seed, read, requests, remaining} = await fixture(t, options);
|
|
520
538
|
const instance = client();
|
|
521
539
|
await seed(instance, 24 * HOUR);
|
|
522
|
-
await instance.
|
|
540
|
+
await instance.Connect();
|
|
523
541
|
assert.equal((await read(instance)).lease_duration, 24 * HOUR);
|
|
524
542
|
await remaining(instance, 12 * HOUR);
|
|
525
|
-
await client().
|
|
543
|
+
await client().Connect();
|
|
526
544
|
assert.equal((await read(instance)).lease_duration, 24 * HOUR);
|
|
527
545
|
await remaining(instance, 8 * HOUR);
|
|
528
|
-
await client().
|
|
546
|
+
await client().Connect();
|
|
529
547
|
assert.equal(requests.filter(req => req.path.endsWith('/renew-self')).length, 1);
|
|
530
548
|
});
|
|
531
549
|
|
|
@@ -534,22 +552,21 @@ test('renewal updates the lease baseline to the newly granted duration', async t
|
|
|
534
552
|
const {client, seed, read, requests, remaining} = await fixture(t, options);
|
|
535
553
|
const instance = client();
|
|
536
554
|
await seed(instance, HOUR);
|
|
537
|
-
await instance.
|
|
555
|
+
await instance.Connect();
|
|
538
556
|
assert.equal((await read(instance)).lease_duration, 24 * HOUR);
|
|
539
557
|
await remaining(instance, 8 * HOUR);
|
|
540
|
-
await client().
|
|
558
|
+
await client().Connect();
|
|
541
559
|
assert.equal(requests.filter(req => req.path.endsWith('/renew-self')).length, 2);
|
|
542
560
|
});
|
|
543
561
|
|
|
544
562
|
test('normal reuse never writes the token cache', async t => {
|
|
545
563
|
const {client, seed, file, requests} = await fixture(t);
|
|
546
|
-
const instance = client();
|
|
564
|
+
const instance = client({token : 'cached-token'});
|
|
547
565
|
await seed(instance);
|
|
548
|
-
instance.VAULT_TOKEN = 'cached-token';
|
|
549
566
|
const before = fs.readFileSync(file, 'utf8');
|
|
550
567
|
const stat = fs.statSync(file);
|
|
551
568
|
instance._save_cached_token = () => { throw new Error('Reuse must not write'); };
|
|
552
|
-
assert.equal(await instance.
|
|
569
|
+
assert.equal(await instance.Connect(), 'cached-token');
|
|
553
570
|
assert.equal(fs.readFileSync(file, 'utf8'), before);
|
|
554
571
|
assert.equal(fs.statSync(file).ino, stat.ino);
|
|
555
572
|
assert.equal(fs.statSync(file).mtimeMs, stat.mtimeMs);
|
|
@@ -560,46 +577,41 @@ test('normal reuse never writes the token cache', async t => {
|
|
|
560
577
|
test('lookup returns the lease duration and preserves a cached renewal baseline', async t => {
|
|
561
578
|
const {client, seed, requests} = await fixture(t, {ttl : HOUR, creation_ttl : 24 * HOUR});
|
|
562
579
|
const instance = client();
|
|
563
|
-
const uncached = await instance.
|
|
580
|
+
const uncached = await instance.LookupSelf('environment-token');
|
|
564
581
|
assert.equal(uncached.lease_duration, 24 * HOUR);
|
|
565
582
|
assert.equal(uncached.ttl, HOUR);
|
|
566
583
|
await seed(instance, 12 * HOUR);
|
|
567
|
-
const cached = await instance.
|
|
584
|
+
const cached = await instance.LookupSelf('cached-token');
|
|
568
585
|
assert.equal(cached.lease_duration, 12 * HOUR);
|
|
569
586
|
assert.equal(requests.length, 2);
|
|
570
587
|
});
|
|
571
588
|
|
|
572
589
|
test('token_cache false skips all cache filesystem access', async t => {
|
|
573
590
|
const {client, requests, file} = await fixture(t);
|
|
574
|
-
const instance = client();
|
|
575
|
-
instance.rc.token_cache = false;
|
|
591
|
+
const instance = client({rc : {token_cache : false}});
|
|
576
592
|
instance._token_cache_file = () => { throw new Error('Disabled cache must not access disk'); };
|
|
577
|
-
assert.equal(await instance.
|
|
593
|
+
assert.equal(await instance.Connect(), 'fresh-token');
|
|
578
594
|
assert.equal(fs.existsSync(file), false);
|
|
579
595
|
assert.deepEqual(requests.map(req => req.path), ['/v1/auth/jwt/login']);
|
|
580
596
|
});
|
|
581
597
|
|
|
582
598
|
test('token_cache false ignores existing tokens on disk without deleting them', async t => {
|
|
583
599
|
const {client, seed, requests, file} = await fixture(t);
|
|
584
|
-
|
|
585
|
-
|
|
600
|
+
await seed(client());
|
|
601
|
+
const instance = client({rc : {token_cache : false}});
|
|
586
602
|
const before = fs.readFileSync(file, 'utf8');
|
|
587
|
-
instance.
|
|
588
|
-
assert.equal(await instance.connect(), 'fresh-token');
|
|
603
|
+
assert.equal(await instance.Connect(), 'fresh-token');
|
|
589
604
|
assert.equal(fs.readFileSync(file, 'utf8'), before);
|
|
590
|
-
const second = client();
|
|
591
|
-
second.
|
|
592
|
-
assert.equal(await second.connect(), 'fresh-token');
|
|
605
|
+
const second = client({rc : {token_cache : false}});
|
|
606
|
+
assert.equal(await second.Connect(), 'fresh-token');
|
|
593
607
|
assert.equal(requests.filter(req => req.path.endsWith('/login')).length, 2);
|
|
594
608
|
});
|
|
595
609
|
|
|
596
610
|
test('token_cache false still respects an environment token without persisting it', async t => {
|
|
597
611
|
const {client, requests, file} = await fixture(t);
|
|
598
|
-
const instance = client();
|
|
599
|
-
instance.
|
|
600
|
-
|
|
601
|
-
assert.equal(await instance.connect(), 'environment-token');
|
|
602
|
-
assert.deepEqual(requests.map(req => req.path), ['/v1/auth/token/lookup-self']);
|
|
612
|
+
const instance = client({token : 'environment-token', rc : {token_cache : false}});
|
|
613
|
+
assert.equal(await instance.Connect(), 'environment-token');
|
|
614
|
+
assert.deepEqual(requests, []);
|
|
603
615
|
assert.equal(fs.existsSync(file), false);
|
|
604
616
|
});
|
|
605
617
|
|
|
@@ -608,7 +620,7 @@ test('real CLI reads token_cache false from .vauthrc', async t => {
|
|
|
608
620
|
const instance = client();
|
|
609
621
|
const home = path.dirname(path.dirname(path.dirname(file)));
|
|
610
622
|
const rc = path.join(home, '.vauthrc');
|
|
611
|
-
fs.writeFileSync(rc, JSON.stringify({vault_addr : instance.VAULT_ADDR, ...instance.
|
|
623
|
+
fs.writeFileSync(rc, JSON.stringify({vault_addr : instance.VAULT_ADDR, ...instance.RC, token_cache : false}));
|
|
612
624
|
const cli = path.resolve(__dirname, '..', 'index.js');
|
|
613
625
|
for(let i = 0; i < 2; i++) {
|
|
614
626
|
const output = await exec(process.execPath, [cli, 'login', '--source'], {
|
|
@@ -625,9 +637,9 @@ test('real CLI reads token_cache false from .vauthrc', async t => {
|
|
|
625
637
|
test('disk cache contains only armor, interoperable with ssh-agent-crypt CLI', async t => {
|
|
626
638
|
const {client, file} = await fixture(t);
|
|
627
639
|
const instance = client();
|
|
628
|
-
await instance.
|
|
640
|
+
await instance.Connect();
|
|
629
641
|
const armor = fs.readFileSync(file, 'utf8');
|
|
630
|
-
assert.match(armor, /^ssh-agent-crypt:
|
|
642
|
+
assert.match(armor, /^ssh-agent-crypt:v3:/);
|
|
631
643
|
assert.ok(!armor.includes('fresh-token'));
|
|
632
644
|
assert.ok(!armor.includes('lease_duration'));
|
|
633
645
|
assert.ok(!armor.includes('test-jwt'));
|
|
@@ -642,23 +654,22 @@ test('a plaintext cache is not accepted as a token cache', async t => {
|
|
|
642
654
|
const instance = client();
|
|
643
655
|
fs.mkdirSync(path.dirname(file), {recursive : true});
|
|
644
656
|
fs.writeFileSync(file, JSON.stringify({token : 'plaintext-token', lease_duration : 86400, expires_at : Date.now() + 86400000}));
|
|
645
|
-
assert.equal(await instance.
|
|
657
|
+
assert.equal(await instance.Connect(), 'fresh-token');
|
|
646
658
|
assert.deepEqual(requests.map(req => req.path), ['/v1/auth/jwt/login']);
|
|
647
|
-
assert.match(fs.readFileSync(file, 'utf8'), /^ssh-agent-crypt:
|
|
659
|
+
assert.match(fs.readFileSync(file, 'utf8'), /^ssh-agent-crypt:v3:/);
|
|
648
660
|
});
|
|
649
661
|
|
|
650
662
|
test('without an SSH agent cache is skipped silently', async t => {
|
|
651
663
|
const {client, file} = await fixture(t);
|
|
652
|
-
const instance = client();
|
|
664
|
+
const instance = client({env : {SSH_AUTH_SOCK : ''}});
|
|
653
665
|
instance._token_cache_file = () => { throw new Error('No agent: no disk access'); };
|
|
654
666
|
const socket = process.env.SSH_AUTH_SOCK;
|
|
655
667
|
const original = console.error;
|
|
656
668
|
const messages = [];
|
|
657
669
|
try {
|
|
658
670
|
delete process.env.SSH_AUTH_SOCK;
|
|
659
|
-
instance.rc.token_cache = false;
|
|
660
671
|
console.error = (...args) => messages.push(format(...args));
|
|
661
|
-
assert.equal(await instance.
|
|
672
|
+
assert.equal(await instance.Connect(), 'fresh-token');
|
|
662
673
|
} finally {
|
|
663
674
|
process.env.SSH_AUTH_SOCK = socket;
|
|
664
675
|
console.error = original;
|
|
@@ -671,13 +682,12 @@ test('without an SSH agent cache is skipped silently', async t => {
|
|
|
671
682
|
|
|
672
683
|
test('without an SSH agent existing armor is left untouched', async t => {
|
|
673
684
|
const {client, seed, file} = await fixture(t);
|
|
674
|
-
|
|
675
|
-
|
|
685
|
+
await seed(client());
|
|
686
|
+
const instance = client({env : {SSH_AUTH_SOCK : ''}});
|
|
676
687
|
const before = fs.readFileSync(file, 'utf8');
|
|
677
688
|
const socket = process.env.SSH_AUTH_SOCK;
|
|
678
689
|
try {
|
|
679
690
|
delete process.env.SSH_AUTH_SOCK;
|
|
680
|
-
instance.rc.token_cache = false;
|
|
681
691
|
await instance._save_cached_token('replacement-token', {lease_duration : 86400, renewable : true});
|
|
682
692
|
} finally {
|
|
683
693
|
process.env.SSH_AUTH_SOCK = socket;
|
|
@@ -692,17 +702,18 @@ test('constructor disables token caching when no SSH agent is configured', async
|
|
|
692
702
|
const instance = client();
|
|
693
703
|
const home = path.dirname(path.dirname(path.dirname(file)));
|
|
694
704
|
const rc = path.join(home, '.vauthrc');
|
|
695
|
-
fs.writeFileSync(rc, JSON.stringify({vault_addr : instance.VAULT_ADDR, ...instance.
|
|
705
|
+
fs.writeFileSync(rc, JSON.stringify({vault_addr : instance.VAULT_ADDR, ...instance.RC, token_cache : true}));
|
|
696
706
|
const module_path = path.resolve(__dirname, '..', 'index.js');
|
|
697
707
|
const script = `const Vauth = require(${JSON.stringify(module_path)});
|
|
698
708
|
const instance = new Vauth();
|
|
699
|
-
|
|
700
|
-
instance.
|
|
709
|
+
let accesses = 0;
|
|
710
|
+
instance._token_cache_file = () => { accesses++; throw new Error('Cache must be disabled'); };
|
|
711
|
+
instance.Login().then(() => process.stdout.write(String(accesses))).catch(() => process.exit(1));`;
|
|
701
712
|
const output = await exec(process.execPath, ['-e', script], {
|
|
702
713
|
cwd : home,
|
|
703
714
|
env : {...process.env, HOME : home, VAUTHRC : rc, VAULT_TOKEN : '', SSH_AUTH_SOCK : ''},
|
|
704
715
|
});
|
|
705
|
-
assert.equal(output.stdout.trim(), '
|
|
716
|
+
assert.equal(output.stdout.trim(), '0');
|
|
706
717
|
assert.ok(!output.stderr.includes('token not persisted'));
|
|
707
718
|
assert.equal(fs.existsSync(file), false);
|
|
708
719
|
});
|
|
@@ -724,13 +735,13 @@ for(const selected of ['identity', 'mail', 'default']) {
|
|
|
724
735
|
} else if(selected === 'mail') {
|
|
725
736
|
process.env.VAUTH_USER_MAIL = comment;
|
|
726
737
|
}
|
|
727
|
-
assert.equal(await instance.
|
|
738
|
+
assert.equal(await instance.Connect(), 'fresh-token');
|
|
728
739
|
const fingerprint = spawnSync('ssh-keygen', ['-lf', key + '.pub'], {encoding : 'utf8'}).stdout.split(/\s+/)[1];
|
|
729
740
|
const armor = fs.readFileSync(file, 'utf8');
|
|
730
|
-
assert.equal(armor.startsWith(`ssh-agent-crypt:
|
|
741
|
+
assert.equal(armor.startsWith(`ssh-agent-crypt:v3:${fingerprint}.`), selected !== 'default');
|
|
731
742
|
delete process.env.VAUTH_USER_IDENTITY;
|
|
732
743
|
delete process.env.VAUTH_USER_MAIL;
|
|
733
|
-
assert.equal(await client().
|
|
744
|
+
assert.equal(await client().Connect(), 'fresh-token');
|
|
734
745
|
} finally {
|
|
735
746
|
spawnSync('ssh-add', ['-d', key]);
|
|
736
747
|
delete process.env.VAUTH_USER_IDENTITY;
|
|
@@ -746,15 +757,15 @@ for(const explicit of [false, true]) {
|
|
|
746
757
|
const credentials = path.join(path.dirname(path.dirname(path.dirname(file))), '.creds');
|
|
747
758
|
const identity = spawnSync('ssh-add', ['-l'], {encoding : 'utf8'}).stdout.split(/\s+/)[1];
|
|
748
759
|
fs.writeFileSync(credentials, await encrypt(JSON.stringify({TEST_SECRET : 'module-secret'})));
|
|
749
|
-
instance.
|
|
760
|
+
instance.config['ssh-agent-crypt'] = {path : credentials};
|
|
750
761
|
if(explicit)
|
|
751
|
-
instance.
|
|
752
|
-
instance.
|
|
753
|
-
instance.
|
|
762
|
+
instance.config['ssh-agent-crypt'].identity = identity;
|
|
763
|
+
instance.config.env = {map : {EXPORTED_SECRET : '$${secrets.TEST_SECRET}'}};
|
|
764
|
+
instance.GetProfile = async () => ({profile : {}, database : {}});
|
|
754
765
|
const previousPath = process.env.PATH;
|
|
755
766
|
try {
|
|
756
767
|
process.env.PATH = path.dirname(credentials);
|
|
757
|
-
const env = await instance.
|
|
768
|
+
const env = await instance.Env();
|
|
758
769
|
assert.equal(env.EXPORTED_SECRET, 'module-secret');
|
|
759
770
|
} finally {
|
|
760
771
|
process.env.PATH = previousPath;
|
|
@@ -767,25 +778,24 @@ test('.creds decryption failures reject instead of exiting successfully', async
|
|
|
767
778
|
const instance = client();
|
|
768
779
|
const credentials = path.join(path.dirname(path.dirname(path.dirname(file))), '.creds');
|
|
769
780
|
fs.writeFileSync(credentials, 'not an armored payload');
|
|
770
|
-
instance.
|
|
771
|
-
instance.
|
|
772
|
-
await assert.rejects(instance.
|
|
781
|
+
instance.config['ssh-agent-crypt'] = {path : credentials};
|
|
782
|
+
instance.GetProfile = async () => ({profile : {}, database : {}});
|
|
783
|
+
await assert.rejects(instance.Env(), /Unsupported format header/);
|
|
773
784
|
});
|
|
774
785
|
|
|
775
786
|
for(const inherited of ['', 'inherited-token']) {
|
|
776
787
|
test(`local .creds environment works offline with inherited token=${!!inherited}`, async t => {
|
|
777
788
|
const {client, file, requests} = await fixture(t, {lookup_status : 503});
|
|
778
|
-
const instance = client();
|
|
789
|
+
const instance = client({noAuth : true, token : inherited});
|
|
779
790
|
const credentials = path.join(path.dirname(path.dirname(path.dirname(file))), '.creds');
|
|
780
791
|
fs.writeFileSync(credentials, await encrypt(JSON.stringify({ARM_CLIENT_SECRET : 'local-secret', VAULT_TOKEN : 'local-token'})));
|
|
781
|
-
instance.
|
|
792
|
+
instance.config = {
|
|
782
793
|
'ssh-agent-crypt' : {path : credentials},
|
|
783
794
|
env : {map : {ARM_CLIENT_SECRET : '$${secrets.ARM_CLIENT_SECRET}', VAULT_TOKEN : '$${secrets.VAULT_TOKEN}'}},
|
|
784
795
|
};
|
|
785
|
-
instance.
|
|
786
|
-
instance._lookup_token = async () => { throw new Error('Local environment must not contact Vault'); };
|
|
796
|
+
instance.LookupSelf = async () => { throw new Error('Local environment must not contact Vault'); };
|
|
787
797
|
instance._read_token_cache = async () => { throw new Error('Local environment must not read the token cache'); };
|
|
788
|
-
const env = await instance.
|
|
798
|
+
const env = await instance.Env();
|
|
789
799
|
assert.equal(env.ARM_CLIENT_SECRET, 'local-secret');
|
|
790
800
|
assert.equal(env.VAULT_TOKEN, 'local-token');
|
|
791
801
|
assert.equal(env.VAULT_ADDR, instance.VAULT_ADDR);
|
|
@@ -802,17 +812,143 @@ test('Windows keeps cache enabled and selects Pageant without SSH_AUTH_SOCK', as
|
|
|
802
812
|
const script = `const Vauth = require(${JSON.stringify(module_path)});
|
|
803
813
|
Object.defineProperty(process, 'platform', {value: 'win32'});
|
|
804
814
|
const instance = new Vauth();
|
|
805
|
-
const result = {
|
|
806
|
-
|
|
815
|
+
const result = {role: instance.RC.ssh_auth.role};
|
|
816
|
+
let attempts = 0;
|
|
817
|
+
instance._token_cache_file = () => { attempts++; return ${JSON.stringify(path.join(home, 'missing-cache'))}; };
|
|
807
818
|
instance._save_cached_token = async () => {};
|
|
808
819
|
instance._login_vault_ssh = async (auth, agent) => {
|
|
809
820
|
result.agent = agent;
|
|
810
821
|
return {client_token: 'windows-test-token', lease_duration: 86400, renewable: true};
|
|
811
822
|
};
|
|
812
|
-
instance.
|
|
823
|
+
instance._read_token_cache().then(() => {
|
|
824
|
+
result.cacheDisabled = attempts === 0;
|
|
825
|
+
instance._read_token_cache = async () => undefined;
|
|
826
|
+
return instance.Connect();
|
|
827
|
+
}).then(() => process.stdout.write(JSON.stringify(result))).catch(() => process.exit(1));`;
|
|
813
828
|
const output = await exec(process.execPath, ['-e', script], {
|
|
814
829
|
cwd : home,
|
|
815
830
|
env : {...process.env, VAUTHRC : rc, VAULT_TOKEN : '', SSH_AUTH_SOCK : ''},
|
|
816
831
|
});
|
|
817
832
|
assert.deepEqual(JSON.parse(output.stdout), {cacheDisabled : false, role : 'windows-role', agent : 'pageant'});
|
|
818
833
|
});
|
|
834
|
+
|
|
835
|
+
test('env.map keys must be environment variable names (they reach `export KEY=` unescaped)', async t => {
|
|
836
|
+
const {client} = await fixture(t);
|
|
837
|
+
const instance = client();
|
|
838
|
+
instance.config = {env : {map : {'SAFE; printf INJECTED >&2; #' : 'dummy'}}};
|
|
839
|
+
await assert.rejects(instance.Env(), /Invalid environment variable name/);
|
|
840
|
+
instance.config = {env : {map : {TF_HTTP_USERNAME : '$${profile.VAUTH_USER_LOGIN}'}}};
|
|
841
|
+
assert.ok('TF_HTTP_USERNAME' in await instance.Env());
|
|
842
|
+
});
|
|
843
|
+
|
|
844
|
+
test('a project package.json may pin vvauth; other dependencies are ignored', async t => {
|
|
845
|
+
const {version} = require('../package.json');
|
|
846
|
+
const cwd = fs.mkdtempSync(path.join(os.tmpdir(), 'vvauth-project-'));
|
|
847
|
+
t.after(() => fs.rmSync(cwd, {recursive : true, force : true}));
|
|
848
|
+
const cli = path.resolve(__dirname, '..', 'index.js');
|
|
849
|
+
const options = {cwd, env : {...process.env, HOME : cwd, VAUTHRC : path.join(cwd, 'none'), VAULT_ADDR : '', VAULT_TOKEN : '', SSH_AUTH_SOCK : ''}};
|
|
850
|
+
|
|
851
|
+
fs.writeFileSync(path.join(cwd, 'package.json'), JSON.stringify({dependencies : {vvauth : `^${version}`, 'not-installed-anywhere' : '^9'}}));
|
|
852
|
+
assert.equal((await exec(process.execPath, [cli, 'version'], options)).stdout.trim(), version);
|
|
853
|
+
|
|
854
|
+
fs.writeFileSync(path.join(cwd, 'package.json'), JSON.stringify({dependencies : {vvauth : '<0.0.1'}}));
|
|
855
|
+
await assert.rejects(exec(process.execPath, [cli, 'connect'], options), /Unsupported vvauth version .* \(project requires <0.0.1\)/);
|
|
856
|
+
});
|
|
857
|
+
|
|
858
|
+
|
|
859
|
+
test('profile, KV and environment use the inherited vauth-login transport', async t => {
|
|
860
|
+
const Login = require('vauth-login');
|
|
861
|
+
assert.equal(Object.getPrototypeOf(Vauth.prototype), Login.prototype);
|
|
862
|
+
for(const name of ['Connect', 'LookupSelf', 'Request']) {
|
|
863
|
+
assert.equal(Object.hasOwn(Vauth.prototype, name), false);
|
|
864
|
+
assert.equal(Vauth.prototype[name], Login.prototype[name]);
|
|
865
|
+
}
|
|
866
|
+
const f = await fixture(t, {
|
|
867
|
+
metadata : {VAUTH_USER_LOGIN : 'alice', VAUTH_USER_NAME : 'Alice', VAUTH_USER_MAIL : 'alice@example.test'},
|
|
868
|
+
database : {TEAM : 'ops'}, secrets : {API_KEY : 'fixture-secret'},
|
|
869
|
+
});
|
|
870
|
+
const v = f.client();
|
|
871
|
+
v.config.env = {map : {LOGIN : '$${profile.VAUTH_USER_LOGIN}', TEAM : '$${profile.TEAM}', API_KEY : '$${secrets.API_KEY}'}, paths : ['app'], git : true};
|
|
872
|
+
const env = await v.Env(false, true);
|
|
873
|
+
assert.equal(env.VAULT_TOKEN, 'fresh-token');
|
|
874
|
+
assert.equal(env.LOGIN, 'alice');
|
|
875
|
+
assert.equal(env.TEAM, 'ops');
|
|
876
|
+
assert.equal(env.API_KEY, 'fixture-secret');
|
|
877
|
+
assert.equal(env.GIT_AUTHOR_NAME, 'Alice');
|
|
878
|
+
assert.deepEqual(f.requests.map(r => r.path), ['/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']);
|
|
879
|
+
assert.ok(f.requests.slice(1).every(r => r.token === 'fresh-token'));
|
|
880
|
+
await v.Set('feature', 'enabled');
|
|
881
|
+
assert.equal(f.database().FEATURE, 'enabled');
|
|
882
|
+
await v.Unset('feature');
|
|
883
|
+
assert.equal(Object.hasOwn(f.database(), 'FEATURE'), false);
|
|
884
|
+
assert.equal(f.requests.filter(r => r.path.endsWith('/login')).length, 1);
|
|
885
|
+
});
|
|
886
|
+
|
|
887
|
+
test('an unknown project Vault cannot receive inherited credentials through env or show', async t => {
|
|
888
|
+
const f = await fixture(t);
|
|
889
|
+
const home = path.dirname(path.dirname(path.dirname(f.file)));
|
|
890
|
+
const project = path.join(home, 'project');
|
|
891
|
+
fs.mkdirSync(project);
|
|
892
|
+
fs.writeFileSync(path.join(project, '.vauthrc'), JSON.stringify({vault_addr : f.client().VAULT_ADDR, env : {map : {LOGIN : '$${profile.VAUTH_USER_LOGIN}'}}}));
|
|
893
|
+
const options = {cwd : project, env : {...process.env, HOME : home, VAUTHRC : '', VAULT_ADDR : '', VAULT_TOKEN : 'inherited-token'}};
|
|
894
|
+
const cli = path.resolve(__dirname, '..', 'index.js');
|
|
895
|
+
assert.equal((await exec(process.execPath, [cli, 'connect'], options)).stdout, 'inherited-token');
|
|
896
|
+
for(const command of ['env', 'show']) {
|
|
897
|
+
await assert.rejects(exec(process.execPath, [cli, command, '--ir://json'], options), err => {
|
|
898
|
+
assert.equal(err.stdout, '');
|
|
899
|
+
assert.match(err.stderr, /Unknown vault/);
|
|
900
|
+
assert.ok(!err.stderr.includes('inherited-token'));
|
|
901
|
+
return true;
|
|
902
|
+
});
|
|
903
|
+
}
|
|
904
|
+
for(const command of ['Request', 'LookupSelf', 'request', 'lookupSelf'])
|
|
905
|
+
await assert.rejects(exec(process.execPath, [cli, command], options), /Usage:/);
|
|
906
|
+
assert.deepEqual(f.requests, []);
|
|
907
|
+
});
|
|
908
|
+
|
|
909
|
+
test('constructor consumes one frozen RC snapshot without reopening the selected file', t => {
|
|
910
|
+
const document = {env : {map : {VALUE : '$${env.LOCAL_VALUE}', USER : '$${profile.USER}'}}, extra : {nested : ['retained']}};
|
|
911
|
+
const read = fs.readFileSync;
|
|
912
|
+
let reads = 0;
|
|
913
|
+
fs.readFileSync = function(file, ...args) {
|
|
914
|
+
const body = read.call(this, file, ...args);
|
|
915
|
+
if(file === process.env.VAUTHRC) {
|
|
916
|
+
reads++;
|
|
917
|
+
fs.unlinkSync(file);
|
|
918
|
+
}
|
|
919
|
+
return body;
|
|
920
|
+
};
|
|
921
|
+
let v;
|
|
922
|
+
try {
|
|
923
|
+
v = configuredClient(t, document, {LOCAL_VALUE : 'local-value', VAULT_ADDR : 'https://vault.example.test'});
|
|
924
|
+
} finally {
|
|
925
|
+
fs.readFileSync = read;
|
|
926
|
+
}
|
|
927
|
+
assert.equal(reads, 1);
|
|
928
|
+
assert.deepEqual(v.RC, document);
|
|
929
|
+
assert.equal(v.config.env.map.VALUE, 'local-value');
|
|
930
|
+
assert.equal(v.config.env.map.USER, '$${profile.USER}');
|
|
931
|
+
v.config.env.map.VALUE = 'consumer-value';
|
|
932
|
+
assert.equal(v.RC.env.map.VALUE, '$${env.LOCAL_VALUE}');
|
|
933
|
+
assert.ok(Object.isFrozen(v.RC.env.map));
|
|
934
|
+
assert.equal(v.rc, undefined);
|
|
935
|
+
assert.equal(v.rc_file, undefined);
|
|
936
|
+
});
|
|
937
|
+
|
|
938
|
+
test('env.gitlab uses the working repository when VAUTHRC lives elsewhere', async t => {
|
|
939
|
+
const project = fs.mkdtempSync(path.join(os.tmpdir(), 'vvauth-git-project-'));
|
|
940
|
+
const home = fs.mkdtempSync(path.join(os.tmpdir(), 'vvauth-git-config-'));
|
|
941
|
+
t.after(() => fs.rmSync(project, {recursive : true, force : true}));
|
|
942
|
+
t.after(() => fs.rmSync(home, {recursive : true, force : true}));
|
|
943
|
+
assert.equal(spawnSync('git', ['init', '-q', project]).status, 0);
|
|
944
|
+
assert.equal(spawnSync('git', ['-C', project, 'remote', 'add', 'origin', 'git@git.example.test:Team/app.git']).status, 0);
|
|
945
|
+
const file = path.join(home, '.vauthrc');
|
|
946
|
+
fs.writeFileSync(file, JSON.stringify({env : {gitlab : true, map : {PROJECT : '$${env.CI_PROJECT_PATH}'}}}));
|
|
947
|
+
const script = `const V = require(${JSON.stringify(path.resolve(__dirname, '..', 'index.js'))}); process.stdout.write(JSON.stringify(new V().config));`;
|
|
948
|
+
for(const CI of ['', '1']) {
|
|
949
|
+
const {stdout} = await exec(process.execPath, ['-e', script], {
|
|
950
|
+
cwd : project, env : {...process.env, HOME : home, VAUTHRC : file, VAULT_TOKEN : '', VAULT_ADDR : '', CI, CI_PROJECT_PATH : 'pipeline/project'},
|
|
951
|
+
});
|
|
952
|
+
assert.equal(JSON.parse(stdout).env.map.PROJECT, CI ? 'pipeline/project' : 'Team/app');
|
|
953
|
+
}
|
|
954
|
+
});
|