-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathservice_accounts.sh
97 lines (76 loc) · 2.8 KB
/
service_accounts.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
#!/bin/bash
az ad sp list --all \
| jq '[ .[]
| select(.appDisplayName != null)
| select(.appDisplayName | match("^SP-.{8,8}-.*"))
| if .passwordCredentials == [] then {name: .appDisplayName, creds: {spn: "null"}}
else {name: .appDisplayName, creds: {spn: .passwordCredentials[].endDate}} end ]' > spn.json
az ad app list --all \
| jq '[ .[]
| select(.displayName != null)
| select(.displayName | match("^SP-.{8,8}-.*"))
| if .passwordCredentials == [] then {name: .displayName, creds: {app: "null"}}
else {name: .displayName, creds: {app: .passwordCredentials[].endDate}} end ]' > app.json
printf "\n All Service Accounts\n ####################\n\n"
jq -s 'flatten | group_by(.name) | map(reduce .[] as $x ({}; . * $x)) | sort_by(.name)' app.json spn.json \
| awk '
/name/ {
gsub("\"", "")
gsub(",", "")
printf("%-30s", $2)
}
/app/ {
gsub("\"", "")
gsub(",", "")
printf("\tapp: %-32s", $2)
}
/spn/ {
gsub("\"", "")
gsub(",", "")
printf("\tspn: %-32s\n", $2)
}'
# Get creds ending within 7 days
OS=$(uname -s)
if [[ "$OS" == "Linux" ]]; then
CURRENT_TIME=$(date -u +'%Y-%m-%dT%H:%M:%S' -d "+7 days") # Linux
elif [[ "$OS" == "Darwin" ]]; then
CURRENT_TIME=$(date -u -v +7d +'%Y-%m-%dT%H:%M:%S') # Mac
else
echo "...is this a Linux box??"
exit 1
fi
jq ".[] | select(.creds.app < \"$CURRENT_TIME\")" app.json > end_app.json
jq ".[] | select(.creds.spn < \"$CURRENT_TIME\")" spn.json > end_spn.json
printf "\n Service Accounts Ending Within 7 Days\n #####################################\n\n"
jq -s 'flatten | group_by(.name) | map(reduce .[] as $x ({}; . * $x)) | sort_by(.name)' end_app.json end_spn.json \
| awk '
/name/ {
gsub("\"", "")
gsub(",", "")
printf("%-30s\n", $2)
}
/app/ {
gsub("\"", "")
gsub(",", "")
printf("\tapp: %-32s\n", $2)
}
/spn/ {
gsub("\"", "")
gsub(",", "")
printf("\tspn: %-32s\n\n", $2)
}'
printf "\n Commands To Filter By Name\n ##########################\n\n"
printf "
az ad app list --all \\
| jq '.[]
| select(.displayName != null)
| select(.displayName | match(\"..insert APP name here..\"))'
Use the ID located in the JSON array called \"appId\" to update the APP\n\n"
printf "
az ad sp list --all \\
| jq '.[]
| select(.displayName != null)
| select(.displayName | match(\"..insert SPN name here..\"))'
Use the ID located in the JSON array called \"servicePrincipalNames\" to update the SPN\n\n"
rm -f ./app.json ./spn.json
rm -f ./end_app.json ./end_spn.json