Skip to content

Commit

Permalink
use jq to construct credentials payload for GraphDB (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
alyssadai authored Feb 11, 2025
1 parent ef1dcc5 commit aff9915
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions scripts/graphdb_setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ SCRIPT_DIR=$(dirname "$0")

echo "The GraphDB server is being accessed at http://localhost:${NB_GRAPH_PORT}."

# Install jq to construct JSON payloads for curl requests while preserving special characters
echo "Installing jq..."
apk add --no-cache jq

##### First time GraphDB setup #####

if [ "${RUN_USER_SETUP}" = "on" ]; then
Expand All @@ -144,8 +148,9 @@ if [ "${RUN_USER_SETUP}" = "on" ]; then
# 1. Change database admin password
echo "Changing the admin password (note: if you have previously set the admin password, this has no effect)..."
# TODO: To change a *previously set* admin password, we need to also provide the current password via -u
curl -X PATCH --header 'Content-Type: application/json' http://localhost:${NB_GRAPH_PORT}/rest/security/users/admin -d "{\"password\": \""${ADMIN_PASS}"\"}"

json_payload=$(jq -n --arg pw "${ADMIN_PASS}" '{password: $pw}')
curl -X PATCH --header 'Content-Type: application/json' http://localhost:${NB_GRAPH_PORT}/rest/security/users/admin -d "${json_payload}"

# 2. If security is not enabled, enable it (i.e. allow only authenticated users access)
is_security_enabled=$(curl -s -X GET http://localhost:${NB_GRAPH_PORT}/rest/security)
if [ "${is_security_enabled}" = "false" ]; then
Expand All @@ -160,12 +165,8 @@ if [ "${RUN_USER_SETUP}" = "on" ]; then
# TODO: Separate this out from the first-time setup? As this can technically be run at any time to create additional users.
# NOTE: If user already exists, response will be "An account with the given username already exists." OK for script.
echo "Creating a new database user ${NB_GRAPH_USERNAME}..."
curl -X POST --header 'Content-Type: application/json' -u "admin:${ADMIN_PASS}" -d @- http://localhost:${NB_GRAPH_PORT}/rest/security/users/${NB_GRAPH_USERNAME} <<EOF
{
"username": "${NB_GRAPH_USERNAME}",
"password": "${NB_GRAPH_PASSWORD}"
}
EOF
json_payload=$(jq -n --arg un "${NB_GRAPH_USERNAME}" --arg pw "${NB_GRAPH_PASSWORD}" '{username: $un, password: $pw}')
curl -X POST --header 'Content-Type: application/json' -u "admin:${ADMIN_PASS}" -d "${json_payload}" http://localhost:${NB_GRAPH_PORT}/rest/security/users/${NB_GRAPH_USERNAME}
fi


Expand Down

0 comments on commit aff9915

Please sign in to comment.