fix(functions): defer secret access granting to release phase to prevent SA 404 race condition - #10983
Conversation
…ent SA 404 race condition ### Description Refactors secret access permission granting during Cloud Functions deployment: - Refactors ensure.secretAccess into ensure.secretsAccessDelta (to calculate secret-to-service-account mappings), ensure.checkSecretAccess (for dry-run output), and ensure.grantSecretAccess (to grant IAM permissions per secret). - Attaches secretAccessPlan to CodebasePlan in planner.ts. - In prepare.ts, computes secretsAccessDelta and runs checkSecretAccess only during --dry-run. Added explicit comments noting that actual grants take place in release. - In fabricator.ts, enqueues grantSecretAccess execution after grantNewRoles() runs (ensuring service accounts are created first) with retryPredicates: [isTransientError, isServiceAccount404] to gracefully handle GCP IAM propagation delays. ### Scenarios Tested - Ran unit test suite covering executor.spec.ts, ensure.spec.ts, planner.spec.ts, fabricator.spec.ts, and lifecycle.spec.ts (182 passing). - Verified npm run lint:changed-files and npm run build. ### Sample Commands - firebase deploy --only functions - firebase deploy --only functions --dry-run
There was a problem hiding this comment.
Code Review
This pull request resolves a secret access permission race condition during Cloud Functions deployment by deferring secret grants to the release phase after service accounts are created. It refactors the executor's retry mechanism to use predicates, enabling retries on service account 404 errors, and adds automatic cleanup of newly created service accounts upon complete deployment failure. Feedback on these changes highlights a performance regression in secretAccess where secret grants are processed serially instead of in parallel.
ajperel
left a comment
There was a problem hiding this comment.
Nice. I realize dry-run could benefit from larger improvements but this seems a step in the right direction. Thanks for quickly fixing this!
ba92705 to
6527959
Compare
Description
Refactors secret access permission granting during Cloud Functions deployment:
Scenarios Tested
Sample Commands
Notes
This issue revealed that I didn't know about the --dry-run command and it might be worth revisiting this command. I left the existing code in prepare becasue dry-run skips release, but there is legitimate stuff happening in release. We should eventually do a tech debt pass where we maybe stop running parallel code in prepare and instead run --dry-run throughout the codebase. This can reveal things in deploy like "create X bucket" "upload to X artfiact registry" "Create X task queue" "create X service account with Y roles". There's simply too much right now to fix it all and I opted for preserving existing behavior rather than improving it.