-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup-sa.sh
executable file
·246 lines (214 loc) · 7.2 KB
/
setup-sa.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/bin/bash
# Copyright 2018 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.
set -e
set -u
o=""
p=""
b=""
f=""
usage() {
echo
echo " Usage:"
echo " $0 -o <organization id> -p <project id> [-b <billing account id>] [-f <folder id>]"
echo " organization id (required)"
echo " project id (required)"
echo " billing accout id (optional)"
echo " folder id (optional)"
echo
echo " The billing account id is required if owned by a different organization"
echo " than the Seed Project organization."
echo
exit 1
}
# Check for input variables
while getopts ":ho:p:b:f:" OPT; do
case ${OPT} in
o )
o=$OPTARG
;;
p )
p=$OPTARG
;;
b )
b=$OPTARG
;;
f )
f=$OPTARG
;;
: )
echo
echo " Error: option -${OPTARG} requires an argument"
usage
;;
\? )
echo
echo " Error: invalid option -${OPTARG}"
usage
;;
esac
done
shift $((OPTIND -1))
# Check for required input variables
if [ -z "${o}" ] || [ -z "${p}" ]; then
echo
echo " Error: -o <organization id> and -p <project id> required."
usage
fi
# Organization ID
echo "Verifying organization..."
ORG_ID="$(gcloud organizations list --format="value(ID)" --filter="$o")"
if [[ $ORG_ID == "" ]]; then
echo "The organization id provided does not exist. Exiting."
exit 1;
fi
# Seed Project
echo "Verifying project..."
SEED_PROJECT="$(gcloud projects list --format="value(projectId)" --filter="$p")"
if [[ $SEED_PROJECT == "" ]]; then
echo "The Seed Project does not exist. Exiting."
exit 1;
fi
# Billing account
if [ -z "${b}" ]; then
echo "Skipping billing account verification... (parameter not passed)"
else
echo "Verifying billing account..."
BILLING_ACCOUNT="$(gcloud beta billing accounts list --format="value(ACCOUNT_ID)" --filter="$b")"
if [[ $BILLING_ACCOUNT == "" ]]; then
echo "The billing account does not exist. Exiting."
exit 1;
fi
fi
# Project Folder
if [ -z "${f}" ]; then
echo "Skipping project folder verification... (parameter not passed)"
else
echo "Verifying project folder..."
FOLDER_ID="$(gcloud resource-manager folders list --format="value(ID)" --organization="$o" --filter="$f")"
if [[ $FOLDER_ID == "" ]]; then
echo "The project folder does not exist. Exiting."
exit 1;
fi
fi
# Seed Service Account creation
SA_NAME="project-factory-${RANDOM}"
SA_ID="${SA_NAME}@${SEED_PROJECT}.iam.gserviceaccount.com"
STAGING_DIR="${PWD}"
KEY_FILE="${STAGING_DIR}/credentials.json"
echo "Creating Seed Service Account..."
gcloud iam service-accounts \
--project "${SEED_PROJECT}" create ${SA_NAME} \
--display-name ${SA_NAME}
echo "Downloading key to credentials.json..."
gcloud iam service-accounts keys create "${KEY_FILE}" \
--iam-account "${SA_ID}" \
--user-output-enabled false
if [[ $FOLDER_ID == "" ]]; then
echo "Skipping grant roles on project folder... (parameter not passed)"
else
echo "Applying permissions for folder $FOLDER_ID..."
# Grant roles/resourcemanager.folderViewer to the Seed Service Account on the folder
echo "Adding role roles/resourcemanager.folderViewer..."
gcloud resource-manager folders add-iam-policy-binding \
"${FOLDER_ID}" \
--member="serviceAccount:${SA_ID}" \
--role="roles/resourcemanager.folderViewer" \
--user-output-enabled false
fi
echo "Applying permissions for org $ORG_ID and project $SEED_PROJECT..."
# Grant roles/resourcemanager.organizationViewer to the Seed Service Account on the organization
echo "Adding role roles/resourcemanager.organizationViewer..."
gcloud organizations add-iam-policy-binding \
"${ORG_ID}" \
--member="serviceAccount:${SA_ID}" \
--role="roles/resourcemanager.organizationViewer" \
--user-output-enabled false
# Grant roles/resourcemanager.projectCreator to the service account on the organization
echo "Adding role roles/resourcemanager.projectCreator..."
gcloud organizations add-iam-policy-binding \
"${ORG_ID}" \
--member="serviceAccount:${SA_ID}" \
--role="roles/resourcemanager.projectCreator" \
--user-output-enabled false
# Grant roles/billing.user to the service account on the organization
echo "Adding role roles/billing.user..."
gcloud organizations add-iam-policy-binding \
"${ORG_ID}" \
--member="serviceAccount:${SA_ID}" \
--role="roles/billing.user" \
--user-output-enabled false
# Grant roles/compute.xpnAdmin to the service account on the organization
echo "Adding role roles/compute.xpnAdmin..."
gcloud organizations add-iam-policy-binding \
"${ORG_ID}" \
--member="serviceAccount:${SA_ID}" \
--role="roles/compute.xpnAdmin" \
--user-output-enabled false
# Grant roles/compute.networkAdmin to the service account on the organization
echo "Adding role roles/compute.networkAdmin..."
gcloud organizations add-iam-policy-binding \
"${ORG_ID}" \
--member="serviceAccount:${SA_ID}" \
--role="roles/compute.networkAdmin" \
--user-output-enabled false
# Grant roles/iam.serviceAccountAdmin to the service account on the organization
echo "Adding role roles/iam.serviceAccountAdmin..."
gcloud organizations add-iam-policy-binding \
"${ORG_ID}" \
--member="serviceAccount:${SA_ID}" \
--role="roles/iam.serviceAccountAdmin" \
--user-output-enabled false
# Grant roles/resourcemanager.projectIamAdmin to the Seed Service Account on the Seed Project
echo "Adding role roles/resourcemanager.projectIamAdmin..."
gcloud projects add-iam-policy-binding \
"${SEED_PROJECT}" \
--member="serviceAccount:${SA_ID}" \
--role="roles/resourcemanager.projectIamAdmin" \
--user-output-enabled false
# Enable required API's
echo "Enabling APIs..."
gcloud services enable \
cloudresourcemanager.googleapis.com \
--project "${SEED_PROJECT}"
gcloud services enable \
cloudbilling.googleapis.com \
--project "${SEED_PROJECT}"
gcloud services enable \
iam.googleapis.com \
--project "${SEED_PROJECT}"
gcloud services enable \
admin.googleapis.com \
--project "${SEED_PROJECT}"
gcloud services enable \
appengine.googleapis.com \
--project "${SEED_PROJECT}"
# enable the billing account
if [[ ${BILLING_ACCOUNT:-} != "" ]]; then
echo "Enabling the billing account..."
gcloud beta billing accounts get-iam-policy "$BILLING_ACCOUNT" > policy-tmp-$$.yml
unamestr=$(uname)
if [ "$unamestr" = 'Darwin' ] || [ "$unamestr" = 'Linux' ]; then
sed -i.bak -e "/^etag:.*/i \\
- members:\\
\ \ - serviceAccount:${SA_ID}\\
\ \ role: roles/billing.user" policy-tmp-$$.yml && rm policy-tmp-$$.yml.bak
gcloud beta billing accounts set-iam-policy "$BILLING_ACCOUNT" policy-tmp-$$.yml
else
echo "Could not set roles/billing.user on service account $SERVICE_ACCOUNT.\
Please perform this manually."
fi
rm -f policy-tmp-$$.yml
fi
echo "All done."