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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

"name" field on google_apikeys_key leads to errors when re-creating issues. #11726

Open
toadjaune opened this issue May 19, 2022 · 5 comments
Open
Labels
forward/review In review; remove label to forward persistent-bug Hard to diagnose or long lived bugs for which resolutions are more like feature work than bug work service/apikeys size/s
Milestone

Comments

@toadjaune
Copy link

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 the issue is assigned to the "modular-magician" user, it is either in the process of being autogenerated, or is planned to be autogenerated soon. If the issue is assigned to a user, that user is claiming responsibility for the issue. If the issue is assigned to "hashibot", a community member has claimed the issue already.

Description

The name field of the google_apikeys_key resource is exposed, and even mandatory.

It seems to be used as a per project unique identifier, however, on resource deletion, previously used values for this field are not immediately made available again (it's been about a day in my case, I have no idea at this point if previous names are eventually released)

This causes an error when creating an api key, deleting it, and attempting to re-create it with the same configuration :

Error: Error creating Key: Resource already exists - apply blocked by lifecycle params: &apikeys.Key{Name:(*string)(0xXXXX), DisplayName:(*string)(0xXXXX), KeyString:(*string)(0xXXXX), Restrictions:(*apikeys.KeyRestrictions)(0xXXXX), Project:(*string)(0xXXXX)}.

New or Affected Resource(s)

  • google_apikeys_key

Potential Terraform Configuration

With the following minimal configuration :

resource "google_apikeys_key" "example" {
  name         = "example"
  project      = "your-project-id"
}

run :

$ terraform apply
# works
$ terraform destroy -target=google_apikeys_key.example
# works
$ terraform apply
# gives the error message above

Resolution suggestions

First of all, as a short term mitigation, this behavior should be explicited in documentation. #11725 addresses this.

Now, considering this field is not user-settable when creating a new API key from the GCP web interface, nor from the gcloud cli utility, I would suggest the following fixes :

  • Make the name field optional. I have no idea if it is mandatory on the GCP API side, but if it is, generate a default value with the same format as other tools (looks like a UUIDv4 for keys created from the web interface)
  • Remove it from all documentation examples
  • Ensure the documentation keeps being explicit regarding the risks of setting such a field

I assume this field is used in various places (say, logging...) and retaining the ability to set it presents value. Also, not removing it avoids a breaking change.

References

@rileykarson
Copy link
Collaborator

rileykarson commented May 23, 2022

Huh- according to https://cloud.google.com/api-keys/docs/reference/rest/v2/projects.locations.keys/create, The id must NOT be a UUID-like string. (our name field maps to the keyId field there). However, the Console is definitely generating a UUIDv4.

Labelling as persistent-bug, since this a bug that looks closer to an enhancement.

@rileykarson rileykarson added persistent-bug Hard to diagnose or long lived bugs for which resolutions are more like feature work than bug work and removed bug labels May 23, 2022
@rileykarson rileykarson added this to the Goals milestone Jun 6, 2022
@rfields-illuminateed
Copy link

I just ran into this today. It is still an issue.

@panoc1
Copy link

panoc1 commented Apr 19, 2023

FYI I just ran into this today, and still an issue. Any updates on this?
Thanks in advance!

@ppawel
Copy link

ppawel commented Aug 4, 2023

In my case, I was able to work around this by using the "Restore deleted credentials" option in the GCP UI, and then doing terraform import of the undeleted resource into the state. After that, Terraform was able to do an in-place update instead of recreating it.

I'm not sure if there is an API for this "Restore deleted credentials" feature, at least I couldn't find a gcloud equivalent in the whole 5 seconds I looked for it.

@github-actions github-actions bot added forward/review In review; remove label to forward service/apikeys labels Dec 4, 2023
@rwalisa
Copy link

rwalisa commented Dec 16, 2023

Deleted credentials fully expire 30 days after deletion, and the ID remains taken for a while. One solution is to use a random suffix for the key ID, so it's different each time. Like this:

resource "random_id" "key_suffix" {
  byte_length = 8
}

resource "google_apikeys_key" "api_key" {
  name         = "key-${random_id.key_suffix.hex}"
  # ...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
forward/review In review; remove label to forward persistent-bug Hard to diagnose or long lived bugs for which resolutions are more like feature work than bug work service/apikeys size/s
Projects
None yet
Development

No branches or pull requests

6 participants