0.4.14 • Published 8 months ago

hashi-vault-js v0.4.14

Weekly downloads
3,926
License
MIT
Repository
github
Last release
8 months ago

Hashi Vault JS

GitHub issues GitHub code size in bytes GitHub repo file count GitHub top language GitHub contributors GitHub package.json dependency version (prod) npm NPM

This module provides a set of functions to help JavaScript Developers working with HashiCorp Vault to authenticate and access API endpoints using JavaScript promises.

This package is NOT affected by the log4shell CVE-2021-44228 vulnerability!

Requirements (MacOS/Windows)

  • NodeJs
    • Minimum: v16.x
    • Recommended: v18.x
  • npm
    • Tested on: v9.2.x
  • HashiCorp Vault
    • Minimum: v1.12.x
    • Accepted: v1.13.x
    • Recommended: v1.14.x

Note: Depending on your Windows setup windows-build-tools may need to be installed first. Also, for MacOS users, you should have xcode-select or entire Xcode App installed.

Table of Contents

Install

npm install hashi-vault-js --save

Uninstall

npm uninstall hashi-vault-js

Release notes and versions

Change log

Class Constructor

{
  // Indicates if the HTTP request to the Vault server should use
  // HTTPS (secure) or HTTP (non-secure) protocol
  https: true,
  // If https is true, then provide client certificate, client key and
  // the root CA cert
  // Client cert and key are optional now
  cert: './client.crt',
  key: './client.key',
  cacert: './ca.crt',
  // Indicate the server name/IP, port and API version for the Vault,
  // all paths are relative to this one
  baseUrl: 'https://127.0.0.1:8200/v1',
  // Sets the root path after the base URL, it translates to a
  // partition inside the Vault where the secret engine / auth method was enabled
  // Can be passed individually on each function through mount parameter
  rootPath: 'secret',
  // HTTP request timeout in milliseconds
  timeout: 1000,
  // If should use a proxy or not by the HTTP request
  // Example:
  // proxy: { host: proxy.ip, port: proxy.port }
  proxy: false,
  // Namespace (multi-tenancy) feature available on all Vault Enterprise versions
  namespace: 'admin'
}

Module usage

Note: This package covers some auth methods and secret engines. Check Limitations section for more details.

  • Production
const Vault = require('hashi-vault-js');

const vault = new Vault( {
    https: true,
    cert: './client.crt',
    key: './client.key',
    cacert: './ca.crt',
    baseUrl: 'https://127.0.0.1:8200/v1',
    rootPath: 'secret',
    timeout: 2000,
    proxy: false,
    // Only for Vault Enterprise
    namespace: 'ns1'
});
  • Development
const Vault = require('hashi-vault-js');

const vault = new Vault( {
    https: true,
    baseUrl: 'https://127.0.0.1:8200/v1',
    rootPath: 'secret',
    timeout: 5000,
    proxy: false
});

Check health status of the Vault server:

const status = await vault.healthCheck();

Perform a login on the Vault with role-id/secret-id pair, (AppRole login) and get a valid client token:

const token = await vault.loginWithAppRole(RoleId, SecretId).client_token;

Perform a login on the Vault with LDAP username/password pair, and get a valid client token:

const token = await vault.loginWithLdap(Username, Password).client_token;

Perform a login on the Vault with Userpass username/password pair, and get a valid client token:

const token = await vault.loginWithUserpass(Username, Password).client_token;

Perform a login on the Vault with Kubernetes service accounts token, and get a valid client token:

const token = await vault.loginWithK8s(Role, Token).client_token;

Perform a login on the Vault with TLS certificate and key, and get a valid client token:

const token = await vault.loginWithCert(certName, Token).client_token;

Define a function to return secret engine information from the Vault:

const secretEngineInfo = function(token) {
  vault.readKVEngineConfig(token).then(function(result){
    return result;
  }).catch(function(error){
    return error;
  });
};

Create a new secret in the Vault:

const Item={
  name: "slack",
  data: {
    bot_token1: "xoxb-123456789012-1234567890123-1w1lln0tt3llmys3cr3tatm3",
    bot_token2: "xoxb-123456789013-1234567890124-1w1lln0tt3llmys3cr3tatm3"
  }
};

const data = await vault.createKVSecret(token, Item.name , Item.data);

