Skip to content

fix: prune secret versions labelled firebase-managed=functions - #11067

Open
IzaakGough wants to merge 26 commits into
mainfrom
@invertase/fix-issue-11066
Open

IzaakGough wants to merge 26 commits into
mainfrom
@invertase/fix-issue-11066

Conversation

@IzaakGough

@IzaakGough IzaakGough commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #11066.

functions:secrets:set and functions:secrets:prune never destroyed old versions of secrets the CLI created, but still reported success.

This makes both commands find those secrets again and destroy only versions no deployed function uses.

  • Disabled versions are skipped. They are a recoverable rollback, so only functions:secrets:destroy removes them.
  • secrets:set destroys only the versions the updated functions moved off. Other unused versions are left to secrets:prune.
  • If a region is unreachable, nothing is destroyed, since its functions could not be checked. secrets:set warns, secrets:prune errors.
  • A function on latest keeps the highest enabled version, so one using a deleted secret no longer stops cleanup.
  • Non-interactive secrets:prune destroys only with --force. Before, it found nothing to prune for most projects, so CI jobs would otherwise start destroying versions without a prompt.

Tested with unit tests and live secrets:set, including a function using a deleted secret. Live secrets:prune and unreachable regions not tested.

Known limits, also on main for firebase-managed=true secrets: prune only sees secret env vars on functions in the current project. Versions used as secret volumes, by functions in other projects, or by Cloud Run services under functionsrunapionly/dartfunctions look unused. Tracked in #11616.

pruneSecrets queried labels.firebase-managed=true, but secrets created
since 13.6.1 are labelled firebase-managed=functions, so the query
returned nothing and both functions:secrets:set and
functions:secrets:prune reported success over an empty list.

List without a label filter and select with isFunctionsManaged, which
already accepts both values and excludes App Hosting secrets.

@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 updates the secret pruning logic to support both firebase-managed=true and firebase-managed=functions labels by fetching all secrets and filtering them client-side. The reviewer suggests optimizing this by performing parallel, server-side filtered API requests for both labels to avoid performance issues in large GCP projects, and updating the unit tests accordingly.

Comment thread src/functions/secrets.ts Resolved
Comment thread src/functions/secrets.spec.ts Resolved
Pruning stale versions lists and destroys secret versions, which the
command did not declare. The calls were unreachable while pruneSecrets
returned nothing, so a caller on a custom role would have failed with a
raw 403 partway through instead of the upfront permission check.
@IzaakGough
IzaakGough marked this pull request as ready for review September 10, 2026 11:31
…ailures

Disabled versions are a user's recoverable rollback, so pruneSecrets now
queries state: ENABLED. destroySecretVersions runs every destroy and
reports which ones failed instead of aborting on the first rejection.
Both commands abort if any region was unreachable, since a missing
function would otherwise read as an unused version. prune counts every
deployed function as a consumer and refuses to run non-interactively
without --force. set only destroys the versions the updated functions
were bound to, leaving the rest to prune. Both report failures per
version.
@cabljac
cabljac self-requested a review September 24, 2026 16:07
@IzaakGough

Copy link
Copy Markdown
Contributor Author

Related: #10840 fixes the same firebase-managed label mismatch in pruneSecrets by widening the server-side filter. This PR covers that case and also adds the other changes listed above.

@CorieW
CorieW self-requested a review November 12, 2026 13:53
…ailing

functions:secrets:set now resolves latest before adding the new version so functions pinned to latest get their old version cleaned up. When a region is unreachable it warns and leaves cleanup to secrets:prune rather than exiting non-zero after redeploying. secrets:prune --non-interactive keeps its previous behaviour of destroying without --force.
pruneSecrets looked up what latest pointed to for every secret a function used, so a deleted or unreadable secret aborted prune and secrets:set cleanup. It now skips secrets it does not manage and takes the highest enabled version it already listed.

@cabljac cabljac 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.

A few comments/questions

.resolves({ secret: SECRET, versionId: "4", state: "ENABLED", createTime: "" });
sandbox.stub(secrets, "updateEndpointSecret").callsFake((_, __, e) => Promise.resolve(e));
existingBackendStub = sandbox.stub(backend, "existingBackend");
pruneSecretsStub = sandbox.stub(secrets, "pruneSecrets");

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 tests would still pass if the code started destroying secret versions that are in use. With pruneSecrets and existingBackend stubbed, the safety logic isn't exercised. For example, each of these changes destroys in-use versions and all tests still pass:

  • passing [] as endpoints to pruneSecrets (L189)
  • dropping forceRefresh on the second backend load (L160)
  • dropping the projectNumber check in secrets.ts:256

