-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstart.sh
227 lines (178 loc) · 8.21 KB
/
start.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
#!/bin/bash
HOST_IP="localhost"
echo "Important: Make sure jq is installed in the system"
echo "Checking Konga admin user"
KONGA_TOKEN=$(curl -s POST http://${HOST_IP}:1337/login --header 'Content-Type: application/json' --header 'Accept: application/json' --data '{"identifier": "admin", "password": "00000000"}' | jq -r '.token')
if [[ $(echo ${KONGA_TOKEN}) != null ]]; then
echo -e "\e[1;31m User already existing! canceling Konga configuration. \e[0m\n"
else
echo "Creating Konga admin user"
curl -s -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "username=admin" -d "email=admin%40test.com" -d "password=00000000" -d "password_confirmation=00000000" http://${HOST_IP}:1337/register
echo "Authentication to Konga"
KONGA_TOKEN=$(curl -s POST http://${HOST_IP}:1337/login --header 'Content-Type: application/json' --header 'Accept: application/json' --data '{"identifier": "admin", "password": "00000000"}' | jq -r '.token')
echo ${KONGA_TOKEN}
echo "Creating kong node"
KONG_NODE=$(curl -s POST http://${HOST_IP}:1337/api/kongnode \
--header "Authorization: Bearer ${KONGA_TOKEN}" \
--header 'Content-Type: application/json' \
--data-raw '[
{
"type": "default",
"jwt_algorithm": "HS256",
"name": "kong",
"kong_admin_url": "http://kong:8001",
"kong_api_key": "",
"kong_version": "2.0.0",
"health_checks": false,
"active": true
}
]')
echo ${KONG_NODE} | jq .
KONG_NODE_ID=$(echo ${KONG_NODE} | jq '.[0].id')
curl -s GET http://${HOST_IP}:1337/kong?connection_id=${KONG_NODE} \
--header "Authorization: Bearer ${KONGA_TOKEN}" \
--header 'Accept: application/json'
curl -s POST http://${HOST_IP}:1337/logout
echo -e "\e[1;31m Finalised Konga configuration. \e[0m\n"
fi
hash jq 2>/dev/null || { echo >&2 "I require foo but it's not installed. Aborting."; exit 1; }
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
echo "Making sure that kong-oidc is installed this should return true"
OIDC=$(curl -s http://${HOST_IP}:8001 | jq .plugins.available_on_server.oidc)
if [[ $(echo ${OIDC}) != 'true' ]]; then
echo -e "\e[1;31m Please verify configuration. Can't find OIDC plugin \e[0m"
exit 0
fi
echo ${OIDC}
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
echo "To test the system, we will use MockBin.org"
echo -n " Please provide mock service name: "
read MOCK
SERVICE=$(curl -s -X POST http://${HOST_IP}:8001/services \
-d name=${MOCK} \
-d url=http://mockbin.org/request \
| jq .)
printf "\nMockBin.org response:\n"
echo ${SERVICE} | jq .
SERVICE_ID=$(echo ${SERVICE} | jq -r '.id')
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
echo "Creating the route mapped to service $MOCK (ID: $SERVICE_ID)"
echo -n " Please choose the relative route through kong: /"
read ROUTE
curl -s -X POST http://${HOST_IP}:8001/services/${SERVICE_ID}/routes -d "paths[]=/$ROUTE" \
| jq .
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
echo "Sending request to /$ROUTE to make sure it is reachable"
curl -v http://${HOST_IP}:8000/${ROUTE} |jq .
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
echo -e "\e[1;31m Mock service and associated routes configured successfully. \e[0m\n"
echo "Authenticate admin cli to KeyCloak"
ADMIN_TKN=$(curl -s POST http://${HOST_IP}:8180/auth/realms/master/protocol/openid-connect/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "username=admin" \
-d 'password=admin' \
-d 'grant_type=password' \
-d 'client_id=admin-cli' | jq -r '.access_token')
printf "\nAdmin token: ${ADMIN_TKN}\n"
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
echo "Fetching Existing realms: "
REALMS=$(curl -s GET http://${HOST_IP}:8180/auth/admin/realms \
-H "Accept: application/json" \
-H "Authorization: Bearer $ADMIN_TKN")
printf "\nList of existing realms: \n"
echo ${REALMS} | jq '.[] .realm'
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
echo "Creating new realm"
echo -n " Please provide realm name to create: "
read REALM_NAME
curl -s POST http://${HOST_IP}:8180/auth/admin/realms \
-H "Authorization: Bearer $ADMIN_TKN" \
-H "Content-Type: application/json" \
--data '{"realm": "'${REALM_NAME}'","displayName": "'${REALM_NAME}'","enabled": true}'
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
echo "Creating new user: test@example.com/test"
curl -s POST http://${HOST_IP}:8180/auth/admin/realms/${REALM_NAME}/users \
-H "Authorization: Bearer $ADMIN_TKN" \
-H "Content-Type: application/json" \
-d @data/test_user.json | jq .
echo "Displaying user info"
curl -s GET http://${HOST_IP}:8180/auth/admin/realms/${REALM_NAME}/users?username=test \
-H "Accept: application/json" \
-H "Authorization: Bearer $ADMIN_TKN" | jq '.[0]'
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
echo -n "Kong client Secret: "
KONG_SECRET=$(cat /dev/urandom | env LC_CTYPE=C tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)
echo ${KONG_SECRET}
echo "Creating Kong client"
curl -s POST http://${HOST_IP}:8180/auth/admin/realms/${REALM_NAME}/clients \
-H "Authorization: Bearer $ADMIN_TKN" \
-H "Content-Type: application/json" \
--data '{"clientId": "kong","name": "kong","enabled": true,"protocol": "openid-connect","rootUrl": "http://'${HOST_IP}':8000","clientAuthenticatorType": "client-secret","publicClient": false,"directAccessGrantsEnabled": true,"adminUrl": "","secret": "'${KONG_SECRET}'","redirectUris": ["*"]}' | jq .
KONG_CLIENT=$(curl -s GET http://${HOST_IP}:8180/auth/admin/realms/${REALM_NAME}/clients?clientId=kong \
-H "Accept: application/json" \
-H "Authorization: Bearer $ADMIN_TKN" | jq '.[0]')
echo ${KONG_CLIENT} | jq .
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
echo "Creating application client"
curl -s POST http://${HOST_IP}:8180/auth/admin/realms/${REALM_NAME}/clients \
-H "Authorization: Bearer $ADMIN_TKN" \
-H "Content-Type: application/json" \
--data-raw '{
"enabled": true,
"attributes": {},
"redirectUris": ["*"],
"webOrigins": ["*"],
"clientId": "myApp",
"rootUrl": "",
"implicitFlowEnabled": true,
"protocol": "openid-connect"
}' | jq .
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
echo -e "\e[1;31m Finalised KeyCloak configuration. \e[0m\n"
echo "KeyCloak Global OIDC configuration"
curl -s http://${HOST_IP}:8180/auth/realms/${REALM_NAME}/.well-known/openid-configuration | jq .
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
echo "Inserting above configuration and client secret to KONG"
curl -s -X POST http://${HOST_IP}:8001/plugins \
-d name=oidc \
-d config.client_id=kong \
-d config.client_secret=${KONG_SECRET} \
-d config.bearer_only=yes \
-d config.realm=${REALM_NAME} \
-d config.discovery=http://${HOST_IP}:8180/auth/realms/${REALM_NAME}/.well-known/openid-configuration \
| jq .
echo "Activating CORS plugin in KONG"
curl -s -X POST http://${HOST_IP}:8001/plugins \
-d name=cors \
-d config.origins=* \
-d enabled=true \
-d config.headers=accept-tz-offset,Access-Control-Allow-Origin,Origin,Content-Type,Access-Control-Allow-Headers,Authorization,X-Requested-With \
-d config.exposed_headers=Access-Control-Allow-Headers,Access-Control-Allow-Origin \
-d config.credentials=true \
-d config.max_age=3600 \
| jq .
echo -e "\e[1;31m Finalised Kong configuration. \e[0m\n"
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
echo -e "\e[1;31mTesting authentication \e[0m"
echo "Sending request to /$ROUTE to make sure it is blocked"
curl -s "http://${HOST_IP}:8000/${ROUTE}" \
-H "Accept: application/json"
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
RAW_TOKEN=$(curl -s -X POST \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "username=test" \
-d "password=test" \
-d 'grant_type=password' \
-d "client_id=myApp" \
http://${HOST_IP}:8180/auth/realms/${REALM_NAME}/protocol/openid-connect/token)
echo ${RAW_TOKEN}
TOKEN=$(echo ${RAW_TOKEN} | jq -r '.access_token')
echo ${TOKEN}
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
curl -s "http://${HOST_IP}:8000/${ROUTE}" \
-H "Accept: application/json" \
-H "Authorization: Bearer $TOKEN"
echo -e "\e[1;31m Finalised integration test. \e[0m\n"
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
echo Konga admin: http://${HOST_IP}:1337 "(Please activate the kong node)"
echo KeyCloak admin: http://${HOST_IP}:8180/auth/admin