Skip to content

Commit fd2340a

Browse files
committed
added createTOTPKey, improved @types
Signed-off-by: Rod Anami <[email protected]>
1 parent 04306d1 commit fd2340a

File tree

7 files changed

+219
-15
lines changed

7 files changed

+219
-15
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* Upgraded development env to Vault server `1.16.3`
77
* Improved AD secret management documentation
88
* Upgraded all dependencies to the latest
9+
* Added *TOTP secret engine* functions:
10+
*
911

1012
* `0.4.14`
1113
* Updated HashiCorp Vault license

Config.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,14 @@ const config = {
117117
adCheckCredOut: ['library', 'check-out', 'post'],
118118
adCheckCredIn: ['library', 'check-in', 'post'],
119119
adGetCredStatus: ['library', 'status', 'get'],
120-
adRotateCred: ['rotate-role', 'post']
120+
adRotateCred: ['rotate-role', 'post'],
121+
totpRootPath: 'totp',
122+
totpCreateKey: ['keys', 'post'],
123+
totpReadKey: ['keys', 'get'],
124+
totpListKeys: ['keys', 'list'],
125+
totpDeleteKey: ['keys', 'delete'],
126+
totpGenerateCode: ['code', 'get'],
127+
totpValidateCode: ['code', 'post']
121128
};
122129

123130
module.exports = config;

Vault.js

+99-1
Original file line numberDiff line numberDiff line change
@@ -100,24 +100,47 @@ const parseAxiosError = function(error){
100100
}
101101

