Conversation
fix(iam): resolve null service identity email error
Refactor service agent identity logic to use deterministic email construction.
Calculate service agent emails using ${project_number} to bypass API race conditions.
Add explicit depends_on between project services and IAM members.
Standardize Compute Engine default service account retrieval.
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Summary of ChangesHello @PaggieChen, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a critical issue where service agent identity emails could be null due to race conditions during API calls. It refactors the core logic for constructing service agent emails to be deterministic, leveraging the project number for consistent generation. Furthermore, it introduces explicit dependencies between key resource types, such as project service enablement, service identity creation, and IAM role assignments, to guarantee that all necessary components are fully initialized before subsequent operations. This significantly enhances the stability and reliability of resource provisioning within the module. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request refactors the service agent identity logic to use deterministic email construction, which is a good approach to avoid race conditions and issues with values not being known during Terraform's plan phase. The explicit depends_on clauses added to various resources also improve the robustness of the module by enforcing the correct order of resource creation.
However, I've found a critical issue with the implementation of the deterministic email construction. The current logic assumes a single, common format for service agent emails, which is not correct for all GCP services. This will cause the module to fail when used with services like BigQuery or Cloud Storage that use a different email format. My review includes a detailed comment and a code suggestion to address this by handling exceptions for different email formats.
| service_agent_emails = { | ||
| for i in var.activate_api_identities : | ||
| i.api => i.api == "compute.googleapis.com" ? | ||
| data.google_compute_default_service_account.default[0].email : | ||
| "service-${data.google_project.project.number}@gcp-sa-${replace(i.api, ".googleapis.com", "")}.iam.gserviceaccount.com" | ||
| } |
There was a problem hiding this comment.
The deterministic construction of service agent emails assumes a single format (service-...@gcp-sa-...), which is not universally true for all Google Cloud services. This will cause failures for services with different service agent email formats, such as BigQuery or Cloud Storage.
To make this more robust, you could use a map of exceptions for services that don't follow the common pattern. Here is a suggested implementation using lookup to handle these exceptions. You may want to expand this map with other known exceptions.
service_agent_emails = {
for i in var.activate_api_identities :
i.api => i.api == "compute.googleapis.com" ?
data.google_compute_default_service_account.default[0].email :
lookup(
{
"bigquery.googleapis.com" = "bq-${data.google_project.project.number}@bigquery-iam.gserviceaccount.com",
"storage.googleapis.com" = "service-${data.google_project.project.number}@gs-project-accounts.iam.gserviceaccount.com"
},
i.api,
"service-${data.google_project.project.number}@gcp-sa-${replace(i.api, ".googleapis.com", "")}.iam.gserviceaccount.com"
)
}
fix(iam): resolve null service identity email error
Refactor service agent identity logic to use deterministic email construction.
Calculate service agent emails using ${project_number} to bypass API race conditions.
Add explicit depends_on between project services and IAM members.
Standardize Compute Engine default service account retrieval.