Skip to content

Commit

Permalink
Merge pull request #9 from kbss-cvut/add-docker-compose-for-internal-…
Browse files Browse the repository at this point in the history
…auth

[New] Add example docker-compose setup for internal authentication
  • Loading branch information
blcham authored Nov 22, 2023
2 parents 9696c48 + 6596d4a commit 6668946
Show file tree
Hide file tree
Showing 15 changed files with 1,956 additions and 0 deletions.
8 changes: 8 additions & 0 deletions deploy/internal-auth/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# In this setting it is assumed there is no apapche/nginx in front of the application
RECORD_SET_NAME=iauth-example
PORT=1235
RECORD_MANAGER_SERVER_URL=http://localhost:1235/record-manager/record-manager-server
FORMGEN_SERVICE_URL=http://s-pipes-engine:8080/s-pipes/service?_pId=clone-form
RECORD_MANAGER_APP_TITLE=Record manager
RECORD_MANAGER_BASENAME=/record-manager
LANGUAGE=en
22 changes: 22 additions & 0 deletions deploy/internal-auth/db-server/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM ontotext/graphdb:10.2.0

# Override parent entrypoint
ENTRYPOINT []

ENV GRAPHDB_HOME=/opt/graphdb/home
ENV GRAPHDB_INSTALL_DIR=/opt/graphdb/dist

WORKDIR ${GRAPHDB_HOME}

# Install libs related to RDF processing
RUN apk add py3-rdflib
RUN apk add perl-uri