102102
// main class constructor
103+
/**
104+
* @constructor
105+
* @param {boolean} [params.https=false]
106+
* @param {string} [params.cert]
107+
* @param {string} [params.key]
108+
* @param {string} [params.cacert]
109+
* @param {string} [params.baseUrl]
110+
* @param {string} [params.rootPath]
111+
* @param {number} [params.timeout=1000]
112+
* @param {boolean} [params.proxy=false]
113+
* @param {string} [params.namespace]
114+
*/
103115
class Vault {
104116
constructor(params) {
117+
/** @type {boolean} */
105118
this.https = params.https || false;
119+
/** @type {string} */
106120
this.cert = params.cert;
121+
/** @type {string} */
107122
this.key = params.key;
123+
/** @type {string} */
108124
this.cacert = params.cacert;
125+
/** @type {string} */
109126
this.baseUrl = params.baseUrl || config.baseUrl;
127+
/** @type {string} */
110128
this.rootPath = params.rootPath;
129+
/** @type {number} */
111130
this.timeout = params.timeout || config.timeout;
131+
/** @type {Object} */
112132
this.proxy = params.proxy || config.proxy;
133+
/** @type {string} */
113134
this.namespace = params.namespace || config.namespace;
114135
try {
115136
if (this.https) {
137+
/** @type {Object | boolean} */
116138
this.agent = getHttpsAgent(this.cert, this.key, this.cacert);
117139
}
118140
else {
119141
this.agent = false;
120142
}
143+
/** @type {Object} */
121144
this.instance = getAxiosInstance(this.baseUrl, this.timeout, this.agent, this.proxy, this.namespace);
122145
} catch (error) {
123146
console.error('Error initiating Vault class:\n', error);
@@ -126,6 +149,7 @@ class Vault {
126149

127150
// /sys API endpoints
128151
/**
152+
* @param {Object} [params]
129153
* @returns {PromiseLike<Object>}
130154
*/
131155
async healthCheck(params){
@@ -1123,7 +1147,7 @@ class Vault {
11231147
url: `${rootPath}/${config.certLogin[0]}`,
11241148
method: config.certLogin[1],
11251149
data: {
1126-
"name": certName
1150+
name: certName
11271151
}
11281152
};
11291153

@@ -3942,6 +3966,80 @@ class Vault {
39423966
return await this.createKVSecretMeta(token, path, metadata, mount);
39433967
}
39443968

3969+
/**
3970+
* @param {string} token
3971+
* @param {string} name
3972+
* @param {Object} params
3973+
* @param {boolean} params.generate
3974+
* @param {boolean} [params.exported]
3975+
* @param {number} [params.key_size=20]
3976+
* @param {string} [params.key_url]
3977+
* @param {string} [params.key]
3978+
* @param {string} [params.issuer]
3979+
* @param {string} [params.account_name]
3980+
* @param {number} [params.period]
3981+
* @param {string} [params.algorithm]
3982+
* @param {number} [params.digits]
3983+
* @param {number} [params.skew]
3984+
* @param {number} [params.gr_size]
3985+
* @param {string} [mount]
3986+
* @returns {PromiseLike<Object>}
3987+
*/
3988+
async createTOTPKey(token, name, params, mount) {
3989+
assert(token, 'createTOTPKey: required parameter missing - token');
3990+
assert(name, 'createTOTPKey: required parameter missing - name');
3991+
let url = "";
3992+
let rootPath = "";
3993+
if (mount) {
3994+
rootPath = mount;
3995+
} else if (this.rootPath) {
3996+
rootPath = this.rootPath;
3997+
} else {
3998+
rootPath = config.totpRootPath;
3999+
}
4000+
4001+
// Defaults - most are probably already defaults from Vault itself
4002+
params = {
4003+
generate: true,
4004+
account_name: "Vault",
4005+
issuer: "Vault",
4006+
...params
4007+
};
4008+
4009+
const { generate, exported, key_size, key_url, key, issuer, account_name,
4010+
period, algorithm, digits, skew, gr_size } = params;
4011+
4012+
url = `${rootPath}/${config.totpCreateKey[0]}/${name}`;
4013+
const Options = {
4014+
url: url,
4015+
method: config.totpCreateKey[1],
4016+
headers: {
4017+
"X-Vault-Token": token
4018+
},
4019+
data: {
4020+
generate: params.generate,
4021+
exported: params.exported,
4022+
key_size: params.key_size,
4023+
url: params.key_url,
4024+
key: params.key,
4025+
issuer: params.issuer,
4026+
account_name: params.account_name,
4027+
period: params.period,
4028+
algorithm: params.algorithm,
4029+
digits: params.digits,
4030+
skew: params.skew,
4031+
gr_size: params.gr_size
4032+
}
4033+
};
4034+
4035+
try {
4036+
const response = await this.instance(Options);
4037+
return parseAxiosResponse(response);
4038+
} catch(err) {
4039+
throw parseAxiosError(err);
4040+
}
4041+
}
4042+
39454043
}
39464044

39474045
module.exports = Vault;

dist/Config.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,11 @@ export let adCheckCredOut: string[];
117117
export let adCheckCredIn: string[];
118118
export let adGetCredStatus: string[];
119119
export let adRotateCred: string[];
120+
export let totpRootPath: string;
121+
export let totpCreateKey: string[];
122+
export let totpReadKey: string[];
123+
export let totpListKeys: string[];
124+
export let totpDeleteKey: string[];
125+
export let totpGenerateCode: string[];
126+
export let totpValidateCode: string[];
120127
//# sourceMappingURL=Config.d.ts.map

dist/Vault.d.ts

+67-12
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,45 @@
1-
/// <reference types="node" />
21
export = Vault;
2+
/**
3+
* @constructor
4+
* @param {boolean} [params.https=false]
5+
* @param {string} [params.cert]
6+
* @param {string} [params.key]
7+
* @param {string} [params.cacert]
8+
* @param {string} [params.baseUrl]
9+
* @param {string} [params.rootPath]
10+
* @param {number} [params.timeout=1000]
11+
* @param {boolean} [params.proxy=false]
12+
* @param {string} [params.namespace]
13+
*/
314
declare class Vault {
415
constructor(params: any);
5-
https: any;
6-
cert: any;
7-
key: any;
8-
cacert: any;
9-
baseUrl: any;
10-
rootPath: any;
11-
timeout: any;
16+
/** @type {boolean} */
17+
https: boolean;
18+
/** @type {string} */
19+
cert: string;
20+
/** @type {string} */
21+
key: string;
22+
/** @type {string} */
23+
cacert: string;
24+
/** @type {string} */
25+
baseUrl: string;
26+
/** @type {string} */
27+
rootPath: string;
28+
/** @type {number} */
29+
timeout: number;
30+
/** @type {Object} */
1231
proxy: any;
13-
namespace: any;
14-
agent: boolean | https.Agent;
32+
/** @type {string} */
33+
namespace: string;
34+
/** @type {Object | boolean} */
35+
agent: any | boolean;
36+
/** @type {Object} */
1537
instance: any;
1638
/**
39+
* @param {Object} [params]
1740
* @returns {PromiseLike<Object>}
1841
*/
19-
healthCheck(params: any): PromiseLike<any>;
42+
healthCheck(params?: any): PromiseLike<any>;
2043
/**
2144
* @returns {PromiseLike<Object>}
2245
*/
@@ -1295,6 +1318,38 @@ declare class Vault {
12951318
* @returns {PromiseLike<Object>}
12961319
*/
12971320
updateKVSecretMeta(token: string, path: string, metadata: string, mount?: string): PromiseLike<any>;
1321+
/**
1322+
* @param {string} token
1323+
* @param {string} name
1324+
* @param {Object} params
1325+
* @param {boolean} params.generate
1326+
* @param {boolean} [params.exported]
1327+
* @param {number} [params.key_size=20]
1328+
* @param {string} [params.key_url]
1329+
* @param {string} [params.key]
1330+
* @param {string} [params.issuer]
1331+
* @param {string} [params.account_name]
1332+
* @param {number} [params.period]
1333+
* @param {string} [params.algorithm]
1334+
* @param {number} [params.digits]
1335+
* @param {number} [params.skew]
1336+
* @param {number} [params.gr_size]
1337+
* @param {string} [mount]
1338+
* @returns {PromiseLike<Object>}
1339+
*/
1340+
createTOTPKey(token: string, name: string, params: {
1341+
generate: boolean;
1342+
exported?: boolean;
1343+
key_size?: number;
1344+
key_url?: string;
1345+
key?: string;
1346+
issuer?: string;
1347+
account_name?: string;
1348+
period?: number;
1349+
algorithm?: string;
1350+
digits?: number;
1351+
skew?: number;
1352+
gr_size?: number;
1353+
}, mount?: string): PromiseLike<any>;
12981354
}
1299-
import https = require("https");
13001355
//# sourceMappingURL=Vault.d.ts.map

0 commit comments

Comments
 (0)