diff --git a/desuto-couchdb/docker-entrypoint.sh b/desuto-couchdb/docker-entrypoint.sh index 419550d..380944a 100755 --- a/desuto-couchdb/docker-entrypoint.sh +++ b/desuto-couchdb/docker-entrypoint.sh @@ -1,109 +1,109 @@ #!/bin/bash # Licensed under the Apache License, Version 2.0 (the "License"); you may not # use this file except in compliance with the License. You may obtain a copy of # the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations under # the License. set -m if [ "$1" = '/opt/couchdb/bin/couchdb' ]; then # we need to set the permissions here because docker mounts volumes as root chown -R couchdb:couchdb /opt/couchdb chmod -R 0770 /opt/couchdb/data chmod 664 /opt/couchdb/etc/*.ini chmod 664 /opt/couchdb/etc/local.d/*.ini chmod 775 /opt/couchdb/etc/*.d if [ ! -z "$NODENAME" ] && ! grep "couchdb@" /opt/couchdb/etc/vm.args; then echo "-name couchdb@$NODENAME" >> /opt/couchdb/etc/vm.args fi if [ "$COUCHDB_USER" ] && [ "$COUCHDB_PASSWORD" ]; then # Create admin printf "[admins]\n%s = %s\n" "$COUCHDB_USER" "$COUCHDB_PASSWORD" > /opt/couchdb/etc/local.d/docker.ini chown couchdb:couchdb /opt/couchdb/etc/local.d/docker.ini fi # if we don't find an [admins] section followed by a non-comment, display a warning if ! grep -Pzoqr '\[admins\]\n[^;]\w+' /opt/couchdb/etc/local.d/*.ini; then # The - option suppresses leading tabs but *not* spaces. :) cat >&2 <<-'EOWARN' **************************************************** WARNING: CouchDB is running in Admin Party mode. This will allow anyone with access to the CouchDB port to access your database. In Docker's default configuration, this is effectively any other container on the same system. Use "-e COUCHDB_USER=admin -e COUCHDB_PASSWORD=password" to set it in "docker run". **************************************************** EOWARN fi #exec gosu couchdb "$@" # Run CouchdB in background gosu couchdb "$@" & # Wait for the server to be up sleep 10 # Variables (set your own) user=$COUCHDB_USER pass=$COUCHDB_PASSWORD host=$COUCHDB_HOST port=$COUCHDB_PORT protocol=$COUCHDB_PROTOCOL dbname=$COUCHDB_DB_NAME permdbname=$COUCHDB_PERM_DB_NAME echo "Base URL for configuring the DB : $protocol://$user:$pass"'@'"$host:$port/" #################### ### Configure DB ### #################### # Set timeout of cookies to 24h curl -H "Content-Type: application/json" -X PUT -d '86400' "$protocol://$user:$pass"'@'"$host:$port/$dbname" # Create system DBs curl -X PUT "$protocol://$user:$pass"'@'"$host:$port/_users" curl -X PUT "$protocol://$user:$pass"'@'"$host:$port/_replicator" curl -X PUT "$protocol://$user:$pass"'@'"$host:$port/_global_changes" # Create annotation DB curl -X PUT "$protocol://$user:$pass"'@'"$host:$port/$dbname" # Create permissions DB curl -X PUT "$protocol://$user:$pass"'@'"$host:$port/$permdbname" # Make permissions DB read-only for non-annotation admins - curl -X PUT $protocol://$user:$pass@$host:$port/$permdbname/_design/auth -d "{ \"language\": \"javascript\", \"validate_doc_update\": \"function(newDoc, oldDoc, userCtx) { if (userCtx.roles.indexOf('annotationadmins') !== -1 || userCtx.name === 'admin') { return; } else { throw ({ forbidden: 'Only annotation admins may edit the database' }); } }\"}" + curl -X PUT "$protocol://$user:$pass@$host:$port/$permdbname/_design/auth" -d "{ \"language\": \"javascript\", \"validate_doc_update\": \"function(newDoc, oldDoc, userCtx) { if (userCtx.roles.indexOf('annotationadmins') !== -1 || userCtx.name === 'admin') { return; } else { throw ({ forbidden: 'Only annotation admins may edit the database' }); } }\"}" # Setup DB security roles curl -H "Content-Type: application/json" -X PUT -d '{"admins":{"names":["admin"],"roles":["admins"]},"members":{"names":[],"roles":["users","pathologists","annotationadmins"]}}' "$protocol://$user:$pass"'@'"$host:$port/$dbname/_security" # Setup permissions DB security roles curl -H "Content-Type: application/json" -X PUT -d '{"admins":{"names":["admin"],"roles":["admins"]},"members":{"names":[],"roles":["users","pathologists","annotationadmins"]}}' "$protocol://$user:$pass"'@'"$host:$port/$permdbname/_security" # Setup users in DB curl -H "Content-Type: application/json" -X PUT "$protocol://$user:$pass"'@'"$host:$port/_users/org.couchdb.user:user" --data-binary '{"_id": "org.couchdb.user:user","name": "user","roles": ["users"],"type": "user","password": "userpass"}' curl -H "Content-Type: application/json" -X PUT "$protocol://$user:$pass"'@'"$host:$port/_users/org.couchdb.user:pathologist1" --data-binary '{"_id": "org.couchdb.user:pathologist1","name": "pathologist1","roles": ["users", "pathologists"],"type": "user","password": "pathologistpass"}' curl -H "Content-Type: application/json" -X PUT "$protocol://$user:$pass"'@'"$host:$port/_users/org.couchdb.user:pathologist2" --data-binary '{"_id": "org.couchdb.user:pathologist2","name": "pathologist2","roles": ["users", "pathologists"],"type": "user","password": "pathologistpass"}' curl -H "Content-Type: application/json" -X PUT "$protocol://$user:$pass"'@'"$host:$port/_users/org.couchdb.user:annotationadmin" --data-binary '{"_id": "org.couchdb.user:annotationadmin","name": "annotationadmin","roles": ["users", "pathologists", "annotationadmins"],"type": "user","password": "annotationpassw"}' # Bring CouchDB to foreground fg fi exec "$@"