Skip to content

Vault

Install

1
npm install @testcontainers/vault --save-dev

Examples

These examples use the following libraries:

Choose an image from the container registry and substitute IMAGE.

Write/read a value

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
await using container = await new VaultContainer(IMAGE).withVaultToken(VAULT_TOKEN).start();

const client = vault({
  apiVersion: "v1",
  endpoint: container.getAddress(),
  token: container.getRootToken(),
});

await client.write("secret/data/hello", {
  data: {
    message: "world",
    other: "vault",
  },
});

const result = await client.read("secret/data/hello");
const data = result?.data?.data;

expect(data.message).toBe("world");
expect(data.other).toBe("vault");

Run CLI init commands at startup

1
2
3
4
5
6
7
8
9
await using container = await new VaultContainer(IMAGE)
  .withVaultToken(VAULT_TOKEN)
  .withInitCommands("secrets enable transit", "write -f transit/keys/my-key")
  .start();

const result = await container.exec(["vault", "read", "-format=json", "transit/keys/my-key"]);

expect(result.exitCode).toBe(0);
expect(result.output).toContain("my-key");