-
Notifications
You must be signed in to change notification settings - Fork 839
/
Copy pathlib.sh
executable file
·658 lines (577 loc) · 22 KB
/
lib.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
#!/usr/bin/env bash
#
# Copyright 2019 The Kubernetes Authors.
#
# 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.
# This is a library of functions used to create GCP stuff.
. "$(dirname "${BASH_SOURCE[0]}")/lib_util.sh"
. "$(dirname "${BASH_SOURCE[0]}")/lib_gcr.sh"
. "$(dirname "${BASH_SOURCE[0]}")/lib_gcs.sh"
# The group that admins all GCR repos.
GCR_ADMINS="k8s-infra-artifact-admins@kubernetes.io"
# The group that admins all GCS buckets.
# We use the same group as GCR
GCS_ADMINS=$GCR_ADMINS
# The service account name for the image promoter.
PROMOTER_SVCACCT="k8s-infra-gcr-promoter"
# The service account name for the image promoter's vulnerability check.
PROMOTER_VULN_SCANNING_SVCACCT="k8s-infra-gcr-vuln-scanning"
# The service account name for the k8s vulnerability dashboard.
VULN_DASHBOARD_SVCACCT="k8s-infra-gcr-vuln-dashboard"
# The service account name for the GCR auditor (Cloud Run runtime service
# account).
AUDITOR_SVCACCT="k8s-infra-gcr-auditor"
# This is a separate service account tied to the Pub/Sub subscription that connects
# GCR Pub/Sub messages to the Cloud Run instance of the GCR auditor.
AUDITOR_INVOKER_SVCACCT="k8s-infra-gcr-auditor-invoker"
# This is the Cloud Run service name of the auditor.
AUDITOR_SERVICE_NAME="cip-auditor"
# TODO: decommission this once we've flipped to prow-build-trusted
# The service account email for Prow (not in this org for now).
PROW_SVCACCT="deployer@k8s-prow.iam.gserviceaccount.com"
# The service account email used by prow-build-trusted to trigger GCB and push to GCS
GCB_BUILDER_SVCACCT="gcb-builder@k8s-infra-prow-build-trusted.iam.gserviceaccount.com"
# The GCP org stuff needed to turn it all on.
GCP_ORG="758905017065" # kubernetes.io
GCP_BILLING="018801-93540E-22A20E"
# Release Engineering umbrella groups
# - admins - edit and KMS access (Release Engineering subproject owners)
# - managers - access to run stage/release jobs (Patch Release Team / Branch Managers)
# - viewers - view access to Release Engineering projects (Release Manager Associates)
RELEASE_ADMINS="k8s-infra-release-admins@kubernetes.io"
RELEASE_MANAGERS="k8s-infra-release-editors@kubernetes.io"
RELEASE_VIEWERS="k8s-infra-release-viewers@kubernetes.io"
# Get the service account email for a given short name
# $1: The GCP project
# $2: The name
function svc_acct_email() {
if [ $# != 2 -o -z "$1" -o -z "$2" ]; then
echo "svc_acct_email(project, name) requires 2 arguments" >&2
return 1
fi
local project="$1"
local name="$2"
echo "${name}@${project}.iam.gserviceaccount.com"
}
# Get the cloud build service account email for a given project
# ref: https://cloud.google.com/cloud-build/docs/securing-builds/configure-access-control#service_account
# $1 The GCP project
function gcb_service_account_email() {
if [ $# != 1 -o -z "$1" ]; then
echo "gcb_service_account_email(project) requires 1 argument" >&2
return 1
fi
local project="$1"
gcloud projects get-iam-policy "${project}" \
--flatten="bindings[].members"\
--filter="bindings.role:roles/cloudbuild.builds.builder AND bindings.members ~ serviceAccount:[0-9]+@cloudbuild" \
--format="value(bindings.members)" |\
sed -e 's/^serviceAccount://'
}
# Ensure that a project exists in our org and has fundamental configurations as
# we want them (e.g. billing).
# $1: The GCP project
function ensure_project() {
if [ $# != 1 -o -z "$1" ]; then
echo "ensure_project(project) requires 1 argument" >&2
return 1
fi
local project="$1"
if ! gcloud projects describe "${project}" >/dev/null 2>&1; then
gcloud projects create "${project}" \
--no-enable-cloud-apis \
--organization "${GCP_ORG}"
else
local org=$(gcloud projects \
describe "${project}" \
--flatten='parent[]' \
--format='csv[no-heading](type, id)' \
| grep ^organization \
| cut -f2 -d,)
if [ "$org" != "${GCP_ORG}" ]; then
echo "project ${project} exists, but not in our org: got ${org}" >&2
return 2
fi
fi
gcloud beta billing projects link "${project}" \
--billing-account "${GCP_BILLING}"
}
# Enable an API
# $1: The GCP project
# $2: The API (e.g. containerregistry.googleapis.com)
function enable_api() {
if [ $# != 2 -o -z "$1" -o -z "$2" ]; then
echo "enable_api(project, api) requires 2 arguments" >&2
return 1
fi
local project="$1"
local api="$2"
gcloud --project "${project}" services enable "${api}"
}
# Grant project viewer privileges to a principal
# $1: The GCP project
# $2: The group email
function empower_group_as_viewer() {
if [ $# -lt 2 -o -z "$1" -o -z "$2" ]; then
echo "empower_group_as_viewer(project, group) requires 2 arguments" >&2
return 1
fi
local project="$1"
local group="$2"
gcloud \
projects add-iam-policy-binding "${project}" \
--member "group:${group}" \
--role roles/viewer
}
# Grant roles for running cip-auditor E2E test
# (https://github.com/kubernetes-sigs/k8s-container-image-promoter/tree/master/test-e2e#cip-auditor-cip-auditor-e2ego).
# $1: The GCP project
# $2: The service account
function empower_service_account_for_cip_auditor_e2e_tester() {
if [ $# -lt 2 -o -z "$1" -o -z "$2" ]; then
echo "empower_service_account_for_cip_auditor_e2e_tester(acct, project) requires 2 arguments" >&2
return 1
fi
local acct="$1"
local project="$2"
local roles=(
roles/errorreporting.admin
roles/logging.admin
roles/pubsub.admin
roles/resourcemanager.projectIamAdmin
roles/run.admin
roles/serverless.serviceAgent
roles/storage.admin
)
for role in "${roles[@]}"; do
gcloud \
projects add-iam-policy-binding "${project}" \
--member "serviceAccount:${acct}" \
--role "${role}"
done
}
# Grant roles for running pull-cip-vuln
# $1: The service account
# $2: The GCP Project
function empower_service_account_for_cip_vuln_scanning() {
if [ $# -lt 2 -o -z "$1" -o -z "$2" ]; then
echo "empower_service_account_for_cip_vuln_scanning(acct, project) requires 2 arguments" >&2
return 1
fi
local acct="$1"
local project="$2"
gcloud \
projects add-iam-policy-binding "${project}" \
--member "serviceAccount:${acct}" \
--role roles/containeranalysis.occurrences.viewer
}
# Grant GCB admin privileges to a principal
# $1: The GCP project
# $2: The group email
function empower_group_for_gcb() {
if [ $# -lt 2 -o -z "$1" -o -z "$2" ]; then
echo "empower_group_for_gcb(project, group) requires 2 arguments" >&2
return 1
fi
local project="$1"
local group="$2"
gcloud \
projects add-iam-policy-binding "${project}" \
--member "group:${group}" \
--role roles/cloudbuild.builds.editor
# TODO(justaugustus/thockin): This only exists to grant the
# serviceusage.services.use permission allow writers access to execute
# Cloud Builds. We should refactor this once we develop custom roles.
#
# Ref: https://cloud.google.com/storage/docs/access-control/iam-console
gcloud \
projects add-iam-policy-binding "${project}" \
--member "group:${group}" \
--role roles/serviceusage.serviceUsageConsumer
}
# Grant KMS admin privileges to a principal
# $1: The GCP project
# $2: The group email
function empower_group_for_kms() {
if [ $# -lt 2 -o -z "$1" -o -z "$2" ]; then
echo "empower_group_for_kms(project, group) requires 2 arguments" >&2
return 1
fi
local project="$1"
local group="$2"
gcloud \
projects add-iam-policy-binding "${project}" \
--member "group:${group}" \
--role roles/cloudkms.admin
gcloud \
projects add-iam-policy-binding "${project}" \
--member "group:${group}" \
--role roles/cloudkms.cryptoKeyEncrypterDecrypter
}
# Grant privileges to prow in a staging project
# $1: The GCP project
# $2: The GCS scratch bucket
function empower_prow() {
if [ $# -lt 2 -o -z "$1" -o -z "$2" ]; then
echo "empower_prow(project, bucket) requires 2 arguments" >&2
return 1
fi
local project="$1"
local bucket="$2"
# commands are copy-pasted so that one set can turn into deletes
# when we're ready to decommission PROW_SVCACCT
# Allow prow to trigger builds.
gcloud \
projects add-iam-policy-binding "${project}" \
--member "serviceAccount:${PROW_SVCACCT}" \
--role roles/cloudbuild.builds.builder
gcloud \
projects add-iam-policy-binding "${project}" \
--member "serviceAccount:${GCB_BUILDER_SVCACCT}" \
--role roles/cloudbuild.builds.builder
# Allow prow to push source and access build logs.
gsutil iam ch \
"serviceAccount:${PROW_SVCACCT}:objectCreator" \
"${bucket}"
gsutil iam ch \
"serviceAccount:${PROW_SVCACCT}:objectViewer" \
"${bucket}"
gsutil iam ch \
"serviceAccount:${GCB_BUILDER_SVCACCT}:objectCreator" \
"${bucket}"
gsutil iam ch \
"serviceAccount:${GCB_BUILDER_SVCACCT}:objectViewer" \
"${bucket}"
}
# Grant full privileges to GCR admins
# $1: The GCP project
# $2: The GCR region (optional)
function empower_gcr_admins() {
if [ $# -lt 1 -o $# -gt 2 -o -z "$1" ]; then
echo "empower_gcr_admins(project, [region]) requires 1 or 2 arguments" >&2
return 1
fi
local project="$1"
local region="${2:-}"
local bucket=$(gcs_bucket_for_gcr "${project}" "${region}")
empower_group_as_viewer "${project}" "${GCR_ADMINS}"
empower_group_to_admin_gcs_bucket "${GCR_ADMINS}" "${bucket}"
}
# Grant full privileges to GCS admins
# $1: The GCP project
# $2: The bucket
function empower_gcs_admins() {
if [ $# -lt 2 -o -z "$1" -o -z "$2" ]; then
echo "empower_gcs_admins(project, bucket) requires 2 arguments" >&2
return 1
fi
local project="${1}"
local bucket="${2}"
empower_group_as_viewer "${project}" "${GCS_ADMINS}"
empower_group_to_admin_gcs_bucket "${GCS_ADMINS}" "${bucket}"
}
# Grant Cloud Run privileges to a group.
# $1: The GCP project
# $2: The googlegroups group
function empower_group_to_admin_artifact_auditor() {
if [ $# != 2 ]; then
echo "empower_group_to_admin_artifact_auditor(project, group_name) requires 2 arguments" >&2
return 1
fi
local project="$1"
local group="$2"
local acct=$(svc_acct_email "${project}" "${AUDITOR_SVCACCT}")
# Grant privileges to deploy the auditor Cloud Run service. See
# https://cloud.google.com/run/docs/reference/iam/roles#additional-configuration.
gcloud \
projects add-iam-policy-binding "${project}" \
--member "group:${group}" \
--role roles/run.admin
# To read auditor's logs, we need serviceusage.services.use
gcloud \
projects add-iam-policy-binding "${project}" \
--member "group:${group}" \
--role roles/serviceusage.serviceUsageConsumer
gcloud \
--project="${project}" \
iam service-accounts add-iam-policy-binding \
"${acct}" \
--member="group:${group}" \
--role="roles/iam.serviceAccountUser"
# Also grant privileges to resolve Stackdriver Error Reporting errors.
gcloud \
projects add-iam-policy-binding "${project}" \
--member "group:${group}" \
--role roles/errorreporting.user
}
# Grant full privileges to the GCR promoter bot
# $1: The GCP project
# $2: The GCR region (optional)
function empower_artifact_promoter() {
if [ $# -lt 1 -o $# -gt 2 -o -z "$1" ]; then
echo "empower_artifact_promoter(project, [region]) requires 1 or 2 arguments" >&2
return 1
fi
local project="$1"
local region="${2:-}"
local acct=$(svc_acct_email "${project}" "${PROMOTER_SVCACCT}")
if ! gcloud --project "${project}" iam service-accounts describe "${acct}" >/dev/null 2>&1; then
gcloud --project "${project}" \
iam service-accounts create \
"${PROMOTER_SVCACCT}" \
--display-name="k8s-infra container image promoter"
fi
empower_svcacct_to_admin_gcr "${acct}" "${project}" "${region}"
}
# Ensure the auditor service account exists and has the ability to write logs and fire alerts to Stackdriver Error Reporting.
# $1: The GCP project
function empower_artifact_auditor() {
if [ $# -lt 1 -o -z "$1" ]; then
echo "empower_artifact_auditor(project) requires 1 argument" >&2
return 1
fi
local project="$1"
local acct=$(svc_acct_email "${project}" "${AUDITOR_SVCACCT}")
if ! gcloud --project "${project}" iam service-accounts describe "${acct}" >/dev/null 2>&1; then
gcloud --project "${project}" \
iam service-accounts create \
"${AUDITOR_SVCACCT}" \
--display-name="k8s-infra container image auditor"
fi
# Allow auditor to write logs.
gcloud \
projects add-iam-policy-binding "${project}" \
--member "serviceAccount:${acct}" \
--role roles/logging.logWriter
# Allow auditor to write Stackdriver Error Reporting alerts.
gcloud \
projects add-iam-policy-binding "${project}" \
--member "serviceAccount:${acct}" \
--role roles/errorreporting.writer
# No other permissions are necessary because the cip-auditor process in the
# Cloud Run instance running as this service account will read the
# production GCR which is already world-readable.
}
# Ensure the artifact auditor invoker service account exists and has the ability
# to invoke the auditor service. The auditor invoker service account is tied to
# the Pub/Sub subscription that triggers the Cloud Run instance (the
# subscription getting its messages from the GCR topic "gcr", where changes to
# GCR are posted).
# $1: The GCP project
function empower_artifact_auditor_invoker() {
if [ $# -lt 1 -o -z "$1" ]; then
echo "empower_artifact_auditor_invoker(project) requires 1 argument" >&2
return 1
fi
local project="$1"
local acct=$(svc_acct_email "${project}" "${AUDITOR_INVOKER_SVCACCT}")
if ! gcloud --project "${project}" iam service-accounts describe "${acct}" >/dev/null 2>&1; then
gcloud --project "${project}" \
iam service-accounts create \
"${AUDITOR_INVOKER_SVCACCT}" \
--display-name="k8s-infra container image auditor invoker"
fi
# Allow it to invoke the specific auditor Cloud Run service.
gcloud \
run \
services \
add-iam-policy-binding \
"${AUDITOR_SERVICE_NAME}" \
--member="serviceAccount:${acct}" \
--role=roles/run.invoker \
--platform=managed \
--project="${project}" \
--region=us-central1
}
# Create a service account
# $1: The GCP project
# $2: The account name (e.g. "foo-manager")
# $3: The account display-name (e.g. "Manages all foo")
function ensure_service_account() {
if [ $# != 3 -o -z "$1" -o -z "$2" -o -z "$3" ]; then
echo "ensure_service_account(project, name, display_name) requires 3 arguments" >&2
return 1
fi
local project="$1"
local name="$2"
local display_name="$3"
local acct=$(svc_acct_email "${project}" "${name}")
if ! gcloud --project "${project}" iam service-accounts describe "${acct}" >/dev/null 2>&1; then
gcloud --project "${project}" \
iam service-accounts create \
"${name}" \
--display-name="${display_name}"
fi
}
# Ensure that DNS managed zone exists, creating one if need.
# $1 The GCP project
# $2 The managed zone name (e.g. kubernetes-io)
# $3 The DNS zone name (e.g. kubernetes.io)
function ensure_dns_zone() {
if [ $# != 3 -o -z "$1" -o -z "$2" -o -z "$3" ]; then
echo "ensure_dns_zone(project, zone_name, dns_name) requires 3 arguments" >&2
return 1
fi
local project="$1"
local zone_name="$2"
local dns_name="$3"
if ! gcloud --project "${project}" dns managed-zones describe "${zone_name}" >/dev/null 2>&1; then
gcloud --project "${project}" \
dns managed-zone create \
"${zone_name}" \
--dns-name "${dns_name}"
fi
}
# Allow a Kubernetes service account (KSA) the ability to authenticate as the as
# the given GCP service account for the given GKE Project's Kubernetes
# namespace; this is also called Workload Identity.
#
# $1: The service account scoped to a (1) GKE project, (2) K8s namespace, and
# (3) K8s service account. The format is currently
#
# "${gke_project}.svc.id.goog[${k8s_namespace}/${k8s_svcacct}]"
#
# This is used to scope the grant of permissions to the combination of the
# above 3 variables
# $2: The GCP project that owns the GCP service account.
# $3: The GCP service account to draw powers from.
function empower_ksa_to_svcacct() {
if [ ! $# -eq 3 -o -z "$1" -o -z "$2" -o -z "$3" ]; then
echo "empower_ksa_to_svcacct(ksa_scope, gcp_project, gcp_scvacct) requires 3 arguments" >&2
return 1
fi
local ksa_scope="$1"
local gcp_project="$2"
local gcp_svcacct="$3"
gcloud iam service-accounts add-iam-policy-binding \
--role roles/iam.workloadIdentityUser \
--member "serviceAccount:${ksa_scope}" \
"${gcp_svcacct}" \
--project="${gcp_project}"
}
# Ensure that a global ip address exists, creating one if needed
# $1 The GCP project
# $2 The address name (e.g. foo-ingress), IPv6 addresses must have a "-v6" suffix
# $3 The address description (e.g. "IP address for the foo GCLB")
function ensure_global_address() {
if [ ! $# -eq 3 -o -z "$1" -o -z "$2" -o -z "$3" ]; then
echo "ensure_global_address(gcp_project, name, description) requires 3 arguments" >&2
return 1
fi
local gcp_project="$1"
local name="$2"
local description="$3"
local ip_version="IPV4"
local re='[a-zA-Z0-9_.-]+-v6$'
if [[ $name =~ $re ]]; then
local ip_version="IPV6"
fi
if ! gcloud --project "${gcp_project}" compute addresses describe "${name}" --global >/dev/null 2>&1; then
gcloud --project "${gcp_project}" \
compute addresses create \
"${name}" \
--description="${description}" \
--ip-version="${ip_version}" \
--global
fi
}
# Ensure that a regional ip address exists, creating one if needed
# $1 The GCP project
# $2 The region (e.g. us-central1)
# $3 The address name (e.g. foo-ingress)
# $4 The address description (e.g. "IP address for the foo GCLB")
function ensure_regional_address() {
if [ ! $# -eq 4 -o -z "$1" -o -z "$2" -o -z "$3" -o -z "$4" ]; then
echo "ensure_regional_address(gcp_project, region, name, description) requires 4 arguments" >&2
return 1
fi
local gcp_project="$1"
local region="$2"
local name="$3"
local description="$4"
if ! gcloud --project "${gcp_project}" compute addresses describe "${name}" --region="${region}" >/dev/null 2>&1; then
gcloud --project "${gcp_project}" \
compute addresses create \
"${name}" \
--description="${description}" \
--region="${region}"
fi
}
# Ensure that custom IAM role exists, creating one if needed
# Arguments:
# $1: The GCP project
# $2: The role name (e.g. "ServiceAccountLister")
# $3: The role title (e.g. "Service Account Lister")
# $4: The role description (e.g. "Can list ServiceAccounts.")
# $5+: The role permissions (e.g. "iam.serviceAccounts.list")
# Example usage:
# ensure_custom_iam_role \
# kubernetes-public \
# ServiceAccountLister \
# "Service Account Lister" \
# "Can list ServiceAccounts." \
# iam.serviceAccounts.list
function ensure_custom_iam_role() {
if [ $# -lt 5 ] || [ -z "${1}" ] || [ -z "${2}" ] || [ -z "${3}" ] \
|| [ -z "${4}" ] || [ -z "${5}" ]
then
echo -n "ensure_custom_iam_role(gcp_project, name, title," >&2
echo " description, permission...) requires at least 5 arguments" >&2
return 1
fi
local gcp_project="${1}"; shift
local name="${1}"; shift
local title="${1}"; shift
local description="${1}"; shift
local permissions; permissions=$(join_by , "$@")
if ! gcloud --project "${gcp_project}" iam roles describe "${name}" \
>/dev/null 2>&1
then
gcloud --project "${gcp_project}" --quiet \
iam roles create "${name}" \
--title "${title}" \
--description "${description}" \
--stage GA \
--permissions "${permissions}"
fi
}
# Ensure that custom IAM role exists and is in sync with definition in file
# Arguments:
# $1: The role name (e.g. "prow.viewer")
# $2: The file (e.g. "/path/to/file.yaml")
function ensure_custom_org_role_from_file() {
if [ ! $# -eq 2 -o -z "$1" -o -z "$2" ]; then
echo "ensure_custom_org_role_from_file(name, file) requires 2 arguments" >&2
return 1
fi
local org="${GCP_ORG}"
local name="${1}"
local file="${2}"
if ! gcloud iam roles describe "${name}" --organization "${org}" \
>/dev/null 2>&1
then
# be noisy when creating a role
gcloud iam roles create "${name}" --organization "${org}" --file "${file}"
else
# be quiet when updating, only output name of role
gcloud iam roles update "${name}" --organization "${org}" --file "${file}" | grep ^name:
fi
}
function custom_org_role_name() {
if [ ! $# -eq 1 -o -z "$1" ]; then
echo "custom_org_role_name(name) requires 1 arguments" >&2
return 1
fi
local name="${1}"
echo "organizations/${GCP_ORG}/roles/${name}"
}