Skip to content

Commit

Permalink
Correctly copy over secret environment variables (#4543)
Browse files Browse the repository at this point in the history
* Correctly copy over secret environment variables

* Changelog

* Run formatter

* Add unit tests
  • Loading branch information
inlined committed May 11, 2022
1 parent 002bcc5 commit e8ea6c9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
@@ -0,0 +1 @@
- Fixes bug where secret environment variables were not set on functions (#4543)
29 changes: 18 additions & 11 deletions src/deploy/functions/runtimes/discovery/v1alpha1.ts
Expand Up @@ -13,7 +13,14 @@ const CHANNEL_NAME_REGEX = new RegExp(
"(?<channel>[A-Za-z\\d\\-_]+)"
);

export type ManifestEndpoint = backend.ServiceConfiguration &
export interface ManifestSecretEnv {
key: string;
secret?: string;
projectId: string;
}

type Base = Omit<backend.ServiceConfiguration, "secretEnvironmentVariables">;
export type ManifestEndpoint = Base &
backend.Triggered &
Partial<backend.HttpsTriggered> &
Partial<backend.CallableTriggered> &
Expand All @@ -24,6 +31,7 @@ export type ManifestEndpoint = backend.ServiceConfiguration &
region?: string[];
entryPoint: string;
platform?: backend.FunctionsPlatform;
secretEnvironmentVariables?: Array<ManifestSecretEnv>;
};

export interface Manifest {
Expand Down Expand Up @@ -249,17 +257,16 @@ function parseEndpoints(
ep,
"secretEnvironmentVariables",
"secretEnvironmentVariables",
(senvs: ManifestEndpoint["secretEnvironmentVariables"]) => {
if (senvs && senvs.length > 0) {
ep.secretEnvironmentVariables = [];
for (const { key, secret } of senvs) {
ep.secretEnvironmentVariables.push({
key,
secret: secret || key, // if secret is undefined, assume env var key == secret name
projectId: project,
});
}
(senvs: Array<ManifestSecretEnv>) => {
const secretEnvironmentVariables: backend.SecretEnvVar[] = [];
for (const { key, secret } of senvs) {
secretEnvironmentVariables.push({
key,
secret: secret || key, // if secret is undefined, assume env var key == secret name
projectId: project,
});
}
return secretEnvironmentVariables;
}
);
allParsed.push(parsed);
Expand Down
19 changes: 18 additions & 1 deletion src/test/deploy/functions/runtimes/discovery/v1alpha1.spec.ts
Expand Up @@ -294,7 +294,10 @@ describe("backendFromV1Alpha1", () => {
}); // Parser errors;

describe("allows valid backends", () => {
const DEFAULTED_ENDPOINT: Omit<backend.Endpoint, "httpsTrigger"> = {
const DEFAULTED_ENDPOINT: Omit<
backend.Endpoint,
"httpsTrigger" | "secretEnvironmentVariables"
> = {
...MIN_ENDPOINT,
platform: "gcfv2",
id: "id",
Expand Down Expand Up @@ -551,6 +554,13 @@ describe("backendFromV1Alpha1", () => {
},
ingressSettings: "ALLOW_INTERNAL_ONLY",
serviceAccountEmail: "sa@",
secretEnvironmentVariables: [
{
key: "SECRET",
secret: "SECRET",
projectId: "project",
},
],
};

const yaml: v1alpha1.Manifest = {
Expand All @@ -560,6 +570,13 @@ describe("backendFromV1Alpha1", () => {
...MIN_ENDPOINT,
httpsTrigger: {},
...fields,
secretEnvironmentVariables: [
{
key: "SECRET",
// Missing "secret"
projectId: "project",
},
],
},
},
};
Expand Down

0 comments on commit e8ea6c9

Please sign in to comment.