Could we add a couple of tests that run the real pruneSecrets against a realistic backend, and only stub the Secret Manager / functions list calls?

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.

Thanks, added in 7c7a3bb. The new tests run the real pruneSecrets against listed functions and only stub the Secret Manager and functions list calls, with GCF reporting the secret's project by number. Each of the three changes you listed now fails at least one test, and so does restoring the deployment-tool filter in prune.

return Promise.resolve(backend.empty());
});
sandbox
.stub(secrets, "pruneSecrets")

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.

Same here: these tests wouldn't catch prune destroying versions that gcloud-deployed functions use. If the deployment-tool filter comes back at functions-secrets-prune.ts:36, those functions stop counting as users of a secret, and nothing fails. A test with a non-Firebase function pinning a version would cover it.

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.

Added in 4b5a40d. A gcloud-deployed function pins v2 and a Firebase one pins v3, and prune destroys only v1. Restoring the deployment-tool filter makes it fail.

sandbox.restore();
});

it("destroys unused versions in non-interactive mode without --force", async () => {

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.

After this PR, CI jobs that run firebase functions:secrets:prune --non-interactive will start destroying secret versions with no prompt and no --force. Before, prune was a no-op for functions-labelled secrets, so the same job printed "Nothing to prune today." The --force help reads as the way to skip the prompt.

Is that intended? If so, worth calling out in the changelog; if not, could non-interactive mode require --force here?

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.

Thanks, that wasn't intended. On main, prune only looked at secrets labelled firebase-managed=true, so for most projects a non-interactive run found nothing. As of 9f73b53, non-interactive prune lists the unused versions and only destroys them with --force. I've noted it in the changelog.

Comment thread src/commands/functions-secrets-set.ts Outdated
const staleVersions = new Set(
secrets
.of(endpointsToUpdate)
.filter((sev) => sev.secret === secret.name)

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.

nit: this can pick up version ids from a same-named secret in another project. It doesn't check sev.projectId, so if a function uses both projects/a/secrets/API_KEY and projects/b/secrets/API_KEY, b's version id lands in staleVersions. Nothing in use gets destroyed since pruneSecrets still filters, but could we match on project too, like inUse does?

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.

Thanks, fixed in 7c7a3bb. It now matches on project ID or number, the same as inUse, and there's a test with a same-named secret from another project.

const haveBackend = await backend.existingBackend(context);
backend.assertAllRegionsReachable(context);
// Every function counts as a consumer, whatever tool deployed it.
const haveEndpoints = backend.allEndpoints(haveBackend);

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.

This might destroy secret versions that users don't expect to be destroyed. Prune only counts a version as in use if a function in this project uses it as an env var, so anything else that uses it is missed. That was already true for firebase-managed=true secrets, but this PR is the first time prune touches functions-labelled ones (most secrets created since v13.6.1). Some examples:

Function in another project. Project app-b has a function with API_KEY set to projects/app-a/secrets/API_KEY/versions/3. In app-a:

firebase functions:secrets:set API_KEY   # moves app-a's functions to v4
firebase functions:secrets:prune         # app-a has no user of v3, so v3 is destroyed

The function in app-b then fails on its next cold start.

Secret mounted as a volume. Someone runs gcloud functions deploy ... --set-secrets=/etc/secrets/api_key=API_KEY:3. endpointFromFunction never reads secretVolumes (cloudfunctions.ts:132), so v3 looks unused and prune destroys it.

Cloud Run services under dartfunctions / functionsrunapionly. The CLI writes secretKeyRef.secret as the short name (runv2.ts:703), e.g. "API_KEY". Reading it back, "API_KEY".split("/") gives projectId and secret as undefined (runv2.ts:638), so pruneSecrets skips it and the version looks unused. I haven't checked whether the Run API returns the short form unchanged.

Not asking to fix these here, but could you note them in the PR body (or a follow-up issue)?

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.

Thanks for writing these up. All three hold, and they also apply on main for firebase-managed=true secrets. I confirmed the volume case live: a version mounted only as a volume shows up as prunable. I've noted them in the PR body and opened #11616 to track them.

Also test secrets:set against the real pruneSecrets, stubbing only the Secret Manager and function list calls.
Prune now covers secrets labelled functions, so a non-interactive job that found nothing before would start destroying versions without a prompt.

@cabljac cabljac 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.

LGTM from my side, all my comments are addressed. Holding off on a formal approval until a Google reviewer has approved.

This branch has not been deployed

No deployments
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.

functions:secrets:set and :prune never destroy anything: pruneSecrets filters firebase-managed=true, but labels() writes firebase-managed=functions

2 participants