Skip to content

Commit 2374af8

Browse files
committed
[SLS] Add workarroud to make functions public.
serverless/serverless-google-cloudfunctions#205 (comment)
1 parent b81cedb commit 2374af8

File tree

4 files changed

+82
-1
lines changed

4 files changed

+82
-1
lines changed

sls/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"eslint": "^7.28.0",
2525
"eslint-config-google": "^0.14.0",
2626
"jsdoc": "^3.6.7",
27-
"pre-commit": "^1.2.2"
27+
"pre-commit": "^1.2.2",
28+
"serverless-plugin-scripts": "^1.0.2"
2829
}
2930
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/bash
2+
3+
# Get a list of functions in the serverless.yml file and format as args
4+
functions=$(sls print --path functions --transform keys --format text 2>/dev/null | xargs)
5+
6+
# Sort functions as public and private
7+
echo "-------------------------------------------------------------------------"
8+
echo "sorting functions as public or private"
9+
echo "-------------------------------------------------------------------------"
10+
pub=()
11+
prv=()
12+
for fn in ${functions[@]}; do
13+
# if the `allowUnauthenticated: true` flag is defined for the function flag it to be made public
14+
if [[ "$(sls print --path functions."$fn" --format yaml 2>/dev/null | xargs)" == *"allowUnauthenticated: true"* ]]; then
15+
pub+=($fn)
16+
else
17+
prv+=($fn)
18+
fi
19+
done
20+
echo "done"
21+
22+
# Run the mkfunc-pub command for each public function
23+
echo "-------------------------------------------------------------------------"
24+
echo "updating public functions"
25+
echo "-------------------------------------------------------------------------"
26+
for fn in ${pub[@]}; do
27+
echo "Making function \""$fn"\" public..."
28+
sls mkfunc-pub --function="$fn"
29+
done
30+
echo "done"
31+
32+
echo "-------------------------------------------------------------------------"
33+
echo "updating private functions"
34+
echo "-------------------------------------------------------------------------"
35+
# Run the mkfunc-pvt command for each private function
36+
for fn in ${prv[@]}; do
37+
echo "Making function \""$fn"\" private..."
38+
sls mkfunc-pvt --function="$fn"
39+
done
40+
echo "done"

sls/serverless.yml

+35
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,64 @@ provider:
2525
frameworkVersion: '3'
2626

2727
plugins:
28+
- serverless-plugin-scripts
2829
- serverless-google-cloudfunctions
2930

3031
# needs more granular excluding in production as only the serverless provider npm
3132
# package should be excluded (and not the whole node_modules directory)
3233
package:
3334
exclude:
3435
- node_modules/**
36+
- scripts/**
3537
- .gitignore
3638
- .git/**
3739

3840
custom:
3941
topicName: ${self:service}-gmail-push
4042
topicResource: projects/${env:GCP_PROJECT}/topics/${self:custom.topicName}
43+
scripts:
44+
# NOTE: uncomment the following if you want the `sls-update-allow-unauthenticated.sh` script to
45+
# run after every deploy. Otherwise just run the script manually.
46+
hooks:
47+
"after:deploy:deploy": ./scripts/sls-update-allow-unauthenticated.sh
48+
commands:
49+
# make the specified function public
50+
mkfunc-pub: gcloud functions add-iam-policy-binding ${self:service}-${self:provider.stage}-${opt:function, ""} --member="allUsers" --role="roles/cloudfunctions.invoker" --project=${self:provider.project} --region=${self:provider.region}
51+
# make the specified function private
52+
mkfunc-pvt: gcloud functions remove-iam-policy-binding ${self:service}-${self:provider.stage}-${opt:function, ""} --member="allUsers" --role="roles/cloudfunctions.invoker" --project=${self:provider.project} --region=${self:provider.region}
4153

4254
functions:
4355
auth_init:
4456
handler: auth_init
4557
events:
4658
- http: true
59+
# TODO: Grant public access
60+
accessControl:
61+
gcpIamPolicy:
62+
bindings:
63+
- role: roles/cloudfunctions.invoker
64+
members:
65+
- "allUsers"
66+
# unofficial flag that ties into the post-deploy script; set to false or omit the key if you
67+
# don't want to make the function public; you will need to run the `sls-update-allow-unauthenticated.sh`
68+
# script to update the function permissions
69+
allowUnauthenticated: true
70+
4771
auth_callback:
4872
handler: auth_callback
4973
events:
5074
- http: true
75+
# TODO: Grant public access
76+
accessControl:
77+
gcpIamPolicy:
78+
bindings:
79+
- role: roles/cloudfunctions.invoker
80+
members:
81+
- "allUsers"
82+
# unofficial flag that ties into the post-deploy script; set to false or omit the key if you
83+
# don't want to make the function public; you will need to run the `sls-update-allow-unauthenticated.sh`
84+
# script to update the function permissions
85+
allowUnauthenticated: true
5186

5287
# NOTE: the following uses an "event" event (pubSub event in this case).
5388
# Please create the corresponding resources in the Google Cloud

sls/yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -2437,6 +2437,11 @@ serverless-google-cloudfunctions@^4.1.0:
24372437
googleapis "^50.0.0"
24382438
lodash "^4.17.21"
24392439

2440+
serverless-plugin-scripts@^1.0.2:
2441+
version "1.0.2"
2442+
resolved "https://registry.yarnpkg.com/serverless-plugin-scripts/-/serverless-plugin-scripts-1.0.2.tgz#21808c3cfd0a1a84e48c0660b0f6f370b5665486"
2443+
integrity sha512-+OL9fFz5r6BXNHfpu9MDLehS/haC0fy/T3V5uJsTfLAnNsn+PzM6BmvefUfWG372hBT7piTbywB1Vl1+4LmI5Q==
2444+
24402445
set-blocking@^2.0.0:
24412446
version "2.0.0"
24422447
resolved "https://registry.yarnpkg.com/set-blocking/-/set-blocking-2.0.0.tgz#045f9782d011ae9a6803ddd382b24392b3d890f7"

0 commit comments

Comments
 (0)