Skip to content

Commit 5a67bbb

Browse files
committed
use PubSub JWT auth for security instead of the hardcoded static key
1 parent 3f31550 commit 5a67bbb

File tree

9 files changed

+51
-33
lines changed

9 files changed

+51
-33
lines changed

cron_empty.yaml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
1-
# Documented in Google App Engine https://cloud.google.com/appengine/docs/standard/python3/scheduling-jobs-with-cron-yaml
2-
cron:
3-
- description: "Invoke /schedule which sends out messages triggering do_label per project/plugin."
4-
url: /schedule
5-
schedule: every day 10:00
6-
target: iris3
1+
cron:

deploy.sh

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,12 @@ while getopts 'cepoh' opt; do
5959
to do both; this is equivalent to -p -o
6060
Flags:
6161
-o: Deploy org-level elements like Log Sink
62-
-p: Deploy project-level elements. Org-level elements are still required.
63-
The default if neither -o or -p are given is to enable both.
62+
-p: Deploy project-level elements.
63+
Org-level elements are a pre-requisite.
64+
This is useful for redeploying to the same project, e.g., to change config.
65+
If you want to deploy to a *different* project,
66+
then you have to deploy the org-level elements.
67+
The default, if neither -o or -p are given, is to enable both.
6468
-c: Label on Cloud Scheduler cron to add labels
6569
-e: Label on-creation-event.
6670
The default if neither -c or -e are given is to enable both.
@@ -104,7 +108,7 @@ gcloud projects describe "$PROJECT_ID" >/dev/null|| {
104108
exit 1
105109
}
106110

107-
echo "Project ID $PROJECT_ID"
111+
#echo "Project ID $PROJECT_ID"
108112
gcloud config set project "$PROJECT_ID"
109113

110114

main.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,12 @@ def __send_pubsub_per_projectplugin(configured_projects):
174174
@app.route("/label_one", methods=["POST"])
175175
def label_one():
176176
"""Message received from PubSub when the log sink detects a new resource"""
177+
logging.info("label_one called")
178+
increment_invocation_count("label_one")
177179
ok = __check_pubsub_jwt()
178180
if not ok:
179181
return "JWT Failed", 400
180182

181-
increment_invocation_count("label_one")
182183
with gae_memory_logging("label_one"):
183184

184185
plugins_found = []
@@ -234,6 +235,10 @@ def label_one():
234235

235236
def __check_pubsub_jwt():
236237
try:
238+
#TODO the sample https://github.com/GoogleCloudPlatform/python-docs-samples/blob/ff4c1d55bb5b6995c63383469535604002dc9ba2/appengine/standard_python3/pubsub/main.py#L69
239+
# has this. I am not sure why or how the token arg gets there.
240+
# if request.args.get("token", "") != current_app.config["PUBSUB_VERIFICATION_TOKEN"]:
241+
# return "Invalid request", 400
237242
bearer_token = flask.request.headers.get("Authorization")
238243
token = bearer_token.split(" ")[1]
239244

@@ -319,11 +324,12 @@ def do_label():
319324
"""Receives a push message from PubSub, sent from schedule() above,
320325
and labels all objects of a given plugin and project_id.
321326
"""
322-
327+
increment_invocation_count("do_label")
328+
logging.info("do_label called")
323329
ok = __check_pubsub_jwt()
324330
if not ok:
325331
return "JWT Failed", 400
326-
increment_invocation_count("do_label")
332+
327333
with gae_memory_logging("do_label"):
328334

329335
project_id = "" # set up variables to allow logging in Exception block at end

scripts/_deploy-org.sh

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ set -e
3737

3838
if [[ "$role_error" != "0" ]]; then
3939
echo "Error in accessing organization.
40-
Please either run ./deploy.sh -p to depoly only project-level elements
41-
(generally what you need for an incremental upgrade)
42-
or run it this script using a role that has organization permissions to
43-
deploy organization-level elements like roles or Log Sink."
40+
If you just want to redeploy to the same project,
41+
e.g., to upgrade the config, and you have the necessary
42+
project role but not the necessary org role,
43+
please run ./deploy.sh -p .
44+
Or get yourself the org-level role as documented in README."
4445
exit $role_error
4546
fi
4647

