vvauth 2.5.0 → 2.6.1
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 +2 -4
- package/.github/workflows/test.yml +15 -0
- package/API_and_tests.md +133 -0
- package/README.md +69 -63
- package/SECURITY_AUDIT_COORDINATION.md +36 -0
- package/SECURITY_AUDIT_PROVENANCE.json +35 -0
- package/SECURITY_AUDIT_claude-opus-5-5.md +288 -0
- package/SECURITY_AUDIT_gpt-6.1-sol.md +410 -0
- package/cmd/vauth/main.go +25 -24
- package/cmd/vauth.js +12 -0
- package/gitlab.go +47 -0
- package/gitlab_test.go +116 -0
- package/go.mod +6 -3
- package/go.sum +8 -2
- package/index.js +127 -351
- package/package.json +9 -8
- package/rc.go +89 -308
- package/test/token-cache.js +382 -138
- package/vault.go +5 -197
- package/vauth.go +140 -259
- package/vauth_test.go +375 -118
- package/cache.go +0 -101
- package/humandiff.go +0 -50
package/index.js
CHANGED
|
@@ -4,248 +4,94 @@
|
|
|
4
4
|
const os = require('os');
|
|
5
5
|
const fs = require('fs');
|
|
6
6
|
const path = require('path');
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const {
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
const {parse} = require('yaml');
|
|
7
|
+
const {decrypt} = require('ssh-agent-crypt');
|
|
8
|
+
const VauthLogin = require('vauth-login');
|
|
9
|
+
const {spawn} = require('child_process');
|
|
10
|
+
const exec = require('nyks/child_process/exec');
|
|
11
|
+
const parseArgs = require('nyks/process/parseArgs');
|
|
12
|
+
const readline = require('readline');
|
|
13
|
+
|
|
15
14
|
const semver = require('semver');
|
|
16
15
|
const trim = require('mout/string/trim');
|
|
17
16
|
const get = require('mout/object/get');
|
|
18
|
-
const
|
|
17
|
+
const isPrimitive = require('mout/lang/isPrimitive');
|
|
19
18
|
const walk = require('nyks/object/walk');
|
|
20
|
-
const
|
|
19
|
+
const structuredClone = require('nyks/object/structuredClone');
|
|
21
20
|
|
|
22
|
-
const request = require('nyks/http/request');
|
|
23
|
-
const drain = require('nyks/stream/drain');
|
|
24
21
|
const replaceEnv = require('nyks/string/replaceEnv');
|
|
25
|
-
const promiser = require('nyks/function/promiser');
|
|
26
|
-
const {args} = require('nyks/process/parseArgs')();
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const {createAgent} = require('ssh2/lib/agent');
|
|
30
|
-
const debug = require('debug');
|
|
31
|
-
|
|
32
|
-
const logger = {
|
|
33
|
-
debug : debug('vvauth:debug'),
|
|
34
|
-
info : debug('vvauth:info'),
|
|
35
|
-
error : debug('vvauth:error'),
|
|
36
|
-
};
|
|
37
22
|
|
|
38
23
|
|
|
39
|
-
const
|
|
24
|
+
const {name : PACKAGE_NAME, version : VERSION} = require('./package.json');
|
|
40
25
|
const FUNCTION_NAME = "venv";
|
|
41
26
|
const FUNCTION_DECL = `function ${FUNCTION_NAME}() { source <(/usr/bin/env vauth env --source "$@"); }`;
|
|
42
27
|
|
|
43
|
-
class
|
|
28
|
+
class Vvauth extends VauthLogin {
|
|
44
29
|
constructor() {
|
|
45
30
|
|
|
46
31
|
|
|
32
|
+
// A project may pin the vauth it expects: `"dependencies": {"vvauth": "^2.5.0"}`.
|
|
33
|
+
// Only vvauth itself is checked, against the running version.
|
|
47
34
|
let manifest = path.resolve('package.json');
|
|
48
35
|
if(fs.existsSync(manifest)) {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
let {version} = require(require.resolve(`${module_name}/package.json`, {
|
|
54
|
-
paths : ['.', ...(require.main && require.main.paths || module.paths)]
|
|
55
|
-
}));
|
|
56
|
-
|
|
57
|
-
if(!semver.satisfies(version, module_version))
|
|
58
|
-
throw `Unsupported ${module_name} version (requires ${module_version})`;
|
|
59
|
-
}
|
|
36
|
+
const {dependencies = {}} = JSON.parse(fs.readFileSync(manifest, 'utf8'));
|
|
37
|
+
const required = dependencies[PACKAGE_NAME];
|
|
38
|
+
if(required && !semver.satisfies(VERSION, required))
|
|
39
|
+
throw `Unsupported ${PACKAGE_NAME} version ${VERSION} (project requires ${required})`;
|
|
60
40
|
}
|
|
61
41
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
let vauth_rc = VAUTH_RC.filter(path => path && fs.existsSync(path))[0];
|
|
65
|
-
if(vauth_rc) {
|
|
66
|
-
let body = fs.readFileSync(vauth_rc, 'utf8');
|
|
67
|
-
let rc = parse(body);
|
|
68
|
-
let env = process.env;
|
|
69
|
-
|
|
70
|
-
if(get(rc, 'env.gitlab') && !env.CI) {
|
|
71
|
-
try {
|
|
72
|
-
let remote_url = String(execFileSync('git', ['remote', 'get-url', 'origin'], {cwd : path.dirname(vauth_rc), stdio : ['ignore', 'pipe', 'ignore']})).trim();
|
|
73
|
-
let match = remote_url.match(/^[^@]+@[^:]+:(.+)$/);
|
|
74
|
-
let CI_PROJECT_PATH = match ? match[1] : trim(new URL(remote_url).pathname, '/');
|
|
75
|
-
CI_PROJECT_PATH = trim(CI_PROJECT_PATH, '/').replace(/\.git$/, '');
|
|
76
|
-
|
|
77
|
-
if(CI_PROJECT_PATH) {
|
|
78
|
-
let parts = CI_PROJECT_PATH.split('/');
|
|
79
|
-
let CI_PROJECT_NAME = parts.pop();
|
|
80
|
-
let CI_PROJECT_NAMESPACE = parts.join('/');
|
|
81
|
-
let CI_PROJECT_PATH_SLUG = String(CI_PROJECT_PATH).toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '');
|
|
82
|
-
|
|
83
|
-
env = {...env, CI_PROJECT_PATH, CI_PROJECT_NAME, CI_PROJECT_NAMESPACE, CI_PROJECT_PATH_SLUG};
|
|
84
|
-
}
|
|
85
|
-
} catch(err) {}
|
|
86
|
-
}
|
|
42
|
+
super();
|
|
87
43
|
|
|
88
|
-
|
|
44
|
+
const rc = this.RC;
|
|
45
|
+
const config = Object.fromEntries(['env', 'ssh-agent-crypt'].filter(key => rc && Object.hasOwn(rc, key)).map(key => [key, rc[key]]));
|
|
46
|
+
this.config = walk(structuredClone(config), v => replaceEnv(v, {env : process.env}));
|
|
47
|
+
if(this.config.env && this.config.env.gitlab === true) {
|
|
48
|
+
const {args, dict} = parseArgs();
|
|
49
|
+
const missing = ['CI_PROJECT_PATH', 'CI_PROJECT_NAME', 'CI_PROJECT_NAMESPACE', 'CI_PROJECT_PATH_SLUG'].filter(key => !process.env[key]);
|
|
50
|
+
if(!(dict.gitlab || args[0] === 'gitlab') && missing.length)
|
|
51
|
+
throw new Error(`Missing ${missing.join(', ')}. Run venv --gitlab first.`);
|
|
89
52
|
}
|
|
90
53
|
|
|
91
54
|
|
|
92
|
-
if(!process.env.SSH_AUTH_SOCK && process.platform !== 'win32')
|
|
93
|
-
this.rc.token_cache = false;
|
|
94
|
-
|
|
95
|
-
this.VAULT_ADDR = this.rc.vault_addr || process.env.VAULT_ADDR; //might be null
|
|
96
|
-
this.VAULT_TOKEN = process.env.VAULT_TOKEN;
|
|
97
|
-
|
|
98
55
|
if(!this.VAULT_ADDR)
|
|
99
56
|
console.error("Not bound to any vault");
|
|
100
57
|
|
|
101
58
|
}
|
|
102
59
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
process.exit();
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
_token_cache_key() {
|
|
111
|
-
const {ssh_auth, jwt_auth} = this.rc;
|
|
112
|
-
return createHash('sha256').update(JSON.stringify({
|
|
113
|
-
vault_addr : trim(this.VAULT_ADDR || '', '/'), ssh_auth, jwt_auth,
|
|
114
|
-
})).digest('hex');
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
_token_cache_file() {
|
|
118
|
-
return path.join(os.homedir(), '.vauth', 'tokens', this._token_cache_key() + '.creds');
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
async _read_token_cache() {
|
|
122
|
-
if(this.rc.token_cache === false)
|
|
123
|
-
return;
|
|
124
|
-
try {
|
|
125
|
-
const armor = fs.readFileSync(this._token_cache_file(), 'utf8');
|
|
126
|
-
return JSON.parse(await decrypt(armor));
|
|
127
|
-
} catch(err) {}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
async _save_cached_token(token, metadata) {
|
|
131
|
-
if(this.rc.token_cache === false)
|
|
132
|
-
return;
|
|
133
|
-
const ttl = metadata.ttl === undefined ? metadata.lease_duration : metadata.ttl;
|
|
134
|
-
const file = this._token_cache_file();
|
|
135
|
-
const temporary = file + '.next';
|
|
136
|
-
try {
|
|
137
|
-
const {lease_duration} = metadata;
|
|
138
|
-
const cache = {
|
|
139
|
-
token, lease_duration, renewable : metadata.renewable === true,
|
|
140
|
-
expires_at : ttl === 0 ? null : Date.now() + ttl * 1000,
|
|
141
|
-
};
|
|
142
|
-
fs.mkdirSync(path.dirname(file), {recursive : true, mode : 0o700});
|
|
143
|
-
const identity = process.env.VAUTH_USER_IDENTITY || process.env.VAUTH_USER_MAIL;
|
|
144
|
-
const armor = await encrypt(JSON.stringify(cache), identity);
|
|
145
|
-
fs.writeFileSync(temporary, armor, {mode : 0o600});
|
|
146
|
-
fs.renameSync(temporary, file);
|
|
147
|
-
} catch(err) {
|
|
148
|
-
// Cache failures must not prevent authentication or expose credentials.
|
|
149
|
-
console.error('Could not encrypt/write token cache; token not persisted');
|
|
150
|
-
} finally {
|
|
151
|
-
try { fs.unlinkSync(temporary); } catch(err) {}
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
_token_status(operation, metadata) {
|
|
156
|
-
const ttl = metadata.ttl === undefined ? metadata.lease_duration : metadata.ttl;
|
|
157
|
-
const validity = ttl === 0 ? 'valid indefinitely' : humanDiff(ttl, 2);
|
|
158
|
-
console.error('Vauth token: %s · %s · %s', operation, this.VAULT_ADDR, validity);
|
|
60
|
+
static Version() /**
|
|
61
|
+
* @alias version
|
|
62
|
+
*/ {
|
|
63
|
+
return VERSION;
|
|
159
64
|
}
|
|
160
65
|
|
|
161
|
-
async
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
if(
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
if(token) {
|
|
179
|
-
try {
|
|
180
|
-
if(cached && cached.token === token) {
|
|
181
|
-
const ttl = cached.expires_at === null ? 0 : (cached.expires_at - Date.now()) / 1000;
|
|
182
|
-
metadata = {...cached, ttl};
|
|
183
|
-
} else {
|
|
184
|
-
metadata = await this._lookup_token(token);
|
|
185
|
-
}
|
|
186
|
-
const {ttl, renewable, lease_duration} = metadata;
|
|
187
|
-
if(ttl > 0 && ttl <= lease_duration / 3) {
|
|
188
|
-
if(renewable) {
|
|
189
|
-
metadata = await this._renew_token(token);
|
|
190
|
-
token = metadata.client_token;
|
|
191
|
-
operation = 'renewed';
|
|
192
|
-
} else {
|
|
193
|
-
token = undefined;
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
} catch(err) {
|
|
197
|
-
// Reauthenticate on invalid tokens, not network/server failures.
|
|
198
|
-
const status = err.res && err.res.statusCode;
|
|
199
|
-
if(![400, 403].includes(status))
|
|
200
|
-
throw err;
|
|
201
|
-
token = undefined;
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
if(!token) {
|
|
206
|
-
const agent_path = process.env.SSH_AUTH_SOCK || (process.platform === 'win32' ? 'pageant' : null);
|
|
207
|
-
if(ssh_auth && agent_path) {
|
|
208
|
-
metadata = await this._login_vault_ssh({...ssh_auth}, agent_path);
|
|
209
|
-
} else if(jwt_auth && jwt_auth.jwt) {
|
|
210
|
-
const {path, jwt, role} = jwt_auth;
|
|
211
|
-
metadata = await this._login_vault(path, {jwt, role});
|
|
212
|
-
}
|
|
213
|
-
token = metadata && metadata.client_token;
|
|
214
|
-
if(!token && (renew || had_token))
|
|
215
|
-
throw new Error('Could not login to vault: no authentication method available');
|
|
216
|
-
operation = 'generated';
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
if(token) {
|
|
220
|
-
if(operation !== 'reused' || !cached || cached.token !== token)
|
|
221
|
-
await this._save_cached_token(token, metadata);
|
|
222
|
-
this._token_status(operation, metadata);
|
|
223
|
-
}
|
|
224
|
-
this.VAULT_TOKEN = token;
|
|
225
|
-
this._connected = true;
|
|
226
|
-
return token;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
async _renew_token(token) {
|
|
230
|
-
const remote_url = `${trim(this.VAULT_ADDR, '/')}/v1/auth/token/renew-self`;
|
|
231
|
-
const query = {...url.parse(remote_url), headers : {'x-vault-token' : token}, expect : 200, json : true};
|
|
232
|
-
const res = await request(query, {});
|
|
233
|
-
const auth = JSON.parse(String(await drain(res))).auth;
|
|
234
|
-
if(!auth.client_token || !Number.isFinite(auth.lease_duration) || auth.lease_duration < 0)
|
|
235
|
-
throw new Error('Invalid Vault token renewal response');
|
|
236
|
-
return auth;
|
|
66
|
+
async Gitlab() /**
|
|
67
|
+
* @alias gitlab
|
|
68
|
+
*/ {
|
|
69
|
+
const remote = String(await exec('git', ['remote', 'get-url', 'origin'], {stdio : ['ignore', 'pipe', 'ignore']})).trim();
|
|
70
|
+
const match = remote.includes('://') ? null : remote.match(/^[^@]+@[^:]+:(.+)$/);
|
|
71
|
+
const project = trim(match ? match[1] : decodeURIComponent(new URL(remote).pathname), '/').replace(/\.git$/, '');
|
|
72
|
+
if(!project)
|
|
73
|
+
throw new Error('Could not resolve the Git project from origin');
|
|
74
|
+
const parts = project.split('/');
|
|
75
|
+
const variables = {
|
|
76
|
+
CI_PROJECT_PATH : project,
|
|
77
|
+
CI_PROJECT_NAME : parts.pop(),
|
|
78
|
+
CI_PROJECT_NAMESPACE : parts.join('/'),
|
|
79
|
+
CI_PROJECT_PATH_SLUG : project.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, ''),
|
|
80
|
+
};
|
|
81
|
+
this.PublishEnv(variables);
|
|
237
82
|
}
|
|
238
83
|
|
|
239
|
-
async
|
|
240
|
-
|
|
84
|
+
async Login(source = false, renew = false) /**
|
|
85
|
+
* @alias login
|
|
86
|
+
*/ {
|
|
87
|
+
await this.Connect(renew);
|
|
241
88
|
if(source) {
|
|
242
|
-
let env = {VAULT_TOKEN : this.VAULT_TOKEN};
|
|
243
|
-
this.
|
|
244
|
-
process.exit();
|
|
89
|
+
let env = {VAULT_TOKEN : this.VAULT_TOKEN || ''};
|
|
90
|
+
this.PublishEnv(env);
|
|
245
91
|
}
|
|
246
92
|
}
|
|
247
93
|
|
|
248
|
-
|
|
94
|
+
PublishEnv(env) {
|
|
249
95
|
let cmds = [];
|
|
250
96
|
for(let [k, v] of Object.entries(env)) {
|
|
251
97
|
cmds.push(`export ${k}=${shellEscape(v)}`);
|
|
@@ -255,8 +101,10 @@ class vvauth {
|
|
|
255
101
|
}
|
|
256
102
|
|
|
257
103
|
|
|
258
|
-
async
|
|
259
|
-
|
|
104
|
+
async Set(k, v) /**
|
|
105
|
+
* @alias set
|
|
106
|
+
*/ {
|
|
107
|
+
let {profile, database} = await this.GetProfile();
|
|
260
108
|
if(!profile.VAUTH_USER_LOGIN)
|
|
261
109
|
throw "Could not resolve VAUTH_USER_LOGIN from vault identity";
|
|
262
110
|
|
|
@@ -264,23 +112,28 @@ class vvauth {
|
|
|
264
112
|
await this._vault_write(`private/${profile.VAUTH_USER_LOGIN}`, '.vauth_database', database);
|
|
265
113
|
}
|
|
266
114
|
|
|
267
|
-
async
|
|
268
|
-
|
|
115
|
+
async Unset(k) /**
|
|
116
|
+
* @alias unset
|
|
117
|
+
*/ {
|
|
118
|
+
await this.Set(k, undefined);
|
|
269
119
|
}
|
|
270
120
|
|
|
271
|
-
async
|
|
272
|
-
|
|
121
|
+
async Show() /**
|
|
122
|
+
* @alias show
|
|
123
|
+
*/ {
|
|
124
|
+
let {profile, database} = await this.GetProfile();
|
|
273
125
|
return {...database, ...profile};
|
|
274
126
|
}
|
|
275
127
|
|
|
276
|
-
async
|
|
277
|
-
await this.
|
|
128
|
+
async GetProfile(renew = false) {
|
|
129
|
+
await this.Connect(renew);
|
|
278
130
|
|
|
279
|
-
if(!this.VAULT_TOKEN)
|
|
280
|
-
return {};
|
|
131
|
+
if(!this.VAULT_TOKEN || !this.VAULT_ADDR)
|
|
132
|
+
return {profile : {}, database : {}};
|
|
281
133
|
|
|
282
|
-
|
|
283
|
-
|
|
134
|
+
const {entity_id} = await this.LookupSelf(this.VAULT_TOKEN);
|
|
135
|
+
const {body} = await this.Request('GET', `identity/entity/id/${entity_id}`, this.VAULT_TOKEN);
|
|
136
|
+
const identity = JSON.parse(body).data;
|
|
284
137
|
let profile = {...(identity.metadata)};
|
|
285
138
|
let database = {};
|
|
286
139
|
|
|
@@ -290,12 +143,22 @@ class vvauth {
|
|
|
290
143
|
return {entity_id, identity, profile, database};
|
|
291
144
|
}
|
|
292
145
|
|
|
293
|
-
async
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
146
|
+
async Env(source = false, renew = false, gitlab = false) /**
|
|
147
|
+
* @alias env
|
|
148
|
+
*/ {
|
|
149
|
+
if(gitlab) {
|
|
150
|
+
await this.Gitlab();
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
let env = {}, secrets = {}, profile = {};
|
|
154
|
+
let {git, map = {}, paths, path : mount = "secrets"} = this.config.env || {};
|
|
155
|
+
const localCredentials = this.config['ssh-agent-crypt'] && !this.RC.ssh_auth && !this.RC.jwt_auth;
|
|
156
|
+
if(localCredentials) {
|
|
157
|
+
await this.Connect(renew);
|
|
158
|
+
} else {
|
|
159
|
+
const current = await this.GetProfile(renew);
|
|
160
|
+
profile = {...current.database, ...current.profile};
|
|
161
|
+
}
|
|
299
162
|
|
|
300
163
|
if(this.VAULT_TOKEN)
|
|
301
164
|
env.VAULT_TOKEN = this.VAULT_TOKEN;
|
|
@@ -303,7 +166,7 @@ class vvauth {
|
|
|
303
166
|
env.VAULT_ADDR = this.VAULT_ADDR;
|
|
304
167
|
|
|
305
168
|
|
|
306
|
-
let {'ssh-agent-crypt' : agent } = this.
|
|
169
|
+
let {'ssh-agent-crypt' : agent } = this.config;
|
|
307
170
|
if(agent) {
|
|
308
171
|
const {path, identity} = agent;
|
|
309
172
|
const body = await decrypt(fs.readFileSync(path), identity);
|
|
@@ -327,106 +190,63 @@ class vvauth {
|
|
|
327
190
|
secrets = {...secrets, ...data};
|
|
328
191
|
}
|
|
329
192
|
}
|
|
330
|
-
for(let [k, v] of Object.entries(map))
|
|
331
|
-
|
|
193
|
+
for(let [k, v] of Object.entries(map)) {
|
|
194
|
+
// the key reaches `export KEY=...` unescaped; .vauthrc may come from an untrusted working directory
|
|
195
|
+
if(!/^[A-Za-z_][A-Za-z0-9_]*$/.test(k))
|
|
196
|
+
throw new Error(`Invalid environment variable name '${k}' in env.map`);
|
|
197
|
+
env[k] = envValue(replaceEnv(envValue(v, k), {env : process.env, profile, secrets}), k);
|
|
198
|
+
}
|
|
332
199
|
|
|
200
|
+
if(source) {
|
|
201
|
+
this.PublishEnv(env);
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
333
204
|
return env;
|
|
334
205
|
}
|
|
335
206
|
|
|
336
|
-
async
|
|
337
|
-
|
|
207
|
+
async Dotenv() /**
|
|
208
|
+
* @alias dotenv
|
|
209
|
+
*/ {
|
|
210
|
+
const env = await this.Env();
|
|
338
211
|
|
|
339
212
|
for(let [k, v] of Object.entries(env)) {
|
|
340
213
|
process.stdout.write(`${k}=${String(v)}\n`);
|
|
341
214
|
process.stderr.write(`export ${k}=[redacted]\n`);
|
|
342
215
|
}
|
|
343
216
|
|
|
344
|
-
process.exit();
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
async env(source = false, renew = false) {
|
|
348
|
-
const env = await this._get_env(renew);
|
|
349
|
-
|
|
350
|
-
if(source) {
|
|
351
|
-
this._publish_env(env);
|
|
352
|
-
process.exit();
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
return env;
|
|
356
217
|
}
|
|
357
218
|
|
|
358
219
|
async _vault_read(mount, secret_path, optional = false) {
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
let res = await request(query);
|
|
362
|
-
let body = String(await drain(res));
|
|
363
|
-
|
|
364
|
-
if(optional && res.statusCode == 404)
|
|
220
|
+
const {status, body} = await this.Request('GET', `${mount}/data/${trim(secret_path, '/')}`, this.VAULT_TOKEN, null, 0);
|
|
221
|
+
if(optional && status === 404)
|
|
365
222
|
return {};
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
throw `Could not read vault secret '${mount}/${trim(secret_path, '/')}' : ${body}`;
|
|
369
|
-
|
|
223
|
+
if(status !== 200)
|
|
224
|
+
throw new VauthLogin.HTTPError(status, body.toString());
|
|
370
225
|
return get(JSON.parse(body), 'data.data');
|
|
371
226
|
}
|
|
372
227
|
|
|
373
228
|
async _vault_write(mount, secret_path, data) {
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
let res = await request(query, {data});
|
|
377
|
-
let body = String(await drain(res));
|
|
378
|
-
|
|
379
|
-
if(res.statusCode != 200)
|
|
380
|
-
throw `Could not write vault secret '${mount}/${trim(secret_path, '/')}' : ${body}`;
|
|
381
|
-
|
|
382
|
-
return body ? JSON.parse(body) : {};
|
|
229
|
+
const {body} = await this.Request('POST', `${mount}/data/${trim(secret_path, '/')}`, this.VAULT_TOKEN, {data}, 200);
|
|
230
|
+
return body.length ? JSON.parse(body) : {};
|
|
383
231
|
}
|
|
384
232
|
|
|
385
|
-
|
|
386
|
-
async _login_vault_ssh({path = 'ssh', role}, agent_path = process.env.SSH_AUTH_SOCK) {
|
|
387
|
-
logger.info("Trying to auth as '%s'", role);
|
|
388
|
-
|
|
389
|
-
let agent = createAgent(agent_path);
|
|
390
|
-
let keys = await promiser(chain => agent.getIdentities(chain));
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
let auth;
|
|
394
|
-
await eachLimit(keys, 1, async (pubKey) => {
|
|
395
|
-
if(auth)
|
|
396
|
-
return;
|
|
397
|
-
|
|
398
|
-
let remote_url = `${trim(this.VAULT_ADDR, '/')}/v1/auth/${path}/nonce`;
|
|
399
|
-
let query = {...url.parse(remote_url), json : true};
|
|
400
|
-
let res = await request(query);
|
|
401
|
-
let {data : {nonce}} = JSON.parse(String(await drain(res)));
|
|
402
|
-
|
|
403
|
-
const signature = (await promiser(chain => agent.sign(pubKey, Buffer.from(nonce), {}, chain))).toString('base64');
|
|
404
|
-
const public_key = pubKey.type + ' ' + pubKey.getPublicSSH().toString('base64');
|
|
405
|
-
const payload = {public_key, role, nonce : Buffer.from(nonce).toString('base64'), signature};
|
|
406
|
-
try {
|
|
407
|
-
auth = await this._login_vault(path, payload);
|
|
408
|
-
} catch(err) {
|
|
409
|
-
logger.debug("ssh : invalid challenge for public key", pubKey.comment);
|
|
410
|
-
}
|
|
411
|
-
});
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
if(!auth || !auth.client_token)
|
|
415
|
-
throw `Could not login to vault`;
|
|
416
|
-
|
|
417
|
-
return auth;
|
|
418
|
-
}
|
|
419
233
|
async _function_exists(alias) {
|
|
420
234
|
let child = spawn('bash', ["-lc", `declare -F ${alias}`]);
|
|
421
235
|
return new Promise(resolve => child.on('exit', resolve));
|
|
422
236
|
}
|
|
423
237
|
|
|
424
|
-
async
|
|
238
|
+
async Install() /**
|
|
239
|
+
* @alias install
|
|
240
|
+
*/ {
|
|
425
241
|
const bashrc_path = path.resolve(os.homedir(), ".bashrc");
|
|
426
242
|
let bashrc = fs.existsSync(bashrc_path) ? fs.readFileSync(bashrc_path, 'utf-8').trim() : '';
|
|
427
243
|
if(await this._function_exists(FUNCTION_NAME) === 0) {
|
|
428
|
-
|
|
429
|
-
|
|
244
|
+
const prompt = readline.createInterface({input : process.stdin, output : process.stdout});
|
|
245
|
+
const answer = await new Promise(resolve => {
|
|
246
|
+
prompt.once('close', () => resolve(''));
|
|
247
|
+
prompt.question('Function already defined, override current definition? [y/N] ', value => { resolve(value); prompt.close(); });
|
|
248
|
+
});
|
|
249
|
+
if(!/^y(es)?$/i.test(answer.trim())) return;
|
|
430
250
|
}
|
|
431
251
|
console.error("Installing function %s in %s", FUNCTION_NAME, bashrc_path);
|
|
432
252
|
|
|
@@ -434,64 +254,20 @@ class vvauth {
|
|
|
434
254
|
console.error(`Installation ok, please \nsource ${bashrc_path}`);
|
|
435
255
|
}
|
|
436
256
|
|
|
437
|
-
async _lookup_token(token) {
|
|
438
|
-
let remote_url = `${trim(this.VAULT_ADDR, '/')}/v1/auth/token/lookup-self`;
|
|
439
|
-
let query = {...url.parse(remote_url), headers : {'x-vault-token' : token}, expect : 200};
|
|
440
|
-
let res = await request(query);
|
|
441
|
-
let response = JSON.parse(await drain(res)).data;
|
|
442
|
-
if(!Number.isFinite(response.ttl) || response.ttl < 0)
|
|
443
|
-
throw new Error('Invalid Vault token TTL');
|
|
444
|
-
const cached = await this._read_token_cache();
|
|
445
|
-
if(cached && cached.token === token)
|
|
446
|
-
response.lease_duration = cached.lease_duration;
|
|
447
|
-
else
|
|
448
|
-
response.lease_duration = response.creation_ttl;
|
|
449
|
-
return response;
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
async _lookup_identity(token, id) {
|
|
453
|
-
let remote_url = `${trim(this.VAULT_ADDR, '/')}/v1/identity/entity/id/${id}`;
|
|
454
|
-
let query = {...url.parse(remote_url), headers : {'x-vault-token' : token}, expect : 200};
|
|
455
|
-
let res = await request(query);
|
|
456
|
-
return JSON.parse(String(await drain(res))).data;
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
async _update_identity(token, id, payload) {
|
|
460
|
-
let remote_url = `${trim(this.VAULT_ADDR, '/')}/v1/identity/entity/id/${id}`;
|
|
461
|
-
let query = {...url.parse(remote_url), headers : {'x-vault-token' : token}, expect : 204, json : true};
|
|
462
|
-
await request(query, payload);
|
|
463
|
-
return payload;
|
|
464
|
-
}
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
async _login_vault(path, payload) {
|
|
469
|
-
let remote_url = `${trim(this.VAULT_ADDR, '/')}/v1/auth/${path}/login`;
|
|
470
|
-
let query = {...url.parse(remote_url), json : true};
|
|
471
|
-
let res = await request(query, payload);
|
|
472
|
-
let response = String(await drain(res));
|
|
473
|
-
|
|
474
|
-
if(res.statusCode !== 200)
|
|
475
|
-
throw `Could not login to vault : ${response}`;
|
|
476
|
-
|
|
477
|
-
const auth = JSON.parse(response).auth;
|
|
478
|
-
if(!auth.client_token || !Number.isFinite(auth.lease_duration) || auth.lease_duration < 0)
|
|
479
|
-
throw new Error('Invalid Vault login response');
|
|
480
|
-
return auth;
|
|
481
|
-
}
|
|
482
257
|
|
|
483
258
|
}
|
|
484
259
|
|
|
260
|
+
const envValue = (value, key) => {
|
|
261
|
+
if(value == null)
|
|
262
|
+
return '';
|
|
263
|
+
if(!isPrimitive(value) || (typeof value === 'number' && !Number.isFinite(value)))
|
|
264
|
+
throw new Error(`Invalid environment value '${key}' in env.map: expected a scalar`);
|
|
265
|
+
return String(value);
|
|
266
|
+
};
|
|
267
|
+
|
|
485
268
|
const shellEscape = (arg) => {
|
|
486
269
|
// see man bash
|
|
487
270
|
return "'" + String(arg).replace(/'/g, '\'"\'"\'') + "'";
|
|
488
271
|
};
|
|
489
272
|
|
|
490
|
-
|
|
491
|
-
if(module.parent === null) {
|
|
492
|
-
let cmd = args.shift();
|
|
493
|
-
const output = process.argv.includes('--ir://json') ? '--ir://json' : '--ir://raw';
|
|
494
|
-
require('cnyks/lib/bundle')(vvauth, null, cmd ? [`--ir://run=${cmd}`, output] : []);
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
module.exports = vvauth;
|
|
273
|
+
module.exports = Vvauth;
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vvauth",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.6.1",
|
|
4
4
|
"description": "Vault Auth helper",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"bin": {
|
|
7
|
-
"vauth": "./
|
|
7
|
+
"vauth": "./cmd/vauth.js"
|
|
8
8
|
},
|
|
9
9
|
"scripts": {
|
|
10
10
|
"test": "node --test test/token-cache.js",
|
|
@@ -13,14 +13,12 @@
|
|
|
13
13
|
"author": "",
|
|
14
14
|
"license": "ISC",
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"cnyks": "^3.2.
|
|
17
|
-
"debug": "^4.3.4",
|
|
16
|
+
"cnyks": "^3.2.4",
|
|
18
17
|
"mout": "^1.2.4",
|
|
19
|
-
"nyks": "^6.
|
|
18
|
+
"nyks": "^6.18.0",
|
|
20
19
|
"semver": "^7.5.4",
|
|
21
|
-
"ssh-agent-crypt": "^1.
|
|
22
|
-
"
|
|
23
|
-
"yaml": "^2.6.1"
|
|
20
|
+
"ssh-agent-crypt": "^1.4.4",
|
|
21
|
+
"vauth-login": "^1.1.7"
|
|
24
22
|
},
|
|
25
23
|
"devDependencies": {
|
|
26
24
|
"eslint": "^8.57.0",
|
|
@@ -32,5 +30,8 @@
|
|
|
32
30
|
"repository": {
|
|
33
31
|
"type": "git",
|
|
34
32
|
"url": "git@github.com:131/vvauth.git"
|
|
33
|
+
},
|
|
34
|
+
"engines": {
|
|
35
|
+
"node": ">=16"
|
|
35
36
|
}
|
|
36
37
|
}
|