Skip to content

Commit 4839dcb

Browse files
authored
Fixes secret params resolution in functions emulator (#7443)
* don't resolve secrets in deploy for emulator * add changelog
1 parent ff49843 commit 4839dcb

File tree

6 files changed

+42
-28
lines changed

6 files changed

+42
-28
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
- Fixes secret params resolution in functions emulator. (#7443)
12
- Fixed bug where `esbuild` execution was throwing an error saying "Command line too long" on Windows (#7250, #6193).
23
- Automatically detect app platform during `init dataconnect:sdk`.

src/deploy/functions/build.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -278,37 +278,43 @@ export type DynamicExtension = {
278278
events: string[];
279279
};
280280

281+
interface ResolveBackendOpts {
282+
build: Build;
283+
firebaseConfig: FirebaseConfig;
284+
userEnvOpt: UserEnvsOpts;
285+
userEnvs: Record<string, string>;
286+
nonInteractive?: boolean;
287+
isEmulator?: boolean;
288+
}
289+
281290
/**
282291
* Resolves user-defined parameters inside a Build, and generates a Backend.
283292
* Returns both the Backend and the literal resolved values of any params, since
284293
* the latter also have to be uploaded so user code can see them in process.env
285294
*/
286295
export async function resolveBackend(
287-
build: Build,
288-
firebaseConfig: FirebaseConfig,
289-
userEnvOpt: UserEnvsOpts,
290-
userEnvs: Record<string, string>,
291-
nonInteractive?: boolean,
296+
opts: ResolveBackendOpts,
292297
): Promise<{ backend: backend.Backend; envs: Record<string, params.ParamValue> }> {
293298
let paramValues: Record<string, params.ParamValue> = {};
294299
paramValues = await params.resolveParams(
295-
build.params,
296-
firebaseConfig,
297-
envWithTypes(build.params, userEnvs),
298-
nonInteractive,
300+
opts.build.params,
301+
opts.firebaseConfig,
302+
envWithTypes(opts.build.params, opts.userEnvs),
303+
opts.nonInteractive,
304+
opts.isEmulator,
299305
);
300306

301307
const toWrite: Record<string, string> = {};
302308
for (const paramName of Object.keys(paramValues)) {
303309
const paramValue = paramValues[paramName];
304-
if (Object.prototype.hasOwnProperty.call(userEnvs, paramName) || paramValue.internal) {
310+
if (Object.prototype.hasOwnProperty.call(opts.userEnvs, paramName) || paramValue.internal) {
305311
continue;
306312
}
307313
toWrite[paramName] = paramValue.toString();
308314
}
309-
writeUserEnvs(toWrite, userEnvOpt);
315+
writeUserEnvs(toWrite, opts.userEnvOpt);
310316

311-
return { backend: toBackend(build, paramValues), envs: paramValues };
317+
return { backend: toBackend(opts.build, paramValues), envs: paramValues };
312318
}
313319

314320
// Exported for testing

src/deploy/functions/params.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ export async function resolveParams(
369369
firebaseConfig: FirebaseConfig,
370370
userEnvs: Record<string, ParamValue>,
371371
nonInteractive?: boolean,
372+
isEmulator = false,
372373
): Promise<Record<string, ParamValue>> {
373374
const paramValues: Record<string, ParamValue> = populateDefaultParams(firebaseConfig);
374375

@@ -381,8 +382,11 @@ export async function resolveParams(
381382
}
382383

383384
const [needSecret, needPrompt] = partition(outstanding, (param) => param.type === "secret");
384-
for (const param of needSecret) {
385-
await handleSecret(param as SecretParam, firebaseConfig.projectId);
385+
// The functions emulator will handle secrets
386+
if (!isEmulator) {
387+
for (const param of needSecret) {
388+
await handleSecret(param as SecretParam, firebaseConfig.projectId);
389+
}
386390
}
387391

388392
if (nonInteractive && needPrompt.length > 0) {

src/deploy/functions/prepare.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,14 @@ export async function prepare(
123123
const userEnvs = functionsEnv.loadUserEnvs(userEnvOpt);
124124
const envs = { ...userEnvs, ...firebaseEnvs };
125125

126-
const { backend: wantBackend, envs: resolvedEnvs } = await build.resolveBackend(
127-
wantBuild,
126+
const { backend: wantBackend, envs: resolvedEnvs } = await build.resolveBackend({
127+
build: wantBuild,
128128
firebaseConfig,
129129
userEnvOpt,
130130
userEnvs,
131-
options.nonInteractive,
132-
);
131+
nonInteractive: options.nonInteractive,
132+
isEmulator: false,
133+
});
133134

134135
let hasEnvsFromParams = false;
135136
wantBackend.environmentVariables = envs;

src/deploy/functions/runtimes/node/parseTriggers.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ import { BEFORE_CREATE_EVENT } from "../../../../functions/events/v1";
99

1010
async function resolveBackend(bd: build.Build): Promise<backend.Backend> {
1111
return (
12-
await build.resolveBackend(
13-
bd,
14-
{
12+
await build.resolveBackend({
13+
build: bd,
14+
firebaseConfig: {
1515
locationId: "",
1616
projectId: "foo",
1717
storageBucket: "foo.appspot.com",
1818
databaseURL: "https://foo.firebaseio.com",
1919
},
20-
{ functionsSource: "", projectId: "PROJECT" },
21-
{},
22-
)
20+
userEnvOpt: { functionsSource: "", projectId: "PROJECT" },
21+
userEnvs: {},
22+
})
2323
).backend;
2424
}
2525

src/emulator/functionsEmulator.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -540,12 +540,14 @@ export class FunctionsEmulator implements EmulatorInstance {
540540
};
541541
const userEnvs = functionsEnv.loadUserEnvs(userEnvOpt);
542542
const discoveredBuild = await runtimeDelegate.discoverBuild(runtimeConfig, environment);
543-
const resolution = await resolveBackend(
544-
discoveredBuild,
545-
JSON.parse(firebaseConfig),
543+
const resolution = await resolveBackend({
544+
build: discoveredBuild,
545+
firebaseConfig: JSON.parse(firebaseConfig),
546546
userEnvOpt,
547547
userEnvs,
548-
);
548+
nonInteractive: false,
549+
isEmulator: true,
550+
});
549551
const discoveredBackend = resolution.backend;
550552
const endpoints = backend.allEndpoints(discoveredBackend);
551553
prepareEndpoints(endpoints);

0 commit comments

Comments
 (0)