Skip to content

Add runtime option to associate secrets to functions #1018

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 19 commits into from
Feb 10, 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 @@
- Add new runtime option for setting secrets.
2 changes: 2 additions & 0 deletions spec/v1/cloud-functions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ describe('makeCloudFunction', () => {
regions: ['us-central1'],
memory: '128MB',
serviceAccount: '[email protected]',
secrets: ['MY_SECRET'],
},
});

Expand All @@ -123,6 +124,7 @@ describe('makeCloudFunction', () => {
},
retry: false,
},
secretEnvironmentVariables: [{ secret: 'MY_SECRET', key: 'MY_SECRET' }],
labels: {},
});
});
Expand Down
36 changes: 35 additions & 1 deletion spec/v1/function-builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,11 +472,45 @@ describe('FunctionBuilder', () => {
).to.throw();
});

it('', () => {
it('should throw an error if private identifier is in the invoker array', () => {
expect(() =>
functions.runWith({
invoker: ['service-account1', 'private', 'service-account2'],
})
).to.throw();
});

it('should allow valid secret config expressed using short form', () => {
const secrets = ['API_KEY'];
const fn = functions
.runWith({ secrets })
.auth.user()
.onCreate((user) => user);

expect(fn.__trigger.secrets).to.deep.equal(secrets);
});

it('should throw error given secrets expressed with full resource name', () => {
expect(() =>
functions.runWith({
secrets: ['projects/my-project/secrets/API_KEY'],
})
).to.throw();
});

it('should throw error given invalid secret config', () => {
expect(() =>
functions.runWith({
secrets: ['ABC/efg'],
})
).to.throw();
});

it('should throw error given invalid secret with versions', () => {
expect(() =>
functions.runWith({
secrets: ['ABC@3'],
})
).to.throw();
});
});
11 changes: 10 additions & 1 deletion src/cloud-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ export interface TriggerAnnotated {
vpcConnectorEgressSettings?: string;
serviceAccountEmail?: string;
ingressSettings?: string;
secrets?: string[];
};
}

Expand Down Expand Up @@ -552,7 +553,8 @@ export function optionsToTrigger(options: DeploymentOptions) {
'ingressSettings',
'vpcConnectorEgressSettings',
'vpcConnector',
'labels'
'labels',
'secrets'
);
convertIfPresent(
trigger,
Expand Down Expand Up @@ -620,6 +622,13 @@ export function optionsToEndpoint(
'serviceAccount',
(sa) => sa
);
convertIfPresent(
endpoint,
options,
'secretEnvironmentVariables',
'secrets',
(secrets) => secrets.map((secret) => ({ secret, key: secret }))
);
if (options?.vpcConnector) {
endpoint.vpc = { connector: options.vpcConnector };
convertIfPresent(
Expand Down
Empty file added src/common/manifest.ts
Empty file.
12 changes: 12 additions & 0 deletions src/function-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,18 @@ function assertRuntimeOptionsValid(runtimeOptions: RuntimeOptions): boolean {
}
}

if (runtimeOptions.secrets !== undefined) {
const invalidSecrets = runtimeOptions.secrets.filter(
(s) => !/^[A-Za-z\d\-_]+$/.test(s)
);
if (invalidSecrets.length > 0) {
throw new Error(
`Invalid secrets: ${invalidSecrets.join(',')}. ` +
'Secret must be configured using the resource id (e.g. API_KEY)'
);
}
}

return true;
}

Expand Down
5 changes: 5 additions & 0 deletions src/function-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ export interface RuntimeOptions {
* Allow requests with invalid App Check tokens on callable functions.
*/
allowInvalidAppCheckToken?: boolean;

/*
* Secrets to bind to a function instance.
*/
secrets?: string[];
}

export interface DeploymentOptions extends RuntimeOptions {
Expand Down
1 change: 1 addition & 0 deletions src/runtime/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface ManifestEndpoint {
labels?: Record<string, string>;
ingressSettings?: string;
environmentVariables?: Record<string, string>;
secretEnvironmentVariables?: { key: string; secret?: string }[];

httpsTrigger?: {
invoker?: string[];
Expand Down