使用节点保险库访问HashiCorp Vault KV秘密

时间:2019-07-14 11:02:00

标签: node.js hashicorp-vault

我正在尝试使用“ node-vault”访问HashiCorp Vault KV,但始终获取“ statusCode:404”

我正在关注节点保险库的示例 https://github.com/kr1sp1n/node-vault

1)我在Windows 10上运行vault_1.1.3_windows_amd64,并在PowerShell上使用“ vault服务器-dev”。

2)然后在另一个PowerShell上运行;

$env:VAULT_ADDR="http://127.0.0.1:8200"
vault secrets enable -version=1 kv
vault status
Key             Value
---             -----
Seal Type       shamir
Initialized     true
Sealed          false
Total Shares    1
Threshold       1
Version         1.1.3
Cluster Name    vault-cluster-28a041c6
Cluster ID      0ec85d70-8e87-dff6-347f-b1959fad8b44
HA Enabled      false

3)然后使用

运行以下代码
const rootKey = //whatever;
const unsealKey = //whatever;

var options = {
    apiVersion: 'v1',
    endpoint: 'http://127.0.0.1:8200',
    token: rootKey
};

var vault = require("node-vault")(options);
vault.unseal({ key: unsealKey })
    .then(() => {
        vault.write('secret/hello', { value: 'world' })
            .then((res) => console.log(res))
            .catch((err) => console.error(err));
    });

vault.write('secret/hello', { value: 'world', lease: '1s' })
    .then( () => vault.read('secret/hello'))
    .then( () => vault.delete('secret/hello'))
    .catch(console.error);

这将返回状态404,需要另外做些什么以避免404?

{ Error: Status 404
    at handleVaultResponse (XX\TestCodes\Node-VaultTest\node_modules\node-vault\src\index.js:49:21)
    at process._tickCallback (internal/process/next_tick.js:68:7)
  response:
   { statusCode: 404,
     body:
      { request_id: '2992e6c2-5146-6569-1f48-55f75da88993',
        lease_id: '',
        renewable: false,
        lease_duration: 0,
        data: null,
        wrap_info: null,
        warnings: [Array],
        auth: null } } }
{ Error: Status 404
    at handleVaultResponse (XX\TestCodes\Node-VaultTest\node_modules\node-vault\src\index.js:49:21)
    at process._tickCallback (internal/process/next_tick.js:68:7)
  response:
   { statusCode: 404,
     body:
      { request_id: '2f280fa4-6596-c06f-2168-091246e0a2a1',
        lease_id: '',
        renewable: false,
        lease_duration: 0,
        data: null,
        wrap_info: null,
        warnings: [Array],
        auth: null } } }

1 个答案:

答案 0 :(得分:0)

您将kv存储库安装为版本1。node-vault用来从版本2 kv存储库读取机密的实际路径是不同的,并且与Vault的v1 kv存储库不兼容。

使用-version 2安装您的kv商店。如果未指定,则默认为v1。