fix(functions): grant Genkit monitoring roles after the managed service account exists - #11161
IzaakGough wants to merge 9 commits into
Conversation
…ce account exists On the first deploy of a codebase using declarative security, prepare granted the Genkit monitoring roles to a managed service account the fabricator had not created yet, so the project IAM update was rejected. The grant now runs in release after grantNewRoles. Dry runs still report the pending bindings from prepare. Fixes #11123
There was a problem hiding this comment.
Code Review
This pull request defers granting Genkit monitoring roles until after the managed service account is created during deployment, moving the call to ensureGenkitMonitoringRoles from the preparation phase to the fabricator's plan application phase. Feedback on this PR highlights that ensureGenkitMonitoringRoles should process both created and updated endpoints (rather than just created ones) to prevent permission errors when upgrading existing functions to use Genkit. This adjustment should be reflected in both the actual deployment path in fabricator.ts and the dry-run path in prepare.ts, along with corresponding updates to the function's parameter names and JSDoc documentation.
…required roles Folding the roles into requiredRoles puts them in the etag and the plan, so grantNewRoles grants them once the managed service account exists and later deploys do not revoke them. The prepare-time grant now skips managed accounts and keeps handling the default and explicit accounts.
CorieW
left a comment
There was a problem hiding this comment.
Nice fix.
I'd say before this can be approved, it should be tested for real, to see that things work as expected.
One nit:
Nothing proves the Genkit permissions stay on later deploys, even though that's one of the fix's main claims. Maybe a test would be beneficial here?
Also, one thing to watch for:
Codebases that are already enrolled and have Genkit functions get a new etag on upgrade. That brings in the setIamPolicy check, so CI deployers without IAM admin rights may start failing. Skipped functions keep the old label, so it can keep happening until the source changes. Might be worth a changelog note.
… changes The etag can change without the source changing, so every function was skipped as unchanged and kept a stale label, leaving later deploys to redo the IAM checks the mismatch implies. The skip predicate and the source upload check now share one up-to-date test that compares the label as well as the hash.
…11123 # Conflicts: # CHANGELOG.md
Fixes #11123.
requiresRolecontains a Genkit function,discoverSecurityDetailsaddsroles/monitoring.metricWriter,roles/cloudtrace.agentandroles/logging.logWritertorequiredRoles, and the fabricator grants them through the samegrantNewRolescall that creates the managed service account. The first deploy no longer binds roles to an account that does not exist yet.setIamPolicypermission check the mismatch implies. The skip predicate and the source upload check now share one up-to-date test that compares the label as well.ensureGenkitMonitoringRolesstill runs for a Genkit function on the default or an explicit service account, and now ignores endpoints on afirebase-fn-account.Verified against a real project: first deploy, unchanged redeploy, upgrade from 15.30.2, adding Genkit to an enrolled codebase, an explicit service account, and opting back out. The one deploy after upgrading redeploys the codebase's functions, as the changelog notes.