node-vault-client 1.0.1 → 2.0.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/LICENSE.txt +200 -200
- package/README.md +337 -154
- package/package.json +64 -44
- package/src/Lease.js +57 -57
- package/src/VaultApiClient.js +94 -55
- package/src/VaultClient.js +288 -263
- package/src/VaultNodeConfig.js +104 -105
- package/src/auth/AuthToken.js +141 -94
- package/src/auth/VaultAppRoleAuth.js +49 -40
- package/src/auth/VaultBaseAuth.js +162 -140
- package/src/auth/VaultIAMAuth.js +212 -187
- package/src/auth/VaultKubernetesAuth.js +49 -48
- package/src/auth/VaultTokenAuth.js +35 -34
- package/src/errors.js +21 -21
package/src/Lease.js
CHANGED
|
@@ -1,57 +1,57 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const _ = require('lodash');
|
|
4
|
-
|
|
5
|
-
class Lease {
|
|
6
|
-
constructor(
|
|
7
|
-
requestId,
|
|
8
|
-
leaseId,
|
|
9
|
-
leaseDuration,
|
|
10
|
-
renewable,
|
|
11
|
-
data
|
|
12
|
-
) {
|
|
13
|
-
this.__requestId = requestId;
|
|
14
|
-
this.__leaseId = leaseId;
|
|
15
|
-
this.__leaseDuration = leaseDuration;
|
|
16
|
-
this.__renewable = renewable;
|
|
17
|
-
this.__data = data === undefined ? {} : data;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
static fromResponse(response) {
|
|
21
|
-
return new Lease(
|
|
22
|
-
response.request_id,
|
|
23
|
-
response.lease_id,
|
|
24
|
-
response.lease_duration,
|
|
25
|
-
response.renewable,
|
|
26
|
-
response.data
|
|
27
|
-
);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* @param {String} key
|
|
32
|
-
* @returns {String}
|
|
33
|
-
*/
|
|
34
|
-
getValue(key) {
|
|
35
|
-
if (this.__data[key] === undefined) {
|
|
36
|
-
throw new Error('Requested key does not exist');
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
return this.__data[key];
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* @returns {Object}
|
|
44
|
-
*/
|
|
45
|
-
getData() {
|
|
46
|
-
return _.cloneDeep(this.__data);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* @returns {bool}
|
|
51
|
-
*/
|
|
52
|
-
isRenewable() {
|
|
53
|
-
return this.__renewable;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
module.exports = Lease;
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _ = require('lodash');
|
|
4
|
+
|
|
5
|
+
class Lease {
|
|
6
|
+
constructor(
|
|
7
|
+
requestId,
|
|
8
|
+
leaseId,
|
|
9
|
+
leaseDuration,
|
|
10
|
+
renewable,
|
|
11
|
+
data
|
|
12
|
+
) {
|
|
13
|
+
this.__requestId = requestId;
|
|
14
|
+
this.__leaseId = leaseId;
|
|
15
|
+
this.__leaseDuration = leaseDuration;
|
|
16
|
+
this.__renewable = renewable;
|
|
17
|
+
this.__data = data === undefined ? {} : data;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
static fromResponse(response) {
|
|
21
|
+
return new Lease(
|
|
22
|
+
response.request_id,
|
|
23
|
+
response.lease_id,
|
|
24
|
+
response.lease_duration,
|
|
25
|
+
response.renewable,
|
|
26
|
+
response.data
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @param {String} key
|
|
32
|
+
* @returns {String}
|
|
33
|
+
*/
|
|
34
|
+
getValue(key) {
|
|
35
|
+
if (this.__data[key] === undefined) {
|
|
36
|
+
throw new Error('Requested key does not exist');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return this.__data[key];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @returns {Object}
|
|
44
|
+
*/
|
|
45
|
+
getData() {
|
|
46
|
+
return _.cloneDeep(this.__data);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @returns {bool}
|
|
51
|
+
*/
|
|
52
|
+
isRenewable() {
|
|
53
|
+
return this.__renewable;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
module.exports = Lease;
|
package/src/VaultApiClient.js
CHANGED
|
@@ -1,55 +1,94 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
* @param {
|
|
11
|
-
* @param {String} config.
|
|
12
|
-
* @param {
|
|
13
|
-
*
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const urljoin = require('url-join');
|
|
4
|
+
const _ = require('lodash');
|
|
5
|
+
|
|
6
|
+
class VaultApiClient {
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @param {Object} config
|
|
10
|
+
* @param {String} config.url - the url of the vault server
|
|
11
|
+
* @param {String} [config.apiVersion='v1']
|
|
12
|
+
* @param {Object} [config.requestOptions] - extra options shallow-merged into every
|
|
13
|
+
* `fetch()` call. Use it to inject an undici `dispatcher` for a proxy/SOCKS agent
|
|
14
|
+
* or for custom TLS trust (self-signed / internal CA). Request-specific fields
|
|
15
|
+
* (`method`, `body`) always take precedence; `headers` are merged with the
|
|
16
|
+
* per-request headers winning. Stored by reference (not deep-cloned) so live
|
|
17
|
+
* objects such as a Dispatcher keep their prototype and remain usable.
|
|
18
|
+
* @param {Object} logger
|
|
19
|
+
*/
|
|
20
|
+
constructor(config, logger) {
|
|
21
|
+
const requestOptions = config && config.requestOptions;
|
|
22
|
+
|
|
23
|
+
this.__config = _.defaultsDeep(_.cloneDeep(_.omit(config, ['requestOptions'])), {
|
|
24
|
+
apiVersion: 'v1',
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
if (requestOptions !== undefined) {
|
|
28
|
+
this.__config.requestOptions = requestOptions;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
this._logger = logger;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
makeRequest(method, path, data, headers) {
|
|
35
|
+
data = data === undefined ? null : data;
|
|
36
|
+
headers = headers === undefined ? {} : headers;
|
|
37
|
+
|
|
38
|
+
const uri = urljoin(this.__config.url, this.__config.apiVersion, path);
|
|
39
|
+
|
|
40
|
+
const requestOptions = this.__config.requestOptions || {};
|
|
41
|
+
|
|
42
|
+
const options = Object.assign(
|
|
43
|
+
{ redirect: 'follow' },
|
|
44
|
+
requestOptions,
|
|
45
|
+
{
|
|
46
|
+
method: method,
|
|
47
|
+
headers: Object.assign(
|
|
48
|
+
{ Accept: 'application/json' },
|
|
49
|
+
requestOptions.headers,
|
|
50
|
+
headers
|
|
51
|
+
),
|
|
52
|
+
}
|
|
53
|
+
);
|
|
54
|
+
if (data !== null) {
|
|
55
|
+
options.body = JSON.stringify(data);
|
|
56
|
+
options.headers['Content-Type'] = 'application/json';
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
this._logger.debug(
|
|
60
|
+
'making request: %s %s',
|
|
61
|
+
method,
|
|
62
|
+
uri
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
return fetch(uri, options).then((response) => {
|
|
66
|
+
return response.text().then((text) => {
|
|
67
|
+
let body;
|
|
68
|
+
if (text) {
|
|
69
|
+
try {
|
|
70
|
+
body = JSON.parse(text);
|
|
71
|
+
} catch {
|
|
72
|
+
body = text;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (!response.ok) {
|
|
77
|
+
const error = new Error(`${response.status} - ${text}`);
|
|
78
|
+
error.statusCode = response.status;
|
|
79
|
+
error.error = body;
|
|
80
|
+
throw error;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
this._logger.debug('%s %s response body:\n%s',
|
|
84
|
+
method,
|
|
85
|
+
uri,
|
|
86
|
+
JSON.stringify(body, null, ' ')
|
|
87
|
+
);
|
|
88
|
+
return body;
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
module.exports = VaultApiClient;
|