Skip to content
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

google_service_account read after creation issue - apply fails with 403 right after creation #19624

Closed
sbocinec opened this issue Sep 25, 2024 · 2 comments · Fixed by GoogleCloudPlatform/magic-modules#11811, #19727 or hashicorp/terraform-provider-google-beta#8336
Assignees

Comments

@sbocinec
Copy link

sbocinec commented Sep 25, 2024

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request.
  • Please do not leave +1 or me too comments, they generate extra noise for issue followers and do not help prioritize the request.
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment.
  • If an issue is assigned to a user, that user is claiming responsibility for the issue.
  • Customers working with a Google Technical Account Manager or Customer Engineer can ask them to reach out internally to expedite investigation and resolution of this issue.

Terraform Version & Provider Version(s)

Terraform v1.8.5
on amd64

  • provider registry.terraform.io/hashicorp/google v5.39.1

Affected Resource(s)

google_service_account

Terraform Configuration

locals {
  agent_id = "cba1234567890xyz"
}

resource "google_service_account" "agent" {
  count = 1
  account_id                   = "agent-${local.agent_id}"
  display_name                 = "Agent ${local.agent_id} Service Account"
  description                  = "Agent ${local.agent_id} Service Account"
  create_ignore_already_exists = true
}

Debug Output

2024-09-20T10:25:44.332Z	google_service_account.agent[0]: Creating...
2024-09-20T10:25:45.052Z	
2024-09-20T10:25:45.052Z	Error: googleapi: Error 403: Permission 'iam.serviceAccounts.get' denied on resource (or it may not exist).
2024-09-20T10:25:45.052Z	Details:
2024-09-20T10:25:45.052Z	[
2024-09-20T10:25:45.052Z	  {
2024-09-20T10:25:45.052Z	    "@type": "type.googleapis.com/google.rpc.ErrorInfo",
2024-09-20T10:25:45.052Z	    "domain": "iam.googleapis.com",
2024-09-20T10:25:45.052Z	    "metadata": {
2024-09-20T10:25:45.052Z	      "permission": "iam.serviceAccounts.get"
2024-09-20T10:25:45.052Z	    },
2024-09-20T10:25:45.052Z	    "reason": "IAM_PERMISSION_DENIED"
2024-09-20T10:25:45.052Z	  }
2024-09-20T10:25:45.052Z	]
2024-09-20T10:25:45.052Z	, forbidden
2024-09-20T10:25:45.052Z	
2024-09-20T10:25:45.052Z	  with google_service_account.agent[0],
2024-09-20T10:25:45.052Z	  on main.tf line 119, in resource "google_service_account" "agent":
2024-09-20T10:25:45.052Z	 119: resource "google_service_account" "agent" {
2024-09-20T10:25:45.052Z	
2024-09-20T10:25:45.129Z	

Expected Behavior

Due to the eventual consistency of the GCP IAM API, the provider is supposed to pollread the SA after creation unless the SA is found for the defined timeout.

As the GCP IAM API returns 403 IAM_PERMISSION_DENIED on a SA read even when the SA is not found, the pollread must handle the permission denied error as it does in the first read after it's created a few lines before.

The pollRead actually throws the error on the first 403 occurrence, as the poller only handles 404 errors thus the polling is not effective and instead it errors out quickly.

I prepared a fix PR for this GoogleCloudPlatform/magic-modules#11811 by using PollCheckForExistenceWith403 poller that continues polling even when 403 is thrown.

Actual Behavior

As you can see, the SA creation fails in 1s, shortly after the creation. It should handle 403 errors in the poller doing post-creation reads.

Steps to reproduce

  1. terraform apply

Important Factoids

References

@sbocinec sbocinec added the bug label Sep 25, 2024
@github-actions github-actions bot added forward/review In review; remove label to forward service/iam-serviceaccount labels Sep 25, 2024
@sbocinec sbocinec changed the title google_service_account read after creation issue - eventual consistency of GCP IAM API google_service_account read after creation issue - apply fails with 403 right after creatio Sep 25, 2024
@sbocinec sbocinec changed the title google_service_account read after creation issue - apply fails with 403 right after creatio google_service_account read after creation issue - apply fails with 403 right after creation Sep 25, 2024
@ggtisc ggtisc self-assigned this Oct 1, 2024
@ggtisc
Copy link
Collaborator

ggtisc commented Oct 2, 2024

Hi @sbocinec!

After executing your code with the specified terraform and Google provider versions everything was successful without errors. A 403 is an unauthorized issue and it is usually related to account permissions. I suggest you check your account permissions and try it again.

Copy link

github-actions bot commented Nov 4, 2024

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 4, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.