feat: Use authorization on the MongoDB server

This commit is contained in:
Alexander Polynomdivision 2018-10-03 19:09:49 +02:00
parent 5b399bf0d2
commit 4a3aa60a4d
4 changed files with 32 additions and 6 deletions

5
backend/README.md Normal file
View File

@ -0,0 +1,5 @@
# Lateinicus Server
## Requirements
- a database called `lateinicus`
- a user called `backend` on the database `lateinicus`
- the role `readWrite` for the database `lateinicus`

View File

@ -1,3 +1,6 @@
import { env } from "process";
import * as assert from "assert";
import * as express from "express";
import * as cors from "cors";
@ -15,13 +18,15 @@ const baseRouter = express.Router();
const authRouter = express.Router();
import { MongoClient } from "mongodb";
const assert = require('assert');
const user = encodeURIComponent("backend");
const password = encodeURIComponent(env["LATEINICUS_USER_PW"]);
(async function() {
// Connection URL
const url = 'mongodb://128.1.0.2:27017/myproject';
// Database Name
const dbName = 'myproject';
const dbName = 'lateinicus';
// Connection URL
const url = `mongodb://${user}:${password}@128.1.0.2:27017/?authMechanism=SCRAM-SHA-1&authSource=${dbName}`;
let client: MongoClient;
try {
@ -30,7 +35,7 @@ const assert = require('assert');
console.log("Connected to MongoDB");
} catch (err) {
console.log(err.stack);
assert(1, 2);
assert(false);
}
const db = client.db(dbName);

View File

@ -2,7 +2,11 @@ version: "3.6"
services:
db:
image: mongo:4.1.3-xenial
image: mongo:4.0.2-xenial
command: mongod --config /etc/mongo/mongod.conf
volumes:
- ./server/db/:/etc/mongo/db/:rw
- ./server/mongod.conf:/etc/mongo/mongod.conf:ro
expose:
- "27017"
networks:
@ -11,6 +15,8 @@ services:
backend:
image: lateinicus/server
environment:
- LATEINICUS_USER_PW=abc123
expose:
- "8080"
depends_on:

10
server/mongod.conf Normal file
View File

@ -0,0 +1,10 @@
storage:
engine: wiredTiger
dbPath: /etc/mongo/db/
security:
authorization: enabled
javascriptEnabled: false
net:
port: 27017