-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdeploy-oauth.sh
More file actions
executable file
·290 lines (233 loc) · 11.3 KB
/
Copy pathdeploy-oauth.sh
File metadata and controls
executable file
·290 lines (233 loc) · 11.3 KB
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
#! /bin/sh
nice_echo() {
echo "\n\033[1;36m >>>>>>>>>> $1 <<<<<<<<<< \033[0m\n"
}
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
# no parameters passed, using default config file
if [ $# -eq 0 ]; then
#using default config file
if [ -e config.cfg ]; then
source config.cfg
echo 'Using default config file at ' ${CURRENT_DIR}/config.cfg
else
echo 'No config file passed and default config file is not available at ' ${CURRENT_DIR}/config.cfg
exit
fi
# usage function
elif [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
echo "Usage: `basename $0` [OPTION] [FILE]...
Options:
-h, --help Display this help and exit
"
exit 0
# config file passed as argument
else
FILENAME=$1 #get filename
if [ -e $FILENAME ]; then
source $FILENAME; # load the file
echo 'Using config file located at ' $FILENAME
else
echo 'Bad config file passed at ' $FILENAME
exit
fi
fi
RED='\n\033[1;31m'
GREEN='\n\033[1;32m'
END_COLOR='\033[0m'
# ******************** Step 1. Login as the pOrg Owner and get token ********************
nice_echo "Step 1. Login as the pOrg Owner and get token"
CURL_BODY='{"username":"'"${pORG_USERNAME}"'","password":"'"${pORG_PASSWORD}"'","realm":"provider/default-idp-2","client_id":"'"${APIM_CLIENT_ID}"'","client_secret":"'"${APIM_CLIENT_SECRET}"'","grant_type":"password"}'
RESPONSE=`curl -s -k -H "Content-Type: application/json" -H "Accept: application/json" -H "Authorization: Bearer $ACTIVATION" -d "$CURL_BODY" ${APIM_SERVER}/api/token`
TOKEN_RESPONSE=`echo "$RESPONSE" | jq -r '.access_token'`
if [[ $TOKEN_RESPONSE == null ]]; #call failed
then
echo "${RED}FAIL${END_COLOR}"
echo 'Failed call with error' $RESPONSE
else
echo "${GREEN}SUCCESS${END_COLOR}"
echo 'Successfully login and obtained token ' #$TOKEN_RESPONSE
fi
echo
read -p "Enter the User Registry type you would like to create (1/2) (1 - Authentication URL / 2 - LDAP)." -n 1 -r
echo
# authentication URL
if [ $REPLY == 1 ]; then
USER_REGISTRY_TYPE='authurl'
USER_REGISTRY_REQ=''
echo '> Selected User Registry type Authentication URL User Registry.'
# ldap
elif [ $REPLY == 2 ]; then
USER_REGISTRY_TYPE='ldap'
USER_REGISTRY_REQ='
"configuration": {
"authentication_method": "'"${USER_REGISTRY_LDAP_AU_METHOD}"'",
"authenticated_bind": "'"${USER_REGISTRY_LDAP_AU_BIND}"'",
"search_dn_base": "'"${USER_REGISTRY_LDAP_DN_BASE}"'",
"protocol_version": "'"${USER_REGISTRY_LDAP_VERSION}"'",
"bind_prefix": "'"${USER_REGISTRY_LDAP_DN_BIND_PREFIX}"'",
"bind_suffix": "'"$USER_REGISTRY_LDAP_DN_BIND_SUFFIX}"'",
"admin_dn": "'"$USER_REGISTRY_LDAP_ADMIN_DN}"'",
"admin_password": "'"$USER_REGISTRY_LDAP_ADMIN_PASSWORD}"'"
},'
echo '> Selected User Registry type LDAP User Registry.'
# bad registry type
else
USER_REGISTRY_TYPE='NA'
echo '> Incorrect User Registry type selected.'
fi
# ******************** Step 2. Get User Registry Integration URL ********************
nice_echo "Step 2. Get User Registry Authentication URL"
RESPONSE=`curl -s -k -H "Content-Type: application/json" -H "Accept: application/json" -H "Authorization: Bearer $TOKEN_RESPONSE" ${APIM_SERVER}/api/cloud/integrations/user-registry/$USER_REGISTRY_TYPE`
INTEGRATION_URL=`echo "$RESPONSE" | jq -r '.url'`
if [[ $RESPONSE_URL == null ]]; #call failed
then
echo "${RED}FAIL${END_COLOR}"
echo 'Failed call with error' $RESPONSE
else
echo "${GREEN}SUCCESS${END_COLOR}"
echo 'Sucessfully retrieved item ' $INTEGRATION_URL
fi
# ******************** Step 3. TLS Profile ********************
nice_echo "Step 3. TLS Profile"
RESPONSE=`curl -s -k -H "Content-Type: application/json" -H "Accept: application/json" -H "Authorization: Bearer $TOKEN_RESPONSE" ${APIM_SERVER}/api/orgs/${pORG_NAME}/tls-client-profiles`
TLS_PROFILE_RESPONSE_URL=`echo "$RESPONSE" | jq -r '.results[] | select(.name =="tls-client-profile-default").url'`
if [[ $TLS_PROFILE_RESPONSE_URL == null ]]; #call failed
then
echo "${RED}FAIL${END_COLOR}"
echo 'Failed call with error' $RESPONSE
exit
else
echo "${GREEN}SUCCESS${END_COLOR}"
echo 'Sucessfully retrieved item ' $TLS_PROFILE_RESPONSE_URL
fi
# ******************** Step 4. Configure TLS Profile in Sandbox Catalog ********************
nice_echo "Step 4. Configure TLS Profile in Sandbox Catalog"
CURL_BODY='{
"tls_client_profile_url": "'"${TLS_PROFILE_RESPONSE_URL}"'"
}'
RESPONSE=`curl -s -k -X POST -H "Content-Type: application/json" -H "Accept: application/json" -H "Authorization: Bearer $TOKEN_RESPONSE" -d "$CURL_BODY" ${APIM_SERVER}/api/catalogs/${pORG_NAME}/${CATALOG_NAME}/configured-tls-client-profiles`
RESPONSE_URL=`echo "$RESPONSE" | jq -r '.url'`
RESPONSE_CODE=`echo "$RESPONSE" | jq -r '.status'`
if [[ $RESPONSE_CODE == 409 ]]; #item exists
then
TLS_CLIENT_PROFILE_ID=`echo "$RESPONSE" | jq -r '.message' | sed -e 's/\(^.*id:[[:space:]]*\)\(.*\)\(already.*$\)/\2/' | tr ')' ' ' | tr '[' ' ' | tr ']' ' ' | tr -d ' ' | xargs`
RESPONSE_URL=${APIM_SERVER}/api/orgs/${pORG_NAME}/tls-client-profiles/$TLS_CLIENT_PROFILE_ID
echo "${GREEN}SUCCESS${END_COLOR}"
echo "TLS Profile already exists, sucessfully retrieved item" $RESPONSE_URL
elif [[ $RESPONSE_URL == null ]]; #call failed
then
echo "${RED}FAIL${END_COLOR}"
echo 'Failed call with error' $RESPONSE
else
echo "${GREEN}SUCCESS${END_COLOR}"
echo 'Sucessfully created item ' $RESPONSE_URL
fi
# ******************** Step 5. Create User Registry ********************
nice_echo "Step 5. Create User Registry"
# Append TLS client profile
USER_REGISTRY_BODY=`echo '"endpoint": {"endpoint": "'"${USER_REGISTRY_URL}"'","tls_client_profile_url": "'"${TLS_PROFILE_RESPONSE_URL}"'"},'$USER_REGISTRY_REQ`
CURL_BODY=`echo '{
"title": "'"${USER_REGISTRY}"'",
"name": "'"${USER_REGISTRY}"'",
"case_sensitive": false,
"registry_type" : "'"${USER_REGISTRY_TYPE}"'",
"identity_providers": [
{
"title": "'"${USER_REGISTRY}"'",
"name": "'"${USER_REGISTRY}"'"
}
],' $USER_REGISTRY_BODY '"integration_url": "'"${INTEGRATION_URL}"'"}'`
RESPONSE=`curl -s -k -X POST -H "Content-Type: application/json" -H "Accept: application/json" -H "Authorization: Bearer $TOKEN_RESPONSE" -d "$CURL_BODY" ${APIM_SERVER}/api/orgs/${pORG_NAME}/user-registries`
RESPONSE_URL=`echo "$RESPONSE" | jq -r '.url'`
RESPONSE_CODE=`echo "$RESPONSE" | jq -r '.status'`
if [[ $RESPONSE_CODE == 409 ]]; #since item exists, update any new info
then
echo "User Registry already exists, performing update now ..." $RESPONSE
USER_REGISTRY_ID=`echo "$RESPONSE" | jq -r '.message' | sed -e 's/\(^.*id:[[:space:]]*\)\(.*\)\(already.*$\)/\2/' | tr ')' ' ' | tr '[' ' ' | tr ']' ' ' | tr -d ' ' | xargs`
#echo "USER REGISTRY ID" $USER_REGISTRY_ID
#echo "URL" ${APIM_SERVER}/api/user-registries/${pORG_NAME}/$USER_REGISTRY_ID
RESPONSE=`curl -s -k -X PATCH -H "Content-Type: application/json" -H "Accept: application/json" -H "Authorization: Bearer $TOKEN_RESPONSE" -d '{ "title": "'"${USER_REGISTRY}"'", "name": "'"${USER_REGISTRY}"'","endpoint": {"endpoint": "'"${USER_REGISTRY_URL}"'","tls_client_profile_url": "'"${TLS_PROFILE_RESPONSE_URL}"'"},"identity_providers": [{"title": "'"${USER_REGISTRY}"'","name": "'"${USER_REGISTRY}"'"}]}' ${APIM_SERVER}/api/user-registries/${pORG_NAME}/$USER_REGISTRY_ID`
RESPONSE_URL=`echo "$RESPONSE" | jq -r '.url'`
if [[ $RESPONSE_URL == null ]];
then
echo "${RED}FAIL${END_COLOR}"
echo 'Failed call with error' $RESPONSE
else
echo "${GREEN}SUCCESS${END_COLOR}"
echo 'Sucessfully updated item ' $RESPONSE_URL
fi
elif [[ $RESPONSE_URL == null ]];
then
echo "${RED}FAIL${END_COLOR}"
echo 'Failed call with error' $RESPONSE
else
echo "${GREEN}SUCCESS${END_COLOR}"
echo 'Sucessfully created item ' $RESPONSE_URL
fi
# ******************** Step 6. Configure User Registry in Sandbox Catalog ********************
nice_echo "Step 6. Configure User Registry in Sandbox Catalog"
CURL_BODY='{
"user_registry_url": "'"${RESPONSE_URL}"'"
}'
RESPONSE=`curl -s -k -X POST -H "Content-Type: application/json" -H "Accept: application/json" -H "Authorization: Bearer $TOKEN_RESPONSE" -d "$CURL_BODY" ${APIM_SERVER}/api/catalogs/${pORG_NAME}/${CATALOG_NAME}/configured-api-user-registries`
RESPONSE_URL=`echo "$RESPONSE" | jq -r '.url'`
if [[ $RESPONSE_URL == null ]]; #call failed
then
echo "${RED}FAIL${END_COLOR}"
echo 'Failed call with error' $RESPONSE
else
echo "${GREEN}SUCCESS${END_COLOR}"
echo 'Sucessfully created item ' $RESPONSE_URL
fi
# ******************** Step 7. Create OAuth Provider URL ********************
nice_echo "Step 7. Create OAuth Provider"
OAUTH_PROVIDER=`curl -s -k -H "Content-Type: application/json" -H "Accept: application/json" $OAUTH_PROVIDER_URL > oauth-provider-tmp.cfg`
RESPONSE=`curl -s -k -X POST -H "Content-Type: application/json" -H "Accept: application/json" -H "Authorization: Bearer $TOKEN_RESPONSE" -d @oauth-provider-tmp.cfg ${APIM_SERVER}/api/orgs/${pORG_NAME}/oauth-providers`
RESPONSE_URL=`echo "$RESPONSE" | tr '\r\n' ' ' | jq -r '.url'`
RESPONSE_CODE=`echo "$RESPONSE" | jq -r '.status'`
if [[ $RESPONSE_CODE == 409 ]]; #since item exists, update any new info
then
echo "OAuth Provider already exists." $RESPONSE
OAUTH_PROVIDER_ID=`echo "$RESPONSE" | jq -r '.message' | sed -e 's/\(^.*id:[[:space:]]*\)\(.*\)\(already.*$\)/\2/' | tr ')' ' ' | tr '[' ' ' | tr ']' ' ' | tr -d ' ' | xargs`
#echo "OAUTH_PROVIDER_ID" $OAUTH_PROVIDER_ID
#echo "URL" ${APIM_SERVER}/api/orgs/${pORG_NAME}/oauth-providers/$OAUTH_PROVIDER_ID
RESPONSE=`curl -s -k -X PATCH -H "Content-Type: application/json" -H "Accept: application/json" -H "Authorization: Bearer $TOKEN_RESPONSE" -d @oauth-provider-tmp.cfg ${APIM_SERVER}/api/orgs/${pORG_NAME}/oauth-providers/$OAUTH_PROVIDER_ID`
RESPONSE_URL=`echo "$RESPONSE" | tr '\r\n' ' ' | jq -r '.url'`
if [[ $RESPONSE_URL == null ]];
then
echo "${RED}FAIL${END_COLOR}"
echo 'Failed call with error' $RESPONSE
else
echo "${GREEN}SUCCESS${END_COLOR}"
echo 'Sucessfully updated item ' $RESPONSE_URL
fi
elif [[ $RESPONSE_URL == null ]]; #call failed
then
echo "${RED}FAIL${END_COLOR}"
echo 'Failed call with error' $RESPONSE
else
echo "${GREEN}SUCCESS${END_COLOR}"
echo 'Sucessfully created item ' $RESPONSE_URL
fi
#remove temporary file
rm oauth-provider-tmp.cfg
# ******************** Step 8. Configure OAuth Provider in Sandbox Catalog ********************
nice_echo "Step 8. Configure OAuth Provider in Sandbox Catalog"
CURL_BODY='{
"oauth_provider_url": "'"${RESPONSE_URL}"'"
}'
RESPONSE=`curl -s -k -X POST -H "Content-Type: application/json" -H "Accept: application/json" -H "Authorization: Bearer $TOKEN_RESPONSE" -d "$CURL_BODY" ${APIM_SERVER}/api/catalogs/${pORG_NAME}/${CATALOG_NAME}/configured-oauth-providers`
RESPONSE_URL=`echo "$RESPONSE" | tr '\r\n' ' ' | jq -r '.url'`
RESPONSE_CODE=`echo "$RESPONSE" | jq -r '.status'`
if [[ $RESPONSE_CODE == 409 ]];
then
echo "${GREEN}SUCCESS${END_COLOR}"
echo "OAuth Provider already exists in" ${CATALOG_NAME} "catalog and does not need to be updated." $RESPONSE
elif [[ $RESPONSE_URL == null ]]; #call failed
then
echo "${RED}FAIL${END_COLOR}"
echo 'Failed call with error' $RESPONSE
else
echo "${GREEN}SUCCESS${END_COLOR}"
echo 'Sucessfully created item ' $RESPONSE_URL
fi