Skip to content

Commit 04ae6c3

Browse files
committed
add hcp dynamic credentials example
1 parent c2e10c6 commit 04ae6c3

File tree

6 files changed

+230
-0
lines changed

6 files changed

+230
-0
lines changed

.idea/workspace.xml

Lines changed: 67 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

hcp/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Bootstrapping trust between a TFC workspace and HCP
2+
3+
This directory contains example code for setting up a Terraform Cloud workspace whose runs will be automatically authenticated to HCP using Workload Identity.
4+
5+
The basic building blocks in `hcp.tf` will configure a workload identity pool and provider and create a service principle that is bound to a particular Terraform Cloud workspace.
6+
7+
The building blocks in `tfc-workspace.tf` will create that Terraform Cloud workspace and set all the configuration variables needed in order to allow runs to authenticate to HCP.
8+
9+
## How to use
10+
11+
You'll need the Terraform CLI installed, and you'll need to set the following environment variables in your local shell:
12+
13+
1. `TFE_TOKEN`: a Terraform Cloud user token with permission to create workspaces within your organization
14+
2. `HCP_CLIENT_ID`: ID of the service principal to configure HCP with, requires `roles/admin` on the organization
15+
3. `HCP_CLIENT_SECRET`: Corresponding secret to the provided HCP client ID
16+
4. `HCP_PROJECT_ID`: ID of the HCP project to create the new service principal that Terraform Cloud will be able to assume during runs
17+
18+
Copy `terraform.tfvars.example` to `terraform.tfvars` and customize the required variables. You can also set values for any other variables you'd like to customize beyond the default.
19+
20+
Run `terraform plan` to verify your setup, and then run `terraform apply`.
21+
22+
Congratulations! You now have a Terraform Cloud workspace where runs will automatically authenticate to HCP when using the HCP provider.

hcp/hcp.tf

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright (c) HashiCorp, Inc.
2+
# SPDX-License-Identifier: MPL-2.0
3+
4+
provider "hcp" {}
5+
6+
# Project data resource that is used to fetch information about the current HCP project
7+
#
8+
# https://registry.terraform.io/providers/hashicorp/hcp/latest/docs/data-sources/project
9+
data "hcp_project" "hcp_project" {
10+
}
11+
12+
# The service principal resource manages a HCP Service Principal.
13+
#
14+
# https://registry.terraform.io/providers/hashicorp/hcp/latest/docs/resources/service_principal
15+
resource "hcp_service_principal" "workload_sp" {
16+
name = "hcp-terraform"
17+
parent = data.hcp_project.hcp_project.resource_name
18+
}
19+
20+
# Grants the service principal the ability to provision and destroy resources in HCP
21+
#
22+
# https://registry.terraform.io/providers/hashicorp/hcp/latest/docs/resources/project_iam_binding
23+
resource "hcp_project_iam_binding" "workload_sp_binding" {
24+
project_id = data.hcp_project.hcp_project.resource_id
25+
principal_id = hcp_service_principal.workload_sp.resource_id
26+
role = "roles/contributor"
27+
}
28+
29+
locals {
30+
sub_regex = "^organization:${var.organization_name}:project:${var.tfc_project_name}:workspace:${var.tfc_workspace_name}:run_phase:.*"
31+
}
32+
33+
# The workload identity provider resource allows federating an external identity to an HCP Service Principal.
34+
#
35+
# https://registry.terraform.io/providers/hashicorp/hcp/latest/docs/resources/iam_workload_identity_provider
36+
resource "hcp_iam_workload_identity_provider" "tfc" {
37+
name = "hcp-terraform-provider"
38+
service_principal = hcp_service_principal.workload_sp.resource_name
39+
description = "Allow HCP Terraform agents to act as the ${hcp_service_principal.workload_sp.name} service principal"
40+
41+
oidc = {
42+
issuer_uri = "https://${var.tfc_hostname}"
43+
allowed_audiences = [var.tfc_hcp_audience]
44+
}
45+
46+
conditional_access = "jwt_claims.sub matches `${local.sub_regex}`"
47+
}

