github.com/zoomfoo/nomad@v0.8.5-0.20180907175415-f28fd3a1a056/website/source/guides/operations/vault-integration/index.html.md (about)

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