Skip to content

Commit

Permalink
Give secret params a .value() method (#1281)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhuleatt committed Oct 28, 2022
1 parent d6d05aa commit 1b8d1e1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
@@ -0,0 +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)
5 changes: 5 additions & 0 deletions spec/params/params.spec.ts
Expand Up @@ -27,6 +27,7 @@ describe("Params value extraction", () => {
process.env.PI = "3.14159";
process.env.TRUE = "true";
process.env.FALSE = "false";
process.env.A_SECRET_STRING = "123456supersecret";
});

afterEach(() => {
Expand All @@ -41,6 +42,7 @@ describe("Params value extraction", () => {
delete process.env.PI;
delete process.env.TRUE;
delete process.env.FALSE;
delete process.env.A_SECRET_STRING;
});

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

const falseParam = params.defineBoolean("FALSE");
expect(falseParam.value()).to.be.false;

const secretParam = params.defineSecret("A_SECRET_STRING");
expect(secretParam.value()).to.equal("123456supersecret");
});

it("extracts the special case internal params from env.FIREBASE_CONFIG", () => {
Expand Down
18 changes: 17 additions & 1 deletion src/params/types.ts
Expand Up @@ -345,7 +345,13 @@ export class SecretParam {

/** @internal */
runtimeValue(): string {
return process.env[this.name] || "";
const val = process.env[this.name];
if (val === undefined) {
logger.warn(
`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.`
);
}
return val || "";
}

/** @internal */
Expand All @@ -355,6 +361,16 @@ export class SecretParam {
name: this.name,
};
}

/** Returns the secret's value at runtime. Throws an error if accessed during deployment. */
value(): string {
if (process.env.FUNCTIONS_CONTROL_API === "true") {
throw new Error(
`Cannot access the value of secret "${this.name}" during function deployment. Secret values are only available at runtime.`
);
}
return this.runtimeValue();
}
}

/**
Expand Down

0 comments on commit 1b8d1e1

Please sign in to comment.