Skip to content

Secret Manager Values in offline testing #255

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

Open
A13k2 opened this issue Jan 29, 2025 · 2 comments · May be fixed by #265
Open

Secret Manager Values in offline testing #255

A13k2 opened this issue Jan 29, 2025 · 2 comments · May be fixed by #265
Assignees

Comments

@A13k2
Copy link

A13k2 commented Jan 29, 2025

Hi there,

looking for an way to use values for the secret manager values while offline testing with firebase-functions-test.

Currently migrating to firebase functions v2. We previously used mockConfig with the runtimefile.

@A13k2
Copy link
Author

A13k2 commented Jan 31, 2025

Found an solution myself, in general I cannot understand why official packages do not support functions v2 when v1 is already deprectated...

According to the firebase-functions implementation:

export class SecretParam {
  static type: ParamValueType = "secret";
  name: string;

  constructor(name: string) {
    this.name = name;
  }

  /** @internal */
  runtimeValue(): string {
    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 */
  toSpec(): ParamSpec<string> {
    return {
      type: "secret",
      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();
  }
}

So just writing the variabled/secrets in process.env gets the job done:

const secretsFile = readFileSync('functions/.secret.local', {
  encoding: 'utf-8',
});
const envFile = readFileSync('functions/.env.local', {
  encoding: 'utf-8',
});
const secrets = secretsFile.split('\n').filter((line) => line.length);
const envVariables = envFile.split('\n').filter((line) => line.length);

for (const line of [...secrets, ...envVariables]) {
  const [key, value] = line.split('=');

  process.env[key] = value;
}

@CorieW
Copy link
Member

CorieW commented Feb 10, 2025

Hi @A13k2,

Thanks for reporting this issue! We’ve received it and are reviewing it. We’ll provide updates as soon as possible.

@CorieW CorieW added the type: bug Something isn't working label Feb 10, 2025
@cabljac cabljac moved this from Needs Acknowledgment to Triaged in [Cloud] Extensions + Functions May 7, 2025
@cabljac cabljac added the triaged Triaged label May 7, 2025
@CorieW CorieW added type: feature request and removed type: bug Something isn't working labels May 9, 2025
@CorieW CorieW moved this from Triaged to In Review in [Cloud] Extensions + Functions May 14, 2025
@CorieW CorieW self-assigned this May 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants