Skip to main content

Federated Authentication on Google Cloud

Run Terraform on Google Cloud without storing a NetActuate API key. The NetActuate provider authenticates with the Google service account the machine runs as, and exchanges its Google ID token for a short-lived NetActuate API token.

How It Works​

  1. You create a federated identity in your NetActuate account that trusts one Google service account and sets what its tokens may do.
  2. On a Compute Engine VM running as that service account, the provider requests a Google ID token from the VM's metadata server.
  3. NetActuate verifies the token and issues an API token limited to the identity's roles, permissions and addresses, valid for at most one hour.
  4. The provider uses that token for all API calls and requests a new one when it expires.

Requirements​

Google Cloud

  • A service account for Terraform.
  • A Compute Engine VM that runs as that service account and has a static external IP address.

NetActuate

  • NetActuate Terraform provider 0.4.11 or later.
  • The account owner's API key, to create the federated identity. Federated tokens cannot manage federated identities, roles or access.

Google Cloud Setup​

Create the service account, a static address and the VM. These resources use the google provider and can live in the same configuration as the federated identity below.

resource "google_service_account" "terraform" {
account_id = "netactuate-terraform"
display_name = "NetActuate Terraform"
}

resource "google_compute_address" "terraform" {
name = "netactuate-terraform"
region = "us-central1"
}

resource "google_compute_instance" "terraform" {
name = "netactuate-terraform"
machine_type = "e2-small"
zone = "us-central1-a"

boot_disk {
initialize_params {
image = "debian-cloud/debian-12"
}
}

network_interface {
network = "default"
access_config {
nat_ip = google_compute_address.terraform.address
}
}

service_account {
email = google_service_account.terraform.email
scopes = ["cloud-platform"]
}
}

Create the Federated Identity​

Apply this with the account owner's API key in NETACTUATE_API_KEY. The identity trusts the service account by its unique ID, grants its tokens a custom role, and allows them only from the VM's address. Give the role the permissions your Terraform configuration needs; see Access Control (IAM).

resource "netactuate_iam_role" "terraform" {
name = "Terraform on Google Cloud"
slug = "terraform-gcp"

permissions = {
"dns.read" = "account"
"account.ssh-key.read" = "account"
"account.ssh-key.manage" = "account"
}
}

resource "netactuate_iam_federated_identity" "terraform" {
name = "terraform-on-gcp"
token_type = "google_id_token"
subject = google_service_account.terraform.unique_id
role_ids = [tonumber(netactuate_iam_role.terraform.id)]

acl {
cidr = "${google_compute_address.terraform.address}/32"
description = "Terraform VM"
}
}

output "netactuate_audience" {
value = netactuate_iam_federated_identity.terraform.audience
}

The audience output is the value the provider on the VM needs.

Federated Identity Settings​

ArgumentDescriptionRequiredDefault
nameName of the identity, also given to the tokens it issuesYes
token_typegoogle_id_token for a Google ID token, or google_sa_jwt for a JWT the service account signs itselfYes
subjectThe service account's unique ID for google_id_token, or its email for google_sa_jwtYes
role_idsRoles granted to issued tokens, system or custom. Look up IDs with the netactuate_iam_roles data sourceOne of role_ids or permission
permissionIndividual permissions granted to issued tokens (key, optional scope)One of role_ids or permission
aclAddress ranges the tokens can be used from (cidr, optional description)One of acl, inherit_api_acl or allow_any_ip
inherit_api_aclTokens can also be used from the account's API allowlistfalse
allow_any_ipTokens can be used from any address. Cannot be combined with acl or inherit_api_aclfalse
token_ttlToken lifetime in seconds, 60 to 3600No3600
enabledWhether new tokens can be issuedNotrue

Setting enabled = false or deleting the identity revokes every token it has issued. Changing its roles, permissions, resources, addresses, token_ttl or resource_mode also revokes them. Changing name or description does not.

The identity's read-only last_exchange shows the most recent exchange: result, and for a refused one the reason and reason_message, such as subject_mismatch when the token was issued to another service account. Refresh the configuration that manages the identity to see it.

Configure the Provider on the VM​

On the VM, the provider needs only the audience from the netactuate_audience output. Do not set an API key.

variable "netactuate_audience" {
type = string
}

provider "netactuate" {
federation {
audience = var.netactuate_audience
}
}

Alternatively, leave the provider block empty and set the audience in the environment:

export NETACTUATE_FEDERATION_AUDIENCE="<netactuate_audience output>"

Run terraform init and terraform plan on the VM to verify the setup.

Running Outside Google Cloud​

Terraform can also use the identity from a machine outside Google Cloud, such as a CI runner. The machine needs Google credentials that can act as the service account: the Service Account Token Creator role on the service account, with the IAM Service Account Credentials API enabled in the project. Add the machine's address to the identity's acl.

Set token_command to a command that prints a Google ID token for the audience. The provider runs it before every token exchange, so runs of any length work, and passes the audience in NETACTUATE_FEDERATION_AUDIENCE.

provider "netactuate" {
federation {
audience = var.netactuate_audience
token_command = [
"gcloud", "auth", "print-identity-token",
"--impersonate-service-account=netactuate-terraform@PROJECT_ID.iam.gserviceaccount.com",
"--audiences=${var.netactuate_audience}",
]
}
}

Alternatively, write the token to a file and set token_file. A Google ID token is valid for one hour, and a NetActuate token issued from it never lasts longer, so regenerate the file before it expires.

provider "netactuate" {
federation {
audience = var.netactuate_audience
token_file = "${path.module}/google-id-token"
}
}

Service Account JWTs​

Instead of a Google ID token, the identity can accept a JWT the service account signs itself. Set token_type = "google_sa_jwt" and subject to the service account's email. Each JWT must carry:

ClaimValue
iss, subThe service account's email
audThe identity's audience
iat, expIssue and expiry time, at most 12 hours apart
jtiA unique ID. Each JWT is accepted once

Because each JWT is accepted once, use token_command with a script that signs a new one each time. Sign with the IAM Service Account Credentials API rather than a downloaded service account key:

#!/bin/sh
set -e
SA=netactuate-terraform@PROJECT_ID.iam.gserviceaccount.com
dir=$(mktemp -d)
trap 'rm -rf "$dir"' EXIT
now=$(date +%s)
printf '{"iss":"%s","sub":"%s","aud":"%s","iat":%d,"exp":%d,"jti":"%s"}' \
"$SA" "$SA" "$NETACTUATE_FEDERATION_AUDIENCE" "$now" "$((now + 600))" "$(od -An -N16 -tx1 /dev/urandom | tr -d ' \n')" > "$dir/claims.json"
gcloud iam service-accounts sign-jwt "$dir/claims.json" "$dir/jwt" --iam-account="$SA" > /dev/null
cat "$dir/jwt"
provider "netactuate" {
federation {
audience = var.netactuate_audience
token_command = ["${path.module}/sign-jwt.sh"]
}
}

Need Help?​

If you need assistance with federated authentication, visit our support page.