Authenticate a Stack
You can authenticate a Stack in two ways: by using OIDC to dynamically authenticate with the built-in identity_token
block or by accessing existing authentication credentials with the store
block. We recommend authenticating your Stack with OIDC because using static credentials to authenticate providers presents a security risk, even if you rotate your credentials regularly.
Authenticate with OIDC
OpenID Connect (OIDC) is an identity layer on top of the OAuth 2.0 protocol. You can use HCP Terraform’s Workload identity tokens, built on the OpenID Connect protocol, to securely connect and authenticate your Stacks with cloud providers.
Hands on: Walk through setting up OIDC for a Stack in the Deploy a Stack with HCP Terraform tutorial.
Stacks have a built-in identity_token
block that creates workload identity tokens, also known as JWT tokens. You can use these tokens to authenticate Stacks with Terraform providers securely.
This guide covers how to set up OIDC for Stacks using the identity_token
block.
Overview
The details of Stack authentication differ based on which cloud provider you are setting up, but the basic steps remain the same:
- Set up the trust configuration between your cloud provider and HCP Terraform, which usually includes creating roles and policies for your cloud provider.
- Add an
identity_token
block to your Stack’s deployment file using the audience and roles you created in the previous step.
Your deployments can reference the value of the identity_token
block to pass the trust relationship role to your Stack’s operations.
Configure trust configuration
In this example, you will set up an example trust policy with AWS. For examples of setting up trust policies with other providers, refer to our repository of example configurations.
We recommend using Terraform to create and configure the trust relationship and permissions for the cloud provider you want to authenticate with. Using the following Terraform configuration, create a new workspace and configure the aws_region
, tf_organization
, tf_project,
and tf_stack
variables for your specific set up.
After setting up your workspace in HCP Terraform, perform a plan and apply operation. HCP Terraform will create your OpenID provider, policy, and role before outputting your new role’s ARN with the role_arn
output. Copy your new AWS ARN role because this is the value you use to configure your cloud provider with your Stack and HCP Terraform.
Adjust access of trust
Workload identity tokens contain useful metadata in their payloads, known as claims. Once a cloud platform verifies a token using HCP Terraform’s public key, it can look at the identity token's claims to match it to the correct permissions or reject it.
You do not need to understand the full token specification or what every claim means, but you can use the claims to adjust your trust relationship.
Workload identity tokens commonly reference the following claims.
Claim | Explanation |
---|---|
jti (JWT ID) | A unique identifier for each token, which is a UUID. |
iss (issuer) | Full URL of the HCP Terraform instance that signed the token. For example, “https://app.terraform.io”. |
iat (issued at) | Unix timestamp when the token was issued. May be required by certain relying parties. |
nbf (not before) | Unix Timestamp when the token can start being used. Should be the same as iat for your purposes and may be required by certain relying parties. |
aud (audience) | Intended audience for the token. For example, “api://AzureADTokenExchange” for Azure. |
exp (expiration) | Unix timestamp based on the timeout of the operation phase that it was issued for. |
sub (subject) | Fully qualified path to a Stack deployment, followed by the operation type:organization:<organization_name>:project:<project_name>:stack:<stack_name>:deployment:my-deployment-name:operation:<operation_type> For example: organization:My_Org:project:My_Project:stack:My_Stack:deployment:staging:operation:apply |
You can further customize your token’s permissions using the following custom claims.
Claim | Explanation |
---|---|
terraform_operation ("run phase") | The Terraform operation this token was issued for. For example, “plan” or “apply”. |
terraform_stack_deployment_name | Name of the deployment that the operation is for. |
terraform_stack_id (stack ID) | External ID of the Stack that the operation is for. |
terraform_stack_name (stack name) | Name of the Stack that the operation is for. |
terraform_project_id (project ID) | External ID of the project that the operation is taking place in. Useful if you want to use the same role across Stacks in a given project. |
terraform_project_name (project name) | Name of the project that the operation is taking place in. |
terraform_organization_id (organization ID) | External ID of the HCP Terraform organization that the operation is taking place in. Useful if you are in a large company that may have more than one organization or if you want to use the same role across your entire organization. |
terraform_organization_name (organization name) | Name of the HCP Terraform organization that the operation is taking place in. |
terraform_plan_id (plan ID) | External ID of the plan that this token is being used for creating or applying. This might be used to track down where a token has been leaked from or to block a specific token if it has been leaked. |
You can also update your configuration to use the current thumbprint for HCP Terraform by running the following command and removing the colons from the value it returns.
Configure HCP Terraform
Stacks use the identity_token
block to define a JSON Web Token (JWT) for a specific deployment. This token enables OIDC-based authentication, allowing your Stack deployments to securely connect to cloud providers like AWS, Azure, and GCP.
When you define the identity_token
block, you specify its audience. For example, the example Stack deployment configuration defines an identity_token
block specifying the AWS audience.
Once defined, you can reference an identity token in a deployment's input variables and reference that token in a provider’s configuration.
You also need to add your trust relationship, your role ARN, to configure your token with the trust relationship to authenticate AWS resources.
Now, the deployments are authenticated with AWS and can pass the trust configuration to your Stack’s providers for authentication.
With this setup, each of your deployments pass the ARN of your trust relationship role and a JWT token generated by AWS each time you plan or apply your Stack. Each of your deployments uses their own role, configured with the specific permissions you require.
For more examples of setting up OIDC with different providers, refer to our repository of example configurations.
Authenticate with existing credentials
You can use the store
block to define key-value secrets in your deployment configuration and then access those values in your deployments.
You can use the store
block to access credentials stored in an HCP Terraform variable set. This example uses a store
block with a variable set to access credentials stored in HCP Terraform. For more information about using store
blocks, refer to the Deployment configuration reference.
Before writing any configuration, ensure your Stack can access the variable set you are targeting by allowing your project to access the set or making the set globally available.
Next, add a store
block to your deployment configuration file to access your variable set. Once defined, your deployments can access your variable set’s values.
For example, if your test
deployment can use your store
to access your variable set’s keys, and therefore, your variable set’s AWS provider credentials.
Your test
deployment can define your AWS credentials as inputs
, allowing your providers
to access those credentials.
This example allows your aws.this
provider to authenticate to AWS using the credentials from a variable set stored in HCP Terraform. For more information about using store
blocks, refer to the Deployment configuration reference.