Skip to content

Support generic OIDC authentication for AzureRM backend #31802

@karlschriek

Description

@karlschriek

Terraform Version

1.2.9

Use Cases

We want to set up workflows that run terraform using Azure Workload Identities. The workload identity approach works by treating an AKS cluster as an OIDC provider, and a specific ServiceAccount within a specific Namespace on that cluster as an identity, which can be federated to an Azure AD Service Principal. (https://azure.github.io/azure-workload-identity/docs/)

A similar (but specific to GitHub) solution was introduced in #30936. Similarly, this was introduced for GitHub on the AzureRM provider here: hashicorp/terraform-provider-azurerm#16555 (the naming of both PRs is very generic, but the actual implementation is GitHub specific).

Generic OIDC: AzureRM Provider

A generic OIDC implementation (which works for Azure Workload Identities) was added in the AzureRM provider here: hashicorp/terraform-provider-azurerm#18118. It uses github.com/hashicorp/go-azure-helpers v0.40.0 and introduces an oidc_token variable

This implementation allows us to configure the provider using values populated by the AWI webhook.

export ARM_CLIENT_ID=$AZURE_CLIENT_ID
export ARM_SUBSCRIPTION_ID=xxx-xxx-xxx-xxx-xxx #this isn't populated by the AWI webhook
export ARM_TENANT_ID=$AZURE_TENANT_ID
export ARM_OIDC_TOKEN=$(cat $AZURE_FEDERATED_TOKEN_FILE)

In conjunction with:

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "3.20.0"
    }
  }
}

and

provider "azurerm" {
  use_oidc = true
  features {}
}

Generic OIDC: AzureRM Backend

A generic OIDC implementation does not exist for the AzureRM Backend yet. I would like to be able to configure the backend as follows, and have it use the same authentication approach as what is in the AzureRM Provider, i.e:

export ARM_CLIENT_ID=$AZURE_CLIENT_ID
export ARM_SUBSCRIPTION_ID=xxx-xxx-xxx-xxx-xxx #this isn't populated by the AWI webhook
export ARM_TENANT_ID=$AZURE_TENANT_ID
export ARM_OIDC_TOKEN=$(cat $AZURE_FEDERATED_TOKEN_FILE)
terraform {
  backend "azurerm" {
    use_oidc=true             
    resource_group_name=myrg
    storage_account_name=mysa
    container_name=myctr
  }
}

or, if setting everything explicitly:

terraform {
  backend "azurerm" {
    use_oidc=true             
    resource_group_name=myrg
    storage_account_name=mysa
    container_name=myctr
    client_id=xxxxxxxxxxxxxxxxx
    subscription_id=xxxxxxxxxxxxxxxxx
    tenant_id=xxxxxxxxxxxxxxxxx
    oidc_token=xxxxxxxxxxxxxxxxx
  }
}

Attempted Solutions

For this specific situation there is no real workaround other than to use a different authentication method altogether.

Proposal

I would propose looking into the changes introduced in hashicorp/terraform-provider-azurerm#18118 and following the same approach to add an oidc_token / ARM_OIDC_TOKEN parameter to the AzureRM Backend implementation.

References

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions