Skip to content

Commit

Permalink
updated almost everything.
Browse files Browse the repository at this point in the history
Please read README.md
  • Loading branch information
Ivo Woltring committed May 1, 2015
1 parent 165b328 commit 1090ddd
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 36 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties


#ignore the cookies.txt file
cookies.txt
Empty file modified CORS.sh
100644 → 100755
Empty file.
35 changes: 31 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
This repository hosts a couple of [CouchDB](http://couchdb.apache.org) configuration
scripts. The scrips are meant to make life just a bit easier.

Added cookie authentication (see cookie-authentications.sh script).
This means that if you have logged in with a user with sufficient rights you can do
any of the other commands without having to login again (unless the session has expired)

# Scripts

## first-admin.sh
Expand All @@ -12,24 +16,47 @@ This script should only be used once.
All other scripts will assume that this script has been run or in
other words, make sure CouchDB is at least secured with an admin user.

This script does not make use of the cookie functionality mentioned above.

## CORS.sh

This script configures CouchDB to enable Cross-origin resource sharing (CORS).
This means that JavaScript from another domain can access the API of CouchDB.
Make sure you know what you are doing before using this.

## admin-users.sh
This script does not make use of the cookie functionality mentioned above.

## login.sh

Well checks if cookies.txt already exists and if so only calls settings.sh
otherwise it will also call cookie-authentication.sh

## settings.sh

Not a stand-alone script.
Used by other scripts and it containes some configuration settings and standard checks.
This script is used by almost all other scripts.

Script for easily creating extra admin users.
## cookie-authentication.sh

Cookie based Login. The received cookie will be saved to a cookie.txt file
is used by almost all commands mentioned below this one.
If you want to login again or as someone else just run the above mentioned script or remove the cookies.txt file.
If the cookies.txt file does not exist the next time you run a command it will ask for credentials.
All commands below will work without having to login in again until you remove the cookies.txt or the session expires.

## admin-users.sh

Script for easily creating extra admin users.

## users.sh

Script for easily creating users.
Users are created without any ROLES.
You can later add these through the console.


## create-database.sh

Creates a database.
Creates databases.


12 changes: 2 additions & 10 deletions admin-users.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,14 @@
echo ========================================================================
echo == CouchDB - Create extra admin users...
echo ========================================================================


echo "First login to CouchDB as an admin..."
read -p "Admin Username: " ADMIN
read -s -p "Admin Password: " ADMPWD
echo
COUCH=http://$ADMIN:$ADMPWD@localhost:5984

echo "Now you can add new admins to CouchDB..."
source ./login.sh "Please login as an Administrator..."

while true
do
read -p "New Administrator: " USER
read -s -p "New Password : " PASSWORD
echo
curl -X PUT $COUCH/_config/admins/$USER -d '"'$PASSWORD'"'
curl --cookie cookies.txt --user-agent Mozilla/4.0 -vX PUT $COUCHDB/_config/admins/$USER -d '"'$PASSWORD'"'

read -p "Another admin (y/n)? " CONT
if [ "$CONT" == "n" ]; then
Expand Down
8 changes: 8 additions & 0 deletions all_docs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
echo ========================================================================
echo == CouchDB - all users
echo ========================================================================
source ./login.sh "Please login as an administrator"

read -p "Database to show: " DATABASE
curl --cookie cookies.txt --user-agent Mozilla/4.0 -X GET $COUCHDB/$DATABASE/_all_docs
7 changes: 7 additions & 0 deletions all_users.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash
echo ========================================================================
echo == CouchDB - all users
echo ========================================================================
source ./login.sh "Please login as an administrator"

curl --cookie cookies.txt --user-agent Mozilla/4.0 -X GET $COUCHDB/_users/_all_docs
16 changes: 16 additions & 0 deletions cookie-authentication.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash
source ./settings.sh
echo "CouchDB login:"
read -p "Username: " USER
read -s -p "Password: " PASSWORD

USRPWD='name='$USER'&password='$PASSWORD

#use -vX for mor info
curl --cookie $COUCHDB_COOKIE --cookie-jar $COUCHDB_COOKIE --user-agent Mozilla/4.0 -X POST $COUCHDB/_session --data "$USRPWD" -H 'Content-Type:application/x-www-form-urlencoded'


#With the following command you can reuse the cookie.
#curl --cookie cookies.txt --user-agent Mozilla/4.0 -X GET $HOST/...
#Depending on the rights of your user you can do stuff.
#if you want to login as a different user just use this script again
14 changes: 3 additions & 11 deletions create-database.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
#!/usr/bin/env bash


echo ========================================================================
echo == CouchDB - Create a database...
echo ========================================================================
source ./login.sh "Please login as an administrator"


echo "First login to CouchDB as an admin..."
read -p "Admin Username: " ADMIN
read -s -p "Admin Password: " ADMPWD
echo
COUCH=http://$ADMIN:$ADMPWD@localhost:5984


echo -p "Database name : " DATABASE
curl -X PUT $COUCH/$DATABASE
read -p "Database name : " DATABASE
curl --cookie cookies.txt --user-agent Mozilla/4.0 -X PUT $COUCHDB/$DATABASE
Empty file modified first-admin.sh
100644 → 100755
Empty file.
6 changes: 6 additions & 0 deletions login.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash
source ./settings.sh
if [ ! -f cookies.txt ]; then
echo $1
source cookie-authentication.sh
fi
5 changes: 5 additions & 0 deletions settings.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

COUCHDB="http://127.0.0.1:5984"
COUCHDB_COOKIE=cookies.txt

12 changes: 2 additions & 10 deletions users.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,15 @@
echo ========================================================================
echo == CouchDB - Create new users...
echo ========================================================================


echo "First login to CouchDB as an admin..."
read -p "Admin Username: " ADMIN
read -s -p "Admin Password: " ADMPWD
echo
COUCH=http://$ADMIN:$ADMPWD@localhost:5984
echo "Now you can add new users to CouchDB..."
source ./login.sh "Please login as an administrator"

while true
do
read -p "Username: " USER
read -s -p "Password: " PASSWORD
echo
JSON='{"_id": "org.couchdb.user:'$USER'","name":"'$USER'","roles": [],"type": "user","password": "'$PASSWORD'"}'
echo "$JSON"
curl -HContent-Type:application/json -vXPUT $COUCH/_users/org.couchdb.user:$USER --data-binary "$JSON"
curl --cookie cookies.txt --user-agent Mozilla/4.0 -HContent-Type:application/json -X PUT $COUCHDB/_users/org.couchdb.user:$USER --data-binary "$JSON"
read -p "Continue (y/n)? " CONT
if [ "$CONT" == "n" ]; then
exit 0;
Expand Down

0 comments on commit 1090ddd

Please sign in to comment.