-
Notifications
You must be signed in to change notification settings - Fork 162
/
pipeline.sh
executable file
·397 lines (344 loc) · 15.8 KB
/
pipeline.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
#!/bin/sh
# Copyright 2020 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.
set -e
SCRIPTPATH="$( cd "$(dirname "$0")" || exit >/dev/null 2>&1 ; pwd -P )"
#################################
### function: set_idp_env_var ###
#################################
set_idp_env_var() {
# discovery document of an OIDC compliant IdP
idp_discovery_document_uri="$1"
# retrieve configuration data from a discovery document
response=$(curl --silent -k1 -fsSL -X GET -H "Accept:application/json" "$idp_discovery_document_uri")
if [ "$( printf '%s' "$response" | grep -c error )" -ne 0 ]; then
echo "$response"
exit 1
fi
# extract data used to feed the kvm
issuer=$( printf '%s' "$response" | jq .issuer )
authorization_endpoint=$( printf '%s' "$response" | jq .authorization_endpoint )
token_endpoint=$( printf '%s' "$response" | jq .token_endpoint )
jwks_uri=$( printf '%s' "$response" | jq .jwks_uri )
userinfo_endpoint=$( printf '%s' "$response" | jq .userinfo_endpoint )
# set env variables for kvm (idpConfig)
TEST_IDP_ISSUER=$(printf '%s' "$issuer" | awk -F\" '{print $2}' | awk -F\" '{print $1}')
export TEST_IDP_ISSUER
TEST_IDP_APIGEE_REDIRECT_URI="$2"
export TEST_IDP_APIGEE_REDIRECT_URI
TEST_IDP_AZ_HOSTNAME=$(printf '%s' "$authorization_endpoint" | awk -F\"https:// '{print $2}' | awk -F\" '{print $1}' | awk -F/ '{print $1}')
export TEST_IDP_AZ_HOSTNAME
TEST_IDP_TOKEN_HOSTNAME=$(printf '%s' "$token_endpoint" | awk -F\"https:// '{print $2}' | awk -F\" '{print $1}' | awk -F/ '{print $1}')
export TEST_IDP_TOKEN_HOSTNAME
TEST_IDP_JWKS_HOSTNAME=$(printf '%s' "$jwks_uri" | awk -F\"https:// '{print $2}' | awk -F\" '{print $1}' | awk -F/ '{print $1}')
export TEST_IDP_JWKS_HOSTNAME
TEST_IDP_USERINFO_HOSTNAME=$(printf '%s' "$userinfo_endpoint" | awk -F\"https:// '{print $2}' | awk -F\" '{print $1}' | awk -F/ '{print $1}')
export TEST_IDP_USERINFO_HOSTNAME
TEST_IDP_TOKEN_URI=$(printf '%s' "$token_endpoint" | awk -F "$TEST_IDP_TOKEN_HOSTNAME"'/' '{print $2}' | awk -F\" '{print $1}')
export TEST_IDP_TOKEN_URI
TEST_IDP_AZ_URI=$(printf '%s' "$authorization_endpoint" | awk -F "$TEST_IDP_AZ_HOSTNAME"'/' '{print $2}' | awk -F\" '{print $1}')
export TEST_IDP_AZ_URI
TEST_IDP_JWKS_URI=$(printf '%s' "$jwks_uri" | awk -F "$TEST_IDP_JWKS_HOSTNAME"'/' '{print $2}' | awk -F\" '{print $1}')
export TEST_IDP_JWKS_URI
TEST_IDP_USERINFO_URI=$(printf '%s' "$userinfo_endpoint" | awk -F "$TEST_IDP_USERINFO_HOSTNAME"'/' '{print $2}' | awk -F\" '{print $1}')
export TEST_IDP_USERINFO_URI
TEST_IDP_APIGEE_CLIENT_ID="${TEST_IDP_APIGEE_CLIENT_ID:=dummy-client_id-apigee123}"
export TEST_IDP_APIGEE_CLIENT_ID
TEST_IDP_APIGEE_CLIENT_SECRET="${TEST_IDP_APIGEE_CLIENT_SECRET:=dummy-client_secret_apigee456}"
export TEST_IDP_APIGEE_CLIENT_SECRET
}
####################################
### function: generate_edge_json ###
####################################
generate_edge_json() {
ENV_NAME=$1
SCRIPTPATH="$( cd "$(dirname "$0")" || exit >/dev/null 2>&1 ; pwd -P )"
rm -f "$SCRIPTPATH"/edge.json
cat <<EOF >> "$SCRIPTPATH/edge.json"
{
"version": "1.0",
"envConfig": {
"$ENV_NAME": {
"targetServers": [],
"virtualHosts": [],
"caches": [
{
"name": "IDP_JWKS_CACHE",
"description": "IdP JWKS Response Cache",
"expirySettings": {
"timeoutInSec": {
"value": "1800"
},
"valuesNull": false
}
}
],
"kvms": [
{
"name": "idpConfigIdentityProxy",
"encrypted": "true",
"entry": [
{
"name": "idp.az.hostname",
"value": "$TEST_IDP_AZ_HOSTNAME"
},
{
"name": "idp.az.uri",
"value": "$TEST_IDP_AZ_URI"
},
{
"name": "idp.apigee.client_id",
"value": "$TEST_IDP_APIGEE_CLIENT_ID"
},
{
"name": "idp.apigee.client_secret",
"value": "$TEST_IDP_APIGEE_CLIENT_SECRET"
},
{
"name": "idp.apigee.redirect_uri",
"value": "$TEST_IDP_APIGEE_REDIRECT_URI"
},
{
"name": "idp.token.hostname",
"value": "$TEST_IDP_TOKEN_HOSTNAME"
},
{
"name": "idp.token.uri",
"value": "$TEST_IDP_TOKEN_URI"
},
{
"name": "idp.jwks.hostname",
"value": "$TEST_IDP_JWKS_HOSTNAME"
},
{
"name": "idp.jwks.uri",
"value": "$TEST_IDP_JWKS_URI"
},
{
"name": "idp.userinfo.hostname",
"value": "$TEST_IDP_USERINFO_HOSTNAME"
},
{
"name": "idp.userinfo.uri",
"value": "$TEST_IDP_USERINFO_URI"
},
{
"name": "idp.issuer",
"value": "$TEST_IDP_ISSUER"
}
]
}
]
}
},
"orgConfig": {
"apiProducts": [
{
"name": "IdentityFacade",
"apiResources": [],
"approvalType": "auto",
"attributes": [
{
"name": "access",
"value": "public"
}
],
"description": "",
"displayName": "Identity Facade",
"environments": [
"$ENV_NAME"
],
"proxies": [
"identity-facade-v1"
]
}
],
"developers": [
{
"email": "[email protected]",
"firstName": "Jane",
"lastName": "Doe",
"userName": "janedoe",
"attributes": []
}
],
"developerApps": {
"[email protected]": [
{
"name": "identityApp",
"attributes": [
{
"name": "displayName",
"value": "identity App"
}
],
"apiProducts": [
"IdentityFacade"
],
"callbackUrl": "https://mocktarget.apigee.net/echo",
"scopes": []
}
]
}
}
}
EOF
}
#################################
### function: set_idp_env_var ###
#################################
set_functional_test_env_var() {
# use timestamp (parameter) to create a unique value
TEST_APP_CONSUMER_KEY="xkey-$1"
export TEST_APP_CONSUMER_KEY
TEST_APP_CONSUMER_SECRET='xsecret'
export TEST_APP_CONSUMER_SECRET
# pkce test variables. Challenge method is set to 'S256' (sha256)
TEST_APP_PKCE_CODE_CHALLENGE='hoLP4vNbBccHkhNAbk_jbyLhlGwgeoRyo53A-Luiirg'
export TEST_APP_PKCE_CODE_CHALLENGE
TEST_APP_PKCE_CODE_VERIFIER='iloveapis1234567890'
export TEST_APP_PKCE_CODE_VERIFIER
}
#################################
### function: set_pkce_config ###
#################################
set_pkce_config() {
# Copy AM policy template
cp "$SCRIPTPATH"/AM-SetPKCEMode.template.xml "$SCRIPTPATH"/apiproxy/policies/AM-SetPKCEMode.xml
# replace pkce tag in AM policy
sed -i.bak "s|@IsPKCEEnabled@|$1|" "$SCRIPTPATH"/apiproxy/policies/AM-SetPKCEMode.xml
# remove .bak files
rm "$SCRIPTPATH"/apiproxy/policies/AM-SetPKCEMode.xml.bak
}
####################################
### function: generate_authz_url ###
####################################
generate_authz_url() {
HOST=$1
BASE_PATH="/v1/oauth20"
AZ_URI="/authorize"
CLIENT_ID="?client_id="$2
RESPONSE_TYPE="&response_type=code"
SCOPE="&scope=openid email profile"
STATE="&state=abcd-1234"
REDIRECT_URI="&redirect_uri=https://mocktarget.apigee.net/echo"
# is pkce enabled (=true) or not
if [ "$4" = "true" ];then
CODE_CHALLENGE_METHOD="&code_challenge_method=S256"
CODE_CHALLENGE="&code_challenge="$3
else
CODE_CHALLENGE_METHOD=""
CODE_CHALLENGE=""
fi
printf "\n"
printf "##########################\n"
printf "#### Authorization URL ###\n"
printf "##########################\n"
printf "You can copy/paste the following authorization URL into your Web browser to initiate the OIDC flow:\n"
printf "https://"%s%s%s%s%s%s%s%s%s%s \
"$HOST" \
"$BASE_PATH" \
"$AZ_URI" \
"$CLIENT_ID" \
"$RESPONSE_TYPE" \
"$SCOPE" \
"$STATE" \
"$REDIRECT_URI" \
"$CODE_CHALLENGE_METHOD" \
"$CODE_CHALLENGE"
printf "\n\n"
}
# generate a timestamp to make some values unique
timestamp=$(date '+%s')
export PATH="$PATH:$SCRIPTPATH/../../tools/apigee-sackmesser/bin"
# default value for pkce enablement
DEFAULT_IS_PKCE_ENABLED=false
# is pkce enabled (=true) or not (=false)
IS_PKCE_ENABLED="${IS_PKCE_ENABLED:-"$DEFAULT_IS_PKCE_ENABLED"}"
set_pkce_config "$IS_PKCE_ENABLED"
if [ -z "$1" ] || [ "$1" = "--apigeeapi" ];then
echo "[INFO] Deploying Identitiy facade to Apigee API (For Edge)"
# deploy the OIDC mock identity provider...
if [ -z ${IDP_DISCOVERY_DOCUMENT+x} ];then
sackmesser deploy --apigeeapi -o "$APIGEE_ORG" -e "$APIGEE_ENV" -u "$APIGEE_USER" -p "$APIGEE_PASS" -d "$SCRIPTPATH"/../oidc-mock
(cd "$SCRIPTPATH"/../oidc-mock && npm i --no-fund && TEST_HOST="$APIGEE_ORG-$APIGEE_ENV.apigee.net" npm test)
fi
# set env variables for google oidc
DISCOVERY_DOCUMENT_URI="${IDP_DISCOVERY_DOCUMENT:-https://$APIGEE_ORG-$APIGEE_ENV.apigee.net/v1/openid-connect/.well-known/openid-configuration}"
CALLBACK_URL="https://$APIGEE_ORG-$APIGEE_ENV.apigee.net/v1/oauth20/callback"
set_idp_env_var "$DISCOVERY_DOCUMENT_URI" "$CALLBACK_URL"
# generate edge.json file
generate_edge_json "$APIGEE_ENV"
set_functional_test_env_var "$timestamp"
# deploy Apigee artifacts: proxy, developer, app, product cache, kvm and proxy
sackmesser deploy --apigeeapi -o "$APIGEE_ORG" -e "$APIGEE_ENV" -u "$APIGEE_USER" -p "$APIGEE_PASS" -d "$SCRIPTPATH"
# set developer app (apigee_client) credentials
curl --fail --silent -X POST \
-u "$APIGEE_USER":"$APIGEE_PASS" -H "Content-Type:application/json" \
--data "{ \"consumerKey\": \"$TEST_APP_CONSUMER_KEY\", \"consumerSecret\": \"$TEST_APP_CONSUMER_SECRET\" }" \
https://api.enterprise.apigee.com/v1/organizations/"$APIGEE_ORG"/developers/[email protected]/apps/identityApp/keys/create
# Set identity product for 'identityApp'
curl --fail --silent -X POST \
-u "$APIGEE_USER":"$APIGEE_PASS" -H "Content-Type:application/json" \
--data "{ \"apiProducts\": [\"IdentityFacade\"] }" \
https://api.enterprise.apigee.com/v1/organizations/"$APIGEE_ORG"/developers/[email protected]/apps/identityApp/keys/"$TEST_APP_CONSUMER_KEY"
# execute integration tests only against mock IDP
if [ -z ${IDP_DISCOVERY_DOCUMENT+x} ] && [ "$IS_PKCE_ENABLED" = "true" ]; then
(cd "$SCRIPTPATH" && npm i --no-fund && TEST_HOST="$APIGEE_ORG-$APIGEE_ENV.apigee.net" IS_PKCE_ENABLED="$IS_PKCE_ENABLED" npm run testPkce)
elif [ -z ${IDP_DISCOVERY_DOCUMENT+x} ]; then
(cd "$SCRIPTPATH" && npm i --no-fund && TEST_HOST="$APIGEE_ORG-$APIGEE_ENV.apigee.net" IS_PKCE_ENABLED="$IS_PKCE_ENABLED" npm run test)
else
echo "no tests run for custom OIDC Idp: $IDP_DISCOVERY_DOCUMENT"
fi
# generate authorization URL
generate_authz_url "$APIGEE_ORG-$APIGEE_ENV.apigee.net" "$TEST_APP_CONSUMER_KEY" "$TEST_APP_PKCE_CODE_CHALLENGE" "$IS_PKCE_ENABLED"
fi
if [ -z "$1" ] || [ "$1" = "--googleapi" ];then
echo "[INFO] Deploying Identitiy facade to Google API (For X/hybrid)"
APIGEE_TOKEN="$(gcloud config config-helper --force-auth-refresh --format json | jq -r '.credential.access_token')"
if [ -z ${IDP_DISCOVERY_DOCUMENT+x} ];
then
sackmesser deploy --googleapi -o "$APIGEE_X_ORG" -e "$APIGEE_X_ENV" -t "$APIGEE_TOKEN" -d "$SCRIPTPATH"/../oidc-mock
(cd "$SCRIPTPATH"/../oidc-mock && npm i --no-fund && TEST_HOST="$APIGEE_X_HOSTNAME" npm test)
fi
# set env variables for google oidc
DISCOVERY_DOCUMENT_URI="${IDP_DISCOVERY_DOCUMENT:-https://$APIGEE_X_HOSTNAME/v1/openid-connect/.well-known/openid-configuration}"
CALLBACK_URL="https://$APIGEE_X_HOSTNAME/v1/oauth20/callback"
set_idp_env_var "$DISCOVERY_DOCUMENT_URI" "$CALLBACK_URL"
# generate edge.json file
generate_edge_json "$APIGEE_X_ENV"
set_functional_test_env_var "$timestamp"
# # deploy Apigee artifacts: proxy, developer, app, product cache, kvm and proxy
sackmesser deploy --googleapi -o "$APIGEE_X_ORG" -e "$APIGEE_X_ENV" -t "$APIGEE_TOKEN" -h "$APIGEE_X_HOSTNAME" -d "$SCRIPTPATH"
# set developer app (apigee_client) credentials
curl --fail --silent -X POST \
-H "Authorization: Bearer $APIGEE_TOKEN" -H "Content-Type:application/json" \
--data "{ \"consumerKey\": \"$TEST_APP_CONSUMER_KEY\", \"consumerSecret\": \"$TEST_APP_CONSUMER_SECRET\" }" \
https://apigee.googleapis.com/v1/organizations/"$APIGEE_X_ORG"/developers/[email protected]/apps/identityApp/keys/create
# Set identity product for 'identityApp'
curl --fail --silent -X POST \
-H "Authorization: Bearer $APIGEE_TOKEN" -H "Content-Type:application/json" \
--data "{ \"apiProducts\": [\"IdentityFacade\"] }" \
https://apigee.googleapis.com/v1/organizations/"$APIGEE_X_ORG"/developers/[email protected]/apps/identityApp/keys/"$TEST_APP_CONSUMER_KEY"
# execute integration tests only against mock IDP
if [ -z ${IDP_DISCOVERY_DOCUMENT+x} ] && [ "$IS_PKCE_ENABLED" = "true" ]; then
(cd "$SCRIPTPATH" && npm i --no-fund && TEST_HOST="$APIGEE_X_HOSTNAME" IS_PKCE_ENABLED="$IS_PKCE_ENABLED" npm run testPkce)
elif [ -z ${IDP_DISCOVERY_DOCUMENT+x} ]; then
(cd "$SCRIPTPATH" && npm i --no-fund && TEST_HOST="$APIGEE_X_HOSTNAME" IS_PKCE_ENABLED="$IS_PKCE_ENABLED" npm run test)
else
echo "no tests run for custom OIDC Idp: $IDP_DISCOVERY_DOCUMENT"
fi
# generate authorization URL
generate_authz_url "$APIGEE_X_HOSTNAME" "$TEST_APP_CONSUMER_KEY" "$TEST_APP_PKCE_CODE_CHALLENGE" "$IS_PKCE_ENABLED"
fi