This repository has been archived by the owner on Oct 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 126
/
cleanup.sh
executable file
·249 lines (210 loc) · 7.87 KB
/
cleanup.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
247
248
249
#!/bin/bash
# Copyright 2019 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.
show_help() {
cat <<EOF
Usage: ${0##*/} -p PROJECT_ID -o ORG_ID -s SERVICE_ACCOUNT_NAME [-f HOST_PROJECT_ID] [-e]
${0##*/} -h
Clean up resources created by the Forseti setup script.
Options:
-p PROJECT_ID The project ID where Forseti resources will be deleted.
-o ORG_ID The organization ID to remove roles from the Forseti service account.
-s SERVICE_ACCOUNT_NAME The service account to remove from the project and organization IAM roles.
-e Remove additional IAM roles for running the real time policy enforcer.
-k Remove additional IAM roles for running Forseti on-GKE
-q Remove additional IAM roles for using private IPs with Cloud SQL
-g Remove additional IAM roles for using private IPs with the GCE VM's
-f HOST_PROJECT_ID ID of a project holding shared VPC.
Examples:
${0##*/} -p forseti-235k -o 22592784945 -s cloud-foundation-forseti-28047
${0##*/} -p forseti-enforcer-99e4 -o 22592784945 -s cloud-foundation-forseti-28047 -e
EOF
}
PROJECT_ID=""
ORG_ID=""
SERVICE_ACCOUNT_NAME=$FORSETI_SETUP_SERVICE_ACCOUNT_NAME
WITH_ENFORCER=""
HOST_PROJECT_ID=""
ON_GKE=""
SQL_PRIVATE_IP=""
GCE_PRIVATE_IP=""
OPTIND=1
while getopts ":hekqgf:p:o:s:" opt; do
case "$opt" in
h)
show_help
exit 0
;;
e)
WITH_ENFORCER=1
;;
f)
HOST_PROJECT_ID=$OPTARG
;;
p)
PROJECT_ID="$OPTARG"
;;
o)
ORG_ID="$OPTARG"
;;
k)
ON_GKE=1
;;
s)
SERVICE_ACCOUNT_NAME="$OPTARG"
;;
q)
SQL_PRIVATE_IP=1
;;
g)
GCE_PRIVATE_IP=1
;;
*)
echo "Unhandled option: -$opt" >&2
show_help >&2
exit 1
;;
esac
done
if [[ -z "$PROJECT_ID" ]]; then
echo "ERROR: PROJECT_ID must be set."
show_help >&2
exit 1
fi
if [[ -z "$ORG_ID" ]]; then
echo "ERROR: ORG_ID must be set."
show_help >&2
exit 1
fi
if [[ -z "$SERVICE_ACCOUNT_NAME" ]]; then
echo "ERROR: SERVICE_ACCOUNT_NAME must be set."
show_help >&2
exit 1
fi
SERVICE_ACCOUNT_EMAIL="$SERVICE_ACCOUNT_NAME@${PROJECT_ID}.iam.gserviceaccount.com"
KEY_FILE="${PWD}/credentials.json"
# Ensure that we can fetch the IAM policy on the Forseti project.
if ! gcloud projects get-iam-policy "$PROJECT_ID" &> /dev/null; then
echo "ERROR: Unable to fetch IAM policy on project $PROJECT_ID."
exit 1
fi
# Ensure that we can fetch the IAM policy on the GCP organization.
if ! gcloud organizations get-iam-policy "$ORG_ID" &> /dev/null; then
echo "ERROR: Unable to fetch IAM policy on organization $ORG_ID."
exit 1
fi
# Ensure that we can query the service account.
if ! gcloud iam service-accounts describe "$SERVICE_ACCOUNT_EMAIL" &> /dev/null; then
echo "ERROR: Unable to fetch service account $SERVICE_ACCOUNT_EMAIL."
exit 1
fi
echo "Removing permissions from $SERVICE_ACCOUNT_EMAIL on organization $ORG_ID ..."
gcloud organizations remove-iam-policy-binding "${ORG_ID}" \
--member="serviceAccount:${SERVICE_ACCOUNT_EMAIL}" \
--role="roles/resourcemanager.organizationAdmin" \
--user-output-enabled false
gcloud organizations remove-iam-policy-binding "${ORG_ID}" \
--member="serviceAccount:${SERVICE_ACCOUNT_EMAIL}" \
--role="roles/iam.securityReviewer" \
--user-output-enabled false
echo "Removing permissions from $SERVICE_ACCOUNT_EMAIL on project $PROJECT_ID ..."
gcloud projects remove-iam-policy-binding "${PROJECT_ID}" \
--member="serviceAccount:${SERVICE_ACCOUNT_EMAIL}" \
--role="roles/compute.instanceAdmin" \
--user-output-enabled false
gcloud projects remove-iam-policy-binding "${PROJECT_ID}" \
--member="serviceAccount:${SERVICE_ACCOUNT_EMAIL}" \
--role="roles/compute.networkViewer" \
--user-output-enabled false
gcloud projects remove-iam-policy-binding "${PROJECT_ID}" \
--member="serviceAccount:${SERVICE_ACCOUNT_EMAIL}" \
--role="roles/compute.securityAdmin" \
--user-output-enabled false
gcloud projects remove-iam-policy-binding "${PROJECT_ID}" \
--member="serviceAccount:${SERVICE_ACCOUNT_EMAIL}" \
--role="roles/serviceusage.serviceUsageAdmin" \
--user-output-enabled false
gcloud projects remove-iam-policy-binding "${PROJECT_ID}" \
--member="serviceAccount:${SERVICE_ACCOUNT_EMAIL}" \
--role="roles/iam.serviceAccountAdmin" \
--user-output-enabled false
gcloud projects remove-iam-policy-binding "${PROJECT_ID}" \
--member="serviceAccount:${SERVICE_ACCOUNT_EMAIL}" \
--role="roles/iam.serviceAccountUser" \
--user-output-enabled false
gcloud projects remove-iam-policy-binding "${PROJECT_ID}" \
--member="serviceAccount:${SERVICE_ACCOUNT_EMAIL}" \
--role="roles/storage.admin" \
--user-output-enabled false
if [[ -n "$SQL_PRIVATE_IP" ]]; then
echo "Removing roles to allow Private IPs with Cloud SQL on project ${PROJECT_ID}..."
gcloud projects remove-iam-policy-binding "${PROJECT_ID}" \
--member="serviceAccount:${SERVICE_ACCOUNT_EMAIL}" \
--role="roles/compute.networkAdmin" \
--user-output-enabled false
fi
if [[ -n "$GCE_PRIVATE_IP" ]]; then
echo "Granting roles to allow Private IPs with GCE VM's on project ${PROJECT_ID}..."
gcloud projects remove-iam-policy-binding "${PROJECT_ID}" \
--member="serviceAccount:${SERVICE_ACCOUNT_EMAIL}" \
--role="roles/compute.networkAdmin" \
--user-output-enabled false
fi
if [[ -n "$WITH_ENFORCER" ]]; then
org_roles=("roles/logging.configWriter" "roles/iam.organizationRoleAdmin")
project_roles=("roles/pubsub.admin")
echo "Revoking real time policy enforcer roles on organization $ORG_ID..."
for org_role in "${org_roles[@]}"; do
gcloud organizations add-iam-policy-binding "${ORG_ID}" \
--member="serviceAccount:${SERVICE_ACCOUNT_EMAIL}" \
--role="$org_role" \
--user-output-enabled false
done
echo "Granting real time policy enforcer roles on project $PROJECT_ID..."
for project_role in "${project_roles[@]}"; do
gcloud projects add-iam-policy-binding "${PROJECT_ID}" \
--member="serviceAccount:${SERVICE_ACCOUNT_EMAIL}" \
--role="$project_role" \
--user-output-enabled false
done
fi
gcloud projects remove-iam-policy-binding "${PROJECT_ID}" \
--member="serviceAccount:${SERVICE_ACCOUNT_EMAIL}" \
--role="roles/cloudsql.admin" \
--user-output-enabled false
if [[ -n "$ON_GKE" ]]; then
gke_roles=("roles/container.admin" "roles/compute.networkAdmin" "roles/resourcemanager.projectIamAdmin")
echo "Removing on-GKE related roles on project $PROJECT_ID..."
for gke_role in "${gke_roles[@]}"; do
gcloud projects remove-iam-policy-binding "${PROJECT_ID}" \
--member="serviceAccount:${SERVICE_ACCOUNT_EMAIL}" \
--role="$gke_role" \
--user-output-enabled false
done
fi
if [[ $HOST_PROJECT_ID != "" ]];
then
gcloud projects remove-iam-policy-binding "${HOST_PROJECT_ID}" \
--member="serviceAccount:${SERVICE_ACCOUNT_EMAIL}" \
--role="roles/compute.securityAdmin" \
--user-output-enabled false
gcloud projects remove-iam-policy-binding "${HOST_PROJECT_ID}" \
--member="serviceAccount:${SERVICE_ACCOUNT_EMAIL}" \
--role="roles/compute.networkAdmin" \
--user-output-enabled false
fi
gcloud iam service-accounts delete "${SERVICE_ACCOUNT_EMAIL}" \
--quiet
rm -rf "$KEY_FILE"
echo "All done."