Skip to content

Commit a485d51

Browse files
committed
fixed package.json, added totp test
Signed-off-by: Rod Anami <[email protected]>
1 parent fd2340a commit a485d51

File tree

7 files changed

+125
-18
lines changed

7 files changed

+125
-18
lines changed

Diff for: CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Improved AD secret management documentation
88
* Upgraded all dependencies to the latest
99
* Added *TOTP secret engine* functions:
10-
*
10+
* `createTOTPKey` and `readTOTPKey`
1111

1212
* `0.4.14`
1313
* Updated HashiCorp Vault license

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ Check below docs for more information on specific function groups.
270270
| **System Backend** | System | SEAL operations | [Doc file](/docs/Sys-Seal-Functions.md) |
271271
| **TLS Certificate** | Auth method | `/auth/cert` | [Doc file](/docs/TLS-Cert-Functions.md) |
272272
| **Token** | Auth method | `/auth/token` | [Doc file](/docs/Token-Functions.md) |
273-
| **TOTP** | Secret engine | `totp` | [Doc file](/docs/TOTP-Functions.md) |
273+
| **TOTP** | Secret engine | `/totp` | [Doc file](/docs/TOTP-Functions.md) |
274274
| **Userpass** | Auth method | `/auth/userpass` | [Doc file](/docs/Userpass-Functions.md) |
275275
| | | | |
276276

Diff for: Vault.js

+45-9
Original file line numberDiff line numberDiff line change
@@ -3971,17 +3971,17 @@ class Vault {
39713971
* @param {string} name
39723972
* @param {Object} params
39733973
* @param {boolean} params.generate
3974-
* @param {boolean} [params.exported]
3974+
* @param {boolean} [params.exported] - whether the key is exportable as QR code
39753975
* @param {number} [params.key_size=20]
39763976
* @param {string} [params.key_url]
39773977
* @param {string} [params.key]
3978-
* @param {string} [params.issuer]
3978+
* @param {string} [params.issuer] - key issuing entity
39793979
* @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]
3980+
* @param {number} [params.period=30] - length of time for the counter on the code calculation
3981+
* @param {string} [params.algorithm=sha1] - code generator algorithm, either "SHA1", "SHA256", or "SHA512"
3982+
* @param {number} [params.digits] - number of code digits, either 6 or 8
3983+
* @param {number} [params.skew=1] - number of delay periods valid for code validation, either 0 or 1
3984+
* @param {number} [params.gr_size=200] - pixel size of the QR code image
39853985
* @param {string} [mount]
39863986
* @returns {PromiseLike<Object>}
39873987
*/
@@ -4001,8 +4001,8 @@ class Vault {
40014001
// Defaults - most are probably already defaults from Vault itself
40024002
params = {
40034003
generate: true,
4004-
account_name: "Vault",
4005-
issuer: "Vault",
4004+
account_name: "vault",
4005+
issuer: "vault",
40064006
...params
40074007
};
40084008

@@ -4039,6 +4039,42 @@ class Vault {
40394039
throw parseAxiosError(err);
40404040
}
40414041
}
4042+
4043+
/**
4044+
* @param {string} token
4045+
* @param {string} name
4046+
* @param {string} [mount]
4047+
* @returns {PromiseLike<Object>}
4048+
*/
4049+
async readTOTPKey(token, name, mount) {
4050+
assert(token, 'readTOTPKey: required parameter missing - token');
4051+
assert(name, 'readTOTPKey: required parameter missing - name');
4052+
let url = "";
4053+
let rootPath = "";
4054+
if (mount) {
4055+
rootPath = mount;
4056+
} else if (this.rootPath) {
4057+
rootPath = this.rootPath;
4058+
} else {
4059+
rootPath = config.totpRootPath;
4060+
}
4061+
4062+
url = `${rootPath}/${config.totpReadKey[0]}/${name}`;
4063+
const Options = {
4064+
url: url,
4065+
method: config.totpReadKey[1],
4066+
headers: {
4067+
"X-Vault-Token": token
4068+
}
4069+
};
4070+
4071+
try {
4072+
const response = await this.instance(Options);
4073+
return parseAxiosResponse(response);
4074+
} catch(err) {
4075+
throw parseAxiosError(err);
4076+
}
4077+
}
40424078

40434079
}
40444080

