Skip to content

fix(functions): retry on HTTP 403 service account propagation errors during function creation - #11251

Merged
shettyvarun268 merged 9 commits into
mainfrom
shettyvarun268/retry-403-sa-propagation
Oct 12, 2026
Merged

shettyvarun268 merged 9 commits into
mainfrom
shettyvarun268/retry-403-sa-propagation

Conversation

@shettyvarun268

@shettyvarun268 shettyvarun268 commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

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) during grantNewRoles and 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/.../functions can 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:

  • HTTP 404: GCFv2 direct IAM lookup (Service account ... was not found) — already retried.
  • HTTP 400: Eventarc dry-run trigger validation (The request was invalid: invalid service account ... provided) — already retried (fix(functions): retry on HTTP 400 service account propagation errors during function creation #11047).
  • HTTP 403: Cloud Run dry-run service validation (Permission 'iam.serviceaccounts.actAs' denied on service account firebase-fn-... (or it may not exist). or Google Cloud Run Service Agent does not have permission to get access tokens for the service account firebase-fn-...), because Cloud Run maps NOT_FOUND on IAM actAs / token checks to 403 PERMISSION_DENIED.

This change updates isServiceAccountPropagationError in src/deploy/functions/release/executor.ts to also match HTTP 403 propagation errors for managed firebase-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, 403 retries remain restricted to firebase-fn-[0-9]+@ accounts and the specific propagation error messages.

Scenarios Tested

  • Unit tests in src/deploy/functions/release/executor.spec.ts:
    • Matches HTTP 403 iam.serviceaccounts.actAs and Cloud Run Service Agent token permission errors on firebase-fn-[0-9]+@ service accounts (both direct and wrapped in FirebaseError.original).
    • Does not match HTTP 403 errors on custom service accounts (custom-sa@...) or unrelated 403 errors.

Sample Commands

firebase deploy --only functions

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@shettyvarun268 shettyvarun268 changed the title Retry 403 SA propagation fix(functions): retry on HTTP 403 service account propagation errors during function creation Oct 9, 2026
@shettyvarun268
shettyvarun268 marked this pull request as ready for review October 9, 2026 17:43
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)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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") ||

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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");.

Comment thread CHANGELOG.md Resolved

@ajperel ajperel left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Went with the second option since it looked cleaner.

@shettyvarun268

Copy link
Copy Markdown
Contributor Author

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.

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
@shettyvarun268
shettyvarun268 merged commit 978e1fd into main Oct 12, 2026
76 of 77 checks passed
@shettyvarun268
shettyvarun268 deleted the shettyvarun268/retry-403-sa-propagation branch October 12, 2026 18:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants