github.com/ncodes/nomad@v0.5.7-0.20170403112158-97adf4a74fb3/website/source/docs/vault-integration/index.html.md (about)

     1  ---
     2  layout: "docs"
     3  page_title: "Vault Integration"
     4  sidebar_current: "docs-vault-integration"
     5  description: |-
     6    Learn how to integrate 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  ## Vault Configuration
    25  
    26  To use the Vault integration, Nomad servers must be provided a Vault token. This
    27  token can either be a root token or a periodic token with permissions to create
    28  from a token role. The root token is the easiest way to get started, but we
    29  recommend a token role based token for production installations. Nomad servers
    30  will renew the token automatically.
    31  
    32  ### Root Token Integration
    33  
    34  If Nomad is given a [root
    35  token](https://www.vaultproject.io/docs/concepts/tokens.html#root-tokens), no
    36  further configuration is needed as Nomad can derive a token for jobs using any
    37  Vault policies.
    38  
    39  ### Token Role based Integration
    40  
    41  Vault's [Token Authentication Backend][auth] supports a concept called "roles".
    42  Token roles allow policies to be grouped together and token creation to be
    43  delegated to a trusted service such as Nomad. By creating a token role, the set
    44  of policies that tasks managed by Nomad can access may be limited compared to
    45  giving Nomad a root token. Token roles allow both white-list and blacklist
    46  management of policies accessible to the role.
    47  
    48  To configure Nomad and Vault to create tokens against a role, the following must
    49  occur:
    50  
    51    1. Create a "nomad-server" policy used by Nomad to create and manage tokens.
    52  
    53    2. Create a Vault token role with the configuration described below.
    54  
    55    3. Configure Nomad to use the created token role.
    56  
    57    4. Give Nomad servers a periodic token with the "nomad-server" policy created
    58       above.
    59  
    60  #### Required Vault Policies
    61  
    62  The token Nomad receives must have the capabilities listed below. An explanation
    63  for the use of each capability is given.
    64  
    65  ```
    66  # Allow creating tokens under "nomad-cluster" token role. The token role name
    67  # should be updated if "nomad-cluster" is not used.
    68  path "auth/token/create/nomad-cluster" {
    69    capabilities = ["update"]
    70  }
    71  
    72  # Allow looking up "nomad-cluster" token role. The token role name should be
    73  # updated if "nomad-cluster" is not used.
    74  path "auth/token/roles/nomad-cluster" {
    75    capabilities = ["read"]
    76  }
    77  
    78  # Allow looking up the token passed to Nomad to validate # the token has the
    79  # proper capabilities. This is provided by the "default" policy.
    80  path "auth/token/lookup-self" {
    81    capabilities = ["read"]
    82  }
    83  
    84  # Allow looking up incoming tokens to validate they have permissions to access
    85  # the tokens they are requesting. This is only required if
    86  # `allow_unauthenticated` is set to false.
    87  path "auth/token/lookup" {
    88    capabilities = ["update"]
    89  }
    90  
    91  # Allow revoking tokens that should no longer exist. This allows revoking
    92  # tokens for dead tasks.
    93  path "auth/token/revoke-accessor" {
    94    capabilities = ["update"]
    95  }
    96  
    97  # Allow checking the capabilities of our own token. This is used to validate the
    98  # token upon startup.
    99  path "sys/capabilities-self" {
   100    capabilities = ["update"]
   101  }
   102  
   103  # Allow our own token to be renewed.
   104  path "auth/token/renew-self" {
   105    capabilities = ["update"]
   106  }
   107  ```
   108  
   109  The above [`nomad-server` policy](/data/vault/nomad-server-policy.hcl) is
   110  available for download. Below is an example of writing this policy to Vault:
   111  
   112  ```
   113  # Download the policy
   114  $ curl https://nomadproject.io/data/vault/nomad-server-policy.hcl -O -s -L
   115  
   116  # Write the policy to Vault
   117  $ vault policy-write nomad-server nomad-server-policy.hcl
   118  ```
   119  
   120  #### Vault Token Role Configuration
   121  
   122  A Vault token role must be created for use by Nomad. The token role can be used
   123  to manage what Vault policies are accessible by jobs submitted to Nomad. The
   124  policies can be managed as a whitelist by using `allowed_policies` in the token
   125  role definition or as a blacklist by using `disallowed_policies`.
   126  
   127  If using `allowed_policies`, tasks may only request Vault policies that are in
   128  the list. If `disallowed_policies` is used, task may request any policy that is
   129  not in the `disallowed_policies` list. There are tradeoffs to both approaches
   130  but generally it is easier to use the blacklist approach and add policies that
   131  you would not like tasks to have access to into the `disallowed_policies` list.
   132  
   133  An example token role definition is given below:
   134  
   135  ```json
   136  {
   137    "disallowed_policies": "nomad-server",
   138    "explicit_max_ttl": 0,
   139    "name": "nomad-cluster",
   140    "orphan": false,
   141    "period": 259200,
   142    "renewable": true
   143  }
   144  ```
   145  
   146  ##### Token Role Requirements
   147  
   148  Nomad checks that token role has an appropriate configuration for use by the
   149  cluster. Fields that are checked are documented below as well as descriptions of
   150  the important fields. See Vault's [Token Authentication Backend][auth]
   151  documentation for all possible fields and more complete documentation.
   152  
   153  * `allowed_policies` - Specifies the list of allowed policies as a
   154    comma-separated string. This list should contain all policies that jobs running
   155    under Nomad should have access to.
   156  
   157  *    `disallowed_policies` - Specifies the list of disallowed policies as a
   158       comma-seperated string. This list should contain all policies that jobs running
   159       under Nomad should **not** have access to. The policy created above that
   160       grants Nomad the ability to generate tokens from the token role should be
   161       included in list of disallowed policies. This prevents tokens created by
   162       Nomad from generating new tokens with different policies than those granted
   163       by Nomad.
   164  
   165       A regression occured in Vault 0.6.4 when validating token creation using a
   166       token role with `disallowed_policies` such that it is not usable with
   167       Nomad. This will be remedied in 0.6.5 and does not effect earlier versions
   168       of Vault.
   169  
   170  * `explicit_max_ttl` - Specifies the max TTL of a token. **Must be set to `0`** to
   171    allow periodic tokens.
   172  
   173  * `name` - Specifies the name of the policy. We recommend using the name
   174    `nomad-cluster`. If a different name is chosen, replace the token role in the
   175    above policy.
   176  
   177  * `orphan` - Specifies whether tokens created against this token role will be
   178    orphaned and have no parents. **Must be set to `false`**. This ensures that the
   179    token can be revoked when the task is no longer needed or a node dies.
   180  
   181  * `period` - Specifies the length the TTL is extended by each renewal in
   182    seconds. It is suggested to set this value on the order of magnitude of 3 days
   183    (259200 seconds) to avoid a large renewal request rate to Vault. **Must be set
   184    to a positive value**.
   185  
   186  * `renewable` - Specifies whether created tokens are renewable. **Must be set to
   187    `true`**. This allows Nomad to renew tokens for tasks.
   188  
   189  The above [`nomad-cluster` token role](/data/vault/nomad-cluster-role.json) is
   190  available for download. Below is an example of writing this role to Vault:
   191  
   192  ```
   193  # Download the token role
   194  $ curl https://nomadproject.io/data/vault/nomad-cluster-role.json -O -s -L
   195  
   196  # Create the token role with Vault
   197  $ vault write /auth/token/roles/nomad-cluster @nomad-cluster-role.json
   198  ```
   199  
   200  
   201  #### Example Configuration
   202  
   203  To make getting started easy, the basic [`nomad-server`
   204  policy](/data/vault/nomad-server-policy.hcl) and
   205  [`nomad-cluster` role](/data/vault/nomad-cluster-role.json) described above are
   206  available for download.
   207  
   208  The below example assumes Vault is accessible, unsealed and the operator has
   209  appropriate permissions.
   210  
   211  ```shell
   212  # Download the policy and token role
   213  $ curl https://nomadproject.io/data/vault/nomad-server-policy.hcl -O -s -L
   214  $ curl https://nomadproject.io/data/vault/nomad-cluster-role.json -O -s -L
   215  
   216  # Write the policy to Vault
   217  $ vault policy-write nomad-server nomad-server-policy.hcl
   218  
   219  # Create the token role with Vault
   220  $ vault write /auth/token/roles/nomad-cluster @nomad-cluster-role.json
   221  ```
   222  
   223  #### Retrieving the Token Role based Token
   224  
   225  After the token role is created, a token suitable for the Nomad servers may be
   226  retrieved by issuing the following Vault command:
   227  
   228  ```
   229  $ vault token-create -policy nomad-server -period 72h
   230  Key             Value
   231  ---             -----
   232  token           f02f01c2-c0d1-7cb7-6b88-8a14fada58c0
   233  token_accessor  8cb7fcb3-9a4f-6fbf-0efc-83092bb0cb1c
   234  token_duration  259200s
   235  token_renewable true
   236  token_policies  [default nomad-server]
   237  ```
   238  
   239  The token can then be set in the server configuration's [vault block][config],
   240  as a command-line flag, or via an environment variable.
   241  
   242  ```
   243  $ VAULT_TOKEN=f02f01c2-c0d1-7cb7-6b88-8a14fada58c0 nomad agent -config /path/to/config
   244  ```
   245  
   246  An example of what may be contained in the configuration is shown below. For
   247  complete documentation please see the [Nomad agent Vault integration][config]
   248  configuration.
   249  
   250  ```hcl
   251  vault {
   252    enabled          = true
   253    ca_path          = "/etc/certs/ca"
   254    cert_file        = "/var/certs/vault.crt"
   255    key_file         = "/var/certs/vault.key"
   256    address          = "https://vault.service.consul:8200"
   257    create_from_role = "nomad-cluster"
   258  }
   259  ```
   260  
   261  ## Agent Configuration
   262  
   263  To enable Vault integration, please see the [Nomad agent Vault
   264  integration][config] configuration.
   265  
   266  ## Vault Definition Syntax
   267  
   268  To configure a job to retrieve Vault tokens, please see the [`vault` job
   269  specification documentation][vault-spec].
   270  
   271  ## Troubleshooting
   272  
   273  Upon startup, Nomad will attempt to connect to the specified Vault server. Nomad
   274  will lookup the passed token and if the token is from a token role, the token
   275  role will be validated. Nomad will not shutdown if given an invalid Vault token,
   276  but will log the reasons the token is invalid and disable Vault integration.
   277  
   278  ## Assumptions
   279  
   280  - Vault 0.6.2 or later is needed.
   281  
   282  [auth]: https://www.vaultproject.io/docs/auth/token.html "Vault Authentication Backend"
   283  [config]: /docs/agent/configuration/vault.html "Nomad Vault Configuration Block"
   284  [createfromrole]: /docs/agent/configuration/vault.html#create_from_role "Nomad vault create_from_role Configuration Flag"
   285  [template]: /docs/job-specification/template.html "Nomad template Job Specification"
   286  [vault]: https://www.vaultproject.io/ "Vault by HashiCorp"
   287  [vault-spec]: /docs/job-specification/vault.html "Nomad Vault Job Specification"