-
-
Notifications
You must be signed in to change notification settings - Fork 2
Security hardening: least-privilege IAM, plan/apply split, bucket protections #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5a3f15b
Security hardening: least-privilege IAM, plan/apply split, bucket pro…
Jeffreyhung 4a8c8a0
fix perms
Jeffreyhung 232ffa2
fix more perm
Jeffreyhung 9d96d64
Fix plan-SA IAM refresh and remove setIamPolicy escalation
Jeffreyhung 21ea924
Complete plan-SA read coverage for terraform refresh
Jeffreyhung 914e161
Pin apply-SA WIF binding to the production environment subject
Jeffreyhung File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| version: 2 | ||
| updates: | ||
| # Keep pinned GitHub Actions (SHA-pinned in .github/workflows) up to date. | ||
| - package-ecosystem: "github-actions" | ||
| directory: "/" | ||
| schedule: | ||
| interval: "weekly" | ||
| labels: | ||
| - "dependencies" | ||
| - "github-actions" | ||
|
|
||
| # Keep the Terraform provider constraints (required_providers in main.tf) and | ||
| # any pinned module sources up to date. | ||
| - package-ecosystem: "terraform" | ||
| directory: "/" | ||
| schedule: | ||
| interval: "weekly" | ||
| labels: | ||
| - "dependencies" | ||
| - "terraform" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,9 @@ | ||
| output "deploy_sa_email" { | ||
| value = var.deploy_sa_email != null ? var.deploy_sa_email : google_service_account.gha_cloud_functions_deployment[0].email | ||
| description = "Privileged service account used by `terraform apply`." | ||
| value = local.apply_sa_email | ||
| } | ||
|
|
||
| output "plan_sa_email" { | ||
| description = "Read-only service account used by `terraform plan` (null in BYO mode)." | ||
| value = var.deploy_sa_email != null ? null : google_service_account.gha_tf_plan[0].email | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,111 @@ | ||
| # Project-wide roles | ||
| # Project-wide roles for the privileged "apply" service account. | ||
| locals { | ||
| roles = [ | ||
| "roles/viewer", # general read-only access to most Google Cloud resources | ||
| "roles/iam.securityReviewer", # READ-ONLY *.getIamPolicy across services, so plan/apply can refresh IAM resources | ||
| "roles/storage.admin", # full access to manage GCS buckets and objects | ||
| "roles/secretmanager.secretAccessor", # access to Secret Manager | ||
| "roles/cloudfunctions.developer", # deploy and manage Cloud Functions | ||
| "roles/logging.viewer", # view logs | ||
| "roles/iam.serviceAccountUser", # necessary to invoke Cloud Functions | ||
| "roles/iam.workloadIdentityPoolViewer", # view workload identity pool | ||
| "roles/iam.serviceAccountCreator", # create service accounts | ||
| "roles/iam.serviceAccountCreator", # create the per-resource runtime service accounts | ||
| "roles/iam.serviceAccountUser", # actAs runtime SAs in order to deploy functions/workflows/cron as them | ||
| "roles/pubsub.admin", # full access to Pub/Sub | ||
| # NOTE on roles/iam.serviceAccountUser: this is a project-wide actAs (impersonation) | ||
| # primitive. It is required because the template autonomously creates dedicated | ||
| # runtime SAs and must actAs them to deploy. It CANNOT be scoped to just those SAs: | ||
| # creating a per-SA actAs binding on a freshly-created SA itself requires | ||
| # iam.serviceAccounts.setIamPolicy on that SA (circular), and IAM Conditions are not | ||
| # supported for service-account resources. actAs is deliberately chosen over a custom | ||
| # role with iam.serviceAccounts.setIamPolicy because setIamPolicy is strictly broader | ||
| # (it can grant ANY principal actAs on ANY SA and rewrite policies). Residual risk is | ||
| # bounded by: apply only runs on refs/heads/main behind the `production` approval gate, | ||
| # is never exposed to untrusted PR code (that path uses the read-only plan SA), and the | ||
| # template assumes a dedicated project. For maximum lockdown, drop this role and create | ||
| # runtime-SA actAs bindings via a privileged bootstrap apply instead (loses CI self-service). | ||
| # NOTE: roles/secretmanager.secretAccessor is intentionally NOT granted. Deploying/binding | ||
| # secrets does not require reading their values; the custom role below grants | ||
| # create + setIamPolicy on secrets without value access. | ||
| ] | ||
| } | ||
|
|
||
| resource "google_project_iam_member" "project_roles" { | ||
| for_each = toset(local.roles) | ||
| project = var.project | ||
| role = each.value | ||
| member = "serviceAccount:${var.deploy_sa_email != null ? var.deploy_sa_email : google_service_account.gha_cloud_functions_deployment[0].email}" | ||
| member = "serviceAccount:${local.apply_sa_email}" | ||
| } | ||
|
|
||
| # Custom role letting the apply SA create and manage Secret Manager secrets and | ||
| # their IAM bindings WITHOUT the ability to read secret payloads. This replaces | ||
| # the previous project-wide roles/secretmanager.secretAccessor grant so that a | ||
| # compromised CI run cannot exfiltrate secret values. | ||
| resource "google_project_iam_custom_role" "tf_secret_manager" { | ||
| role_id = "cfTemplateSecretManager" | ||
| title = "CF Template Secret Manager (no value access)" | ||
| description = "Create and manage Secret Manager secrets and their IAM bindings without reading secret values" | ||
| permissions = [ | ||
| "secretmanager.secrets.create", | ||
| "secretmanager.secrets.delete", | ||
| "secretmanager.secrets.get", | ||
| "secretmanager.secrets.list", | ||
| "secretmanager.secrets.update", | ||
| "secretmanager.secrets.getIamPolicy", | ||
|
Check notice on line 52 in infrastructure/permissions.tf
|
||
| "secretmanager.secrets.setIamPolicy", | ||
| ] | ||
| } | ||
|
|
||
| resource "google_project_iam_member" "apply_secret_manager" { | ||
| project = var.project | ||
| role = google_project_iam_custom_role.tf_secret_manager.name | ||
| member = "serviceAccount:${local.apply_sa_email}" | ||
| } | ||
|
|
||
|
sentry-warden[bot] marked this conversation as resolved.
|
||
| # Read-only project access for the plan SA. roles/viewer covers resource reads | ||
| # (gets/lists) and roles/iam.securityReviewer covers *.getIamPolicy so that | ||
| # `terraform plan` can refresh IAM resources. Neither grants | ||
| # secretmanager.versions.access, so the plan identity cannot read secret values. | ||
| # (State-bucket read + lock-object write is granted on the bucket in main.tf.) | ||
| resource "google_project_iam_member" "plan_viewer" { | ||
| count = var.deploy_sa_email != null ? 0 : 1 | ||
| project = var.project | ||
| role = "roles/viewer" | ||
| member = "serviceAccount:${google_service_account.gha_tf_plan[0].email}" | ||
| } | ||
|
|
||
| resource "google_project_iam_member" "plan_security_reviewer" { | ||
| count = var.deploy_sa_email != null ? 0 : 1 | ||
| project = var.project | ||
| role = "roles/iam.securityReviewer" | ||
| member = "serviceAccount:${google_service_account.gha_tf_plan[0].email}" | ||
| } | ||
|
|
||
| # Plan SA needs read on the workload identity pool/provider resources to refresh | ||
| # them (securityReviewer only grants list + getIamPolicy, not get). | ||
| resource "google_project_iam_member" "plan_wif_viewer" { | ||
| count = var.deploy_sa_email != null ? 0 : 1 | ||
| project = var.project | ||
| role = "roles/iam.workloadIdentityPoolViewer" | ||
| member = "serviceAccount:${google_service_account.gha_tf_plan[0].email}" | ||
| } | ||
|
|
||
| # roles/viewer does not include storage.buckets.get, which plan needs to refresh | ||
| # every bucket resource. Grant ONLY bucket-metadata get project-wide (no object | ||
| # listing or content). Object-content read is granted separately and scoped to | ||
| # the staging bucket (see main.tf), so the plan identity cannot read the contents | ||
| # of other buckets (e.g. pub/sub sink data) even though it is exposed to PR code. | ||
| resource "google_project_iam_custom_role" "tf_plan_bucket_reader" { | ||
| count = var.deploy_sa_email != null ? 0 : 1 | ||
| role_id = "cfTemplatePlanBucketReader" | ||
| title = "CF Template Plan Bucket Metadata Reader" | ||
| description = "Read bucket metadata (storage.buckets.get) for terraform plan refresh, no object access" | ||
| permissions = [ | ||
| "storage.buckets.get", | ||
| ] | ||
| } | ||
|
|
||
| resource "google_project_iam_member" "plan_bucket_reader" { | ||
| count = var.deploy_sa_email != null ? 0 : 1 | ||
| project = var.project | ||
| role = google_project_iam_custom_role.tf_plan_bucket_reader[0].name | ||
| member = "serviceAccount:${google_service_account.gha_tf_plan[0].email}" | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.