forked from openwallet-foundation/acapy-vc-authn-oidc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanage
executable file
·421 lines (351 loc) · 11.4 KB
/
manage
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#!/bin/bash
export MSYS_NO_PATHCONV=1
export DOCKERHOST=${APPLICATION_URL-$(docker run --rm --net=host eclipse/che-ip)}
set -e
function echoError (){
_msg=${1}
_red='\e[31m'
_nc='\e[0m' # No Color
echo -e "${_red}${_msg}${_nc}"
}
function echoWarning (){
_msg=${1}
_yellow='\e[33m'
_nc='\e[0m' # No Color
echo -e "${_yellow}${_msg}${_nc}"
}
function echoRed (){
_msg=${1}
_red='\e[31m'
_nc='\e[0m' # No Color
echo -e "${_red}${_msg}${_nc}"
}
function echoYellow (){
_msg=${1}
_yellow='\e[33m'
_nc='\e[0m' # No Color
echo -e "${_yellow}${_msg}${_nc}"
}
function isInstalled () {
rtnVal=$(type "$1" >/dev/null 2>&1)
rtnCd=$?
if [ ${rtnCd} -ne 0 ]; then
return 1
else
return 0
fi
}
function isS2iInstalled () {
S2I_EXE=s2i
if ! isInstalled ${S2I_EXE}; then
echoError "The ${S2I_EXE} executable is needed and not on your path."
echoError "It can be downloaded from here: https://github.com/openshift/source-to-image/releases"
echoError "Make sure you extract the binary and place it in a directory on your path."
exit 1
fi
}
function isCurlInstalled () {
CURL_EXE=curl
if ! isInstalled ${CURL_EXE}; then
echoError "The ${CURL_EXE} executable is required and was not found on your path."
echoError "If your shell of choice doesn't come with curl preinstalled, try installing it using either [Homebrew](https://brew.sh/) (MAC) or [Chocolatey](https://chocolatey.org/) (Windows)."
exit 1
fi
}
function isJQInstalled () {
JQ_EXE=jq
if ! isInstalled ${JQ_EXE}; then
echoError "The ${JQ_EXE} executable is required and was not found on your path."
echoError "Installation instructions can be found here: https://stedolan.github.io/jq/download"
echoError "Alternatively, a package manager such as Chocolatey (Windows) or Brew (Mac) can be used to install this dependecy."
exit 1
fi
}
function isNgrokInstalled () {
NGROK_EXE=ngrok
if ! isInstalled ${NGROK_EXE}; then
echoError "The ${NGROK_EXE} executable is needed and not on your path."
echoError "It can be downloaded from here: https://ngrok.com/download"
echoError "Alternatively, a package manager such as Chocolatey (Windows) or Brew (Mac) can be used to install this dependecy."
exit 1
fi
}
SCRIPT_HOME="$(cd "$(dirname "$0")" && pwd)"
# =================================================================================================================
# Usage:
# -----------------------------------------------------------------------------------------------------------------
usage() {
cat <<-EOF
Usage: $0 [command] [options]
Commands:
build - Build the docker images for the project.
You need to do this first, since the builds require
a combination of Docker and S2I builds.
You can build individual components as shown below, components that have dependencies
will have these dependencies built too.
Examples:
- Build the api only.
$0 build oidc-django
By default all containers that components comprise of, will be rebuilt.
$0 build
up - Creates the application containers from the built images
and starts the services based on the docker-compose.yml file.
You can pass in a list of containers to start.
By default all containers will be started.
Examples:
$0 start
$0 start oidc-django
start - Same as up
logs - Display the logs from the docker compose run (ctrl-c to exit).
stop - Stops the services. This is a non-destructive process. The volumes and containers
are not deleted so they will be reused the next time you run start.
down - Brings down the services and removes the volumes (storage) and containers.
rm - Same as down
EOF
exit 1
}
# -----------------------------------------------------------------------------------------------------------------
# Default Settings:
# -----------------------------------------------------------------------------------------------------------------
DEFAULT_CONTAINERS="oidc-angular oidc-django database"
# -----------------------------------------------------------------------------------------------------------------
# Functions:
# -----------------------------------------------------------------------------------------------------------------
build-oidc-django() {
#
# oidc-django
#
BASE_IMAGE="centos/python-36-centos7"
echo -e "\nBuilding oidc-django image from ${BASE_IMAGE}..."
${S2I_EXE} build \
-e "UPGRADE_PIP_TO_LATEST=true" \
-e "PIP_NO_CACHE_DIR=" \
-v "${COMPOSE_PROJECT_NAME}_dcbr-pip-cache:/opt/app-root/.cache/pip" \
'../oidc-django' \
"$BASE_IMAGE" \
'oidc-django'
# this is only required in Windows
if [[ "$OSTYPE" == "msys" ]]; then
echo -e "\nFixing file permissions in final oidc-django image"
docker build -q \
--build-arg BASE_IMAGE='oidc-django:latest' \
-t 'oidc-django' \
-f 'utils/fix-permissions/Dockerfile' 'utils/'
fi
}
build-oidc-angular() {
echo -e "\nBuilding oidc-angular image..."
docker build -t oidc-angular -f ../oidc-angular/Dockerfile ../oidc-angular
}
build-all() {
build-oidc-django
build-oidc-angular
}
updateKeycloakIdentityProviderUrls() {
echo "update keycloak realm config..."
echo "set identity provider tokenUrl=$NGROK_CONTROLLER_URL/vc/connect/token"
echo "set identity provider authorizationUrl=$NGROK_CONTROLLER_URL/vc/connect/authorize"
# auth
TKN=$(curl -X POST 'http://localhost: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')
# getting identity provider config and prepare that to update update URL's
ID_PROVIDER=$(curl -X GET 'http://localhost:8180/auth/admin/realms/vc-authn/identity-provider/instances/vc-authn' \
-H "Accept: application/json" \
-H "Authorization: Bearer $TKN" | \
jq --arg tokenUrl "$NGROK_CONTROLLER_URL/vc/connect/token" --arg authUrl "$NGROK_CONTROLLER_URL/vc/connect/authorize" '.config.tokenUrl = $tokenUrl | .config.authorizationUrl = $authUrl')
# save/update identity provider config
curl -X PUT 'http://localhost:8180/auth/admin/realms/vc-authn/identity-provider/instances/vc-authn' \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "Authorization: Bearer $TKN" \
--data-raw "${ID_PROVIDER}"
}
configureEnvironment() {
if [ -f .env ]; then
while read line; do
if [[ ! "$line" =~ ^\# ]] && [[ "$line" =~ .*= ]]; then
export ${line//[$'\r\n']}
fi
done <.env
fi
for arg in "$@"; do
# Remove recognized arguments from the list after processing.
shift
# echo "arg: ${arg}"
# echo "Remaining: ${@}"
case "$arg" in
*=*)
# echo "Exporting ..."
export "${arg}"
;;
*)
# echo "Saving for later ..."
# If not recognized, save it for later processing ...
set -- "$@" "$arg"
;;
esac
done
export COMPOSE_PROJECT_NAME="${COMPOSE_PROJECT_NAME:-vc-authn-demo}"
export STI_SCRIPTS_PATH=${STI_SCRIPTS_PATH:-/usr/libexec/s2i}
export DEBUG=True
# database
export POSTGRESQL_DATABASE="DCBR"
export POSTGRESQL_USER="DB_USER"
export POSTGRESQL_PASSWORD="DB_PASSWORD"
export EXPOSED_DATABASE_SERVICE_PORT=5434
# oidc-django
export APP_SCRIPT="run-prod.sh"
export DJANGO_DEBUG=${DEBUG}
export DATABASE_SERVICE_HOST="database"
export DATABASE_SERVICE_PORT=5432
export DATABASE_NAME=${POSTGRESQL_DATABASE}
export DATABASE_USER=${POSTGRESQL_USER}
export DATABASE_PASSWORD=${POSTGRESQL_PASSWORD}
export DJANGO_SECRET_KEY="(*=#bv_2cgwguymnxlsf2)m96uj+jn6hai^=(czao&6e=%*)+n"
export DJANGO_DEBUG=True
export DJANGO_ADMIN_USER="admin"
export DJANGO_ADMIN_PASSWORD="admin"
export DJANGO_ADMIN_EMAIL="[email protected]"
export OIDC_RP_PROVIDER_ENDPOINT="${NGROK_CONTROLLER_URL:-http://DOCKERHOST:5000}"
export OIDC_RP_CLIENT_ID="django-oidc-demo"
export OIDC_RP_CLIENT_SECRET="django-oidc-demo"
export OIDC_RP_SCOPES="openid profile vc_authn"
export VC_AUTHN_PRES_REQ_CONF_ID="verified-email"
}
getInputParams() {
ARGS=""
for arg in $@; do
case "$arg" in
*=*)
# Skip it
;;
*)
ARGS+=" $arg"
;;
esac
done
echo ${ARGS}
}
getStartupParams() {
CONTAINERS=""
ARGS=""
for arg in $@; do
case "$arg" in
*=*)
# Skip it
;;
-*)
ARGS+=" $arg"
;;
*)
CONTAINERS+=" $arg"
;;
esac
done
if [ -z "$CONTAINERS" ]; then
CONTAINERS="$DEFAULT_CONTAINERS"
fi
echo ${ARGS} ${CONTAINERS}
}
deleteVolumes() {
_projectName=${COMPOSE_PROJECT_NAME:-docker}
echo "Stopping and removing any running containers ..."
docker-compose down -v
_pattern="^${_projectName}_\|^docker_"
_volumes=$(docker volume ls -q | grep ${_pattern})
if [ ! -z "${_volumes}" ]; then
echo "Removing project volumes ..."
echo ${_volumes} | xargs docker volume rm
else
echo "No project volumes exist."
fi
echo "Removing build cache ..."
rm -Rf ../client/tob-web/.cache
}
toLower() {
echo $(echo ${@} | tr '[:upper:]' '[:lower:]')
}
echoError (){
_msg=${1}
_red='\033[0;31m'
_nc='\033[0m' # No Color
echo -e "${_red}${_msg}${_nc}" >&2
}
functionExists() {
(
if [ ! -z ${1} ] && type ${1} &>/dev/null; then
return 0
else
return 1
fi
)
}
# =================================================================================================================
pushd ${SCRIPT_HOME} >/dev/null
COMMAND=$(toLower ${1})
shift || COMMAND=usage
case "${COMMAND}" in
start|up)
isS2iInstalled
isJQInstalled
# Set environment variables
if [ -z "$NGROK_CONTROLLER_URL" ]; then
isCurlInstalled
isNgrokInstalled
export NGROK_CONTROLLER_URL=$(${CURL_EXE} http://localhost:4040/api/tunnels | ${JQ_EXE} --raw-output '.tunnels | map(select(.name | contains("vc-authn-controller"))) | .[0] | .public_url')
fi
if [ -z "$NGROK_CONTROLLER_URL" ]; then
echoError "The NGROK_CONTROLLER_URL has not been set."
exit 1
fi
echo "Running in demo mode, will use ${NGROK_CONTROLLER_URL} for the controller endpoint."
_startupParams=$(getStartupParams --force-recreate $@)
configureEnvironment "$@"
updateKeycloakIdentityProviderUrls
docker-compose up -d ${_startupParams}
;;
restart)
_startupParams=$(getStartupParams $@)
configureEnvironment "$@"
docker-compose stop ${_startupParams}
docker-compose up -d ${_startupParams}
;;
logs)
configureEnvironment "$@"
docker-compose logs -f
;;
stop)
configureEnvironment
docker-compose stop
;;
rm|down)
configureEnvironment
deleteVolumes
;;
build)
isS2iInstalled
configureEnvironment "$@"
buildImage=$(toLower ${1})
shift || buildImage=all
# buildImage=$(echo ${buildImage} | sed s~^tob-~~)
case "$buildImage" in
*=*)
buildImage=all
;;
esac
if functionExists "build-${buildImage}"; then
eval "build-${buildImage}"
else
echoError "\nThe build function, build-${buildImage}, does not exist. Please check your build parameters and try again.\nUse '-h' to get full help details."
exit 1
fi
;;
*)
usage
;;
esac
popd >/dev/null