Skip to content

Adds 'optional' config field to secret params - #1966

Open
Berlioz wants to merge 5 commits into
masterfrom
vsfan_optional_secret_2
Open

Berlioz wants to merge 5 commits into
masterfrom
vsfan_optional_secret_2

Conversation

@Berlioz

@Berlioz Berlioz commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

relnote: none

@Berlioz
Berlioz requested a review from ajperel September 15, 2026 21:28

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces support for optional secret parameters by adding an optional property to secret parameter specifications and options. This allows users to bypass creating a backing Cloud Secret Manager resource, resulting in an undefined runtime value without triggering warnings. While the changes successfully update SecretParam and the associated test suites, the feedback highlights that JsonSecretParam also inherits these options but does not yet support optional values in its runtimeValue() implementation, which currently throws an error. It is recommended to update JsonSecretParam to support optional values and add corresponding test coverage for it.

Comment thread src/params/types.ts
Comment on lines +458 to +459
/** If true, allows the user to decline to create a backing Cloud Secret Manager resource, resulting in undefined runtime value. */
optional?: boolean;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

Since optional is now added to SecretParamOptions, it is also accepted by JsonSecretParam (which uses SecretParamOptions in its constructor). However, JsonSecretParam.runtimeValue() currently always throws an error if the environment variable is undefined, regardless of whether optional is set to true.

To support optional JSON secrets, JsonSecretParam should be updated to return undefined when the secret is missing and optional is true.

For example, in JsonSecretParam (around line 604):

export class JsonSecretParam<T = any> {
  // ...
  runtimeValue(): T | undefined {
    const val = process.env[this.name];
    if (val === undefined) {
      if (this.options.optional) {
        return undefined;
      }
      throw new Error(
        `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.`
      );
    }
    try {
      return JSON.parse(val) as T;
    } catch (error) {
      throw new Error(
        `"${this.name}" could not be parsed as JSON. Please verify its value in Secret Manager. Details: ${error}`
      );
    }
  }

  value(): T | undefined {
    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();
  }
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Okay, what I did instead is clunky, but also there appear to be literally zero users of non-required JSON secrets in actual 1p or 3p extensions right now so...eh....

Comment thread spec/params/params.spec.ts

@ajperel ajperel left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this looks good but will wait on API review

Comment thread src/params/types.ts
export type ParamOptions<T extends string | number | boolean | string[]> = Omit<
ParamSpec<T>,
"name" | "type"
"name" | "type" | "optional"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For my own learning what is this doing?

@Berlioz Berlioz Sep 15, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ParamOptions is the type being used for a Param<string>, Param<number> etc, so this is expressing that optional is not a valid option for those.

Comment thread src/params/types.ts Outdated
const val = process.env[this.name];
if (val === undefined) {
if (this.options.optional) {
return {} as T;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should we have a test for this return value?

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.

3 participants