# Copy initialization data and repository config
COPY init-data /root/graphdb-import
COPY init-config /repo-config
COPY bin/* ${GRAPHDB_INSTALL_DIR}/bin/

EXPOSE 7200

CMD ${GRAPHDB_INSTALL_DIR}/bin/repo-init.sh /repo-config ${GRAPHDB_HOME} & ${GRAPHDB_INSTALL_DIR}/bin/graphdb -Dgraphdb.home=${GRAPHDB_HOME} -Dgraphdb.logback=${GRAPHDB_INSTALL_DIR}/conf/logback.xml
75 changes: 75 additions & 0 deletions deploy/internal-auth/db-server/bin/get-rdf-subject-by-type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/usr/bin/python3

import sys
from rdflib import Graph, URIRef

def log(message):
print("ERROR: " + message, file=sys.stderr)

def check_params():
if len(sys.argv) != 3:
log(f"""Illegal number of parameters.
Script returns single subject of triple matching the pattern '?result a <type-uri>'
from the file specified by <rdf-file-path>.
Usage: {sys.argv[0]} <rdf-file-path> <type-uri>
Example: {sys.argv[0]} "./init-data/forms/example-1.ttl" "http://onto.fel.cvut.cz/ontologies/form/form-template"
""")
sys.exit(1)

def check_only_one_instance(results, rdf_type):
if len(results) == 0:
log(f"No instance found for the specified {rdf_type}.")
sys.exit(2)
elif len(results) > 1:
error_message = f"Multiple instances found for the type {rdf_type}. Triple that match pattern '?s a <{rdf_type}>' are:\n"
for row in results:
subject = row[0]
error_message += f" {subject.n3()} a <{rdf_type}> .\n"
log(error_message)
sys.exit(3)


def load_rdf_graph(file_path):
# Load RDF file into an RDFLib graph
g = Graph()

# Explicitly specify the format based on the file extension
if file_path.endswith(".ttl"):
g.parse(file_path, format="turtle")
elif file_path.endswith(".rdf"):
g.parse(file_path, format="xml")
else:
log(f"Unsupported RDF file format of {file_path}.")
sys.exit(1)

return g

def main():
check_params()

file_path = sys.argv[1]
rdf_type = URIRef(sys.argv[2])

g = load_rdf_graph(file_path)

# Query for subjects with the specified RDF type
query = f"""
SELECT ?subject
WHERE {{
?subject a <{rdf_type}>.
}}
"""

results = g.query(query)

check_only_one_instance(results, rdf_type)

for row in results:
subject = row[0]
print(subject.n3())

if __name__ == "__main__":
main()
73 changes: 73 additions & 0 deletions deploy/internal-auth/db-server/bin/get-value-of-rdf-property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/usr/bin/python3

import sys
from rdflib import Graph, URIRef

def log(message):
print("ERROR: " + message, file=sys.stderr)

def check_params():
if len(sys.argv) != 3:
log(f"""Illegal number of parameters.
Script returns single value of <rdf-property-uri> from file specified by <rdf-file-path>.
Usage: {sys.argv[0]} <rdf-file-path> <rdf-property-uri>
Example: {sys.argv[0]} "./init-config/repo-config.ttl" "http://www.openrdf.org/config/repository#repositoryID"
""")
sys.exit(1)


def check_property_has_single_value(results, rdf_property):
if len(results) == 0:
log(f"No values found for the specified property {rdf_property}.")
sys.exit(2)
elif len(results) > 1:
error_message = f"Multiple values found for the property {rdf_property}. Triple that match pattern '?s <{rdf_property}> ?o' are:\n"
for row in results:
subject, value = row
error_message += f" {subject} {rdf_property} {value} .\n"
log(error_message)
sys.exit(3)

def load_rdf_graph(file_path):
# Load RDF file into an RDFLib graph
g = Graph()

# Explicitly specify the format based on the file extension
if file_path.endswith(".ttl"):
g.parse(file_path, format="turtle")
elif file_path.endswith(".rdf"):
g.parse(file_path, format="xml")
else:
log(f"Unsupported RDF file format of {file_path}.")
sys.exit(1)
return g

def main():
check_params()

file_path = sys.argv[1]
rdf_property = URIRef(sys.argv[2])

g = load_rdf_graph(file_path)

# Query for subjects with the specified property
query = f"""
SELECT ?subject ?value
WHERE {{
?subject <{rdf_property}> ?value.
}}
"""
results = g.query(query)

check_property_has_single_value(results, rdf_property)

for row in results:
subject, value = row
print(f"{value}")

if __name__ == "__main__":
main()

99 changes: 99 additions & 0 deletions deploy/internal-auth/db-server/bin/rdf4j-deploy-context.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/bin/bash


### DEFAULTS ###
APPEND=true
CONTENT_TYPE='text/turtle'
#CONTENT_TYPE='application/trig'
#CONTENT_TYPE='application/rdf+xml'
#CONTENT_TYPE='text/x-nquads'
#CONTENT_TYPE='application/ld+json'

function print_usage() {
echo "Rdf4j server deploy script."
echo "Parameters: "
echo -e "\t-R -- replace content of GRAPH_IRI (appends by default)"
echo -e "\t-C CONTENT_TYPE -- content type of input FILES, i.e. text/turtle (default), application/rdf+xml ..."
echo "Usage: "
echo -e "\t$0 -R -C <CONTENT_TYPE> -s <RDF4J_SERVER> -r <REPO_ID> -c <GRAPH_IRI> <FILES>"
echo -e "\t$0 -R -C <CONTENT_TYPE> -u <RDF4J_REPOSITORY_URL> -c <GRAPH_IRI> <FILES>"
echo "Examples: "
echo -e "\tEXAMPLE-1 (append context): $0 -s http://onto.mondis.cz/openrdf-RDF4J -r ontomind_owlim -c http://onto.mondis.cz/resource/mdr-1.0-SNAPSHOT-temporary mdr.owl"
echo -e "\tEXAMPLE-2 (replace context): $0 -R -C 'text/turtle' -s http://onto.fel.cvut.cz/rdf4j-server -r test -c http://vfn.cz/ontologies/fertility-saving-study study-model.ttl"
echo -e "\tEXAMPLE-3 (replace repository): $0 -R -C 'text/x-nquads' -s http://onto.fel.cvut.cz/rdf4j-server -r test fss-study-formgen.ng"
echo -e "\tEXAMPLE-4 (use of repository url): $0 -R -C 'text/turtle' -u http://onto.fel.cvut.cz/rdf4j-server/repositories/test -c http://vfn.cz/ontologies/fertility-saving-study study-model.ttl"
}

if [ "$#" -eq 0 ]; then
print_usage
exit
fi


while getopts "h:s:r:u:c:RC:" arg; do
case $arg in
h)
print_usage
exit 0
;;
s)
RDF4J_SERVER=$OPTARG
;;
r)
REPOSITORY=$OPTARG
;;
u)
REPOSITORY_URL=$OPTARG
;;
c)
GRAPH=$(perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "<$OPTARG>")
;;
R)
APPEND=false
;;
C)
CONTENT_TYPE=$OPTARG
;;
esac
done

shift $(($OPTIND-1))
FILES=$@

REPOSITORY_URL=${REPOSITORY_URL:-$RDF4J_SERVER/repositories/$REPOSITORY}

echo "INFO: *** DEPLOYING ***"
[ ! -z "$REPOSITORY_URL" ] && echo "INFO: - repository url: $REPOSITORY_URL"
[ ! -z "$RDF4J_SERVER" ] && echo "INFO: destination server: $RDF4J_SERVER"
[ ! -z "$REPOSITORY" ] && echo "INFO: - repository $REPOSITORY"
echo "INFO: - graph $GRAPH"
echo "INFO: - files $FILES"

if [ "$APPEND" = false ] ; then
echo "INFO: *** CLEARING THE GRAPH ***"
TEMP_FILE=`mktemp`
QUERY_PARAMS="context=$GRAPH"
if [ ! "$GRAPH" ]; then QUERY_PARAMS= ; fi
curl $REPOSITORY_URL/statements?$QUERY_PARAMS -v -X DELETE &> $TEMP_FILE
cat $TEMP_FILE | grep "HTTP/1.1 204" &>/dev/null && echo 'INFO: clearing graph was sucessfull'
cat $TEMP_FILE | grep "HTTP/1.1 204" &>/dev/null || ( echo 'ERROR: clearing graph failed. Output of the process : '; cat $TEMP_FILE )
fi

echo "INFO: *** SENDING DATA ***"
for FILE in $FILES
do
echo INFO: " -- sending $FILE";
TEMP_FILE=`mktemp`
QUERY_PARAMS="context=$GRAPH"
if [ ! "$GRAPH" ]; then QUERY_PARAMS= ; fi

curl -X POST -H "Content-Type: $CONTENT_TYPE" --data-binary "@$FILE" -o - -v "$REPOSITORY_URL/statements?$QUERY_PARAMS" 2> $TEMP_FILE
cat $TEMP_FILE | grep "HTTP/1.1 204" &>/dev/null && echo 'INFO: sending data was sucessfull'
cat $TEMP_FILE | grep "HTTP/1.1 204" &>/dev/null || ( echo 'ERROR: sending data failed. Output of the process : '; cat $TEMP_FILE )
done;

echo "INFO: *** CHECKING ***"
TEMP_FILE=`mktemp`
GRAPH_SIZE=`curl $REPOSITORY_URL/size 2> $TEMP_FILE`
echo "INFO: size of the new graph is $GRAPH_SIZE"

48 changes: 48 additions & 0 deletions deploy/internal-auth/db-server/bin/repo-init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/sh

#
# Initializes Record Manager GraphDB repositories if it does not already exist
#

SOURCE_DIR=$1
GRAPHDB_HOME=$2

SCRIPT_DIR="`dirname $0`"

echo "INFO: Running initializer for Record Manager repositories ..."

# Wait for GraphDB to start up
echo "INFO: Waiting for GraphDB to start up..."
sleep 15s

ls ${SOURCE_DIR}/*-config.ttl | while read REPO_CONFIG_FILE; do

REPO_NAME=`$SCRIPT_DIR/get-value-of-rdf-property.py $REPO_CONFIG_FILE 'http://www.openrdf.org/config/repository#repositoryID'`

if [ -z "$REPO_NAME" ]; then
echo "ERROR: Could not parse repository name from file $REPO_CONFIG_FILE"
exit 1
fi

if [ ! -d ${GRAPHDB_HOME}/data/repositories/${REPO_NAME} ] || [ -z "$(ls -A ${GRAPHDB_HOME})/data/repositories/${REPO_NAME}" ]; then
echo "INFO: Initializing repository $REPO_NAME..."

# Create repository based on configuration
echo "INFO: Creating repository $REPO_NAME..."
curl -X POST --header "Content-Type: multipart/form-data" -F "config=@${REPO_CONFIG_FILE}" "http://localhost:7200/rest/repositories"
echo "INFO: Repository $REPO_NAME successfully initialized."
else
echo "INFO: Repository $REPO_NAME already exists. Skipping initialization..."
fi
done


DATA_DIR=/root/graphdb-import

ls ${DATA_DIR}/forms/*.ttl | while read DATA_FILE; do
REPO_NAME="record-manager-formgen"
CONTEXT=`$SCRIPT_DIR/get-rdf-subject-by-type.py $DATA_FILE 'http://onto.fel.cvut.cz/ontologies/form/form-template' | sed 's/[<>]//g'`

echo "INFO: Deploying form templates ${DATA_FILE} into ${CONTEXT}."
$SCRIPT_DIR/rdf4j-deploy-context.sh -R -C 'text/turtle' -s http://localhost:7200 -r ${REPO_NAME} -c ${CONTEXT} ${DATA_FILE}
done
Loading

0 comments on commit 6668946

Please sign in to comment.