github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/website/pages/docs/vault-integration/index.mdx (about)

     1  ---
     2  layout: docs
     3  page_title: Vault Integration
     4  sidebar_title: Vault Integration
     5  description: >-
     6    Learn how to integrate Nomad with HashiCorp Vault and retrieve Vault tokens
     7    for
     8  
     9    tasks.
    10  ---
    11  
    12  # Vault Integration
    13  
    14  Many workloads require access to tokens, passwords, certificates, API keys, and
    15  other secrets. To enable secure, auditable and easy access to your secrets,
    16  Nomad integrates with HashiCorp's [Vault][]. Nomad servers and clients
    17  coordinate with Vault to derive a Vault token that has access to only the Vault
    18  policies the tasks needs. Nomad clients make the token available to the task and
    19  handle the tokens renewal. Further, Nomad's [`template` block][template] can
    20  retrieve secrets from Vault making it easier than ever to secure your
    21  infrastructure.
    22  
    23  Note that in order to use Vault with Nomad, you will need to configure and
    24  install Vault separately from Nomad. Nomad does not run Vault for you.
    25  
    26  -> **Note:** Vault integration requires Vault version 0.6.2 or higher.
    27  
    28  ## Vault Configuration
    29  
    30  To use the Vault integration, Nomad servers must be provided a Vault token. This
    31  token can either be a root token or a periodic token with permissions to create
    32  from a token role. The root token is the easiest way to get started, but we
    33  recommend a token role based token for production installations. Nomad servers
    34  will renew the token automatically. **Note that the Nomad clients do not need to
    35  be provided with a Vault token.**
    36  
    37  ### Root Token Integration
    38  
    39  If Nomad is given a [root
    40  token](https://www.vaultproject.io/docs/concepts/tokens#root-tokens), no
    41  further configuration is needed as Nomad can derive a token for jobs using any
    42  Vault policies.
    43  
    44  ### Token Role based Integration
    45  
    46  Vault's [Token Authentication Backend][auth] supports a concept called "roles".
    47  Token roles allow policies to be grouped together and token creation to be
    48  delegated to a trusted service such as Nomad. By creating a token role, the set
    49  of policies that tasks managed by Nomad can access may be limited compared to
    50  giving Nomad a root token. Token roles allow both white-list and blacklist
    51  management of policies accessible to the role.
    52  
    53  To configure Nomad and Vault to create tokens against a role, the following must
    54  occur:
    55  
    56  1. Create a "nomad-server" policy used by Nomad to create and manage tokens.
    57  
    58  2. Create a Vault token role with the configuration described below.
    59  
    60  3. Configure Nomad to use the created token role.
    61  
    62  4. Give Nomad servers a periodic token with the "nomad-server" policy created
    63     above.
    64  
    65  #### Required Vault Policies
    66  
    67  The token Nomad receives must have the capabilities listed below. An explanation
    68  for the use of each capability is given.
    69  
    70  ```hcl
    71  # Allow creating tokens under "nomad-cluster" token role. The token role name
    72  # should be updated if "nomad-cluster" is not used.
    73  path "auth/token/create/nomad-cluster" {
    74    capabilities = ["update"]
    75  }
    76  
    77  # Allow looking up "nomad-cluster" token role. The token role name should be
    78  # updated if "nomad-cluster" is not used.
    79  path "auth/token/roles/nomad-cluster" {
    80    capabilities = ["read"]
    81  }
    82  
    83  # Allow looking up the token passed to Nomad to validate # the token has the
    84  # proper capabilities. This is provided by the "default" policy.
    85  path "auth/token/lookup-self" {
    86    capabilities = ["read"]
    87  }
    88  
    89  # Allow looking up incoming tokens to validate they have permissions to access
    90  # the tokens they are requesting. This is only required if
    91  # `allow_unauthenticated` is set to false.
    92  path "auth/token/lookup" {
    93    capabilities = ["update"]
    94  }
    95  
    96  # Allow revoking tokens that should no longer exist. This allows revoking
    97  # tokens for dead tasks.
    98  path "auth/token/revoke-accessor" {
    99    capabilities = ["update"]
   100  }
   101  
   102  # Allow checking the capabilities of our own token. This is used to validate the
   103  # token upon startup.
   104  path "sys/capabilities-self" {
   105    capabilities = ["update"]
   106  }
   107  
   108  # Allow our own token to be renewed.
   109  path "auth/token/renew-self" {
   110    capabilities = ["update"]
   111  }
   112  ```
   113  
   114  The above [`nomad-server` policy](/data/vault/nomad-server-policy.hcl) is
   115  available for download. Below is an example of writing this policy to Vault:
   116  
   117  ```shell
   118  # Download the policy
   119  $ curl https://nomadproject.io/data/vault/nomad-server-policy.hcl -O -s -L
   120  
   121  # Write the policy to Vault
   122  $ vault policy write nomad-server nomad-server-policy.hcl
   123  ```
   124  
   125  #### Vault Token Role Configuration
   126  
   127  A Vault token role must be created for use by Nomad. The token role can be used
   128  to manage what Vault policies are accessible by jobs submitted to Nomad. The
   129  policies can be managed as a whitelist by using `allowed_policies` in the token
   130  role definition or as a blacklist by using `disallowed_policies`.
   131  
   132  If using `allowed_policies`, tasks may only request Vault policies that are in
   133  the list. If `disallowed_policies` is used, task may request any policy that is
   134  not in the `disallowed_policies` list. There are trade-offs to both approaches
   135  but generally it is easier to use the blacklist approach and add policies that
   136  you would not like tasks to have access to into the `disallowed_policies` list.
   137  
   138  An example token role definition is given below:
   139  
   140  ```json
   141  {
   142    "disallowed_policies": "nomad-server",
   143    "token_explicit_max_ttl": 0,
   144    "name": "nomad-cluster",
   145    "orphan": true,
   146    "token_period": 259200,
   147    "renewable": true
   148  }
   149  ```
   150  
   151  ##### Token Role Requirements
   152  
   153  Nomad checks that token role has an appropriate configuration for use by the
   154  cluster. Fields that are checked are documented below as well as descriptions of
   155  the important fields. See Vault's [Token Authentication Backend][auth]
   156  documentation for all possible fields and more complete documentation.
   157  
   158  - `allowed_policies` - Specifies the list of allowed policies as a
   159    comma-separated string. This list should contain all policies that jobs running
   160    under Nomad should have access to.
   161  
   162  - `disallowed_policies` - Specifies the list of disallowed policies as a
   163    comma-separated string. This list should contain all policies that jobs running
   164    under Nomad should **not** have access to. The policy created above that
   165    grants Nomad the ability to generate tokens from the token role should be
   166    included in list of disallowed policies. This prevents tokens created by
   167    Nomad from generating new tokens with different policies than those granted
   168    by Nomad.
   169  
   170    A regression occurred in Vault 0.6.4 when validating token creation using a
   171    token role with `disallowed_policies` such that it is not usable with
   172    Nomad. This was remedied in 0.6.5 and does not effect earlier versions
   173    of Vault.
   174  
   175  - `token_explicit_max_ttl` - Specifies the max TTL of a token. **Must be set to `0`** to
   176    allow periodic tokens.
   177  
   178  - `name` - Specifies the name of the policy. We recommend using the name
   179    `nomad-cluster`. If a different name is chosen, replace the token role in the
   180    above policy.
   181  
   182  - `orphan` - Specifies whether tokens created against this token role will be
   183    orphaned and have no parents. Nomad does not enforce the value of this field
   184    but understanding the implications of each value is important.
   185  
   186    If set to false, all tokens will be revoked when the Vault token given to
   187    Nomad expires. This makes it easy to revoke all tokens generated by Nomad but
   188    forces all Nomad servers to use the same Vault token, even through upgrades of
   189    Nomad servers. If the Vault token that was given to Nomad and used to generate
   190    a tasks token expires, the token used by the task will also be revoked which
   191    is not ideal.
   192  
   193    When set to true, the tokens generated for tasks will not be revoked when
   194    Nomad's token is revoked. However Nomad will still revoke tokens when the
   195    allocation is no longer running, minimizing the lifetime of any task's token.
   196    With orphaned enabled, each Nomad server may also use a unique Vault token,
   197    making bootstrapping and upgrading simpler. As such, **setting `orphan = true`
   198    is the recommended setting**.
   199  
   200  - `token_period` - Specifies the length the TTL is extended by each renewal in
   201    seconds. It is suggested to set this value on the order of magnitude of 3 days
   202    (259200 seconds) to avoid a large renewal request rate to Vault. **Must be set
   203    to a positive value**.
   204  
   205  - `renewable` - Specifies whether created tokens are renewable. **Must be set to
   206    `true`**. This allows Nomad to renew tokens for tasks.
   207  
   208  The above [`nomad-cluster` token role](/data/vault/nomad-cluster-role.json) is
   209  available for download. Below is an example of writing this role to Vault:
   210  
   211  ```shell
   212  # Download the token role
   213  $ curl https://nomadproject.io/data/vault/nomad-cluster-role.json -O -s -L
   214  
   215  # Create the token role with Vault
   216  $ vault write /auth/token/roles/nomad-cluster @nomad-cluster-role.json
   217  ```
   218  
   219  #### Example Configuration
   220  
   221  To make getting started easy, the basic [`nomad-server`
   222  policy](/data/vault/nomad-server-policy.hcl) and
   223  [`nomad-cluster` role](/data/vault/nomad-cluster-role.json) described above are
   224  available for download.
   225  
   226  The below example assumes Vault is accessible, unsealed and the operator has
   227  appropriate permissions.
   228  
   229  ```shell
   230  # Download the policy and token role
   231  $ curl https://nomadproject.io/data/vault/nomad-server-policy.hcl -O -s -L
   232  $ curl https://nomadproject.io/data/vault/nomad-cluster-role.json -O -s -L
   233  
   234  # Write the policy to Vault
   235  $ vault policy write nomad-server nomad-server-policy.hcl
   236  
   237  # Create the token role with Vault
   238  $ vault write /auth/token/roles/nomad-cluster @nomad-cluster-role.json
   239  ```
   240  
   241  #### Retrieving the Token Role based Token
   242  
   243  After the token role is created, a token suitable for the Nomad servers may be
   244  retrieved by issuing the following Vault command:
   245  
   246  ```shell-sessionvault token create -policy nomad-server -period 72h -orphan
   247  Key             Value
   248  ---             -----
   249  token           f02f01c2-c0d1-7cb7-6b88-8a14fada58c0
   250  token_accessor  8cb7fcb3-9a4f-6fbf-0efc-83092bb0cb1c
   251  token_duration  259200s
   252  token_renewable true
   253  token_policies  [default nomad-server]
   254  ```
   255  
   256  The `-orphan` flag is included when generating the Nomad server token above to
   257  prevent revocation of the token when its parent expires. Vault typically
   258  creates tokens with a parent-child relationship. When an ancestor token is
   259  revoked, all of its descendant tokens and their associated leases are revoked
   260  as well.
   261  
   262  When generating Nomad's Vault token, we need to ensure that revocation of the
   263  parent token does not revoke Nomad's token. To prevent this behavior we
   264  specify the `-orphan` flag when we create the Nomad's Vault token. All
   265  other tokens generated by Nomad for jobs will be generated using the policy
   266  default of `orphan = false`.
   267  
   268  More information about creating orphan tokens can be found in
   269  [Vault's Token Hierarchies and Orphan Tokens documentation][tokenhierarchy].
   270  
   271  The token can then be set in the server configuration's
   272  [`vault` stanza][config], as a command-line flag, or via an environment
   273  variable.
   274  
   275  ```shell-sessionVAULT_TOKEN=f02f01c2-c0d1-7cb7-6b88-8a14fada58c0 nomad agent -config /path/to/config
   276  
   277  ```
   278  
   279  An example of what may be contained in the configuration is shown below. For
   280  complete documentation please see the [Nomad agent Vault integration][config]
   281  configuration.
   282  
   283  ```hcl
   284  vault {
   285    enabled          = true
   286    ca_path          = "/etc/certs/ca"
   287    cert_file        = "/var/certs/vault.crt"
   288    key_file         = "/var/certs/vault.key"
   289    address          = "https://vault.service.consul:8200"
   290    create_from_role = "nomad-cluster"
   291  }
   292  ```
   293  
   294  ## Agent Configuration
   295  
   296  To enable Vault integration, please see the [Nomad agent Vault
   297  integration][config] configuration.
   298  
   299  ## Vault Definition Syntax
   300  
   301  To configure a job to retrieve Vault tokens, please see the [`vault` job
   302  specification documentation][vault-spec].
   303  
   304  ## Troubleshooting
   305  
   306  ### Invalid Vault token
   307  
   308  Upon startup, Nomad will attempt to connect to the specified Vault server. Nomad
   309  will lookup the passed token and if the token is from a token role, the token
   310  role will be validated. Nomad will not shutdown if given an invalid Vault token,
   311  but will log the reasons the token is invalid and disable Vault integration.
   312  
   313  ### Permission Denied errors
   314  
   315  If you are using a Vault version less than 0.7.1 with a Nomad version greater than or equal to 0.6.1, you will need to update your task's policy (listed in [the `vault` stanza of the job specification][vault-spec]) to add the following:
   316  
   317  ```hcl
   318  path "sys/leases/renew" {
   319      capabilities = ["update"]
   320  }
   321  ```
   322  
   323  This is included in Vault's "default" policy beginning with Vault 0.7.1 and is relied upon by Nomad's Vault integration beginning with Nomad 0.6.1. If you're using a newer Nomad version with an older Vault version, your default policy may not automatically include this and you will see "permission denied" errors in your Nomad logs similar to the following:
   324  
   325  ```plaintext
   326  Code: 403. Errors:
   327  URL: PUT https://vault:8200/v1/sys/leases/renew
   328  * permission denied
   329  ```
   330  
   331  ### No Secret Exists
   332  
   333  Vault has two APIs for secrets, [`v1` and `v2`][vault-secrets-version]. Each version
   334  has different paths, and Nomad does not abstract this for you. As such you will
   335  need to specify the path as reflected by Vault's HTTP API, rather than the path
   336  used in the `vault kv` command.
   337  
   338  You can see examples of `v1` and `v2` syntax in the
   339  [template documentation][vault-kv-templates].
   340  
   341  [auth]: https://www.vaultproject.io/docs/auth/token 'Vault Authentication Backend'
   342  [config]: /docs/configuration/vault 'Nomad Vault Configuration Block'
   343  [createfromrole]: /docs/configuration/vault#create_from_role 'Nomad vault create_from_role Configuration Flag'
   344  [template]: /docs/job-specification/template 'Nomad template Job Specification'
   345  [vault]: https://www.vaultproject.io/ 'Vault by HashiCorp'
   346  [vault-spec]: /docs/job-specification/vault 'Nomad Vault Job Specification'
   347  [tokenhierarchy]: https://www.vaultproject.io/docs/concepts/tokens#token-hierarchies-and-orphan-tokens 'Vault Tokens - Token Hierarchies and Orphan Tokens'
   348  [vault-secrets-version]: https://www.vaultproject.io/docs/secrets/kv 'KV Secrets Engine'
   349  [vault-kv-templates]: /docs/job-specification/template#vault-kv-api-v1 'Vault KV API v1'