11# node-vault
22
3- [ ![ Build Status] ( https://img.shields.io/github/checks-status/nodevault/node-vault/master.svg?style=flat-square )] ( https://github.com/nodevault/node-vault/actions?query=branch%3Amaster )
4- [ ![ Coverage Status] ( https://img.shields.io/codecov/c/github/nodevault/node-vault/master.svg?style=flat-square )] ( https://app.codecov.io/gh/nodevault/node-vault/tree/master )
3+ [ ![ Build Status] ( https://img.shields.io/github/actions/workflow/status/nodevault/node-vault/lint-and-test.yaml?branch=master&style=flat-square )] ( https://github.com/nodevault/node-vault/actions/workflows/lint-and-test.yaml )
54[ ![ Download Status] ( https://img.shields.io/npm/dm/node-vault.svg?style=flat-square )] ( https://www.npmjs.com/package/node-vault )
6- [ ![ test] ( https://img.shields.io/npm/v/node-vault?style=flat-square )] ( https://www.npmjs.com/package/node-vault )
5+ [ ![ NPM Version] ( https://img.shields.io/npm/v/node-vault?style=flat-square )] ( https://www.npmjs.com/package/node-vault )
6+ [ ![ License] ( https://img.shields.io/npm/l/node-vault?style=flat-square )] ( https://github.com/nodevault/node-vault/blob/master/LICENSE )
77[ ![ Dependency Status] ( https://img.shields.io/librariesio/release/npm/node-vault.svg?style=flat-square )] ( https://libraries.io/npm/node-vault/ )
88[ ![ Open Collective backers and sponsors] ( https://img.shields.io/opencollective/all/node-vault?style=flat-square )] ( https://opencollective.com/node-vault/contribute )
99
1010A client for the HTTP API of HashiCorp's [ Vault] written for Node.js.
1111
1212
1313## Install
14+
1415Prerequisites:
15- - NodeJS >= ` 16 .0.0`
16+ - Node.js >= ` 18 .0.0`
1617
1718``` bash
1819npm install -S node-vault
1920```
2021
21- > The year is 2023; If, for whatever reason, you need to use an older version of node.js (yet still ` >= 6.x ` ), use ` node-vault <= v0.10.0 `
22- >
23- > Please note that ` node-vault <= v0.10.0 ` contains multiple vulnerabilities ☠️
22+ > ** Note:** If you need to use an older version of Node.js (>= 6.x), use ` node-vault <= v0.10.0 ` .
23+ > Please be aware that ` node-vault <= v0.10.0 ` contains multiple known vulnerabilities ☠️
24+
25+ TypeScript definitions are included in the package.
2426
2527
2628## Test
2729
28- Run tests using docker-compose (includes vault, postgres and running the tests inside ) with:
30+ Run tests using docker-compose (includes vault and postgres ) with:
2931``` bash
3032docker-compose up --force-recreate test
3133```
3234
35+ ## Configuration
36+
37+ ### Client Options
38+
39+ ``` javascript
40+ const vault = require (' node-vault' )({
41+ apiVersion: ' v1' , // API version (default: 'v1')
42+ endpoint: ' http://127.0.0.1:8200' , // Vault server URL (default: 'http://127.0.0.1:8200')
43+ token: ' MY_TOKEN' , // Vault token for authentication
44+ pathPrefix: ' ' , // Optional prefix for all request paths
45+ namespace: ' my-namespace' , // Vault Enterprise namespace
46+ noCustomHTTPVerbs: false , // Use GET with ?list=1 instead of LIST HTTP method
47+ requestOptions: {}, // Custom axios request options applied to all requests
48+ });
49+ ```
50+
51+ ### Environment Variables
52+
53+ The client reads the following environment variables as defaults:
54+
55+ | Variable | Description |
56+ | --- | --- |
57+ | ` VAULT_ADDR ` | Vault server URL (overridden by ` endpoint ` option) |
58+ | ` VAULT_TOKEN ` | Vault token (overridden by ` token ` option) |
59+ | ` VAULT_NAMESPACE ` | Vault Enterprise namespace (overridden by ` namespace ` option) |
60+ | ` VAULT_PREFIX ` | Request path prefix (overridden by ` pathPrefix ` option) |
61+ | ` VAULT_SKIP_VERIFY ` | When set, disables SSL certificate verification |
62+
63+
3364## Usage
3465
3566### Init and unseal
3667
3768``` javascript
38- var options = {
39- apiVersion: ' v1' , // default
40- endpoint: ' http://127.0.0.1:8200' , // default
41- token: ' MY_TOKEN' // optional client token; can be fetched after valid initialization of the server
42- };
43-
44- // get new instance of the client
45- var vault = require (" node-vault" )(options);
69+ const vault = require (' node-vault' )({
70+ apiVersion: ' v1' ,
71+ endpoint: ' http://127.0.0.1:8200' ,
72+ token: ' MY_TOKEN' , // optional; can be set after initialization
73+ });
4674
4775// init vault server
4876vault .init ({ secret_shares: 1 , secret_threshold: 1 })
49- .then ( (result ) => {
50- var keys = result .keys ;
51- // set token for all following requests
52- vault .token = result .root_token ;
53- // unseal vault server
54- return vault .unseal ({ secret_shares: 1 , key: keys[0 ] })
55- })
56- .catch (console .error );
77+ .then ((result ) => {
78+ const keys = result .keys ;
79+ // set token for all following requests
80+ vault .token = result .root_token ;
81+ // unseal vault server
82+ return vault .unseal ({ secret_shares: 1 , key: keys[0 ] });
83+ })
84+ .catch (console .error );
5785```
5886
59- ### Write, read and delete secrets
87+ ### Write, read, update and delete secrets
6088
6189``` javascript
6290vault .write (' secret/hello' , { value: ' world' , lease: ' 1s' })
63- .then ( () => vault .read (' secret/hello' ))
64- .then ( () => vault .delete (' secret/hello' ))
65- .catch (console .error );
91+ .then (() => vault .read (' secret/hello' ))
92+ .then (() => vault .delete (' secret/hello' ))
93+ .catch (console .error );
94+ ```
95+
96+ The ` update ` method sends a ` PATCH ` request with ` application/merge-patch+json ` content type:
97+
98+ ``` javascript
99+ vault .update (' secret/data/hello' , { data: { value: ' new-world' } })
100+ .catch (console .error );
66101```
102+
103+ ### List secrets
104+
105+ ``` javascript
106+ vault .list (' secret/metadata/' )
107+ .then ((result ) => console .log (result .data .keys ))
108+ .catch (console .error );
109+ ```
110+
67111### Kubernetes Auth Example
112+
68113``` javascript
114+ const fs = require (' fs' );
69115
70- // if vault kubernets endpoint is /auth/example-cluster/login and role is example-role
71- // read token from default token mount path
72- const token = await fs .readFileSync (' /var/run/secrets/kubernetes.io/serviceaccount/token' , { encoding: ' utf8' });
73- vault .kubernetesLogin ({role: ' example-role' ,
74- jwt: token,
75- kubernetesPath: ' example-cluster' })
116+ // Read service account token from default mount path
117+ const jwt = fs .readFileSync (' /var/run/secrets/kubernetes.io/serviceaccount/token' , { encoding: ' utf8' });
118+
119+ // If the Vault Kubernetes auth endpoint is /auth/example-cluster/login and the role is example-role
120+ vault .kubernetesLogin ({
121+ role: ' example-role' ,
122+ jwt: jwt,
123+ mount_point: ' example-cluster' ,
124+ }).catch (console .error );
125+ ```
126+
127+ ### AppRole Auth Example
128+
129+ ``` javascript
130+ const vault = require (' node-vault' )();
131+
132+ vault .approleLogin ({
133+ role_id: ' my-role-id' ,
134+ secret_id: ' my-secret-id' ,
135+ })
136+ .then ((result ) => {
137+ // client token is automatically set on successful login
138+ console .log (result .auth .client_token );
139+ })
140+ .catch (console .error );
76141```
77142
143+ ### Error Handling
144+
145+ The client exposes two error types accessible from the module:
146+
147+ - ** ` VaultError ` ** — Base error class for all vault-related errors.
148+ - ** ` ApiResponseError ` ** — Thrown on non-200/204 responses. Contains a ` response ` property with ` statusCode ` and ` body ` .
149+
150+ ``` javascript
151+ vault .read (' secret/missing' )
152+ .catch ((err ) => {
153+ console .error (err .message ); // Error message from Vault
154+ if (err .response ) {
155+ console .error (err .response .statusCode ); // e.g. 404
156+ console .error (err .response .body ); // Response body from Vault
157+ }
158+ });
159+ ```
160+
161+ ### Custom Commands
162+
163+ You can register custom API commands using ` generateFunction ` :
164+
165+ ``` javascript
166+ vault .generateFunction (' myCustomEndpoint' , {
167+ method: ' GET' ,
168+ path: ' /my-custom/endpoint/{{id}}' ,
169+ });
170+
171+ // Use the generated function
172+ vault .myCustomEndpoint ({ id: ' abc123' })
173+ .then (console .log )
174+ .catch (console .error );
175+ ```
176+
177+
78178## Docs
79- Just generate [ docco] docs via ` npm run docs ` .
179+ Generate [ docco] docs via:
180+ ``` bash
181+ npm run docs
182+ ```
80183
81184
82185## Examples
83- Please have a look at the [ examples] and the generated [ feature list] to see what is already implemented .
186+ Please have a look at the [ examples] and the generated [ feature list] to see all supported Vault API endpoints .
84187
85- Instead of installing all the dependencies like vault itself, postgres and other stuff you can
86- use [ docker] and [ docker-compose] to link and run multiple docker containers with all of its dependencies.
188+ Instead of installing all the dependencies like vault itself and postgres, you can
189+ use [ docker] and [ docker-compose] to link and run multiple docker containers with all of their dependencies.
87190
88191``` bash
89192git clone git@github.com:nodevault/node-vault.git
@@ -97,8 +200,8 @@ First of all you should initialize and unseal the vault:
97200``` bash
98201node example/init.js
99202```
100- You should see ` root_token: ` followed by a long key in the response.
101- Please copy that long key and export it as environment variable:
203+ You should see ` root_token: ` followed by a long key in the response.
204+ Please copy that long key and export it as an environment variable:
102205``` bash
103206export VAULT_TOKEN=< insert long key here>
104207```
@@ -108,24 +211,25 @@ Now you are able to run all of the other [examples]:
108211node example/policies.js
109212```
110213
111- ## Connecting to vault through a bastion host
214+ ## Connecting to Vault Through a Bastion Host
112215
113- To connect to a vault server in a private network with a bastion host, you'll need to first open a connection:
216+ To connect to a vault server in a private network through a bastion host, first open a SOCKS proxy connection:
114217``` bash
115- ssh -D < socks4Port > bastion.example.com
218+ ssh -D < socksPort > bastion.example.com
116219```
117220
221+ Then configure the client with a SOCKS proxy agent:
118222``` javascript
119- const SocksProxyAgent = require (' socks-proxy-agent' );
120- const agent = new SocksProxyAgent (` socks://127.0.0.1:${ socks4Port} ` , true );
121- const options = {
223+ const { SocksProxyAgent } = require (' socks-proxy-agent' );
224+ const agent = new SocksProxyAgent (` socks://127.0.0.1:${ socksPort} ` );
225+
226+ const vault = require (' node-vault' )({
122227 apiVersion: ' v1' ,
123228 requestOptions: {
124- agent,
229+ httpsAgent: agent,
230+ httpAgent: agent,
125231 },
126- };
127-
128- const vault = require (' node-vault' )(options);
232+ });
129233```
130234
131235## Custom SSL/TLS Configuration
@@ -169,8 +273,7 @@ See [example/pass_request_options.js](example/pass_request_options.js) for more
169273[ examples ] : https://github.com/nodevault/node-vault/tree/master/example
170274[ docker-compose.yml ] : https://github.com/nodevault/node-vault/tree/master/docker-compose.yml
171275[ Vault ] : https://vaultproject.io/
172- [ docker-compose ] : https://www.docker.com/docker-compose
173- [ docker ] : http://docs.docker.com/
174- [ docker toolbox ] : https://www.docker.com/toolbox
276+ [ docker-compose ] : https://docs.docker.com/compose/
277+ [ docker ] : https://docs.docker.com/
175278[ docco ] : http://jashkenas.github.io/docco
176279[ feature list ] : https://github.com/nodevault/node-vault/tree/master/features.md
0 commit comments