-
Notifications
You must be signed in to change notification settings - Fork 1
/
gh-oidc-sub
executable file
·215 lines (190 loc) · 5.85 KB
/
gh-oidc-sub
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
#!/usr/bin/env bash
set -e
PrintUsage()
{
cat <<EOM
Usage:
<command> [options]:
get (--org ORG | --repo owner/repo) [--jq JQ_FILTER]
set (--org ORG | --repo owner/repo) --subs "SUBS LIST"
use-default (--org ORG | --repo owner/repo)
list-claims
list-repos (--org ORG)
Command:
get - Gets the value of the sub customization
set - Sets the value of the sub customization
use-default - Use default value instead of a customization
list-claims - Lists the claims available for customization
list-repos - Generates a tsv with all repo configurations. It has two fields: if repo uses default claims and the claims list.
Options:
-h, --help : Show script help
-o, --org : Name of the organization to get/set sub
-r, --repo,--repository : Name of the repo to get/set sub
-q, --jq : JQ expression to filter the get output (optional)
-s, --subs : The list of sub customization to set (comma separated).
Mandatory for organnization set operation and optional for repo set operation.
Description:
Allows setting and getting OIDC sub customization claims for an organization or a repo.
Learn more on https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/about-security-hardening-with-openid-connect
By default the output JSON returned by the API calls. You can use the --jq option to filter the output.
Examples:
# Get the sub customization for the organization mona
get --org mona
# Sets the sub customization for the organization mona with claims repository_id,environment
set --org mona --subs "repo,context,job_workflow_ref"
# Gets the list of claims available for customization
list-claims
# get only the claims list
get --org mona --jq ".include_claim_keys"
# Gets the settings for all repos in a org
list-repos --org mona
EOM
}
####################################
# Read in the parameters if passed #
####################################
PARAMS=""
while (( "$#" )); do
case "$1" in
-h|--help)
PrintUsage;
exit 0;
;;
-o|--org)
ORG=$2
shift 2
;;
-r|--repo|--repository)
REPO=$2
shift 2
;;
-s|--subs)
SUBS=$2
shift 2
;;
-q|--jq)
JQ=$2
shift 2
;;
--) # end argument parsing
shift
break
;;
-*) # unsupported flags
echo "Error: Unsupported flag $1" >&2
exit 1
;;
*) # preserve positional arguments
PARAMS="$PARAMS $1"
shift
;;
esac
done
ValidateParams()
{
if [ -z "$ORG" ] && [ -z "$REPO" ]; then
echo "Error: --org or --repo is required"
return 1
fi
return 0
}
ListClaims()
{
if ! which curl > /dev/null ; then
echo "Error: curl is required"
else
if ! which jq > /dev/null ; then
echo "Warning: jq is not installed. Please install jq, will try to show claims using grep. This might not work as expected."
echo
curl -sS "https://token.actions.githubusercontent.com/.well-known/openid-configuration" | grep -oP '(?<="claims_supported":\[).[^\]]*(?=\])' | tr -d '"' | tr ',' '\n'
else
curl -sS "https://token.actions.githubusercontent.com/.well-known/openid-configuration" | jq -r '.claims_supported[]'
fi
fi
}
GetSubs ()
{
if [ -n "$JQ" ]; then
query=(--jq "$JQ")
fi
if [ -n "$REPO" ]; then
gh api "repos/$REPO/actions/oidc/customization/sub" "${query[@]}"
fi
if [ -n "$ORG" ]; then
gh api "orgs/$ORG/actions/oidc/customization/sub" "${query[@]}"
fi
}
SetSubs ()
{
if [ -n "$REPO" ]; then
# if subs is not define just don't use default
if [ -z "$SUBS" ]; then
echo "Subject claims not passed. Will configure repo to use organization customization."
gh api -X PUT "repos/$REPO/actions/oidc/customization/sub" -F use_default=false
else
echo -n "$SUBS" | jq -r --slurp --raw-input '{"use_default": false,"include_claim_keys": [split(",") | .[] | rtrimstr(" ") | ltrimstr(" ")]}' | \
gh api -X PUT "repos/$REPO/actions/oidc/customization/sub" --input -
fi
fi
if [ -n "$ORG" ]; then
if [ -z "$SUBS" ]; then
echo "Error: --subs is required"
return 1
fi
echo -n "$SUBS" | jq -r --slurp --raw-input '{"include_claim_keys": [split(",") | .[] | rtrimstr(" ") | ltrimstr(" ")]}' | \
gh api -X PUT "orgs/$ORG/actions/oidc/customization/sub" --input -
fi
}
UseDefaultSubs ()
{
if [ -n "$REPO" ]; then
gh api -X PUT "repos/$REPO/actions/oidc/customization/sub" -F use_default=true
fi
if [ -n "$ORG" ]; then
gh api -X PUT "orgs/$ORG/actions/oidc/customization/sub" -f include_claim_keys[]=context -f include_claim_keys[]=repo
fi
}
ListRepos ()
{
if [ -z "$ORG" ]; then
echo "Error: --org option is required."
exit 1
fi
# header
echo -e "repository\tuse default\tclaims"
gh api "orgs/$ORG/repos" --paginate -q .[].full_name | while read -r reponame ; do
echo -e -n "$reponame"
gh api "repos/$reponame/actions/oidc/customization/sub" --template '{{printf "\t%t\t" .use_default}}{{ range $index, $claim := .include_claim_keys}}{{if $index}},{{end}}{{$claim}}{{end}}{{"\n"}}'
done
}
# if no parameters have been passed
if [ -z "$PARAMS" ]; then
PrintUsage;
exit 0;
fi
case $PARAMS in
" get")
ValidateParams || exit 1
GetSubs
;;
" set")
ValidateParams || exit 1
SetSubs || exit 1
;;
" usedefault" | " use-default")
ValidateParams || exit 1
UseDefaultSubs
;;
" list-claims")
ListClaims
;;
" list-repos")
ListRepos
;;
*)
echo "Unknown command: $PARAMS"
echo
PrintUsage
exit 1
;;
esac