Skip to content
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

Give secret params a .value() method #1281

Merged
merged 8 commits into from Oct 28, 2022
Merged

Conversation

jhuleatt
Copy link
Contributor

Description

Fixes #1264

Code sample

const functions = require('firebase-functions');
const { defineSecret } = require('firebase-functions/params');
const discordApiKey = defineSecret('DISCORD_API_KEY');

export const postToDiscord = functions.runWith({ secrets: [discordApiKey] }).https.onRequest(
  (req, res) => {
    const apiKey = discordApiKey.value();
    //…

https://firebase.google.com/docs/functions/config-env#secret_parameters

@taeold
Copy link
Contributor

taeold commented Oct 26, 2022

Can we add a changelog? I think this is a bugfix.

@jhuleatt
Copy link
Contributor Author

Thanks, I added a changelog!

A couple of questions:

  1. Do we need to add any guards to prevent this param type from being used outside of runtime, since secret values are only available to the function at runtime? Or does not inheriting from Parameter solve this for us?
  2. Should the .value function throw an error if the secret value isn't available to the function (for example, if a developer forgot to add the secrets config array to the function)?

Copy link
Contributor

@Berlioz Berlioz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than one nit, LGTM.

@@ -345,7 +345,13 @@ export class SecretParam {

/** @internal */
runtimeValue(): string {
return process.env[this.name] || "";
const val = process.env[this.name];
if (!val) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

couldn't someone deliberately set a secret to the empty string, which is falsy?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed from a falsy check to val === undefined

@Berlioz
Copy link
Contributor

Berlioz commented Oct 28, 2022

LGTM

@jhuleatt jhuleatt merged commit 1b8d1e1 into master Oct 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Environment Secrets have no value
3 participants