hcp/terraform.tfvars.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
tfc_organization_name = "my-organization"
2+
tfc_project_name = "my project"

hcp/tfc-workspace.tf

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Copyright (c) HashiCorp, Inc.
2+
# SPDX-License-Identifier: MPL-2.0
3+
4+
provider "tfe" {
5+
hostname = var.tfc_hostname
6+
}
7+
8+
# Data source used to grab the project under which a workspace will be created.
9+
#
10+
# https://registry.terraform.io/providers/hashicorp/tfe/latest/docs/data-sources/project
11+
data "tfe_project" "tfc_project" {
12+
name = var.tfc_project_name
13+
organization = var.organization_name
14+
}
15+
16+
# Runs in this workspace will be automatically authenticated
17+
# to HCP with the permissions set in the HCP policy.
18+
#
19+
# https://registry.terraform.io/providers/hashicorp/tfe/latest/docs/resources/workspace
20+
resource "tfe_workspace" "my_workspace" {
21+
name = var.tfc_workspace_name
22+
organization = var.organization_name
23+
project_id = data.tfe_project.tfc_project.id
24+
}
25+
26+
# The following variables must be set to allow runs
27+
# to authenticate to HCP.
28+
#
29+
# https://registry.terraform.io/providers/hashicorp/tfe/latest/docs/resources/variable
30+
resource "tfe_variable" "enable_hcp_provider_auth" {
31+
workspace_id = tfe_workspace.my_workspace.id
32+
33+
key = "TFC_HCP_PROVIDER_AUTH"
34+
value = "true"
35+
category = "env"
36+
37+
description = "Enable the Workload Identity integration for HCP."
38+
}
39+
40+
# The resource name of the provider for which the external identity
41+
# will be exchanged against using the credential file.
42+
resource "tfe_variable" "tfc_hcp_provider_resource_name" {
43+
workspace_id = tfe_workspace.my_workspace.id
44+
45+
key = "TFC_HCP_RUN_PROVIDER_RESOURCE_NAME"
46+
value = hcp_iam_workload_identity_provider.tfc.resource_name
47+
category = "env"
48+
49+
description = "The resource name of the provider for which the external identity will be exchanged against using the credential file."
50+
}
51+
52+
# The value to use as the `aud` claim in run identity tokens
53+
resource "tfe_variable" "tfc_hcp_audience" {
54+
workspace_id = tfe_workspace.my_workspace.id
55+
56+
key = "TFC_HCP_WORKLOAD_IDENTITY_AUDIENCE"
57+
value = var.tfc_hcp_audience
58+
category = "env"
59+
60+
description = "The value to use as the audience claim in run identity tokens"
61+
}

hcp/vars.tf

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright (c) HashiCorp, Inc.
2+
# SPDX-License-Identifier: MPL-2.0
3+
4+
variable "tfc_hcp_audience" {
5+
type = string
6+
default = "hcp.workload.identity"
7+
description = "The audience value to use in run identity tokens if the default audience value is not desired."
8+
}
9+
10+
variable "tfc_hostname" {
11+
type = string
12+
default = "app.terraform.io"
13+
description = "The hostname of the TFC or TFE instance you'd like to use with HCP"
14+
}
15+
16+
variable "organization_name" {
17+
type = string
18+
description = "The name of your Terraform Cloud organization"
19+
}
20+
21+
variable "tfc_project_name" {
22+
type = string
23+
default = "Default Project"
24+
description = "The project under which a workspace will be created"
25+
}
26+
27+
variable "tfc_workspace_name" {
28+
type = string
29+
default = "my-hcp-workspace"
30+
description = "The name of the workspace that you'd like to create and connect to HCP"
31+
}

0 commit comments

Comments
 (0)