vvauth 0.1.0 → 0.1.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/index.js +33 -20
- package/package.json +3 -3
package/index.js
CHANGED
|
@@ -9,7 +9,7 @@ const net = require('net');
|
|
|
9
9
|
const {spawn} = require('child_process');
|
|
10
10
|
|
|
11
11
|
const {parse} = require('yaml');
|
|
12
|
-
const {args} = require('nyks/process/parseArgs')();
|
|
12
|
+
const {args, dict} = require('nyks/process/parseArgs')();
|
|
13
13
|
const SSHAgent = require('ssh-agent-js/client');
|
|
14
14
|
const trim = require('mout/string/trim');
|
|
15
15
|
const get = require('mout/object/get');
|
|
@@ -27,16 +27,18 @@ const logger = {
|
|
|
27
27
|
};
|
|
28
28
|
|
|
29
29
|
|
|
30
|
-
const
|
|
30
|
+
const VAUTH_RC = ".vauthrc";
|
|
31
|
+
const FUNCTION_NAME = "vauth";
|
|
32
|
+
const FUNCTION_DECL = "function vauth() { source <(/usr/bin/env vauth --source $*); }";
|
|
31
33
|
|
|
32
|
-
class
|
|
34
|
+
class vvauth {
|
|
33
35
|
constructor(rc = null) {
|
|
34
36
|
this.rc = {};
|
|
35
37
|
if(rc) {
|
|
36
38
|
this.rc = rc;
|
|
37
39
|
} else {
|
|
38
|
-
if(fs.existsSync(
|
|
39
|
-
let body = fs.readFileSync(
|
|
40
|
+
if(fs.existsSync(VAUTH_RC)) {
|
|
41
|
+
let body = fs.readFileSync(VAUTH_RC, 'utf8');
|
|
40
42
|
this.rc = parse(body);
|
|
41
43
|
}
|
|
42
44
|
}
|
|
@@ -57,6 +59,15 @@ class vcreds {
|
|
|
57
59
|
}
|
|
58
60
|
|
|
59
61
|
async login(publish = true) {
|
|
62
|
+
if(!dict['source'] && publish) {
|
|
63
|
+
console.error(`echo please use "${FUNCTION_NAME} login"`);
|
|
64
|
+
process.exit(1);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
let {vault_addr} = this.rc;
|
|
68
|
+
console.error("Connecting to %s", vault_addr);
|
|
69
|
+
|
|
70
|
+
|
|
60
71
|
let VAULT_TOKEN = await this._get_token();
|
|
61
72
|
if(publish) {
|
|
62
73
|
let env = {VAULT_TOKEN};
|
|
@@ -72,6 +83,7 @@ class vcreds {
|
|
|
72
83
|
cmds.push(`echo export ${k}=[redacted] >&2`);
|
|
73
84
|
}
|
|
74
85
|
process.stdout.write(cmds.join("\n") + "\n");
|
|
86
|
+
process.exit();
|
|
75
87
|
}
|
|
76
88
|
|
|
77
89
|
async _login_vault_ssh({vault_addr, path = 'ssh', role}) {
|
|
@@ -108,29 +120,26 @@ class vcreds {
|
|
|
108
120
|
|
|
109
121
|
return token;
|
|
110
122
|
}
|
|
111
|
-
async
|
|
112
|
-
let child = spawn('bash', ["-lc", `
|
|
123
|
+
async _function_exists(alias) {
|
|
124
|
+
let child = spawn('bash', ["-lc", `declare -F ${alias}`]);
|
|
113
125
|
return new Promise(resolve => child.on('exit', resolve));
|
|
114
126
|
}
|
|
115
127
|
|
|
116
128
|
async install() {
|
|
117
|
-
const alias_name = "vauth";
|
|
118
|
-
const alias_value = "source <(vcreds login)";
|
|
119
129
|
const bashrc_path = path.resolve(os.homedir(), ".bashrc");
|
|
120
130
|
let bashrc = fs.existsSync(bashrc_path) ? fs.readFileSync(bashrc_path, 'utf-8').trim() : '';
|
|
121
|
-
let exists = await this.
|
|
131
|
+
let exists = await this._function_exists(FUNCTION_NAME);
|
|
122
132
|
if(exists == 0) {
|
|
123
|
-
console.error("
|
|
133
|
+
console.error("Function %s already installed", FUNCTION_NAME);
|
|
124
134
|
return;
|
|
125
135
|
}
|
|
126
|
-
console.error("Alias %s not installed, pushing it to %s",
|
|
136
|
+
console.error("Alias %s not installed, pushing it to %s", FUNCTION_NAME, bashrc_path);
|
|
127
137
|
|
|
128
|
-
fs.writeFileSync(bashrc_path, [bashrc,
|
|
138
|
+
fs.writeFileSync(bashrc_path, [bashrc, FUNCTION_DECL, ""].join("\n"));
|
|
129
139
|
console.error(`Installation ok, please \nsource ${bashrc_path}`);
|
|
130
140
|
}
|
|
131
141
|
|
|
132
142
|
async _login_vault(vault_addr, path, payload) {
|
|
133
|
-
console.error("Connecting to %s", vault_addr);
|
|
134
143
|
let remote_url = `${trim(vault_addr, '/')}/v1/auth/${path}/login`;
|
|
135
144
|
let query = {...url.parse(remote_url), json : true};
|
|
136
145
|
let res = await request(query, payload);
|
|
@@ -145,17 +154,21 @@ class vcreds {
|
|
|
145
154
|
return token;
|
|
146
155
|
}
|
|
147
156
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
157
|
}
|
|
152
158
|
|
|
153
159
|
//ensure module is called directly, i.e. not required
|
|
154
160
|
if(module.parent === null) {
|
|
155
|
-
let cmd = args.shift();
|
|
161
|
+
let cmd = args.shift(), i = process.argv.indexOf(cmd);
|
|
162
|
+
if(cmd && i != -1)
|
|
163
|
+
process.argv.splice(i, 1);
|
|
164
|
+
|
|
165
|
+
if(dict['source'] && !cmd) {
|
|
166
|
+
console.error(`please use "${FUNCTION_NAME} login"`);
|
|
167
|
+
process.exit(1);
|
|
168
|
+
}
|
|
156
169
|
let run = cmd ? [`--ir://raw`, `--ir://run=${cmd}`] : [];
|
|
157
|
-
require('cnyks/lib/bundle')(
|
|
170
|
+
require('cnyks/lib/bundle')(vvauth, null, run);
|
|
158
171
|
}
|
|
159
172
|
|
|
160
173
|
|
|
161
|
-
module.exports =
|
|
174
|
+
module.exports = vvauth;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vvauth",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "Vault
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Vault Auth helper",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"bin": {
|
|
7
7
|
"vvauth": "./index.js"
|
|
@@ -28,6 +28,6 @@
|
|
|
28
28
|
},
|
|
29
29
|
"repository": {
|
|
30
30
|
"type": "git",
|
|
31
|
-
"url": "git@github.com:131/
|
|
31
|
+
"url": "git@github.com:131/vvauth.git"
|
|
32
32
|
}
|
|
33
33
|
}
|