Read a secret from the Vault:

const secrets = await vault.readKVSecret(token, Item.name);

Update secret version 1 in the Vault:

const data = await vault.updateKVSecret(token, Item.name , newData, 1);

TypeScript

hashi-vault-js includes TypeScript definitions in the Vault.d.ts.

let response: ReadKVSecretResponse = null;
try {
  const { data } = await vault.readKVSecret(token, Item.name);
  response = data;
}

Mount points

Most of the Vault Server API endpoints can be mounted on non-default path. For that reason, there's a last parameter in the related functions to allow using a custom mount path.

For instance, if you want to enable KV v2 on a different path, you can do so:

vault secrets enable -path=knight kv-v2

Now you call this helper library functions with the correct mount path:

const config = await vault.readKVEngineConfig(token, "knight")

Error handling

This package extends the error stack to differentiate if the exception occurred on the Vault API layer or not. Also, adds a help message from the Vault API docs.

try {
  vault.function(...);
}
// An exception happened and it was thrown
catch(err) {
  if(err.isVaultError) {
    // This an error from Vault API
    // Check Vault hint on this error
    console.log(err.vaultHelpMessage);
  }
  else {
    // Here is still the full Axios error, e.g. err.isAxiosError, err.response, err.request
    // This allows handling of network/tls related issues
    // Or just re-kthrow if you don't care
    throw err;
  }
}

Check below docs for more information on specific function groups.

Available functions

GroupTypeDefault mount pointLink
Active Directory (AD)Secret engine/adDoc file
AppRoleAuth method/auth/approleDoc file
LDAPAuth method/auth/ldapDoc file
KubernetesAuth method/auth/kubernetesDoc file
KV v2Secret engine/kvDoc file
PKISecret engine/pkiDoc file
System BackendSystemGeneral operationsDoc file
System BackendSystemSEAL operationsDoc file
TLS CertificateAuth method/auth/certDoc file
TokenAuth method/auth/tokenDoc file
UserpassAuth method/auth/userpassDoc file

Coverage and limitations

The following HashiCorp Vault API endpoints are currently covered:

MethodCoverage status
AppRolePartially
LDAPAll endpoints
UserpassAll endpoints
KubernetesAll endpoints
TLS CertPartially
TokenMost of them
  • Secret engines:
EngineCoverage status
Active Directory (AD)Most of them
KV Version 2All endpoints
PKIMost of them

Test environment

Follow the detailed instructions from this doc

References

  • HashiCorp Vault Using KV engine doc

  • HashiCorp Vault Docker Hub page

  • Ruan Bekker's Blog post

Contributing

If you want to contribute to the module and make it better, your help is very welcome. You can do so submitting a Pull Request. It will be reviewed and merged to main branch if accepted.

By contributing to this public repository, you fully agree with the following Developer's Certificate of Origin document.

Reporting an issue

If you have found what you believe to be an issue with hashi-vault-js please do not hesitate to file an issue on the GitHub repository here.

Suggesting a new feature

If you want to see new features or enhancements to the current ones, we would love to hear them. Please submit an issue on the GitHub repository here.

Authors

Written by Rod Anami rod.anami@kyndryl.com, June 2020.

Contributors

License

This project is licensed under the MIT license.

HashiCorp Vault is licensed under the Business Source License 1.1.

0.4.14

8 months ago

0.4.13

1 year ago

0.4.12

1 year ago

0.4.11

2 years ago

0.4.10

2 years ago

0.4.9

2 years ago

0.4.8

2 years ago

0.4.7

2 years ago

0.4.6

3 years ago

0.4.5

3 years ago

0.4.4

3 years ago

0.4.3

3 years ago

0.4.2

3 years ago

0.4.1

3 years ago

0.4.0

3 years ago

0.3.22

3 years ago

0.3.21

3 years ago

0.3.20

3 years ago

0.3.19

3 years ago

0.3.18

3 years ago

0.3.17

3 years ago

0.3.16

3 years ago

0.3.15

3 years ago

0.3.14

4 years ago

0.3.13

4 years ago

0.3.12

4 years ago

0.3.9

4 years ago

0.3.8

4 years ago

0.3.7

4 years ago

0.3.6

4 years ago

0.3.3

4 years ago

0.3.1

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago