# workload-identity / oci / t1 "Standard" # # Short-lived Oracle Cloud access for GitHub Actions, limited to named # branches or deployment environments of one repository, with your own # grants, and optionally the same for one OKE service account. # # How it works. The workflow presents its GitHub token to the identity # domain's token exchange endpoint and gets back a session token. The session # is bound to a key pair the workflow made for the run, lasts at most an hour, # and ends no later than the GitHub token that bought it. The endpoint # requires a confidential client to authenticate, so one secret still lives # in GitHub: that client's. The client has no role and no policy names it, # so the secret alone cannot call any Oracle Cloud API. The trust decides # whose GitHub tokens count. # # OKE needs none of that. A pod's service account token is accepted directly, # and policy names the cluster, namespace, and service account it must carry. data "oci_identity_domain" "this" { domain_id = var.identity_domain_ocid } locals { tags = merge({ owner = var.owner environment = var.environment data_classification = "internal" managed_by = "terraform" }, var.extra_tags) domain_url = data.oci_identity_domain.this.url audience = "https://cloud.oracle.com" # The group by name, so the statements are known at plan time and the # plan rules can read them. An OCID would be unknown until apply. group = "group '${data.oci_identity_domain.this.display_name}'/'${oci_identity_domains_group.deploy.display_name}'" pipeline_statements = [for g in var.grants : "Allow ${local.group} to ${g.verb} ${g.resource} in compartment id ${var.compartment_ocid}${g.where != "" ? " where ${g.where}" : ""}" ] oke = var.oke_cluster_ocid != "" oke_grants = length(var.oke_grants) > 0 ? var.oke_grants : var.grants # Every condition at once: the principal is a workload, from this cluster, # this namespace, this service account. Drop one and every pod in the # cluster, or every cluster in the tenancy, qualifies. workload = "request.principal.type = 'workload', request.principal.cluster_id = '${var.oke_cluster_ocid}', request.principal.namespace = '${var.oke_namespace}', request.principal.service_account = '${var.oke_service_account}'" oke_statements = [for g in local.oke_grants : "Allow any-user to ${g.verb} ${g.resource} in compartment id ${var.compartment_ocid} where all {${local.workload}${g.where != "" ? ", ${g.where}" : ""}}" ] } # The confidential client that may call the exchange endpoint. No role, no # policy: it can exchange tokens and nothing else. resource "oci_identity_domains_app" "exchange" { idcs_endpoint = local.domain_url display_name = "github-token-exchange-${replace(var.repository, "/", "-")}" description = "Lets ${var.repository} exchange GitHub tokens for sessions. Has no role." schemas = ["urn:ietf:params:scim:schemas:oracle:idcs:App"] active = true based_on_template { value = "CustomWebAppTemplateId" } is_oauth_client = true client_type = "confidential" allowed_grants = ["client_credentials"] } # The identity the workflow becomes. A service user cannot sign in, have a # password, or have an API key. resource "oci_identity_domains_user" "deploy" { idcs_endpoint = local.domain_url user_name = "github-deploy-${replace(var.repository, "/", "-")}" description = "Acted as by ${var.repository} through token exchange" schemas = [ "urn:ietf:params:scim:schemas:core:2.0:User", "urn:ietf:params:scim:schemas:oracle:idcs:extension:user:User", ] urnietfparamsscimschemasoracleidcsextensionuser_user { service_user = true } } resource "oci_identity_domains_group" "deploy" { idcs_endpoint = local.domain_url display_name = "github-deploy-${replace(var.repository, "/", "-")}" schemas = ["urn:ietf:params:scim:schemas:core:2.0:Group"] members { type = "User" value = oci_identity_domains_user.deploy.id } } # One rule per subject, each an exact match, each mapping to the one service # user. The first matching rule wins and no rule matches anything else. resource "oci_identity_domains_identity_propagation_trust" "github" { idcs_endpoint = local.domain_url name = "github-actions-${replace(var.repository, "/", "-")}" description = "GitHub Actions, ${var.repository}" schemas = ["urn:ietf:params:scim:schemas:oracle:idcs:IdentityPropagationTrust"] type = "JWT" active = true issuer = "https://token.actions.githubusercontent.com" public_key_endpoint = "https://token.actions.githubusercontent.com/.well-known/jwks" oauth_clients = [oci_identity_domains_app.exchange.name] client_claim_name = "aud" client_claim_values = [local.audience] subject_claim_name = "sub" subject_type = "User" allow_impersonation = true dynamic "impersonation_service_users" { for_each = toset(var.allowed_subjects) content { rule = "sub eq '${impersonation_service_users.value}'" value = oci_identity_domains_user.deploy.id } } } resource "oci_identity_policy" "deploy" { count = length(local.pipeline_statements) > 0 ? 1 : 0 compartment_id = var.compartment_ocid name = "github-deploy-${replace(var.repository, "/", "-")}" description = "What ${var.repository} may do here" statements = local.pipeline_statements freeform_tags = local.tags } # OKE workload identity. Anyone able to run a pod with that service account # in that namespace can act under these grants too. resource "oci_identity_policy" "oke" { count = local.oke && length(local.oke_statements) > 0 ? 1 : 0 compartment_id = var.compartment_ocid name = "oke-workload-${var.oke_namespace}-${var.oke_service_account}" description = "What the ${var.oke_namespace}/${var.oke_service_account} service account may do here" statements = local.oke_statements freeform_tags = local.tags }