Repository navigation
Conversation
…ad its startup scripts Both modules wrap the startup script in a startup-script module whose staging bucket only the default Compute Engine account could read, so a VM given its own least-privilege account (e.g. from service-account-profiles) got 403s fetching its runners and never ran its startup script. Grant the module's service account read access through bucket_viewers, only on the bucket the module creates: a user-provided startup_script_path bucket is left as it is, since that binding is authoritative. startup_from_network_storage.tf stays identical in both modules (tools/duplicate-diff.py); each module provides its account as local.startup_script_service_account.
Summary of ChangesHello, 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 permissions issue where VM instances and Batch jobs using custom, least-privilege service accounts were unable to read their startup scripts from the module-generated GCS bucket. By explicitly passing the service account as a bucket viewer to the underlying startup-script module, the change ensures that the VM can successfully download and execute its startup scripts without requiring additional manual IAM configuration. 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 the 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 counterproductive. 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. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request updates the vm-instance and batch-job-template modules to grant the configured service account read access to the automatically created startup-script GCS bucket. It also updates the corresponding variable descriptions and README documentation. However, in both modules, if the service account is set to an empty string, the current condition will evaluate to true and attempt to add an invalid IAM member ("serviceAccount:"), which will cause a Terraform apply error. It is recommended to update the condition to also check that the service account is not an empty string.
| startup_script_bucket_viewers = ( | ||
| local.startup_script_gcs_bucket_path == null && local.startup_script_service_account != null | ||
| ) ? ["serviceAccount:${local.startup_script_service_account}"] : [] |
There was a problem hiding this comment.
If local.startup_script_service_account is set to an empty string (""), the condition local.startup_script_service_account != null will evaluate to true. This results in ["serviceAccount:"] being passed to bucket_viewers, which is an invalid IAM member format and will cause a Terraform apply error.
To prevent this, we should also check that the service account is not an empty string.
startup_script_bucket_viewers = (
local.startup_script_gcs_bucket_path == null && local.startup_script_service_account != null && local.startup_script_service_account != ""
) ? ["serviceAccount:${local.startup_script_service_account}"] : []
There was a problem hiding this comment.
valid. applied the fix now.
| startup_script_bucket_viewers = ( | ||
| local.startup_script_gcs_bucket_path == null && local.startup_script_service_account != null | ||
| ) ? ["serviceAccount:${local.startup_script_service_account}"] : [] |
There was a problem hiding this comment.
If local.startup_script_service_account is set to an empty string (""), the condition local.startup_script_service_account != null will evaluate to true. This results in ["serviceAccount:"] being passed to bucket_viewers, which is an invalid IAM member format and will cause a Terraform apply error.
To prevent this, we should also check that the service account is not an empty string.
startup_script_bucket_viewers = (
local.startup_script_gcs_bucket_path == null && local.startup_script_service_account != null && local.startup_script_service_account != ""
) ? ["serviceAccount:${local.startup_script_service_account}"] : []
…or an empty service account An empty service_account_email leaves the VM on the default Compute Engine account, as before; granting "serviceAccount:" would fail the apply.
vm-instance: let the VM's service account read its startup scripts
Problem
modules/compute/vm-instancewraps itsstartup_scriptin astartup-scriptmodule (startup_from_network_storage.tf). That module stages the runners in a bucket the module creates,<deployment>-startup-scripts-<suffix>, and the VM downloads them at boot.Only the default Compute Engine account, which usually has project Editor, can read that bucket. A VM given its own least-privilege account through
service_account_email(for example fromcommunity/modules/iam/service-account-profiles) gets HTTP 403 onstorage.objects.get. It never runs its startup script:This hasn't shown up before because examples either leave the default account in place or use accounts with project-wide storage roles.
service-account-profilesmakes least-privilege accounts the norm, so the gap now matters.A blueprint can work around it only by granting the account read access to every bucket in the project, because the bucket name isn't an output of the module.
modules/scheduler/batch-job-templateshares the same file and has the same problem for Batch VMs.Change
In
startup_from_network_storage.tf, which is kept identical in both modules (tools/duplicate-diff.py), pass the VM's account to the wrappedstartup-scriptmodule as a bucket viewer.Each module sets the account in its own
main.tf, so the shared file stays identical:vm-instance:startup_script_service_account = var.service_account_emailbatch-job-template:startup_script_service_account = try(var.service_account.email, null)The descriptions of
service_account_emailandservice_accountin both modules' variables and README tables now mention the read access.Safety
bucket_viewersstays[], as it is today, when the default Compute Engine account is used.startup-scriptappliesbucket_viewerswithgoogle_storage_bucket_iam_binding, which is authoritative forroles/storage.objectViewer. Adding a member to a user-providedstartup_script_pathbucket would remove its other viewers, so the grant applies only to the bucket the module creates.objectViewermember on that module's own bucket. Nothing is replaced.examples/image-builder.yaml, which grants a profile's accountbucket_viewerson its startup-script bucket.Related
community/modules/remote-desktop/novnc-desktopstages its own runners in a secondstartup-scriptbucket and needs the same one-line grant for itsservice_account_email. That fix will be rolled into a separate PR (IAP Gateway handling in OFE - once #6751 lands).