Repository navigation
fix: prune secret versions labelled firebase-managed=functions - #11067
IzaakGough wants to merge 26 commits into
Conversation
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.
There was a problem hiding this comment.
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.
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.
…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.
…11066 # Conflicts: # CHANGELOG.md
…11066 # Conflicts: # CHANGELOG.md
…11066 # Conflicts: # CHANGELOG.md
|
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. |
# Conflicts: # CHANGELOG.md
…11066 # Conflicts: # CHANGELOG.md
# Conflicts: # CHANGELOG.md
…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
left a comment
There was a problem hiding this comment.
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"); |
There was a problem hiding this comment.
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 topruneSecrets(L189) - dropping
forceRefreshon the second backend load (L160) - dropping the
projectNumbercheck insecrets.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?
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 () => { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
| const staleVersions = new Set( | ||
| secrets | ||
| .of(endpointsToUpdate) | ||
| .filter((sev) => sev.secret === secret.name) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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)?
There was a problem hiding this comment.
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.
…11066 # Conflicts: # CHANGELOG.md
cabljac
left a comment
There was a problem hiding this comment.
LGTM from my side, all my comments are addressed. Holding off on a formal approval until a Google reviewer has approved.
Fixes #11066.
functions:secrets:setandfunctions:secrets:prunenever 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.
functions:secrets:destroyremoves them.secrets:setdestroys only the versions the updated functions moved off. Other unused versions are left tosecrets:prune.secrets:setwarns,secrets:pruneerrors.latestkeeps the highest enabled version, so one using a deleted secret no longer stops cleanup.secrets:prunedestroys 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. Livesecrets:pruneand unreachable regions not tested.Known limits, also on main for
firebase-managed=truesecrets: 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 underfunctionsrunapionly/dartfunctionslook unused. Tracked in #11616.