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

Handle compute.googleapis.com properly when passed in via activate_api_identities #628

Merged

Conversation

rsinnet
Copy link
Contributor

@rsinnet rsinnet commented Oct 9, 2021

Synopsis

Process roles for the compute.googleapis.com service identity separately from other service identities when passed via activate_api_identities.

Details

This service identity cannot be created with the google_project_service_identity resource. Attempting to activate this service identity leads to an error response. It seems that this API call is not permitted despite lack of documentation. This can be seen by calling:

gcloud alpha services identity create --service=compute.googleapis.com --log-http

Calling this same command with, e.g., cloudbuild.googleapis.com does not result in an error.

It seems the service is activated by default, so instead just retrieve the email using google_compute_default_service_account. So get this email, create a combined map with this service identity (if present) and other service identities (if present), and then process this map as before, adding each role for each service identity.

@rsinnet rsinnet requested a review from a team as a code owner October 9, 2021 14:29
@comment-bot-dev
Copy link

comment-bot-dev commented Oct 9, 2021

Thanks for the PR! 🚀
✅ Lint checks have passed.

@morgante
Copy link
Contributor

morgante commented Oct 9, 2021

Instead of raising an error/warning, should we simply filter this value out of the list automatically?

@rsinnet
Copy link
Contributor Author

rsinnet commented Oct 13, 2021

Instead of raising an error/warning, should we simply filter this value out of the list automatically?

That's an interesting suggestion. I like it because it defines the error out of existence and the outcome is what the user would probably expect. I would be in favor of that interface. Since I'm in the code at the moment, I'll go ahead and push a proposed implementation.

@rsinnet rsinnet changed the title Explain error when passing compute.googleapis.com to activate_api_identities Filter out compute.googleapis.com from activate_api_identities Oct 13, 2021
@rsinnet rsinnet changed the title Filter out compute.googleapis.com from activate_api_identities Handle compute.googleapis.com properly when passed in via activate_api_identities Oct 13, 2021
…ely from other service identities when passed via activate_api_identities.

This service identity cannot be created with the `google_project_service_identity` resource. Attempting to activate this service identity leads to an error response. It seems that this API call is not permitted despite lack of documentation. This can be seen by calling:

```sh
gcloud alpha services identity create --service=compute.googleapis.com --log-http
```

Calling this same command with, e.g., cloudbuild.googleapis.com does not result in an error.

It seems the service is activated by default, so instead just retrieve the email using `google_compute_default_service_account`. So get this email, create a combined map with this service identity (if present) and other service identities (if present), and then process this map as before, adding each role for each service identity.
@rsinnet rsinnet force-pushed the user/rsinnet/update-readme branch from ded4d38 to aa659f8 Compare October 13, 2021 15:18
@rsinnet
Copy link
Contributor Author

rsinnet commented Oct 13, 2021

Instead of raising an error/warning, should we simply filter this value out of the list automatically?

@morgante I thought through it a bit more and modified it so it would still activate the specified roles for the compute account as it would for any other service identity to keep the interface consistent. Let me know what you think!

Copy link
Contributor

@morgante morgante left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this, that is indeed what I had in mind.

@@ -15,7 +15,8 @@
*/

locals {
services = var.enable_apis ? toset(concat(var.activate_apis, [for i in var.activate_api_identities : i.api])) : toset([])
activate_compute_identity = 0 != length([for i in var.activate_api_identities : i if i.api == "compute.googleapis.com"])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic can be simplified:

Suggested change
activate_compute_identity = 0 != length([for i in var.activate_api_identities : i if i.api == "compute.googleapis.com"])
activate_compute_identity = contains(var.activate_api_identities, "compute.googleapis.com")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh cool didn't know that worked for keys in maps. Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh actually it might not. Nevermind.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could use a splat expression if that is better. Could change the two lines like this:

  activate_compute_identity = contains(var.activate_api_identities[*].api, "compute.googleapis.com")
  services                  = var.enable_apis ? toset(concat(var.activate_apis, var.activate_api_identities[*].api)) : toset([])

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ended up merging your original approach since it worked fine.

@morgante morgante merged commit 777092c into terraform-google-modules:master Oct 13, 2021
@rsinnet rsinnet deleted the user/rsinnet/update-readme branch October 13, 2021 23:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants