Skip to content

Commit 1b8d1e1

Browse files
authored
Give secret params a .value() method (#1281)
1 parent d6d05aa commit 1b8d1e1

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Fix a bug where [secret parameters](https://firebase.google.com/docs/functions/config-env#secret_parameters), defined using `defineSecret()`, were missing a `.value()` method (#1281)

spec/params/params.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ describe("Params value extraction", () => {
2727
process.env.PI = "3.14159";
2828
process.env.TRUE = "true";
2929
process.env.FALSE = "false";
30+
process.env.A_SECRET_STRING = "123456supersecret";
3031
});
3132

3233
afterEach(() => {
@@ -41,6 +42,7 @@ describe("Params value extraction", () => {
4142
delete process.env.PI;
4243
delete process.env.TRUE;
4344
delete process.env.FALSE;
45+
delete process.env.A_SECRET_STRING;
4446
});
4547

4648
it("extracts identity params from the environment", () => {
@@ -58,6 +60,9 @@ describe("Params value extraction", () => {
5860

5961
const falseParam = params.defineBoolean("FALSE");
6062
expect(falseParam.value()).to.be.false;
63+
64+
const secretParam = params.defineSecret("A_SECRET_STRING");
65+
expect(secretParam.value()).to.equal("123456supersecret");
6166
});
6267

6368
it("extracts the special case internal params from env.FIREBASE_CONFIG", () => {

src/params/types.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,13 @@ export class SecretParam {
345345

346346
/** @internal */
347347
runtimeValue(): string {
348-
return process.env[this.name] || "";
348+
const val = process.env[this.name];
349+
if (val === undefined) {
350+
logger.warn(
351+
`No value found for secret parameter "${this.name}". A function can only access a secret if you include the secret in the function's dependency array.`
352+
);
353+
}
354+
return val || "";
349355
}
350356

351357
/** @internal */
@@ -355,6 +361,16 @@ export class SecretParam {
355361
name: this.name,
356362
};
357363
}
364+
365+
/** Returns the secret's value at runtime. Throws an error if accessed during deployment. */
366+
value(): string {
367+
if (process.env.FUNCTIONS_CONTROL_API === "true") {
368+
throw new Error(
369+
`Cannot access the value of secret "${this.name}" during function deployment. Secret values are only available at runtime.`
370+
);
371+
}
372+
return this.runtimeValue();
373+
}
358374
}
359375

360376
/**

0 commit comments

Comments
 (0)