Skip to content

Commit

Permalink
Fixed db auth issues with docker containers.
Browse files Browse the repository at this point in the history
Updated readme.
  • Loading branch information
nero120 committed Nov 12, 2018
1 parent 47e3209 commit b512c2b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 16 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ Once configured, you can begin syncing your browser data to your xBrowserSync se

## Upgrading from an earlier version

### <= v1.1.5

From v1.1.6, database users are created in the admin database. When upgrading from an earlier version you'll either need to drop the existing users in the xbrowsersync database and recreate them in the admin database, or simply add the following to your `config/settings.json` file:

```
"db": {
"authSource": "xbrowsersync"
}
```

### <= v1.0.3

If you are curently running v1.0.3 (or earlier) of the xBrowserSync API, you will need to export existing syncs and delete the xBrowserSync database before upgrading.

To export existing syncs, run the following command:
Expand Down Expand Up @@ -49,15 +61,16 @@ CD into the source directory and install dependencies (use `unsafe-perm` flag if

$ npm install --unsafe-perm

### 2. Configure mongoDB database
### 2. Configure mongoDB databases

1. Run the following commands in the mongo shell:

(Replace `[password]` with a cleartext password of your choice)

```
use admin
db.createUser({ user: "xbrowsersyncdb", pwd: "[password]", roles: [ { role: "readWrite", db: "xbrowsersync" }, { role: "readWrite", db: "xbrowsersynctest" } ] })
use xbrowsersync
db.createUser({ user: "xbrowsersyncdb", pwd: "[password]", roles: ["readWrite"] })
db.newsynclogs.createIndex( { "expiresAt": 1 }, { expireAfterSeconds: 0 } )
db.newsynclogs.createIndex({ "ipAddress": 1 })
```
Expand Down Expand Up @@ -116,6 +129,7 @@ Config Setting | Description | Default Value
-------------- | ----------- | -------------
`allowedOrigins` | Array of origins permitted to access the service. Each origin can be a `String` or a `RegExp`. For example `[ 'http://example1.com', /\.example2\.com$/ ]` will accept any request from `http://example1.com` or from a subdomain of `example2.com`. If the array is empty, all origins are permitted | `[]` (All origins permitted)
`dailyNewSyncsLimit` | The maximum number of new syncs that a single IP address can create per day. If this setting is enabled, logs are created in newsynclogs collection to track IP addresses (cleared the following day). Set as `0` to disable. | `3`
`db.authSource` | The database to use for authentication. | `admin`
`db.connTimeout` | The connection timeout period to use for mongoDB. Using a high value helps prevent dropped connections in a hosted environment. | `30000` (30 secs)
`db.host` | The mongoDB server address to connect to, either a hostname, IP address, or UNIX domain socket. | `127.0.0.1`
`db.name` | Name of the mongoDB database to use. | `xbrowsersync`
Expand Down
2 changes: 1 addition & 1 deletion config/settings.default.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"allowedOrigins": [],
"dailyNewSyncsLimit": 3,
"db": {
"authSource": "xbrowsersync",
"authSource": "admin",
"connTimeout": 30000,
"host": "127.0.0.1",
"name": "xbrowsersync",
Expand Down
13 changes: 7 additions & 6 deletions dist/core/db.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/core/db.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions src/core/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ export default class DB {
const options: mongoose.ConnectionOptions = {
connectTimeoutMS: Config.get().db.connTimeout,
keepAlive: 1,
pass: Config.get().db.password || process.env.XBROWSERSYNC_DB_PWD,
useNewUrlParser: true,
user: Config.get().db.username || process.env.XBROWSERSYNC_DB_USER
useNewUrlParser: true
};

// Get db username and password
const username = Config.get().db.username || process.env.XBROWSERSYNC_DB_USER;
const password = Config.get().db.password || process.env.XBROWSERSYNC_DB_PWD;

// Connect to the host and db name defined in config settings
const dbServerUrl = `mongodb://${Config.get().db.host}:${Config.get().db.port}/${Config.get().db.name}?authSource=${Config.get().db.authSource}`;
const dbServerUrl = `mongodb://${username}:${password}@${Config.get().db.host}:${Config.get().db.port}/${Config.get().db.name}?authSource=${Config.get().db.authSource}`;
mongoose.connect(dbServerUrl, options);
const dbConn = mongoose.connection;

Expand All @@ -56,8 +58,8 @@ export default class DB {
});

dbConn.on('error', (err: mongoose.Error) => {
this.log(LogLevel.Error, 'Uncaught exception occurred in database', null, err);
reject(err);
this.log(LogLevel.Error, 'Database error', null, err);
reject(new Error('Unable to connect to database.'));
});

dbConn.once('open', resolve);
Expand Down

0 comments on commit b512c2b

Please sign in to comment.