@creditkarma/vault-client

中文文档
1.0.0发布于 2022/06/23Apache-2.0TypeScriptCommonJS近 30 天下载 2592

A client for communicating with Hashicorp Vault written in TypeScript

原始使用文档
文档目录7 节
  1. Install
  2. Usage
  3. VaultClient
  4. VaultService
  5. Running Tests
  6. Contributing
  7. License

Vault Client

A client for Hashicorp Vault written in TypeScript.

Install

$ npm install --save @creditkarma/vault-client

Usage

This library exposes two classes for working with Vault, VaultClient and VaultService. VaultClient provides a slightly higher level of abstraction and a more limited API.

VaultClient

VaultClient provides two methods: get and set. Both methods return Promises.

Options

VaultClient expects the access token for Vault to be available in a local file. The path to this file is passed as an option.

Available options:

  • apiVersion - API version to use. Currently this can only be 'v1'.
  • protocol - The protocol to use 'http' or 'https'. Defaults to 'http'.
  • destination - The location of the Vault instance. Defaults to 'localhost:8200'.
  • mount - The mount at which secrets can be found. Defaults to 'secret'.
  • namespace - A namespace for secrets. This path will be prepended to all get/set requests. Defaults to ''.
  • tokenPath - The local file path to a file containing the Vault token. Defaults to '/tmp/token'.
  • requestOptions - Options passed to the underlying Request library. The options can be overriden on a per-request basis by passing an optional final parameter to any of the service or client methods. This will be used to set up TLS.

Mount vs Namespace

The mount is the underlying Vault path at which secrets are stored. By default this is secret. So all of your secrets would be stored at an address like: http://localhost:8200/secret/<key>. This can be configured differently per Vault instance.

The namespace is an addition on this path to organize your secrets. Your service may share a Vault instance with other services. The namespace could then be your service name. All your secrets would be stored at: http://localhost:8200/secret/<namespace>/<key>.

Data Formatting

When a secret is written to Vault the value you set will be wrapped in an object of this form:

{
  "value": value
}

When reading values with VaultClient objects of this form are assumed. If there is no value property an exception will be raised. When performing a get only the value of the value key will be returned. This allows get and set methods to operate on primitive values.

Example

import { IHVConfig, VaultClient } from '@creditkarma/vault-client'

const options: IHVConfig = {
    apiVersion: 'v1',
    protocol: 'http',
    destination: 'localhost:8200',
    mount: 'secret',
    namespace: '',
    tokenPath: '/tmp/token',
    requestOptions: {
        headers: {
            host: 'localhost'
        }
    }
}
const client: VaultClient = new VaultClient(options)

// Because we set a namespace this is actually written to 'secret/key'
client.set('key', 'value').then(() => {
    // value successfully written
    client.get<string>('key').then((val: string) => {
        // val = 'value'
    })
})

VaultService

VaultService provides more direct access to the raw Vault HTTP API. Method arguments and method return types conform to the HTTP Vault API.

Options

VaultService accepts a sub-set of the options that VaultClient accepts:

  • apiVersion
  • protocol
  • destination
  • requestOptions

Example

Like VaultClient all methods return Promises.

import { IServiceConfig, VaultService } from '@creditkarma/vault-client'

const options: IServiceConfig = {
    apiVersion: 'v1',
    protocol: 'http',
    destination: 'localhost:8200',
    requestOptions: {
        headers: {
            host: 'localhost'
        }
    }
}
const service: VaultService = new VaultService(options)

service.status()
service.init({ secret_shares: 1, secret_threshold: 1 })
service.unseal({ key: 'key', reset: true })
service.read(path, token)
service.write(path, value, token)

Running Tests

The good ol' npm test will work. However, running tests requires a running Vault server. This is done with docker. If you don't have docker-compose on your system you will be unable to run tests. Make sure you have docker.

$ npm test

You can spin up the Vault server without running tests:

$ npm run docker

This docker image has a little sugar on top of the base Vault image. It exposes an endpoint for retrieving the token.

$ curl localhost:8201/client-token

Contributing

For more information about contributing new features and bug fixes, see our Contribution Guidelines. External contributors must sign Contributor License Agreement (CLA)

License

This project is licensed under Apache License Version 2.0

版本与发布记录

@creditkarma/vault-client 共发布 32 个版本,首次发布于 2017/12/06;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.0latest2022/06/234 年前
0.5.14-5next2022/06/144 年前
0.5.14-32022/06/094 年前
0.5.14-22022/06/084 年前
0.5.14-12022/06/034 年前
0.5.14-02022/06/024 年前

其他发布通道:next → 0.5.14-5,fix → 0.4.5。

关于 @creditkarma/vault-client 的常见问题

@creditkarma/vault-client 是什么?

A client for communicating with Hashicorp Vault written in TypeScript

如何安装 @creditkarma/vault-client?

在项目目录运行 npm install @creditkarma/vault-client,或使用 pnpm add / yarn add / bun add。

@creditkarma/vault-client 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@creditkarma/vault-client 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@creditkarma/vault-client 可以商用吗?

@creditkarma/vault-client 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@creditkarma/vault-client 的下载量有多少?

根据 npm 官方下载统计,@creditkarma/vault-client 在 2026/10/11 – 2026/11/09 这 30 天内被下载 2,592 次,日均约 86 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@creditkarma/vault-client 还在维护吗?

最新版本 1.0.0 发布于 2022/06/23(4 年前);至今共发布 32 个版本,其中过去 12 个月发布了 0 个;首次发布于 2017/12/06。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。