Browse Source

Add MongoDB users and authentication.

KrisVos130 8 years ago
parent
commit
6e11b50e89
4 changed files with 74 additions and 10 deletions
  1. 70 7
      README.md
  2. 1 1
      backend/config/template.json
  3. 2 2
      backend/logic/db/index.js
  4. 1 0
      docker-compose.yml

+ 70 - 7
README.md

@@ -80,15 +80,50 @@ Now you have different paths here.
 
    `docker-compose build`
 
-2. Start the databases and tools in the background, as we usually don't need to monitor these for errors
+2. Set up the MongoDB database
+
+	1. Disable auth
+	
+		In `docker-compose.yml` remove `--auth` from the line `command: "--auth"` for mongo.
+	
+	2. Start the database
+	
+		`docker-compose up mongo`
+		
+	3. Connect to Mongo
+	
+		`docker-compose exec mongo mongo admin`
+	
+	4. Create an admin user
+	
+		`db.createUser({user: 'admin', pwd: 'PASSWORD_HERE', roles: [{role: 'userAdminAnyDatabase', db: 'admin'}]})`
+		
+	5. Connect to the Musare database
+	
+		`use musare`
+		
+	6. Create the musare user
+	
+		`db.createUser({user: 'musare', pwd: 'OTHER_PASSWORD_HERE', roles: [{role: 'readWrite', db: 'musare'}]})`
+	
+	7. Exit
+	
+		`exit`
+	
+	8. Add back authentication
+	
+		In `docker-compose.yml` add back `--auth` on the line `command: ""` for mongo.
+	
+
+3. Start the databases and tools in the background, as we usually don't need to monitor these for errors
 
    `docker-compose up -d mongo mongoclient redis`
 
-3. Start the backend and frontend in the foreground, so we can watch for errors during development
+4. Start the backend and frontend in the foreground, so we can watch for errors during development
 
    `docker-compose up backend frontend`
 
-4. You should now be able to begin development! The backend is auto reloaded when
+5. You should now be able to begin development! The backend is auto reloaded when
    you make changes and the frontend is auto compiled and live reloaded by webpack
    when you make changes. You should be able to access Musare in your local browser
    at `http://<docker-machine-ip>:8080/` where `<docker-machine-ip>` can be found below:
@@ -108,10 +143,38 @@ Steps 1-4 are things you only have to do once. The steps to start servers follow
 		"C:\Program Files\MongoDB\Server\3.2\bin\mongod.exe" --dbpath "D:\Programming\HTML\MusareNode\.database"
 
 	Make sure to adjust your paths accordingly.
-
-3. In the folder where you installed Redis, edit the `redis.windows.conf` file. In there, look for the property `notify-keyspace-events`. Make sure that property is uncommented and has the value `Ex`. It should look like `notify-keyspace-events Ex` when done.
-
-4. Create a file called `startRedis.cmd` in the main folder with the contents:
+	
+3. Set up the MongoDB database
+	
+	1. Start the database by executing the script `startMongo.cmd` you just made
+		
+	2. Connect to Mongo from a command prompt
+	
+		`mongo admin`
+	
+	3. Create an admin user
+	
+		`db.createUser({user: 'admin', pwd: 'PASSWORD_HERE', roles: [{role: 'userAdminAnyDatabase', db: 'admin'}]})`
+		
+	4. Connect to the Musare database
+	
+		`use musare`
+		
+	5. Create the musare user
+	
+		`db.createUser({user: 'musare', pwd: 'OTHER_PASSWORD_HERE', roles: [{role: 'readWrite', db: 'musare'}]})`
+	
+	6. Exit
+	
+		`exit`
+	
+	7. Add the authentication
+	
+		In `startMongo.cmd` add ` --auth` at the end of the first line
+
+4. In the folder where you installed Redis, edit the `redis.windows.conf` file. In there, look for the property `notify-keyspace-events`. Make sure that property is uncommented and has the value `Ex`. It should look like `notify-keyspace-events Ex` when done.
+
+5. Create a file called `startRedis.cmd` in the main folder with the contents:
 
 		"D:\Redis\redis-server.exe" "D:\Redis\redis.windows.conf"
 

+ 1 - 1
backend/config/template.json

@@ -36,7 +36,7 @@
 	  	"url": "redis://redis:6379/0"
 	},
   	"mongo": {
-	  	"url": "mongodb://mongo:27017/musare"
+	  	"url": "mongodb://musare:PASSWORD@mongo:27017/musare"
 	},
   	"cookie": {
 	  	"domain": "",

+ 2 - 2
backend/logic/db/index.js

@@ -1,6 +1,7 @@
 'use strict';
 
 const mongoose = require('mongoose');
+const config = require('config');
 
 const bluebird = require('bluebird');
 
@@ -25,11 +26,10 @@ let lib = {
 	models: {},
 
 	init: (url, cb) => {
-
 		lib.connection = mongoose.connect(url).connection;
 
 		lib.connection.on('error', err => {
-			console.error('Database error: ' + err.message)
+			console.error('Database error: ' + err);
 			process.exit();
 		});
 

+ 1 - 0
docker-compose.yml

@@ -19,6 +19,7 @@ services:
     image: mongo
     ports:
     - "27018:27018"
+    command: "--auth"
   mongoclient:
     image: mongoclient/mongoclient
     ports: