Description
Grant a Google Cloud service account roles that match its tasks and the resources it uses.
Potential impact
Project-wide Editor or Owner access lets an automation error or a compromised account change unrelated resources.
Remediation
Choose a predefined or custom role for the required operations. For a job that only reads and lists objects, use roles/storage.objectViewer on the relevant bucket. Check inherited grants and who can impersonate the account.
Examples
google_iam_policy generates a policy document. Apply the new binding to the bucket and check the required reads. Remove the superseded broad grant where it was assigned, then verify that reads still succeed and unneeded writes are denied.
Before
hcl
data "google_iam_policy" "policy" {
binding {
role = "roles/editor"
members = [
"serviceAccount:${var.service_account_email}",
]
}
}
After
hcl
data "google_iam_policy" "policy" {
binding {
role = "roles/storage.objectViewer"
members = [
"serviceAccount:${var.service_account_email}",
]
}
}