Diff for: package-lock.json

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+18-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"types": "dist/Vault.d.ts",
77
"scripts": {
88
"compile": "npx tsc",
9+
"test": "bash -c 'source tests/process.env && jest tests/*.test.js'",
910
"test:approle": "bash -c 'source tests/process.env && jest tests/AppRole.test.js'",
1011
"test-ts:approle": "bash -c 'source tests/process.env && jest tests/AppRole.test.ts'",
1112
"test:kv": "bash -c 'source tests/process.env && jest tests/KV.test.js'",
@@ -20,7 +21,8 @@
2021
"test:k8s": "bash -c 'source tests/process.env && jest tests/K8s.test.js'",
2122
"test:ad-config": "bash -c 'source tests/process.env && jest tests/AD-config.test.js'",
2223
"test:ad-roles": "bash -c 'source tests/process.env && jest tests/AD-roles.test.js'",
23-
"test:ad-libraries": "bash -c 'source tests/process.env && jest tests/AD-libraries.test.js'"
24+
"test:ad-libraries": "bash -c 'source tests/process.env && jest tests/AD-libraries.test.js'",
25+
"test:totp": "bash -c 'source tests/process.env && jest tests/TOTP.test.js'"
2426
},
2527
"repository": {
2628
"type": "git",
@@ -57,16 +59,29 @@
5759
"@types/jest": "^29.5.12",
5860
"@types/node": "^20.14.2",
5961
"jest": "^29.7.0",
62+
"jest-config": "^29.7.0",
63+
"jest-environment-node": "^29.7.0",
6064
"random-words": "^2.0.1",
6165
"ts-jest": "^29.1.4",
6266
"typescript": "^5.4.5"
6367
},
6468
"jest": {
69+
"verbose": true,
6570
"testEnvironment": "node",
71+
"testEnvironmentOptions": {
72+
"NODE_ENV": "test"
73+
},
6674
"transform": {
6775
"^.+\\.ts?$": "ts-jest"
6876
},
69-
"testRegex": "/tests/.*\\.(test|spec)?\\.(ts|tsx)$",
70-
"moduleFileExtensions": [ "ts", "tsx", "js", "jsx", "json", "node" ]
77+
"testRegex": "/tests/.*\\.(test|spec)?\\.(js|ts|tsx)$",
78+
"moduleFileExtensions": [
79+
"ts",
80+
"tsx",
81+
"js",
82+
"jsx",
83+
"json",
84+
"node"
85+
]
7186
}
7287
}

Diff for: tests/TOTP-smoke-test.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,24 @@ const vault = new Vault( {
2121
const name = "my-totp-key";
2222

2323
const params = {
24-
issuer: "hashi-vault-js",
25-
account_name: "hashi-vault-js",
26-
algorithm: "SHA256",
27-
generate: true
24+
generate: true,
25+
exported: true,
26+
issuer: "hashi-vault-js",
27+
account_name: "chatopsknight",
28+
algorithm: "SHA512",
29+
digits: 8,
30+
period: 60,
31+
skew: 0,
32+
qr_size: 400
2833
};
2934

3035
vault.createTOTPKey(RootToken, name, params).then(function(data){
3136
console.log('1> createTOTPKey output:\n',data);
37+
vault.readTOTPKey(RootToken, name).then(function(data){
38+
console.log('2> readTOTPKey output:\n',data);
39+
}).catch(function(readError){
40+
console.error('2> readTOTPKey error:\n',readError);
41+
});
3242
}).catch(function(createError){
3343
console.error('1> createTOTPKey error:\n',createError);
3444
console.error('1> createTOTPKey error:\n',createError.response.data);

Diff for: tests/TOTP.test.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const Vault = require('../Vault');
2+
3+
const ClientCert = process.env.CLIENT_CERT;
4+
const ClientKey = process.env.CLIENT_KEY;
5+
const CACert = process.env.CA_CERT;
6+
const VaultUrl = process.env.VAULT_URL;
7+
const RootToken = process.env.VAULT_ROOT_TOKEN;
8+
9+
const vault = new Vault( {
10+
https: true,
11+
cert: ClientCert,
12+
key: ClientKey,
13+
cacert: CACert,
14+
baseUrl: VaultUrl,
15+
rootPath: 'totp',
16+
timeout: 3000,
17+
proxy: false
18+
});
19+
20+
const KeyName = "my-totp-key";
21+
22+
const KeyParams = {
23+
generate: true,
24+
exported: true,
25+
issuer: "hashi-vault-js",
26+
account_name: "chatopsknight",
27+
algorithm: "SHA512",
28+
digits: 8,
29+
period: 60,
30+
skew: 0,
31+
qr_size: 400
32+
};
33+
34+
test('createTOTPKey: the result is a TOTP key created - HTTP 200', async () => {
35+
const data = await vault.createTOTPKey(RootToken, KeyName, KeyParams);
36+
console.log(data);
37+
return expect(data).toBeDefined();
38+
});
39+
40+
test('readTOTPKey: the result is a TOTP key information retrieved', async () => {
41+
const data = await vault.readTOTPKey(RootToken, KeyName);
42+
console.log(data);
43+
return expect(data).toBeDefined();
44+
});

0 commit comments

Comments
 (0)