Open oonisim opened 2 years ago
Just to add as a workaround using terraform I have been using the following after creating a project.
data "google_iam_policy" "editor" {
binding {
members = [
"serviceAccount:${google_project.project.number}@cloudservices.gserviceaccount.com",
# "serviceAccount:${google_project.project.number}-compute@developer.gserviceaccount.com",
]
role = "roles/editor"
}
}
resource "google_project_iam_policy" "add" {
policy_data = data.google_iam_policy.editor.policy_data
project = google_project.project.project_id
}
This will fix the issue preventing the GKE cluster from being removed.
This will still remove any permissions not tracked by Terraform. Including the users which created the project which would generally have the owner role.
However, the issue then is if you add any additional policies later that arent all tracked one big policy will override the one which was previously created. The same is true for I am binding you can get in a loop where one will override the other and each terraform apply
will always delete already applied values.
The only way you can get it so that it won't override is if all changes to policies are applied using one google_project_iam_policy or google_project_iam_binding per project.
Ran into this myself, seems like we have to use "google_project_iam_member" instead. This will add a role to a member, without removing the other members from the role you are assigning.
google_project_iam_binding Authoritative for a given role. Updates the IAM policy to grant a role to a list of members. Other roles within the IAM policy for the project are preserved. -> Applies the role to the list of members, retaining the other roles for those members, removing the roles from all the members not in that list. (And for some reason also removing the iam user (member), maybe if they only had one role, that is now removed?)
google_project_iam_member Non-authoritative. Updates the IAM policy to grant a role to a new member. Other members for the role for the project are preserved. -> applies the role to a member, retaining the roles of that member, retaining all members having that role.
Tested with:
Terraform v1.2.9
terraform/provider hashicorp/google:4.38.0
problem persists.
This is a feature of the provider and the project_iam_binding module. In authoritative mode, it removes any additions to the specified role. You just have to add the "Google-managed service accounts" to your terraform specification for those roles.
bindings = {
# We need it because of this.
# https://cloud.google.com/iam/docs/service-accounts#google-managed
"roles/editor" = [
"serviceAccount:${module.project-factory.project_number}-compute@developer.gserviceaccount.com",
"serviceAccount:${module.project-factory.project_number}@cloudservices.gserviceaccount.com"
]
# ...
}
Removing iam-serviceaccount
team because I don't think this is an issue with the google_service_account
resource.
Updated the description to prevent our bot from continuing to add the label
Ran into this myself, seems like we have to use "google_project_iam_member" instead. This will add a role to a member, without removing the other members from the role you are assigning.
- google_project_iam_binding Authoritative for a given role. Updates the IAM policy to grant a role to a list of members. Other roles within the IAM policy for the project are preserved. -> Applies the role to the list of members, retaining the other roles for those members, removing the roles from all the members not in that list. (And for some reason also removing the iam user (member), maybe if they only had one role, that is now removed?)
- google_project_iam_member Non-authoritative. Updates the IAM policy to grant a role to a new member. Other members for the role for the project are preserved. -> applies the role to a member, retaining the roles of that member, retaining all members having that role.
If anyone using pulumi
my service accounts roles where deleted I believe due to using new gcp.projects.IAMBinding
I changed it to:
new gcp.projects.IAMMember(`foo`, {
project: gcpServiceAccountProject || gcpProject,
role,
member: pulumi.interpolate`serviceAccount:${gcpServiceAccount.email}`,
})
Also had to delete the original service accounts and then recreate them.
Update
See Usability improvements for _iam_policy and _iam_binding resources #8354
As in Terraform google_project_iam_binding deletes GCP compute engine default service account from IAM principals,
Not sure who can get the clear idea what terraform does with
google_project_iam_binding
but as GCP has identified, Terraform google_project_iam_binding has deleted all the accounts not in the members attribute that have "roles/Editor" role.Still, I believe this is a terraform defect.
As per the Google APIs Service Agent document, it is the essential service accounts that GCP internally manages. Terraform should not delete any such GCP managed internal service accounts as it bring the GCP projects down. I doubt in what use cases do we need this to happen.
Please, instead of of the assertin "work as designed", do not delete the GCP managed internal service accounts, as they are essential to make the GCP project work.
Original issue raised
Terraform google_project_iam_binding deletes GCP compute engine default service account from IAM principals has the detailed step-by-step reproduction steps and snapshots.
Community Note
modular-magician
user, it is either in the process of being autogenerated, or is planned to be autogenerated soon. If an issue is assigned to a user, that user is claiming responsibility for the issue. If an issue is assigned tohashibot
, a community member has claimed the issue already.Terraform Version
Affected Resource(s)
GCP IAM Compute Engine default service account. It gets deleted by Terraform and cannot manage Compute Engine, hence GKE nodes as well.
Terraform Configuration Files
After further investigation, "roles/Editor" is sufficient to reproduce the issue.
Debug Output
Panic Output
Expected Behavior
Terraform will not remove the GCP Compute Engine Default service account from the IAM principals.
Actual Behavior
Before running the script, the Compute Engine default account exists in the IAM principals (with Compute Engine API enabled).
After running the terraform script. The GCP Compute Engine default service account get deleted by the script.
gcloud projects get-iam-policy
command does not show the Compute Engine default service account 1079157603081-compute@developer.gserviceaccount.com, either.Because of this, GKE cluster cannot be deleted, created because Compute Engine permissions have gone.
Steps to Reproduce
Please see * Terraform google_project_iam_binding deletes GCP compute engine default service account from IAM principals
terraform apply
Now the GCP Compute Engine default service account was compromised and cannot manage Compute Engines and GKE nodes.
Important Factoids
No
References
Terraform google_project_iam_binding deletes GCP compute engine default service account from IAM principals
GCP GKE - Google Compute Engine: Not all instances running in IGM
Impact
GKE cannot be created anymore after the GCP Compute Engine Default Service Account disappeared in the IAM console. Need to create another project to be able to create GKE.
Cause
GCP identified that Terraform has deleted the Google APIs Service Agent which is Google-managed service accounts.
Terraform should not delete any such GCP managed internal service account essential to run GCP services, hence I regard this is a Terraform bug.
Fix
According to GCP:
As per the error message, add '1079157603081@cloudservices.gserviceaccount.com' in IAM.
The Google APIs Service Agent is restored in the view.
Create GKE.
b/304725229