-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
Copy pathsetup.sh
executable file
·67 lines (58 loc) · 2.55 KB
/
setup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/bash
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Precreate script for Cloud Run button
export SECRET_NAME="idp-sql-secrets"
export SERVICE_ACCOUNT="idp-sql-identity"
gcloud config set project $GOOGLE_CLOUD_PROJECT
# Add Identity Platform config
sed -i "s/PROJECT_ID/$GOOGLE_CLOUD_PROJECT/" static/config.js
sed -i "s/API_KEY/$API_KEY/" static/config.js
# Enable Cloud SQl and Secret Manager APIs
gcloud services enable sqladmin.googleapis.com secretmanager.googleapis.com
# Create Cloud SQl instance
gcloud sql instances describe ${CLOUD_SQL_INSTANCE_NAME}
if [ $? -eq 1 ]; then
echo "Create Cloud SQL instance with postgreSQL database (this might take a few minutes)..."
gcloud sql instances create ${CLOUD_SQL_INSTANCE_NAME} \
--database-version=POSTGRES_12 \
--region=${GOOGLE_CLOUD_REGION} \
--cpu=2 \
--memory=7680MB \
--root-password=${DB_PASSWORD}
fi
# Add Cloud SQL config to secret file
sed -i "s/PROJECT_ID/$GOOGLE_CLOUD_PROJECT/" postgres-secrets.json
sed -i "s/REGION/$GOOGLE_CLOUD_REGION/" postgres-secrets.json
sed -i "s/PASSWORD_SECRET/$DB_PASSWORD/" postgres-secrets.json
sed -i "s/INSTANCE/$CLOUD_SQL_INSTANCE_NAME/" postgres-secrets.json
# Add secret file to Secret Manager
gcloud secrets describe ${SECRET_NAME}
if [ $? -eq 1 ]; then
echo "Creating secret ..."
gcloud secrets create ${SECRET_NAME} \
--replication-policy="automatic"
fi
echo "Adding secret version ..."
gcloud secrets versions add ${SECRET_NAME} --data-file=postgres-secrets.json
# Create service account
gcloud iam service-accounts create ${SERVICE_ACCOUNT}
# Allow service account to access secret
gcloud secrets add-iam-policy-binding ${SECRET_NAME} \
--member serviceAccount:${SERVICE_ACCOUNT}@${GOOGLE_CLOUD_PROJECT}.iam.gserviceaccount.com \
--role roles/secretmanager.secretAccessor
# Allow service account to access Cloud SQL
gcloud projects add-iam-policy-binding $GOOGLE_CLOUD_PROJECT \
--member serviceAccount:${SERVICE_ACCOUNT}@${GOOGLE_CLOUD_PROJECT}.iam.gserviceaccount.com \
--role roles/cloudsql.client