This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
ahr-sa-ctl
executable file
·974 lines (717 loc) · 25.6 KB
/
ahr-sa-ctl
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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
#!/usr/bin/env bash
# Copyright 2020-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.
# ahsam apigee hybrid service account manager
# Parameters:
# $1 ACTION create|delete|config
# $2 Role
# TODO: [ ] refactor all commands to lists of sas and profiles
# TODO: [ ] check for SA commands to work with suffixes
# TODO: [ ] check if rebind is really needed
# TODO: [ ] verify sa token: Apigee ORG verification: check
# TODO: [ ] refactor help to sub-command helps
# TODO: [ ] refactor help and auto-complete
set -e
BASEDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
. $BASEDIR/ahr-lib.sh
# check required variables
check_envvars "PROJECT SA_DIR"
# options
pps=""
while(($#)); do
case "$1" in
-f|--force)
FORCE=Y
shift;;
--profiles)
PROFILES=$2
shift 2;;
-s|--suffix)
SUFFIX=$2
shift 2;;
-n|--name)
NAME=$2
shift 2;;
--sa_dir)
SA_DIR=$2
shift 2;;
-k|--force-key-file)
FORCE_KEY_FILE=Y
shift;;
-y|--dry-run)
DRY_RUN=Y
shift;;
--filter)
FILTER="$2"
shift 2;;
-d|--debug)
DEBUG=Y
shift;;
*)
pps="$pps $1"
shift;;
esac
done
eval set -- "$pps"
#
# Apigee Hybrid role and permissions layout
#
# https://cloud.google.com/iam/docs/understanding-roles#apigee-roles
#
# Profile processing euristics:
# * if sa_id is found in profile, use this profile only
# * if sa_id is prefix of a profile name, use this profile
# * otherwise, use --profiles value
# * if --profiles is missing, report an error
# 1.5 APIGEE_SA_ALL="apigee-synchronizer,apigee-mart,apigee-udca,apigee-logger,apigee-metrics,apigee-watcher,apigee-cassandra,apigee-distributed-trace"
# 1.6
APIGEE_SA_ALL="apigee-synchronizer,apigee-mart,apigee-udca,apigee-logger,apigee-metrics,apigee-watcher,apigee-cassandra,apigee-runtime"
APIGEE_ASM_ROLES='roles/gkehub.connect'
APIGEE_ASM_PERMS='"gkehub.endpoints.connect"'
APIGEE_ASM_NAME='ASM Service Account'
APIGEE_SYNCHRONIZER_ROLES='roles/apigee.synchronizerManager'
APIGEE_SYNCHRONIZER_PERMS='"apigee.environments.get","apigee.environments.manageRuntime"'
APIGEE_SYNCHRONIZER_NAME='Synchronizer Manager Service Account'
APIGEE_MART_ROLES='roles/apigeeconnect.Agent'
APIGEE_MART_PERMS='"apigeeconnect.endpoints.connect"'
APIGEE_MART_NAME='MART Service Account'
APIGEE_UDCA_ROLES='roles/apigee.analyticsAgent'
APIGEE_UDCA_PERMS='"apigee.environments.getDataLocation"'
APIGEE_UDCA_NAME='UDCA Service Account'
APIGEE_LOGGER_ROLES='roles/logging.logWriter'
APIGEE_LOGGER_PERMS='"logging.logEntries.create"'
APIGEE_LOGGER_NAME='Logs Writer Service Account'
APIGEE_METRICS_ROLES='roles/monitoring.metricWriter'
APIGEE_METRICS_PERMS='"monitoring.metricDescriptors.create","monitoring.metricDescriptors.get","monitoring.metricDescriptors.list","monitoring.monitoredResourceDescriptors.get","monitoring.monitoredResourceDescriptors.list","monitoring.timeSeries.create"'
APIGEE_METRICS_NAME='Metrics Writer Service Account'
APIGEE_WATCHER_ROLES='roles/apigee.runtimeAgent'
APIGEE_WATCHER_PERMS='"apigee.canaryevaluations.create","apigee.canaryevaluations.get","apigee.ingressconfigs.get","apigee.instances.reportStatus","apigee.operations.get","apigee.operations.list"'
APIGEE_WATCHER_NAME='Apigee Runtime Agent Service Account'
APIGEE_CASSANDRA_ROLES='roles/storage.objectAdmin'
APIGEE_CASSANDRA_PERMS='"resourcemanager.projects.get","resourcemanager.projects.list","storage.objects.*"'
APIGEE_CASSANDRA_NAME='Cassandra Backups Service Account'
# 1.5
APIGEE_DISTRIBUTED_TRACE_ROLES='roles/cloudtrace.agent'
APIGEE_DISTRIBUTED_TRACE_PERMS='"cloudtrace.traces.patch"'
APIGEE_DISTRIBUTED_TRACE_NAME='Distributed Trace Service Account'
# 1.6
APIGEE_RUNTIME_ROLES='roles/cloudtrace.agent'
APIGEE_RUNTIME_PERMS='"cloudtrace.traces.patch"'
APIGEE_RUNTIME_NAME='Runtime Service Account'
function show_help(){
echo "Generate or remove collection of Apigee Hybrid Service Account roles"
echo ""
echo " ahr-sa-ctl create|config|delete|rebind \$PROJECT_ID"
echo ""
echo " <sa-id> -- all or [comma-separated combination of] apigee-synchronizer,apigee-mart,apigee-watcher,apigee-udca,apigee-logger,apigee-metrics,apigee-cassandra,apigee-distributed-trace,apigee-runtime"
echo " Note: 1.5: apigee-distributed-trace; 1.6: apigee-runtime"
echo ""
echo ""
echo " ahr-sa-ctl create-sa all|<sa-id> [--force]"
echo " ahr-sa-ctl create-sa <sa-id> --role-profiles <role,role> [--force]"
echo " "
echo " --role-profiles is a comma-separated list of apigee profiles."
echo " if absent, it defaults to the name of the sa-id"
echo ""
echo " ahr-sa-ctl create-key all|<sa-id> [--force-key-file]"
echo " ahr-sa-ctl config all|apigee-udca|apigee-synchronizer|apigee-mart|apigee-watcher|apigee-cassandra <sa-id>"
echo " ahr-sa-ctl config apigee-udca apigee-udca1"
echo " ahr-sa-ctl validate-key <sa-id> <key-json-file>"
echo " ahr-sa-ctl delete all|apigee-synchronizer|apigee-udca|apigee-mart|apigee-logger|apigee-metrics|apigee-watcher|apigee-cassandra|apigee-distributed-trace"
echo " ahr-sa-ctl sa-key-expires-at all|apigee-synchronizer|apigee-udca|apigee-mart|apigee-logger|apigee-metrics|apigee-watcher|apigee-cassandra|apigee-distributed-trace"
echo ""
echo "The key .json files will be generated in a current directory."
echo "Utility relies on your current credentials being active for gcloud."
echo ""
}
if [ "$#" -lt 1 ]; then
show_help
exit 5
fi
action=$1
function get(){
local sa_id=$1
local property=$2
local value=$(echo -n "${sa_id}${property}"|awk '{gsub(/-/, "_");print toupper($0)}')
echo "${!value}"
}
function get_sa_email() {
local PROJECT=$1
local SA_ID=$2
echo "${SA_ID}@${PROJECT}.iam.gserviceaccount.com"
}
function get_sa_key_properties() {
local KEY=$1
# get private key id
local SA_KEY_ID=$(echo "$KEY" | jq --raw-output .private_key_id)
# get client email
local SA_CLIENT_EMAIL=$(echo "$KEY" | jq --raw-output .client_email)
local SA_ID="${SA_CLIENT_EMAIL%%@*}"
# get expiry date
local EXPIRES_AT=$(gcloud iam service-accounts keys list --iam-account $SA_CLIENT_EMAIL --format='table[no-heading](EXPIRES_AT)' --filter="KEY_ID:$SA_KEY_ID")
let DTE=(`date +%s -d "$EXPIRES_AT"`-`date +%s`)/86400 # Days To Expiry
echo "$SA_CLIENT_EMAIL" "$SA_ID" "$SA_KEY_ID" "$EXPIRES_AT" "$DTE"
}
# SA_ID: process role(s)
# SA_ID name of the SA
# SA_PROFILES roles to add
function create_sa() {
local PROJECT=$1
local SA_ID=$2
local SA_PROFILES=$3
local SA_NAME=$4
if [ "$SA_PROFILES" = "" ]; then
SA_PROFILES="$SA_ID"
fi
if [ "$SA_NAME" = "" ]; then
local NAMES=""
for profile in $SA_PROFILES; do
if [ "$NAMES" = "" ]; then
NAMES="$(get $profile "_NAME")"
else
NAMES="$NAMES; $(get $profile "_NAME")"
fi
done
SA_NAME="$NAMES"
fi
local SA_EMAIL=$(get_sa_email "${PROJECT}" "${SA_ID}")
if [ "$(gcloud iam service-accounts list --format="value(name)" --filter="name:$SA_EMAIL" | wc -l)" -eq 1 ]; then
if [ "$FORCE" = "Y" ]; then
delete_sa "$PROJECT" "$SA_ID"
FORCE_KEY_FILE=Y
fi
fi
set +e
gcloud iam service-accounts create ${SA_ID} --display-name "${SA_NAME}"
set -e
for profile in $SA_PROFILES; do
local SA_ROLE=$(get $profile "_ROLES")
if [ "$SA_ROLE" != "" ]; then
echo " Binding serviceAccount:$SA_EMAIL to role: $SA_ROLE"
gcloud projects add-iam-policy-binding ${PROJECT} --member serviceAccount:$SA_EMAIL --role ${SA_ROLE} > /dev/null
fi
done
}
function create_key() {
local PROJECT=$1
local SA_ID=$2
local FORCE_KEY_FILE=$3
local SA_EMAIL=$(get_sa_email "${PROJECT}" "${SA_ID}")
local SA_KEY_JSON=${SA_DIR}/${PROJECT}-${SA_ID}.json
# generate key json;
# check if present;
# skip if exists if option is present
if [ ! -f "$SA_KEY_JSON" ] || [ "$FORCE_KEY_FILE" = "Y" ]; then
gcloud iam service-accounts keys create ${SA_KEY_JSON} --iam-account $SA_EMAIL
else
echo "ERROR: JSON file exists: $SA_KEY_JSON. Skipping. Use --force-key-file to override"
fi
}
function sa_exists(){
local PROJECT="$1"
local SA_ID="$2"
local SA_EMAIL=$(get_sa_email "${PROJECT}" "${SA_ID}")
set +e
gcloud iam service-accounts describe $SA_EMAIL >/dev/null 2>&1
}
#
function view_sa() {
local PROJECT=$1
local SA_ID=$2
local SA_EMAIL=$3
local SA_KEY_JSON=$4
local SA_PERMISSIONS="$(get $SA_ID '_PERMS')"
echo "Service Account: $SA_EMAIL"
gcloud projects get-iam-policy $PROJECT --flatten="bindings[].members" --format='table(bindings.role)' --filter="bindings.members:$SA_EMAIL"
# sa token
local satoken=$(export GOOGLE_APPLICATION_CREDENTIALS=$SA_KEY_JSON; gcloud auth application-default print-access-token)
echo "GCP Project Permission:"
local response=$(curl --silent -X POST -H "Authorization: Bearer $satoken" -H "Content-Type:application/json" https://cloudresourcemanager.googleapis.com/v1/projects/$PROJECT:testIamPermissions --data-binary @- <<EOF
{
"permissions": [
$SA_PERMISSIONS
]
}
EOF
)
echo $response
if [ $( grep -c error <<< "$response" ) -ne 0 ]; then
exit 1
fi
if [[ ! ";apigee-logger;apigee-mart;apigee-metrics;" =~ ";$SA_ID;" ]]; then
echo "Apigee Hybrid Org/Env Permission:"
response=$(curl --silent -X POST -H "Authorization: Bearer $satoken" -H "Content-Type:application/json" https://apigee.googleapis.com/v1/organizations/$ORG/environments/$ENV:testIamPermissions --data-binary @- <<EOF
{
"permissions": [
$SA_PERMISSIONS
]
}
EOF
)
echo $response
if [ $( grep -c error <<< "$response" ) -ne 0 ]; then
exit 1
fi
fi
}
#
function view_sa_by_id(){
local PROJECT=$1
local SA_ID=$2
local SA_EMAIL=$(get_sa_email "${PROJECT}" "${SA_ID}")
local SA_KEY_JSON=${SA_DIR}/${PROJECT}-${SA_ID}.json
view_sa "$PROJECT" "$SA_ID" "$SA_EMAIL" "$SA_KEY_JSON"
}
#
function view_sa_by_key_file(){
local PROJECT=$1
local SA_ID=$2
local SA_KEY_JSON=$3
local SA_EMAIL=$(cat $SA_KEY_JSON|jq --raw-output .client_email)
local SA_ID_CUST=${SA_EMAIL%@*}
view_sa "$PROJECT" "$SA_ID" "$SA_EMAIL" "$SA_KEY_JSON"
}
#
function rebind_sa(){
local PROJECT=$1
local SA_ID=$2
local SA_PROFILES=$3
local SA_EMAIL="$(get_sa_email $PROJECT $SA_ID)"
local SA_ROLE=$(get $COMP "_ROLES")
for profile in $SA_PROFILES; do
local SA_ROLE=$(get $profile "_ROLES")
set +e
echo " Removing Binding serviceAccount:$SA_EMAIL to role: $SA_ROLE"
gcloud projects remove-iam-policy-binding $PROJECT --member serviceAccount:$SA_EMAIL --role $SA_ROLE > /dev/null
set -e
echo " Removing Binding serviceAccount:$SA_EMAIL to role: $SA_ROLE"
gcloud projects add-iam-policy-binding $PROJECT --member serviceAccount:$SA_EMAIL --role $SA_ROLE > /dev/null
done
}
function delete_sa() {
local PROJECT=$1
local SA_ID=$2
local SA_EMAIL="$(get_sa_email $PROJECT $SA_ID)"
local SA_KEY_JSON=${SA_DIR}/${PROJECT}-${SA_ID}.json
# Check if exists. If exists, delete it
if [ "$(gcloud iam service-accounts list --format="value(name)" --filter="name:$SA_EMAIL" | wc -l)" -eq 1 ]; then
gcloud iam service-accounts delete $SA_EMAIL --quiet
# TODO: [ ] Should we really remove it?
# rm -f $SA_KEY_JSON
else
echo "ahr-sa-ctl: WARNING delete: SA doesn't exist $SA_ID"
fi
}
#'-------------------
function sa_keys_report() {
local CRD=$1
local A=$2
local SECRET_NAME=$3
local KEY=$(kubectl get secret -n apigee $SECRET_NAME -o json | jq --raw-output '.data."client_secret.json"' | base64 -d)
if [ "$KEY" ]; then
read SA_EMAIL SA_ID SA_KEY_ID SA_KEY_EXPIRES_AT DTE < <(get_sa_key_properties "$KEY")
echo "$CRD $A $SECRET_NAME $SA_ID $SA_KEY_ID $DTE $SA_KEY_EXPIRES_AT"
else
echo "$CRD $A - - - - -"
fi
}
function sa_key_iterator() {
fiterator=$1
report="KIND RESOURCE SECRET_NAME SA_ID SA_KEY_ID DTE EXPIRES_AT"$'\n'
# 1.4.x
#
# Iterate for all SA Key containing deployments
#
printf "Processing:"
# ORG SAs
for sa_id in apigee-connect-agent apigee-mart apigee-watcher; do
AD=$sa_id-$(get_org_env_sha "$ORG" "")
SECRET_NAME=$AD-svc-account
printf " $ORG:AD/$sa_id"
report=$report$($fiterator "AD" "$AD" "$SECRET_NAME")$'\n'
done
# iterate on ENV SAs
for env in $(ahr-runtime-ctl env-list|jq -r '.[]'); do
for sa_id in apigee-synchronizer apigee-udca apigee-runtime; do
AD=$sa_id-$(get_org_env_sha "$ORG" "$env")
SECRET_NAME=$AD-svc-account
printf " $env:AD/$sa_id"
if kubectl get secret -n apigee "$SECRET_NAME" > /dev/null 2>&1; then
report=$report$($fiterator "AD" "$AD" "$SECRET_NAME")$'\n'
else
report=$report"AD $AD - - - - -"$'\n'
fi
done
done
# TELEMETRY SAs
SECRET_NAME=apigee-metrics-svc
for AD in apigee-metrics-apigee-telemetry-app apigee-metrics-apigee-telemetry-proxy; do
printf " AD/$AD"
report=$report$($fiterator "AD" "$AD" "$SECRET_NAME")$'\n'
done
SECRET_NAME=apigee-logger-svc
for AT in apigee-telemetry; do # TODO: [ ] add logger
printf " AT/$AT"
report=$report$($fiterator "AT" "$AT" "$SECRET_NAME")$'\n'
done
printf "\n"
echo "$report" | column -t
}
#
# all --suffix '-dev'
#
# apigee-mart # defaults are populating roles and name
# apigee-master --profiles all --name 'Uber SA'
# apigee-mart-dev --profiles apigee-mart --name 'Mart for Dev'
#
if [ "$action" = "create-sa" ]; then
SA_IDS="$2"
if [ "$SA_IDS" = "" ]; then
echo "Missing a Service Account ID(s) or all"
echo ""
show_help
exit 1
fi
# PROFILES and NAME are passed via options and are optional
# for all, --suffix is checked
if [ "$SA_IDS" = "all" ]; then
SA_IDS="$APIGEE_SA_ALL"
fi
# iterate through SA_ID list
IFS=","
for SA_ID in $SA_IDS; do
if [ "$PROFILES" ]; then
sa_profiles="$PROFILES"
else
sa_profiles="$SA_ID"
fi
create_sa "${PROJECT}" "$SA_ID$SUFFIX" "$sa_profiles" "$NAME"
done
#---
elif [ "$action" = "create-key" ]; then
function show_create_key_help(){
echo ""
echo "Example: ahr-sa-ctl create-key apigee-logger,apigee-mart|all --sa-dir=$(pwd) --force-key"
}
if [ ! "$SA_DIR" ]; then
echo "ERROR: SA_DIR is not configured. Either set up $SA_DIR env variable or use --sa_dir <dir> option."
fi
check_envvars "PROJECT"
check_commands "gcloud kubectl jq"
echo "ahr-sa-ctl: INFO Service account keys directory: $SA_DIR"
SA_IDS=$2
if [ "$SA_IDS" = "" ]; then
echo "Missing Service Account ID(s) or all"
show_create_key_help
exit 1
fi
if [ "$SA_IDS" = "all" ]; then
SA_IDS="$APIGEE_SA_ALL"
fi
IFS=","
for sa_id in $SA_IDS; do
create_key "${PROJECT}" "$sa_id" "$FORCE_KEY_FILE"
done
#---
elif [ "$action" = "sa-keys-list" ]; then
function show_sa_keys_list_help(){
echo ""
echo "Example: ahr-sa-ctl sa-keys-list all"
}
SA_IDS=$2
if [ "$SA_IDS" = "" ]; then
echo "Missing Service Account ID(s) or all"
show_sa_keys_list_help
exit 1
fi
if [ "$SA_IDS" = "all" ]; then
SA_IDS="$APIGEE_SA_ALL"
fi
IFS=","
for sa_id in $SA_IDS; do
sa_email=$(get_sa_email $PROJECT $sa_id)
echo "Keys for SA: $sa_email"
gcloud iam service-accounts keys list --managed-by=user --iam-account $sa_email
done
#---
elif [ "$action" = "sa-keys-delete" ]; then
SA_IDS=$2
if [ "$SA_IDS" = "" ]; then
echo "Missing Service Account ID(s) or all"
echo ""
exit 1
fi
if [ "$SA_IDS" = "all" ]; then
SA_IDS="$APIGEE_SA_ALL"
fi
if [ "$FILTER" = "" ]; then
echo "--filter=<field><comp><iso-date> option must be provided. Example:"
echo ' ahr-sa-ctl sa-keys-delete all --dry-run --filter "EXPIRES_AT<$(date -Ins)"'
echo ' ahr-sa-ctl sa-keys-delete all --dry-run --filter "EXPIRES_AT<2021-07-07T23:59'
exit 2
fi
check_envvars "PROJECT"
check_commands "gcloud"
check_gnu_date
IFS=","
for sa_id in $SA_IDS; do
sa_email=$(get_sa_email $PROJECT $sa_id)
echo "Deleting Keys for SA: $sa_email with --filter: $FILTER"
if [ "$DRY_RUN" ]; then
echo "DRY_RUN Mode for SA: $sa_email"
gcloud iam service-accounts keys list --iam-account $sa_email --managed-by=user --filter="$FILTER"
else
gcloud iam service-accounts keys list --iam-account $sa_email --managed-by=user --format='value(KEY_ID)' --filter="$FILTER" | while read -r key_id; do
gcloud iam service-accounts keys delete $key_id --iam-account=$sa_email --quiet
done
fi
done
#'----
elif [ "$action" = "view-sa" ]; then
SA_IDS=$2
if [ "$SA_IDS" = "" ]; then
echo "Missing List of SA IDs"
echo ""
show_help
exit 1
fi
if [ "$SA_IDS" = "all" ]; then
SA_IDS="$APIGEE_SA_ALL"
fi
IFS=","
for sa_id in $SA_IDS; do
if sa_exists $PROJECT $sa_id ; then
view_sa_by_id $PROJECT "$sa_id"
else
echo "WARNING: SA does not exist: $sa_id"
fi
done
#---
elif [ "$action" = "validate-key-file" ]; then
function show_validate_key_file_help(){
echo ""
echo "Example: ahr-sa-ctl validate-key apigee-logger-key.json --profiles apigee-logger"
}
KEY_FILE=$2
if [ "$KEY_FILE" = "" ]; then
echo "Missing Key File: $KEY_FILE"
show_validate_key_file_help
exit 1
fi
if [ ! -f "$KEY_FILE" ]; then
echo "ERROR: Key file doesn't exist: $KEY_FILE"
exit 2
fi
if [ "$PROFILES" = "" ]; then
echo "ERROR: Missing mandatory --profiles option to verify against."
show_validate_key_file_help
exit 3
fi
echo " Checking key Properties:"
read sa_email sa_id sa_key_id sa_key_expires_at < <(get_sa_key_properties "$(cat $KEY_FILE)")
echo " SA_EMAIL: $sa_email SA_KEY_ID: $sa_key_id SA_KEY_eXPIRES_AT: $sa_key_expires_at"
if [ ! "$sa_key_expires_at" ]; then
echo "ERROR: key with id not found: $sa_key_id"
exit 4
fi
IFS=","
for profile in $PROFILES; do
echo " Validating profile: $profile"
view_sa_by_key_file "$PROJECT" "$profile" "$KEY_FILE"
done
#'-------------------
elif [ "$action" = "rebind-sa" ]; then
function show_rebind_sa_help(){
echo ""
echo "Rebind SA roles"
echo "Example: ahr-sa-ctl rebind-sa apigee-synchronizer"
}
SA_IDS=$2
if [ "$SA_IDS" = "" ]; then
echo "Missing a Service Account ID(s) or all"
show_help
exit 1
fi
if [ "$SA_IDS" = "all" ]; then
SA_IDS="$APIGEE_SA_ALL"
fi
check_envvars "PROJECT"
check_commands "gcloud"
IFS=","
for SA_ID in $SA_IDS; do
if [ "$PROFILES" ]; then
sa_profiles="$PROFILES"
else
sa_profiles="$SA_ID"
fi
rebind_sa "$PROJECT" "$SA_ID" "$sa_profiles"
done
#'---
elif [ "$action" = "delete-sa" ]; then
SA_IDS=$2
if [ "$SA_IDS" = "" ]; then
echo "Missing Service Account ID: "
echo ""
show_help
exit 1
fi
if [ "$SA_IDS" = "all" ]; then
SA_IDS="$APIGEE_SA_ALL"
fi
IFS=","
for sa_id in $SA_IDS; do
delete_sa $PROJECT "$sa_id"
done
# ----------------------------------------------------------------------
elif [ "$action" = "sa-keys-report" ]; then
check_commands "gcloud kubectl jq ahr-runtime-ctl"
check_envvars "ORG"
check_gnu_date
sa_key_iterator "sa_keys_report"
# ----------------------------------------------------------------------
elif [ "$action" = "sa-keys-rotate" ]; then
if [ "$2" != "" ]; then
SA_DIR="$2"
fi
if [ "$SA_DIR" = "" ]; then
SA_DIR=$(pwd)
fi
check_envvars "PROJECT SA_DIR"
check_commands "gcloud kubectl jq"
check_gnu_date
function get_key_from_secret() {
local sn=$1
local key=$(kubectl get secret -n apigee $sn -o json | jq --raw-output '.data."client_secret.json"' | base64 -d)
if [ "$key" = "" ]; then
# key is empty; trying to recover it from last-applied-configuration..."
local lac=$(kubectl get secret -n apigee $sn -o json | jq --raw-output '.metadata.annotations."kubectl.kubernetes.io/last-applied-configuration"')
if [ "$lac" ]; then
key=$(echo "$lac" | jq --raw-output '.data."client_secret.json"' | base64 -d)
fi
fi
echo "$key"
}
printf "ahr-sa-ctl: INFO Service account keys directory: $SA_DIR\n"
declare -A ads
declare -A ids
declare -A sns
declare -A dss
report="KIND RESOURCE DTE SA_EMAIL SA_KEY_ID EXPIRES_AT"$'\n'
printf "Processing:"
# ApigeeDeployment
for ad in $(kubectl get -n apigee ad -o json|jq .items[].metadata.name -r); do
sn=$(kubectl -n apigee get ad $ad -o json|jq '.spec.template.spec.volumes[]|select(.name|contains("svc-account-volume")).secret.secretName // empty' -r)
if [ "$sn" ]; then
key=$( get_key_from_secret "$sn" )
if [ "$key" ]; then
read sa_email sa_id sa_key_id sa_key_expires_at sa_key_dte < <(get_sa_key_properties "$key")
printf " AD/$sa_id"
report=$report"AD $ad $sa_key_dte $sa_email $sa_key_id $sa_key_expires_at"$'\n'
if [ ! "${ids[$sa_id]}" ]; then
ids[$sa_id]="$sa_email"
fi
ads[$ad]="$sa_id"
sns[$ad]="$sn"
else
echo "ERROR: key for AD "$AD" is not recoverable it might not exist; configure it using ....."
fi
else
report=$report"AD $ad - - - -"$'\n'
fi
done
# ApigeeTelemetry DaemonSet
ds=apigee-logger-apigee-telemetry
sn=$( kubectl -n apigee get ds $ds -o json|jq '.spec.template.spec.volumes[]|select(.name|contains("svc-account-volume")).secret.secretName // empty' -r)
if [ "$sn" ]; then
key=$( get_key_from_secret "$sn" )
if [ "$key" ]; then
read sa_email sa_id sa_key_id sa_key_expires_at sa_key_dte < <(get_sa_key_properties "$key")
report=$report"DS $ds $sa_key_dte $sa_email $sa_key_id $sa_key_expires_at"$'\n'
if [ ! "${ids[$sa_id]}" ]; then
ids[$sa_id]="$sa_email"
fi
dss[$ds]="$sa_id"
sns[$ds]="$sn"
else
echo "ERROR: key for DS "$ds" is not recoverable it might not exist; configure it using ....."
fi
else
report=$report"DS $ds - - - -"$'\n'
fi
printf "\n"
echo "$report" | column -t
if [ "$DEBUG" ]; then
echo "ids[] List of Service Account IDs: SA_ID --> SA_EMAIL"
for id in "${!ids[@]}"; do
echo ": $id ${ids[$id]}"
done
echo "sns[] List of Secret Names: RESOURCE --> SECRET_NAME"
for sn in "${!sns[@]}"; do
echo ": $sn ${sns[$sn]}"
done
echo "ads[] Service Account IDs: RESOURCE --> SA_ID"
for ad in "${!ads[@]}"; do
echo ": $ad ${ads[$ad]}"
done
# echo "dss[]"
# for ds in "${!dss[@]}"; do
# echo ": $ds ${dss[$ds]}"
# done
fi
if [ "$DRY_RUN" ]; then
exit
fi
echo "ahr-sa-ctl: INFO rotate secrets for project/org: $PROJECT"
for sa_id in "${!ids[@]}"; do
echo "Processing SA Id: $sa_id"
sa_email="${ids[$sa_id]}"
sa_key_json=$SA_DIR/${PROJECT}-${sa_id}.json
# check if present; move to timestamp .bak
if [ -f "$sa_key_json" ]; then
sa_key_json_bak=$sa_key_json.$(date '+%Y%m%d-%H%M')
echo " Key File: $sa_key_json exists; moving to: $sa_key_json_bak"
mv "$sa_key_json" "$sa_key_json_bak"
fi
echo " Creating Key File: $sa_key_json"
gcloud iam service-accounts keys create ${sa_key_json} --iam-account $sa_email
done
function patch_key_secret (){
local sa_id=$1
local sn=$2
local secret_property=$3
sa_key_json=$SA_DIR/${PROJECT}-${sa_id}.json
echo " Patching Secret property $SECRET_PROPERTY of secret: $sn"
kubectl patch -n apigee secret $sn -p="{\"data\":{\"$secret_property\": \"$(cat $sa_key_json|base64 -w 0)\"}}"
}
# ApigeeDeployment
for ad in "${!ads[@]}"; do
echo "Processing ApigeeDeployment: $ad"
patch_key_secret "${ads[$ad]}" "${sns[$ad]}" "client_secret.json"
echo " ApigeeDeployment rollout restart: $ad"
ahr-runtime-ctl ad-rollout-restart $ad
done
# DaemonSet
for ds in "${!dss[@]}"; do
echo "Processing DaemonSet: $ds"
patch_key_secret "${dss[$ds]}" "${sns[$ds]}" "client_secret.json"
echo " ApigeeDeployment rollout restart: $at"
kubectl rollout restart ds $ds -n apigee
done
# ----------------------------------------------------------------------
# ----------------------------------------------------------------------
else
echo "Unknown action: $action"
echo ""
show_help
fi