scripts/_deploy-project.sh

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env bash
22
#
3-
# Deploys Iris to Google App Engine, setting up Sinks, Topics, and Subscriptions as needed.
3+
# Deploys Iris to Google App Engine,
4+
# first setting up Sinks, Topics, Subscriptions, and Role Bindings as needed.
45
# Usage
56
# - Called from deploy.sh
67
# - Pass the project as the first command line argument.
@@ -102,7 +103,13 @@ PUBSUB_SERVICE_ACCOUNT="service-${project_number}@gcp-sa-pubsub.iam.gserviceacco
102103

103104
msg_sender_sa_name=iris-msg-sender
104105

105-
gcloud iam service-accounts create --project $PROJECT_ID $msg_sender_sa_name ||true
106+
set +e
107+
gcloud iam service-accounts describe ${msg_sender_sa_name}@${PROJECT_ID}.iam.gserviceaccount.com --project $PROJECT_ID
108+
if [[ $? -ne 0 ]]; then
109+
set -e
110+
gcloud iam service-accounts create --project $PROJECT_ID $msg_sender_sa_name
111+
fi
112+
set -e
106113

107114
MSGSENDER_SERVICE_ACCOUNT=${msg_sender_sa_name}@${PROJECT_ID}.iam.gserviceaccount.com
108115

