Conversation
There was a problem hiding this comment.
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.
| * 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 { |
There was a problem hiding this comment.
🔴 [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;
}
}
}
}There was a problem hiding this comment.
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)) { |
There was a problem hiding this comment.
🟡 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.
| if (isKitConfig(codebaseConfig)) { | |
| if (isKitConfig(codebaseConfig)) { | |
| build.applyEndpointPrefix(discoveredBuild, addKitPrefix(codebase)); | |
| build.applyKitSecretRefPrefix(discoveredBuild, codebase); | |
| } else { | |
| build.applyEndpointPrefix(discoveredBuild, codebaseConfig.prefix ?? ""); | |
| } |
There was a problem hiding this comment.
I could go either way on this one.
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
🔴 [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).
There was a problem hiding this comment.
Does the emulator support local kit emulation?
| build.parseSecretRef(unparsed), | ||
| ); | ||
| for (const secretParam of options.params.filter((p) => params.isSecretParam(p))) { | ||
| secretParam.resourceId = `${options.instanceId}-${secretParam.name}`; |
There was a problem hiding this comment.
This is missing the kit- at the beginning since you didn't use addKitPrefix
| httpsTrigger: {}, | ||
| secretEnvironmentVariables: [ | ||
| { key: "API_KEY", secret: "API_KEY", projectId: "test-project" }, | ||
| { key: "SMTP_ADDRESS", secret: " SMTP_ADDRESS", projectId: "test-project" }, |
There was a problem hiding this comment.
nit extra space in " SMTP_ADDRESS""
No description provided.