Skip to content

Give secret params a .value() method #1281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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