Skip to content

Commit aa3a2aa

Browse files
authored
feat: add security_level field for https triggers (#266)
1 parent 3db672b commit aa3a2aa

File tree

4 files changed

+81
-2
lines changed

4 files changed

+81
-2
lines changed

.github/workflows/integration.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,48 @@ jobs:
188188
CLEANUP_FUNCTION_NAME: '${{ steps.deploy.outputs.id }}'
189189
run: 'npm run cleanup'
190190

191+
https_trigger:
192+
if: ${{ github.event_name == 'push' || github.repository == github.event.pull_request.head.repo.full_name && github.actor != 'dependabot[bot]' }}
193+
name: 'https_trigger'
194+
permissions:
195+
contents: 'read'
196+
id-token: 'write'
197+
runs-on: 'ubuntu-latest'
198+
steps:
199+
- uses: 'actions/checkout@v2'
200+
201+
- uses: 'actions/setup-node@v2'
202+
with:
203+
node-version: '12.x'
204+
205+
- name: 'npm build'
206+
run: 'npm ci && npm run build'
207+
208+
- uses: 'google-github-actions/auth@main'
209+
with:
210+
workload_identity_provider: '${{ secrets.WIF_PROVIDER_NAME }}'
211+
service_account: '${{ secrets.DEPLOY_CF_SA_EMAIL }}'
212+
213+
- id: 'deploy'
214+
uses: './'
215+
with:
216+
name: 'https-trigger-${{ github.run_number }}'
217+
runtime: 'nodejs10'
218+
entry_point: 'helloWorld'
219+
source_dir: './tests/test-node-func/'
220+
https_trigger_security_level: 'secure_always'
221+
222+
# Auth as the main account for integration and cleanup
223+
- uses: 'google-github-actions/auth@main'
224+
with:
225+
credentials_json: '${{ secrets.DEPLOY_CF_SA_KEY_JSON }}'
226+
227+
- name: 'cleanup'
228+
if: ${{ always() }}
229+
env:
230+
CLEANUP_FUNCTION_NAME: '${{ steps.deploy.outputs.id }}'
231+
run: 'npm run cleanup'
232+
191233
event_trigger:
192234
if: ${{ github.event_name == 'push' || github.repository == github.event.pull_request.head.repo.full_name && github.actor != 'dependabot[bot]' }}
193235
name: 'event_trigger'

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ jobs:
133133

134134
- `max_instances`: (Optional) The maximum number of instances for the function.
135135

136+
- `https_trigger_security_level`: (Optional) The security level for an
137+
HTTP(s)trigger. If set to `"secure_always"`, the function will only be
138+
accessible over the https protocol. If set to `"secure_optional"`, the
139+
function will be accessible over the http and https protocols. The default
140+
value is `"security_level_unspecified"`, which uses the platform's default
141+
value. We recommend setting this value to `"secure_always"` unless you need
142+
your function to be accessible over a non-TLS connection.
143+
136144
- `event_trigger_type`: (Optional) Specifies which action should trigger the function. Defaults to creation of http trigger.
137145

138146
- `event_trigger_resource`: (Optional) Specifies which resource from eventTrigger is observed.

action.yaml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ inputs:
105105
resource name of a Google Secret Manager secret of the format
106106
"projects/p/secrets/s/versions/v". If the project is omitted, it will be
107107
inferred from the Cloud Function project ID. If the version is omitted, it
108-
will default to "latest"
108+
will default to "latest".
109109
required: false
110110

111111
secret_volumes:
@@ -116,7 +116,7 @@ inputs:
116116
resource name of a Google Secret Manager secret of the format
117117
"projects/p/secrets/s/versions/v". If the project is omitted, it will be
118118
inferred from the Cloud Function project ID. If the version is omitted, it
119-
will default to "latest"
119+
will default to "latest".
120120
required: false
121121

122122
service_account_email:
@@ -140,6 +140,16 @@ inputs:
140140
The maximum number of instances for the function.
141141
required: false
142142

143+
https_trigger_security_level:
144+
description: |-
145+
The security level for an HTTP(s) trigger. If set to "secure_always", the
146+
function will only be accessible over the https protocol. If set to
147+
"secure_optional", the function will be accessible over the http and https
148+
protocols. The default value is "security_level_unspecified", which uses
149+
the platform's default value.
150+
default: "security_level_unspecified"
151+
required: false
152+
143153
event_trigger_type:
144154
description: |-
145155
Specifies which action should trigger the function.

src/main.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ async function run(): Promise<void> {
6666
const timeout = parseDuration(getInput('timeout'));
6767
const maxInstances = presence(getInput('max_instances'));
6868
const minInstances = presence(getInput('min_instances'));
69+
const httpsTriggerSecurityLevel = presence(
70+
getInput('https_trigger_security_level'),
71+
);
6972
const eventTriggerType = presence(getInput('event_trigger_type'));
7073
const eventTriggerResource = presence(getInput('event_trigger_resource'));
7174
const eventTriggerService = presence(getInput('event_trigger_service'));
@@ -111,6 +114,16 @@ async function run(): Promise<void> {
111114
}
112115

113116
// Validation
117+
if (
118+
httpsTriggerSecurityLevel &&
119+
httpsTriggerSecurityLevel.toUpperCase() != 'SECURITY_LEVEL_UNSPECIFIED' &&
120+
eventTriggerType
121+
) {
122+
throw new Error(
123+
`Only one of 'https_trigger_security_level' or 'event_trigger_type' ` +
124+
`may be specified.`,
125+
);
126+
}
114127
if (!sourceDir) {
115128
// Note: this validation will need to go away once we support deploying
116129
// from a docker repo.
@@ -211,6 +224,7 @@ async function run(): Promise<void> {
211224
};
212225

213226
if (eventTriggerType && eventTriggerResource) {
227+
// Set event trigger properties.
214228
cf.eventTrigger = {
215229
eventType: eventTriggerType,
216230
resource: eventTriggerResource,
@@ -233,7 +247,12 @@ async function run(): Promise<void> {
233247
`Event triggered functions must define 'event_trigger_type' and 'event_trigger_resource'`,
234248
);
235249
} else {
250+
// Set https trigger properties.
236251
cf.httpsTrigger = {};
252+
253+
if (httpsTriggerSecurityLevel) {
254+
cf.httpsTrigger.securityLevel = httpsTriggerSecurityLevel.toUpperCase();
255+
}
237256
}
238257

239258
// Deploy the Cloud Function

0 commit comments

Comments
 (0)