Skip to content

fix(functions): don't prune secret versions mounted as volumes or used by non-Firebase functions - #11623

Open
rarya618 wants to merge 3 commits into
firebase:mainfrom
rarya618:fix/secrets-prune-volumes
Open

rarya618 wants to merge 3 commits into
firebase:mainfrom
rarya618:fix/secrets-prune-volumes

Conversation

@rarya618

@rarya618 rarya618 commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Description

Fixes #11616 (secret volumes, and functions not deployed by the Firebase CLI; the cross-project case is left open, see below).

functions:secrets:prune decides a Firebase-managed secret version is unused if no function in the project references it. Two gaps made it destroy versions that functions still need:

  1. Secret volumes were never read. endpointFromFunction in src/gcp/cloudfunctions.ts and src/gcp/cloudfunctionsv2.ts copied secretEnvironmentVariables but not secretVolumes, and backend.Endpoint had no field for them. A version that a function only mounts as a file looked unused.
  2. Only Firebase-deployed functions were checked. functions-secrets-prune.ts filtered the existing functions with isFirebaseManaged before checking usage. A function deployed another way, like the gcloud functions deploy in the issue's repro, didn't protect a version even through an env var. So fixing (1) alone wouldn't fix the reported repro.

Changes:

  • backend.Endpoint gets an optional secretVolumes field, filled in by both endpointFromFunction conversions. It's read-only: functionFromEndpoint doesn't send it, so the CLI still never deploys secret volumes and doesn't touch volumes set by other tools.
  • pruneSecrets counts every version a volume mounts. A volume that lists no versions mounts the latest, so it counts as latest and is resolved the same way as an env var pinned to latest. Volumes referencing other projects are ignored, as env vars already are.
  • functions:secrets:prune passes every function to pruneSecrets, as functions:secrets:set already does. The filter dates back to the original secrets release (Release CF3's support for environment variables and secrets #4149) with no stated reason. Removing it can only make prune keep more versions, never destroy more. It's a separate commit in case you'd rather keep it.

Not covered

  • Functions in other projects. A function in project B referencing projects/A/secrets/API_KEY/versions/1 still doesn't stop prune in project A, because prune can't list another project's functions. That seems to need a product decision (for example, a warning in the confirmation prompt) rather than a code fix, so I left it out.
  • Functions served directly from Cloud Run (runv2.endpointFromService): secret volumes there live in the service template's volumes, which isn't read here.

Scenarios Tested

  • New pruneSecrets tests:
    • A volume mounting versions 1 and 2 keeps both.
    • A volume with no versions keeps the version latest resolves to, including when the volume references the project by number.
    • A volume from another project protects nothing.
    • The first two fail without the fix.
  • New endpointFromFunction tests for GCF v1 and v2: secretVolumes is copied onto the endpoint.
  • npx mocha over src/deploy/functions, src/functions, src/gcp and the functions commands: 1931 passing, 0 failing. tsc is clean and lint reports no errors.
  • Not run: the issue's repro against a live project.

Sample Commands

firebase functions:secrets:set API_KEY
gcloud functions deploy my-fn --gen2 --set-secrets=/etc/secrets/api_key=API_KEY:1 ...
firebase functions:secrets:prune
# Before: lists API_KEY@1 as unused and offers to destroy it
# After:  API_KEY@1 is kept because my-fn mounts it

…irebase#11616)

functions:secrets:prune counted a version as in use only if a function
used it as a secret environment variable. Functions read from GCF v1 and
v2 now carry their secretVolumes, and prune counts every version they
mount, or the latest version when a volume lists none. The CLI still never
deploys secret volumes.
functions:secrets:prune only looked at functions the Firebase CLI had
deployed, so a function deployed another way (for example with gcloud)
did not stop prune from destroying a secret version it used.
functions:secrets:set already checks every function before pruning.

@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 functions:secrets:prune command to ensure it does not destroy secret versions that are mounted as volumes or used by functions deployed outside of the Firebase CLI. It introduces a new SecretVolume interface, updates GCF v1 and v2 endpoint parsers to preserve secret volumes, and incorporates these volumes into the pruning logic. Comprehensive unit tests have been added to validate these scenarios. As there are no review comments to evaluate, I have no feedback to provide.

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:prune destroys secret versions that are used outside the current project's secret env vars

1 participant