25
loading...
This website collects cookies to deliver better user experience
docker run --name vault -p 8200:8200 vault:1.7.3
WARNING! dev mode is enabled! In this mode, Vault runs entirely in-memory and starts unsealed with a single unseal key. The root token is already authenticated to the CLI, so you can immediately begin using Vault.
KV backend
enabled at path secret
. This comes enabled in dev mode by default.Enable New Engine
and then selecting KV backend
and follow through the setup.Access
from the top menu. You’ll see only the token
method enabled. Enable New Method
and select AppRole
. Leave the settings to default and click Enable Method
.Policies
from the top menu and click Create ACL Policy
.readonly-kv-backend
, and enter following content for Policy
.path "secret/data/mysql/webapp" {
capabilities = [ "read" ]
}
Create Policy
to save it.docker exec -it vault /bin/sh
VAULT_ADDR
and VAULT_TOKEN
environment variables.export VAULT_ADDR=http://localhost:8200
export VAULT_TOKEN=<ROOT TOKEN>
vault write auth/approle/role/node-app-role \
token_ttl=1h \
token_max_ttl=4h \
token_policies=readonly-kv-backend
Success! Data written to: auth/approle/role/node-app-role
RoleID
and SecretID
, much like a username and password. The application can exchange this RoleID
and SecretID
for a token, which can then be used in subsequent requests.RoleID
pertaining to the node-app-role via the following command:vault read auth/approle/role/node-app-role/role-id
SecretID
:vault write -f auth/approle/role/node-app-role/secret-id
SecretID
to our applications like this. You should use response wrapping to securely deliver SecretID
to your application. For the purpose of this demo, we’ll pass SecretID
as an environment variable to our application.vault kv put secret/mysql/webapp db_name="users" username="admin" password="passw0rd"
node-vault
package first, if not already installed.npm install node-vault
ROLE_ID
and SECRET_ID
environment variables to pass these values to the application.export ROLE_ID=<role id fetched in previous section>
export SECRET_ID=<secret id fetched in previous section>
const vault = require("node-vault")({
apiVersion: "v1",
endpoint: "http://127.0.0.1:8200",
});
const roleId = process.env.ROLE_ID;
const secretId = process.env.SECRET_ID;
const run = async () => {
const result = await vault.approleLogin({
role_id: roleId,
secret_id: secretId,
});
vault.token = result.auth.client_token; // Add token to vault object for subsequent requests.
const { data } = await vault.read("secret/data/mysql/webapp"); // Retrieve the secret stored in previous steps.
const databaseName = data.data.db_name;
const username = data.data.username;
const password = data.data.password;
console.log({
databaseName,
username,
password,
});
console.log("Attempt to delete the secret");
await vault.delete("secret/data/mysql/webapp"); // This attempt will fail as the AppRole node-app-role doesn't have delete permissions.
};
run();
index.js
and run it via the node index.js
command.