This document is intended to cover the idea of how we can create an alias in vault token role via Nomad API using "Token Role-based Integration".
As you know, Vault's Token Authentication Backend supports a concept called "roles". Token roles allow policies to be grouped together and token creation to be delegated to a trusted service such as Nomad. By creating a token role, the set of policies that tasks managed by Nomad can access may be limited compared to giving Nomad a root token. Token roles allow both allowlist and denylist management of policies accessible to the role.
We can create vault token roles on the basis of policies for Nomad Cluster and can manage them like read/update/delete via API from Nomad clusters.
We can follow the below steps to achieve the same thing -
Step 1 - Create Policy
First, we need to create a policy that will be used by Nomad to read and create the alias in the vault token role. The policy which will create must have the below capabilities that have been defined in the policy file named "nomad-server-policy.hcl".
|
Run the below command to create the policy -
vault policy write nomad-server nomad-server-policy.hcl |
Step 2 - Create a Vault Token Role
Please create a token role using the below JSON file template named "nomad-cluster-role.json" -
{ |
Run below command to create token role -
vault write /auth/token/roles/nomad-cluster @nomad-cluster-role.json
|
Step 3 - Create token for Nomad
Use the below command to create a token for Nomad using the Vault token role which we created in Step 2.
vault token create -policy nomad-server -period 72h -orphan |
Here, we have created tokens for 72 hours. You can define it as your convenience.
Step 4 - Configure Nomad to use the created token role.
Configure your Nomad Server configuration file using the role name and token. we created in previous steps like below -
vault { |
Step 5 - Now try to read the current token role by running the below command from the Nomad server -
curl -s --header "X-Vault-Token: $VAULT_TOKEN" $VAULT_ADDR/v1/auth/token/roles/nomad-cluster |
Note -
Please set below environment variable before running the above curl command -
$VAULT_TOKEN - Which we created in Step-3.
$VAULT_ADDR - VAULT Server address and port.
Step 6 - Create your alias file named - payload.json using the below sample JSON template file -
{ |
Step 7 - Try to post your created payload file "payload.json" using the below command -
curl --header "X-Vault-Token: $VAULT_TOKEN" --request POST --data @payload.json $VAULT_ADDR/v1/auth/token/roles/nomad-cluster
|
Step 8 - Again, run the below command to check whether your aliases have been updated in the token role "nomad-cluster".
curl -s --header "X-Vault-Token: $VAULT_TOKEN" $VAULT_ADDR/v1/auth/token/roles/nomad-cluster |
Note - Once you run the above command, please check the below tag in output -
{ |
Thanks and Happy Learning..!!
Resources -
https://www.nomadproject.io/docs/integrations/vault-integration
https://www.vaultproject.io/api/auth/token