Skip to content

Prefix secret resource IDs for kits instances with kit instance ID - #11092

Open
Berlioz wants to merge 9 commits into
mainfrom
vsfan_kits_prefixed_secret_resource_ids
Open

Berlioz wants to merge 9 commits into
mainfrom
vsfan_kits_prefixed_secret_resource_ids

Conversation

@Berlioz

@Berlioz Berlioz commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

No description provided.

@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 renames applyPrefix to applyEndpointPrefix and introduces a new function applyKitSecretRefPrefix to prefix secret resource IDs with kit instance IDs. However, the newly added applyKitSecretRefPrefix function is never invoked in the codebase. Feedback suggests integrating this function into the deployment, emulation, and installation flows to ensure the feature is fully implemented.

Comment thread src/deploy/functions/build.ts
Comment thread src/deploy/functions/prepare.ts
Comment thread src/emulator/functionsEmulator.ts
Comment thread src/functions/kits/install.ts
Comment thread src/deploy/functions/build.ts Outdated
Comment thread src/deploy/functions/build.ts Outdated
* These will be overwritten if the secret is defined in .envs, since
* applyEnvSecretBindings will get run later in deploy prepare.
*/
export function applyKitSecretRefPrefix(build: Build, instanceId: string): void {

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.

🔴 [Logic & Runtime Safety] Update endpoint.secretEnvironmentVariables in applyKitSecretRefPrefix and hoist prefix computation

Rationale: In Build, secret references exist in both build.params (SecretParam.resourceId, used by ensureSecret to create/verify secrets in Cloud Secret Manager) and build.endpoints[ep].secretEnvironmentVariables (SecretEnvVar.secret, copied by toBackend and validated by validateSecretVersions).

Because applyEnvSecretBindingsToBuild only updates secretEnvironmentVariables for secrets explicitly overridden in .env files, any default kit secret (not in .env) will have kit-<instanceId>-<name> created in Secret Manager by ensureSecret, while toBackend and validateSecretVersions still reference the unprefixed <name>. Additionally, per go/tsstyle#descriptive-names and go/ts-practices/iteration#for-of, avoid single-letter variable p, intermediate .filter() array allocation, and repeated addKitPrefix(instanceId) calls inside the loop.

Suggested Fix:

export function applyKitSecretRefPrefix(build: Build, instanceId: string): void {
  if (!instanceId) {
    return;
  }
  const prefix = addKitPrefix(instanceId);
  const prefixedSecrets = new Map<string, string>();

  for (const param of build.params) {
    if (params.isSecretParam(param)) {
      const prefixedId = `${prefix}-${param.name}`;
      param.resourceId = prefixedId;
      prefixedSecrets.set(param.name, prefixedId);
    }
  }

  for (const endpoint of Object.values(build.endpoints)) {
    for (const envVar of endpoint.secretEnvironmentVariables ?? []) {
      const prefixedSecret = prefixedSecrets.get(envVar.key);
      if (prefixedSecret) {
        envVar.secret = prefixedSecret;
      }
    }
  }
}

@Berlioz Berlioz Sep 16, 2026

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.

just for the record, if you go to the go links and find what rules the bot is referencing:

Exception: Variables that are in scope for 10 lines or fewer, including arguments that are not part of an exported API, may use short (e.g. single letter) variable names.

For simple transformations, prefer the appropriate transformation function. So the bit about dropping filter in here is specifically against instructions.

: codebaseConfig.prefix || "";
build.applyPrefix(discoveredBuild, prefix);
build.applyEndpointPrefix(discoveredBuild, prefix);
if (isKitConfig(codebaseConfig)) {

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: Consolidate duplicate isKitConfig(codebaseConfig) checks (go/tsstyle#control-flow-statements-blocks)

isKitConfig(codebaseConfig) is evaluated in the ternary on line 855 and immediately checked again in the if statement on line 859.

Suggested change
if (isKitConfig(codebaseConfig)) {
if (isKitConfig(codebaseConfig)) {
build.applyEndpointPrefix(discoveredBuild, addKitPrefix(codebase));
build.applyKitSecretRefPrefix(discoveredBuild, codebase);
} else {
build.applyEndpointPrefix(discoveredBuild, codebaseConfig.prefix ?? "");
}

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 could go either way on this one.

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.

i'm not super convinced this is more readable either yeah

? cloneDeep(options.preDiscoveredBuild)
: await discoverKitBuild(options, options.absSourcePath);
build.applyPrefix(discoveredBuild, prefix);
build.applyEndpointPrefix(discoveredBuild, prefix);

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.

🔴 [Architecture & Consistency] kits:install (promptAndWriteKitParams) provisions unprefixed secrets

Rationale: During firebase functions:kits:install (lines 1346 and 1596), discoveredBuild.params is passed to promptAndWriteKitParams without calling applyKitSecretRefPrefix. Consequently, ensureSecret creates unprefixed secrets in Cloud Secret Manager (and writes unprefixed FIREBASE_SECRET_REF_<NAME>=<NAME> bindings to .env.<projectId> when secretEnvParams is enabled). When the user later runs firebase deploy, prepare.ts expects kit-<instanceId>-<NAME>.

Please ensure applyKitSecretRefPrefix is applied to the discovered kit build/params before applyEnvSecretBindingsToParams and resolveParams run in install.ts (and similarly in src/emulator/functionsEmulator.ts:629 for local kit emulation).

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.

Does the emulator support local kit emulation?

Comment thread src/deploy/functions/build.spec.ts
Comment thread src/functions/kits/install.ts Outdated
build.parseSecretRef(unparsed),
);
for (const secretParam of options.params.filter((p) => params.isSecretParam(p))) {
secretParam.resourceId = `${options.instanceId}-${secretParam.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.

This is missing the kit- at the beginning since you didn't use addKitPrefix

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.

Ack

Comment thread src/deploy/functions/build.spec.ts Outdated
httpsTrigger: {},
secretEnvironmentVariables: [
{ key: "API_KEY", secret: "API_KEY", projectId: "test-project" },
{ key: "SMTP_ADDRESS", secret: " SMTP_ADDRESS", projectId: "test-project" },

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 extra space in " SMTP_ADDRESS""

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.

Ack

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.

4 participants