@@ -129,7 +136,6 @@ if [[ $? -ne 0 && $BINDING_ERR_OUTPUT == *"gcp-sa-pubsub.iam.gserviceaccount.co
129136
--role="roles/pubsub.publisher" --project $PROJECT_ID >/dev/null
130137

131138
fi
132-
133139
set -e
134140

135141
# Create PubSub subscription receiving commands from the /schedule handler that is triggered from cron
@@ -197,7 +203,8 @@ else
197203
--max-retry-delay=$MAX_RETRY \
198204
--quiet >/dev/null
199205
else
200-
gcloud pubsub subscriptions create "$LABEL_ONE_SUBSCRIPTION" --topic "$LOGS_TOPIC" --project="$PROJECT_ID" \
206+
gcloud pubsub subscriptions create "$LABEL_ONE_SUBSCRIPTION" \
207+
--topic "$LOGS_TOPIC" --project="$PROJECT_ID" \
201208
--push-endpoint="$LABEL_ONE_SUBSCRIPTION_ENDPOINT" \
202209
--push-auth-service-account $MSGSENDER_SERVICE_ACCOUNT \
203210
--ack-deadline=$ACK_DEADLINE \
@@ -214,6 +221,8 @@ else
214221
--role="roles/pubsub.subscriber" --project $PROJECT_ID >/dev/null
215222
fi
216223

224+
225+
217226
if [[ "$LABEL_ON_CRON" == "true" ]]; then
218227
cp cron_full.yaml cron.yaml
219228
else

test_scripts/integration_test.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,6 @@ def create_and_describe_resources(test_project, run_id, gce_zone):
208208
create_resources(test_project, run_id, gce_zone)
209209
# Next line necessary to let the labels be visible for PubSub topics and subscriptions.
210210
# Could speed it up by repeatedly checking for the labels
211-
# up to 5 times, sleeping 2 sec in between each check.
212211
time.sleep(30)
213212
describe_resources(test_project, run_id, gce_zone)
214213

@@ -295,7 +294,7 @@ def pause_for_user_input():
295294
print("Type ENTER to proceed to clean up resources ")
296295
_ = sys.stdin.readline()
297296

298-
# pause_for_user_input()# Use this in debugging, to keep the test resources alive until you hit E
297+
#pause_for_user_input()# Use this in debugging, to keep the test resources alive until you hit E
299298

300299
remove_config_file()
301300
commands = [

uninstall.sh

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,22 @@ while getopts 'poh' opt; do
5656
cat <<EOF
5757
Usage uninstall.sh PROJECT_ID
5858
Argument:
59-
The project to which Iris was deployed
59+
The project to which Iris was deployed
6060
Options, to be given before project ID.
6161
If neither -p nor -o is given, the default behavior is used:
62-
Both are uninstalled; equivalent to -p -o
62+
both are uninstalled; this is equivalent to giving both -p -o
6363
Flags:
6464
-p: Uninstall project-level elements of Iris.
6565
This is useful if you deployed Iris to two projects
6666
in an org and want to delete it on one of those.
67+
Warning: If you delete the project-level elements,
68+
and the log sink still exists in the org, you will get
69+
error-message emails from Google (since the log sink
70+
no longer has a topic to send to)
6771
-o: Uninstall org-level elements like Log Sink
6872
Environment variable:
6973
IRIS_CUSTOM_ROLE (Optional, default is iris3) An identifier for
70-
the Iris custom role that you want to delete.
74+
the Iris custom role that was used, if non-default.
7175
EOF
7276
exit 1
7377
;;
@@ -95,7 +99,7 @@ gcloud projects describe "$PROJECT_ID" >/dev/null`` || {
9599
exit 1
96100
}
97101

98-
echo "Project ID $PROJECT_ID"
102+
#echo "Project ID $PROJECT_ID"
99103
gcloud config set project "$PROJECT_ID"
100104

101105

uninstall_scripts/_uninstall-for-org.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ gcloud organizations remove-iam-policy-binding "$ORGID" --all \
2222
--member "serviceAccount:$PROJECT_ID@appspot.gserviceaccount.com" \
2323
--role "organizations/$ORGID/roles/$IRIS_CUSTOM_ROLE" >/dev/null|| true
2424

25-
gcloud iam roles delete -q "$IRIS_CUSTOM_ROLE" --organization "$ORGID" >/dev/null || true
25+
# Just leave the role; it causes too much trouble in its "soft delete" state
26+
#gcloud iam roles delete -q "$IRIS_CUSTOM_ROLE" --organization "$ORGID" >/dev/null || true
2627

2728
if gcloud logging sinks describe --organization="$ORGID" "$LOG_SINK" >&/dev/null; then
2829
svcaccount=$(gcloud logging sinks describe --organization="$ORGID" "$LOG_SINK" |

uninstall_scripts/_uninstall-for-project.sh

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,12 @@ gcloud pubsub subscriptions remove-iam-policy-binding $DO_LABEL_SUBSCRIPTION \
2828
--role="roles/pubsub.subscriber" --project $PROJECT_ID || true
2929

3030
gcloud pubsub subscriptions remove-iam-policy-binding $LABEL_ONE_SUBSCRIPTION \
31-
--member="serviceAccount:$PUBSUB_SERVICE_ACCOUNT"\
32-
--role="roles/pubsub.subscriber" --project $PROJECT_ID ||true
33-
34-
gcloud projects remove-iam-policy-binding --project ${PROJECT_ID} \
35-
--member="serviceAccount:${MSGSENDER_SERVICE_ACCOUNT}"\
36-
--role='roles/iam.serviceAccountTokenCreator'
31+
--member="serviceAccount:$PUBSUB_SERVICE_ACCOUNT"\
32+
--role="roles/pubsub.subscriber" --project $PROJECT_ID ||true
3733

34+
gcloud projects remove-iam-policy-binding ${PROJECT_ID} \
35+
--member="serviceAccount:${MSGSENDER_SERVICE_ACCOUNT}"\
36+
--role='roles/iam.serviceAccountTokenCreator' ||true
3837

3938
gcloud pubsub subscriptions delete $DEADLETTER_SUB --project="$PROJECT_ID" -q || true
4039
gcloud pubsub subscriptions delete "$DO_LABEL_SUBSCRIPTION" -q --project="$PROJECT_ID" ||true

0 commit comments

Comments
 (0)