# The Google Cloud resources Kosli Cloud Capture needs in a customer's # project, for use with Infrastructure Manager; the README beside this # directory says how to deploy it and why it is shaped as it is. # # Cloud Capture runs in AWS and reaches the project through Workload Identity # Federation: it exchanges its own short-lived AWS credentials for a # short-lived Google token, then impersonates the service account created # here. No service account key is ever created, stored or shared. The pool # makes two checks before it issues a token: the credentials were issued by # the Kosli AWS account that serves the customer, and within that account by # the one IAM role Kosli keeps for the customer's organisation. A Cloud # Capture job for another tenant runs under a different role and is refused. terraform { required_version = ">= 1.5" required_providers { google = { source = "hashicorp/google" version = ">= 5.0, < 8.0" } } } provider "google" { project = var.project_id } variable "project_id" { type = string description = "The GCP project that holds the Cloud Run services and jobs, and the GKE clusters, Cloud Capture will snapshot." validation { condition = can(regex("^[a-z][a-z0-9-]{4,28}[a-z0-9]$", var.project_id)) error_message = "Must be a project id: 6 to 30 lowercase letters, digits and hyphens, starting with a letter." } } variable "kosli_aws_account_id" { type = string description = <<-EOT The AWS account in which Cloud Capture runs, supplied by Kosli. It differs per tenant because more than one Kosli account serves tenants. There is no default and no value you can derive yourself. EOT validation { condition = can(regex("^[0-9]{12}$", var.kosli_aws_account_id)) error_message = "Must be the 12-digit AWS account id issued to you by Kosli." } } variable "kosli_role_name" { type = string description = <<-EOT The name of the IAM role, in the Kosli AWS account, that Cloud Capture uses when working for your organisation. Supplied by Kosli. Each Kosli organisation has its own role, and the workload identity pool accepts only this one, so only Cloud Capture jobs run on behalf of your organisation can obtain a token from your project. In the AWS setup, the external ID plays the same part. EOT validation { condition = can(regex("^[A-Za-z0-9+=,.@_-]{1,64}$", var.kosli_role_name)) error_message = "Must be the IAM role name issued to you by Kosli." } } # The APIs the exchange and the inventory go through. Cloud Run is enabled # already in a project with services in it; the IAM ones are what creating a # pool, a service account and the custom roles, and impersonating the account, # need; Cloud Resource Manager is what granting the project roles below # needs, and a fresh project does not have it on; Cloud Asset is the # inventory a GKE snapshot reads the project's pods from, and costs nothing # to have on. None is disabled on destroy: they may well be in use by # something else in the project. # # The Kubernetes Engine API is not in the list. A project with clusters has # it on already, and enabling it in a project without them would switch on # Compute Engine and its other dependencies for nothing. resource "google_project_service" "needed" { for_each = toset([ "iam.googleapis.com", "iamcredentials.googleapis.com", "sts.googleapis.com", "run.googleapis.com", "cloudresourcemanager.googleapis.com", "cloudasset.googleapis.com", ]) project = var.project_id service = each.key disable_on_destroy = false } resource "google_iam_workload_identity_pool" "kosli_capture" { project = var.project_id workload_identity_pool_id = "kosli-capture" display_name = "Cloud Capture" description = <<-EOT Read-only access for Cloud Capture SDLC compliance evidence collection. Managed by Infrastructure Manager; do not edit in place. EOT depends_on = [google_project_service.needed] } resource "google_iam_workload_identity_pool_provider" "kosli_aws" { project = var.project_id workload_identity_pool_id = google_iam_workload_identity_pool.kosli_capture.workload_identity_pool_id workload_identity_pool_provider_id = "kosli-aws" display_name = "Cloud Capture (AWS)" aws { account_id = var.kosli_aws_account_id } # The role is read out of the assumed-role ARN AWS signed, less its # session name, so every session of the role is the one principal. The # subject is the role as well, and not the whole ARN: a subject is at most # 127 bytes, and the ARN carries the account, the session name and the # role's name, which can be 64 characters, so a long role name would put # the ARN over the limit and every exchange would fail. attribute_mapping = { "google.subject" = "assertion.arn.extract('assumed-role/{role}/')" "attribute.account" = "assertion.account" "attribute.aws_role" = "assertion.arn.extract('assumed-role/{role}/')" } # Two checks before the pool issues a token. The account check accepts only # credentials issued by the Kosli AWS account, however the token reaches # Google. The role check accepts, within that account, only the Kosli-side # role dedicated to your organisation. A Cloud Capture job for another tenant # runs under a different role and is refused here. In the AWS setup, these # checks correspond to the trust policy principal and the external ID. attribute_condition = join(" && ", [ "attribute.account == \"${var.kosli_aws_account_id}\"", "attribute.aws_role == \"${var.kosli_role_name}\"", ]) } resource "google_service_account" "kosli_capture" { project = var.project_id account_id = "kosli-capture" display_name = "Cloud Capture" description = "Impersonated by Cloud Capture to snapshot Cloud Run services and jobs, and GKE clusters. Managed by Infrastructure Manager." # Ordered after the APIs, as the pool is: creating a service account needs # iam.googleapis.com, which a project may not have enabled before this, and # the four bindings below follow through their references to the account. depends_on = [google_project_service.needed] } # Only the Kosli-side role for your organisation may impersonate the service # account. The pool's attribute condition already refuses every other identity; # naming the role here as well keeps the binding correct even if that condition # is later loosened. resource "google_service_account_iam_member" "kosli_impersonation" { service_account_id = google_service_account.kosli_capture.name role = "roles/iam.workloadIdentityUser" member = "principalSet://iam.googleapis.com/${google_iam_workload_identity_pool.kosli_capture.name}/attribute.aws_role/${var.kosli_role_name}" } # What the service account may read. Discovery lists the region's Cloud Run # services and jobs and reads each one's labels from the same listing, and # the snapshot reads each service's revisions and the images they run: all # of it is in roles/run.viewer, the predefined read-only role, and none of it # returns application data. A revision's spec is the widest: it holds the # container image, the command, and any environment variables written into # the spec in plain text; a value taken from a Secret is named there rather # than resolved, so what comes back is the reference and not the secret. resource "google_project_iam_member" "kosli_capture_run" { project = var.project_id role = "roles/run.viewer" member = google_service_account.kosli_capture.member } # The snapshot also resolves each image pinned by a tag to its digest, which # the Kosli CLI (2.57.0 on) reads from the Artifact Registry API: metadata # about the tag, and never the image. The predefined read-only role would # also grant downloadArtifacts, which pulls image layers, so a custom role # grants the one read the lookup makes and nothing that returns a layer. # The CLI's other lookup, a tag for an image pinned by digest, is behind a # flag Cloud Capture does not pass, so its permission # (artifactregistry.dockerimages.get) is not granted either. The CLI falls # back to reading the manifest when the API call fails; under this grant # that fallback cannot succeed, and nothing depends on it, since the # registries it could read are the ones the API lookup reads. Cloud Run # reports a service's image already pinned by digest, which the CLI takes # from the name, so what this grant resolves in practice is a job's image. # Without this grant a snapshot still succeeds, with an empty digest for # each tag-pinned image the account could not read. resource "google_project_iam_custom_role" "kosli_capture_registry" { project = var.project_id role_id = "kosliCaptureRegistry" title = "Cloud Capture registry metadata" description = "Read-only image tag metadata for Cloud Capture SDLC compliance evidence collection." permissions = ["artifactregistry.tags.get"] depends_on = [google_project_service.needed] } resource "google_project_iam_member" "kosli_capture_registry" { project = var.project_id role = google_project_iam_custom_role.kosli_capture_registry.id member = google_service_account.kosli_capture.member } # What the service account may read of the project's GKE clusters (beta). # Cloud Capture never connects to a cluster: it needs no network path to one, # no kubeconfig and no Kubernetes RBAC. Discovery lists the project's # clusters, with their labels, through the Kubernetes Engine API; the # snapshot reads the project's pods from Cloud Asset Inventory, Google's own # record of what is in the project, and Google requires # serviceusage.services.use of every call to it. # # A custom role because the predefined ones are wider than this: # roles/cloudasset.viewer lists every asset type in the project, and # roles/container.clusterViewer more than the one listing. It cannot be # narrower than every pod in the project, since Cloud Asset Inventory filters # by neither cluster nor namespace. A pod's record holds its spec and status: # the images, commands and arguments, its labels and annotations, and any # environment variables written into the spec in plain text; a value taken # from a Secret is named there rather than resolved, so what comes back is # the reference and not the secret. A cluster's listing holds its # configuration and no credentials. resource "google_project_iam_custom_role" "kosli_capture_gke" { project = var.project_id role_id = "kosliCaptureGke" title = "Cloud Capture (GKE)" description = "Lists GKE clusters, and their pods from Cloud Asset Inventory, for Cloud Capture. Managed by Infrastructure Manager." permissions = [ "container.clusters.list", "cloudasset.assets.listContainerPod", "serviceusage.services.use", ] depends_on = [google_project_service.needed] } resource "google_project_iam_member" "kosli_capture_gke" { project = var.project_id role = google_project_iam_custom_role.kosli_capture_gke.id member = google_service_account.kosli_capture.member } # Kosli needs these two values to connect. Infrastructure Manager records them # on the deployment's latest revision, under applyResults.outputs. output "kosli_capture_provider" { description = "Full resource name of the workload identity provider; give this to Kosli." value = google_iam_workload_identity_pool_provider.kosli_aws.name } output "kosli_capture_service_account" { description = "Email of the service account Cloud Capture impersonates; give this to Kosli." value = google_service_account.kosli_capture.email }