-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathinitial_setup.sh
220 lines (187 loc) · 7.27 KB
/
initial_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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/env bash
# all this is intended to run within cloud shell bash
usage() { echo "Usage: bash $0 [-e GITHUB_EMAIL] [-u GITHUB_USERNAME] [-p PROJECT_ID] [-s SERVICE_ACCOUNT_NAME] [-g GCP_USERNAME] [-b GITHUB_BRANCH_NAME] | \
Example: bash $0 -e example@gmail.com -u user_123 -p ferrous-weaver-256122 -s demo-service-account -g gcp_signup_name_3 -b master" 1>&2; exit 1;}
while getopts ":e:u:p:s:g:b:" o; do
case "${o}" in
e)
e=${OPTARG}
;;
u)
u=${OPTARG}
;;
p)
p=${OPTARG}
;;
s)
s=${OPTARG}
;;
g)
g=${OPTARG}
;;
b)
b=${OPTARG}
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
if [ -z "${e}" ] || [ -z "${u}" ] || [ -z "${p}" ] || [ -z "${s}" ] || [ -z "${g}" ] || [ -z "${b}" ]; then
usage
fi
# set command line arguments
GITHUB_EMAIL=${e}
GITHUB_USERNAME=${u}
PROJECT_ID=${p}
SERVICE_ACCOUNT_NAME=${s}
GCP_USERNAME=${g}
GITHUB_BRANCH_NAME=${b}
echo "GITHUB_EMAIL = $GITHUB_EMAIL"
echo "GITHUB_USERNAME = $GITHUB_USERNAME"
echo "PROJECT_ID = $PROJECT_ID"
echo "SERVICE_ACCOUNT_NAME = $SERVICE_ACCOUNT_NAME"
echo "GCP_USERNAME = $GCP_USERNAME"
echo "GITHUB_BRANCH_NAME = $GITHUB_BRANCH_NAME"
echo "***********************"
echo "Setup git configs for authorship"
echo "***********************"
git config --global user.email $GITHUB_EMAIL
git config --global user.name $GITHUB_USERNAME
echo "***********************"
echo "Create versioned buckets for tfstate and encrypted service account json private key"
echo "***********************"
gsutil mb gs://$PROJECT_ID-secure-bucket-tfstate
gsutil mb gs://$PROJECT_ID-secure-bucket-secrets
gsutil versioning set on gs://$PROJECT_ID-secure-bucket-tfstate
gsutil versioning set on gs://$PROJECT_ID-secure-bucket-secrets
echo "***********************"
echo "Create a service account"
echo "***********************"
gcloud iam service-accounts create $SERVICE_ACCOUNT_NAME \
--description "service account used to launch terraform locally" \
--display-name $SERVICE_ACCOUNT_NAME
echo "***********************"
echo "Wait for the service account to be created"
echo "***********************"
sleep 10s
echo "***********************"
echo "List service account to verify creation and capture email"
echo "***********************"
SERVICE_ACCOUNT_EMAIL=$(gcloud iam service-accounts list --filter=$SERVICE_ACCOUNT_NAME | grep -v "^NAME" | head -n 1 | awk '{print $2}')
echo "***********************"
echo "Enable newly created service account based on what's listed"
echo "***********************"
gcloud beta iam service-accounts enable $SERVICE_ACCOUNT_EMAIL
echo "***********************"
echo "Add editor encryptor, and security admin role, so it has permissions to launch many kinds of terraform resources"
echo "***********************"
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member serviceAccount:$SERVICE_ACCOUNT_EMAIL \
--role roles/editor
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member serviceAccount:$SERVICE_ACCOUNT_EMAIL \
--role roles/cloudkms.cryptoKeyEncrypter
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member serviceAccount:$SERVICE_ACCOUNT_EMAIL \
--role roles/iam.securityAdmin
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member serviceAccount:$SERVICE_ACCOUNT_EMAIL \
--role roles/cloudkms.admin
echo "***********************"
echo "Check if roles updated"
echo "Note: may not be accurate even though console shows the update"
echo "***********************"
gcloud iam service-accounts get-iam-policy $SERVICE_ACCOUNT_EMAIL
echo "***********************"
echo "Download the service account key into the local directory"
echo "***********************"
gcloud iam service-accounts keys create ~/iot-python-webapp/service_account.json \
--iam-account $SERVICE_ACCOUNT_EMAIL
echo "***********************"
echo "Enable apis for first_build to work properly for a NEW project"
echo "These are redundant to what Terraform implements"
echo "The majority of these will be removed once terraform updates module dependencies"
echo "***********************"
gcloud services enable \
cloudbuild.googleapis.com \
cloudkms.googleapis.com \
compute.googleapis.com \
iam.googleapis.com \
cloudresourcemanager.googleapis.com \
dataflow.googleapis.com \
bigtableadmin.googleapis.com \
bigquery.googleapis.com \
bigquery-json.googleapis.com \
cloudfunctions.googleapis.com \
pubsub.googleapis.com \
storage-component.googleapis.com \
run.googleapis.com \
cloudiot.googleapis.com \
bigtable.googleapis.com
echo "***********************"
echo "Retrieve cloud build service account email"
echo "***********************"
CLOUDBUILD_SA="$(gcloud projects describe $PROJECT_ID \
--format 'value(projectNumber)')@cloudbuild.gserviceaccount.com"
echo "***********************"
echo "Add roles to the cloud build service account that mimics the demo service account"
echo "***********************"
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member serviceAccount:$CLOUDBUILD_SA \
--role roles/editor
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member serviceAccount:$CLOUDBUILD_SA \
--role roles/cloudkms.cryptoKeyEncrypterDecrypter
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member serviceAccount:$CLOUDBUILD_SA \
--role roles/iam.securityAdmin
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member serviceAccount:$CLOUDBUILD_SA \
--role roles/cloudkms.admin
echo "***********************"
echo "Create kms keyring and key for service account json file and then encrypt it into ciphertext"
echo "***********************"
keyring_name=$PROJECT_ID-keyring
key_name=$PROJECT_ID-key
gcloud kms keyrings create $keyring_name \
--location=global
gcloud kms keys create $key_name \
--purpose=encryption \
--location=global \
--keyring=$keyring_name \
--protection-level=software
gcloud kms encrypt \
--key=$key_name \
--keyring=$keyring_name \
--location=global \
--plaintext-file=service_account.json \
--ciphertext-file=ciphertext_file.enc
echo "***********************"
echo "Copy encrypted file to cloud storage bucket"
echo "***********************"
gsutil cp ciphertext_file.enc gs://$PROJECT_ID-secure-bucket-secrets
echo "***********************"
echo "Create terraform.tfvars file based on passed in parameters"
echo "***********************"
printf "project = "\"$PROJECT_ID\""\nservice_account_email = "\"$SERVICE_ACCOUNT_EMAIL\""\nstartup_script_username = "\"$GCP_USERNAME\""\ngithub_owner = "\"$GITHUB_USERNAME\""\ngithub_branch_name = "\"$GITHUB_BRANCH_NAME\""\n" > ./tf_modules/terraform.tfvars
echo "***********************"
echo "Create the terraform backend.tf storage bucket config file"
echo "***********************"
printf "terraform {\n backend "\"gcs\"" {\n bucket = "\"$PROJECT_ID-secure-bucket-tfstate\""\n }\n}\n" > ./tf_modules/backend.tf
echo "***********************"
echo "Validate the secrets file is uploaded into bucket and expected changes in terraform config files"
echo "***********************"
echo "secrets file location..."
echo " "
gsutil ls -r gs://$PROJECT_ID-secure-bucket-secrets/
echo "***********************"
echo "terraform.tfvars contents..."
echo " "
cat ./tf_modules/terraform.tfvars
echo "***********************"
echo "backend.tf contents..."
echo " "
cat ./tf_modules/backend.tf
echo "***********************"