Repository navigation
fix(functions): retry on HTTP 403 service account propagation errors during function creation - #11251
Conversation
There was a problem hiding this comment.
Code Review
This pull request adds support for retrying Cloud Functions creation on HTTP 403 service account propagation errors, in addition to existing HTTP 400 and 404 errors. It introduces a regex to identify Firebase-managed service accounts and updates the error checking logic to handle specific permission-denied messages from Cloud Run service validation, accompanied by corresponding unit tests. Feedback suggests refining the check for "does not have permission" to "does not have permission to get access tokens" to make it more precise and avoid false positives on unrelated permission failures.
| return message.includes("invalid service account") && /\bfirebase-fn-[0-9]+@/i.test(message); | ||
| // Only retry 400/403 for Firebase-managed service accounts (firebase-fn-<digits>@) | ||
| // created dynamically during this deploy. Fail fast for user-provided custom service accounts. | ||
| if (!MANAGED_SERVICE_ACCOUNT_REGEX.test(message)) { |
There was a problem hiding this comment.
I realize I started this trend with only checking firebase-fn for 400 and not 404 but really... I don't know why 403 or 400 should be treated differently that 404 do you?
It feels like either .. we treat service account issues as transient always in case they were newly created. Or we always restrict this to only managed service accounts because we assume that people don't manually create a service account and then deploy with it within the next 20 seconds.
I'm tempted to say this test should be combined with the !hasSA check.
There was a problem hiding this comment.
Fair point. Restricting all retries (404, 400, and 403) to managed firebase-fn-* service accounts makes this consistent and avoids retrying on typos in user-managed service accounts. Combined MANAGED_SERVICE_ACCOUNT_REGEX.test(message) directly into hasSa and updated the 404 unit tests accordingly.
| // Cloud Run service validation reports an unpropagated service account as HTTP 403 | ||
| // when checking iam.serviceaccounts.actAs or Service Agent token permissions. | ||
| return ( | ||
| message.includes("iam.serviceaccounts.actas") || |
There was a problem hiding this comment.
These feel like the wrong parts of the message to anchor on. We wouldn't normally retry on permission denied.
The reason we're retrying is because of "or it may not exist" ... this is also a case of propagation delay.
While it probably will work the same in practice, if they had a different error message that included this but defintiively wasn't a case of not existing we wouldn't want to retry. I'd instead check for the piece of this error message that contains what we see as the "smoking gun" which is "or it may not exist"
There was a problem hiding this comment.
Noted. Anchoring on "or it may not exist" directly targets the propagation delay symptom rather than generic permission strings. Updated the 403 check to return message.includes("or it may not exist");.
…chor 403 check on 'or it may not exist'
ajperel
left a comment
There was a problem hiding this comment.
I asked you to do it, but this is a bit bigger of a change for changing the scope at which we check for managed service accounts.
If you really wanted to be 100% confident it's safe with real world error messages you could have an agent deploy and delete firestore-bigquery-export in a loop until you saw at least the pre-existing 404 retry happen since that's the case we changed. And once validating that merge.
| message = String(err).toLowerCase(); | ||
| } | ||
| const hasSa = message.includes("serviceaccount") || message.includes("service account"); | ||
| const hasSa = |
There was a problem hiding this comment.
Hmm this is a little awkward now. You're calling it "hasSA" but it's now has a managed service account.
I would either.. call it "hasManagedSA"
OR
revert this change and just change the if block below
if (!hasSa || !MANAGED_SERVICE_ACCOUNT_REGEX.test(message)) {
There was a problem hiding this comment.
Done. Went with the second option since it looked cleaner.
I ran a few manual deploy/delete cycles on my test project with declarative security enabled (creating and deleting the firebase-fn-* service account and v2 functions) and verified the error messages include the firebase-fn-* account name and everything deploys/cleans up smoothly. |
…403-sa-propagation # Conflicts: # CHANGELOG.md
Description
When deploying Cloud Functions configured with Declarative Security (
roles: [...]), the CLI dynamically provisions a new managed service account (firebase-fn-<id>@<project>.iam.gserviceaccount.com) duringgrantNewRolesand immediately proceeds to create/update the functions.Because IAM service account creation is eventually consistent across replicas, GCFv2's synchronous pre-flight validation on
POST /v2/.../functionscan hit a lagging IAM replica. Depending on which parallel pre-flight check encounters the unpropagated service account first, the API returns different HTTP status codes:Service account ... was not found) — already retried.The request was invalid: invalid service account ... provided) — already retried (fix(functions): retry on HTTP 400 service account propagation errors during function creation #11047).Permission 'iam.serviceaccounts.actAs' denied on service account firebase-fn-... (or it may not exist).orGoogle Cloud Run Service Agent does not have permission to get access tokens for the service account firebase-fn-...), because Cloud Run mapsNOT_FOUNDon IAMactAs/ token checks to403 PERMISSION_DENIED.This change updates
isServiceAccountPropagationErrorinsrc/deploy/functions/release/executor.tsto also match HTTP403propagation errors for managedfirebase-fn-[0-9]+@service accounts so function creation retries until the service account finishes propagating. To avoid masking genuine permission errors on user-managed service accounts,403retries remain restricted tofirebase-fn-[0-9]+@accounts and the specific propagation error messages.Scenarios Tested
src/deploy/functions/release/executor.spec.ts:403iam.serviceaccounts.actAsand Cloud Run Service Agent token permission errors onfirebase-fn-[0-9]+@service accounts (both direct and wrapped inFirebaseError.original).403errors on custom service accounts (custom-sa@...) or unrelated403errors.Sample Commands