diff --git a/.gitignore b/.gitignore index da9bbec808da1301c6f5a214a993667863f7c737..6244ba5c3e62b087d9198991c5e9d5e62eca6ca0 100644 --- a/.gitignore +++ b/.gitignore @@ -6,9 +6,9 @@ tmp/* bin/smartdata/importers/ bin/workflow/tutorial/ cassandra -certmanager +/certmanager log -.* +/.* bin/workflow new/ prometheus/ diff --git a/Dockerfile b/Dockerfile index 95748f3f83d620ff1dee5f467bd27206e9ad934d..e3b575d9068a24993c1ebf03c038cbda40fb1047 100644 --- a/Dockerfile +++ b/Dockerfile @@ -45,3 +45,10 @@ ADD docker/web/index.html /var/www/html/ #smartdata/Config.php #RUN mkdir /usr/local/tmp/ && touch /usr/local/tmp/log && chmod 777 /usr/local/tmp/log + +# Certificate management +RUN mkdir /certmanager +ADD docker/certmanager/config /certmanager/ +ADD docker/certmanager/genclient.sh /certmanager/ +ADD docker/certmanager/create-ca.sh /certmanager/ + diff --git a/README.md b/README.md index 8346e756661c21363c20a145e58a1f670aa182bf..d0ad9455ba1f947db354f2eeb8bd664cc6772f08 100644 --- a/README.md +++ b/README.md @@ -4,3 +4,15 @@ cp bin/smartdata/Config.php.template bin/smartdata/Config.php ``` * Edit `bin/smartdata/Config.php` to customize your environment. + +# Initial setup +Execute the following command to create the CA certificate in the environment: +``` +docker compose exec -it web /certmanager/create-ca.sh +``` + +# Create client certificates +Execute the following command to create client certificates: +``` +docker compose exec -it web /certmanager/genclient.sh clientId clientDescription +``` diff --git a/docker-compose.yml b/docker-compose.yml index 8351ac692718169438d90d6af702ca4a7b8e6904..4a33759bda4f17d3893eded01d1049592664693c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,6 +12,7 @@ services: #TODO Fix this in the code - ./tmp:/var/www/html/tmp - ./tmp:/smartdata/tmp + - ./docker/certmanager/certificates:/certmanager/certificates #TODO Custom certificates #- ./crt:/etc/apache2/ssl/server.crt #- ./key:/etc/apache2/ssl/server.key diff --git a/docker/certmanager/.gitignore b/docker/certmanager/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..195e616971c0463859cc440c8e64bdc745cfb309 --- /dev/null +++ b/docker/certmanager/.gitignore @@ -0,0 +1 @@ +/certificates/ \ No newline at end of file diff --git a/docker/certmanager/config b/docker/certmanager/config new file mode 100644 index 0000000000000000000000000000000000000000..d265935490a4a2b033b012f3cc8870c279475ad6 --- /dev/null +++ b/docker/certmanager/config @@ -0,0 +1,12 @@ +export CERTNAME=rootCA +export KEYSIZE=2048 +export CLIENTEXP=1491 +export ROOTEXP=2992 + +export NUMCLIENTS=1 + +export STATE="SC" +export CITY="Florianopolis" +export ORG="UFSC" +export UNIT="Lisha" +export HOST="localhost" diff --git a/docker/certmanager/create-ca.sh b/docker/certmanager/create-ca.sh new file mode 100755 index 0000000000000000000000000000000000000000..929a585ef379087a22a7e6ec24c67bddaf24fc23 --- /dev/null +++ b/docker/certmanager/create-ca.sh @@ -0,0 +1,18 @@ +#!/bin/bash +# Caciano Machado/Juliano Zatta - 16/10/2017 + +cd /certmanager + +. config + +cd certificates + +if [ -a ${CERTNAME}.pem ]; then + echo "WARNING: Certificate with name $CERTNAME already exists." + echo "WARNING: If you really want to delete it then do it manually." +else + ROOTCA_SERIAL=`openssl rand -hex 8` + openssl genrsa -out ${CERTNAME}.key $KEYSIZE + openssl req -subj "/C=BR/ST=$STATE/L=$CITY/O=$ORG/OU=$UNIT/CN=Root\ Certificate" -x509 -new -nodes -key ${CERTNAME}.key -sha256 -days $ROOTEXP -out ${CERTNAME}.pem -set_serial "0x$ROOTCA_SERIAL" + echo $ROOTCA_SERIAL > rootCA.srl +fi \ No newline at end of file diff --git a/docker/certmanager/genclient.sh b/docker/certmanager/genclient.sh new file mode 100755 index 0000000000000000000000000000000000000000..6c5dccb63ac02048f9d4982fb5a53fe827df5b98 --- /dev/null +++ b/docker/certmanager/genclient.sh @@ -0,0 +1,59 @@ +#!/bin/bash +# Caciano Machado/Juliano Zatta - 16/10/2017 + +cd /certmanager + +. config + +cd certificates + +# Initilize if not available the certificate list +NUMPREVCERT=`tail -n 1 certificate_list | cut -d " " -f 1` +ISNUM='^[0-9]+$' + +# Update - 30-10-2020 - Roberto M. Scheffel - Added receiver and purpose description, for documentation + +receiver=$1 +descr=$2 +if [ -z "$receiver" ] +then + echo 'Usage: ' $0 '<receiver name>' '<purpose short description>.' + exit +fi +if [ -z "$descr" ] +then + echo 'Usage: ' $0 '<receiver name>' '<purpose short description>.' + exit +fi + +if [[ -a certificate_list ]]; then + if ! [[ $NUMPREVCERT =~ $ISNUM ]]; then + echo "ERROR: File certificate_list corrupted. This file stores the list of generated certificates and respective serial numbers." + exit -1 + fi + CURRCLIENT=$((NUMPREVCERT + 1)) + LASTCLIENT=$((NUMCLIENTS + NUMPREVCERT)) +else + CURRCLIENT=1 + LASTCLIENT=$NUMCLIENTS +fi + +for i in `seq $CURRCLIENT $LASTCLIENT`; do + # Generate RSA key + + echo "openssl genrsa -out client-$i.key $KEYSIZE" + openssl genrsa -out client-$i.key $KEYSIZE + + # Generate certificate request + echo "openssl req -subj \"/C=BR/ST=$STATE/L=$CITY/O=$ORG/OU=$UNIT/CN=$HOST\" -new -key client-$i.key -out client-$i.req" + openssl req -subj "/C=BR/ST=$STATE/L=$CITY/O=$ORG/OU=$UNIT/CN=$HOST" -new -key client-$i.key -out client-$i.req + # Generate certificate + echo "openssl x509 -req -in client-$i.req -CA ${CERTNAME}.pem -CAkey ${CERTNAME}.key -CAcreateserial -out client-$i.pem -days $CLIENTEXP -sha256" + openssl x509 -req -in client-$i.req -CA ${CERTNAME}.pem -CAkey ${CERTNAME}.key -CAcreateserial -out client-$i.pem -days $CLIENTEXP -sha256 + serial=`openssl x509 -in client-$i.pem -serial -noout | cut -d "=" -f 2` + echo $i $serial + echo $i $serial `date "+%F-%T"` `whoami` ' | ' $receiver ' | ' $descr >> certificate_list + mv client-$i.key client-$i-$serial.key + mv client-$i.pem client-$i-$serial.pem + rm -f client-$i.req +done \ No newline at end of file