Skip to content
This repository has been archived by the owner on May 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #169 from rmetcalf9/master
Browse files Browse the repository at this point in the history
Pullrequest for #168
  • Loading branch information
pantsel committed Feb 6, 2018
2 parents 8cf04ba + 275c0b9 commit 182badc
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 29 deletions.
59 changes: 59 additions & 0 deletions DEFAULTUSERSEEDDATA.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Changing the default user seed data

Konga will populate the user table with default entries if it finds none are present. If you wish to change the default data you can supply a file with alternative values. A sample file could look like:

````
module.exports = [
{
"username": "myadmin",
"email": "myadmin@some.domain",
"firstName": "Peter",
"lastName": "Administrator",
"node_id": "http://kong:8001",
"admin": true,
"active" : true,
"password": "somepassword"
},
{
"username": "otheruser",
"email": "otheruser@some.domain",
"firstName": "Joe",
"lastName": "Blogs",
"node_id": "http://kong:8001",
"admin": false,
"active" : true,
"password": "anotherpassword"
}
]
````

To make Konga use this file instead of the hardcoded default users you should set the enviroment variable KONGA_SEED_USER_DATA_SOURCE_FILE to point to the files location:
````
export KONGA_SEED_USER_DATA_SOURCE_FILE=~/userdb.data
````

This is espically useful when running Konga in a container as part of a Docker swarm. The file can be setup as a Docker secret and supplied to the container. This can be done with an entry in a compose file simular to:

````
version: "3.1"
secrets:
konga_user_seed:
external: true
services:
konga:
image: pantsel/konga
secrets:
- konga_user_seed
environment:
- KONGA_SEED_USER_DATA_SOURCE_FILE=/run/secrets/konga_user_seed
deploy:
restart_policy:
condition: on-failure
ports:
- 1337:1337
````

(This will work if the swarm is setup with the konga_user_seed secret set with it's value as the contents of the user file.)

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ login: admin | password: adminadminadmin
*Demo user*
login: demo | password: demodemodemo

This user data is populated to the database if there is not already any user data in it. [It is possible to alter the default user seed data.](DEFAULTUSERSEEDDATA.md)

## Upgrading
In some cases a newer version of Konga may introduce new db tables, collections or changes in schemas.
The only thing you need to do is to start Konga in dev mode once so that the migrations will be applied.
Expand Down
29 changes: 20 additions & 9 deletions api/hooks/load-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var async = require('async');
var _ = require('lodash')
var defUserSeedData = require('../../config/default-user-seed-data.js');

/**
* load-db.js
Expand Down Expand Up @@ -29,15 +30,25 @@ module.exports = function hook(sails) {
var passportsFns = []
users.forEach(function(user){
passportsFns.push(function(_cb){
sails.models.passport
.create({
protocol: "local",
password : user.username == 'admin' ? 'adminadminadmin' : 'demodemodemo',
user : user.id
}).exec(function(err,passport){
if(err) return _cb(err)
return _cb(null)
})
var passwordToSetArr = defUserSeedData.seedData.filter( function (orig) {
return (orig.username == user.username)
});
var passwordToSet = undefined;
if (passwordToSetArr.length == [1]) {
passwordToSet = passwordToSetArr[0].password;
}
// Only set the password if we have one
if (typeof(passwordToSet) != 'undefined') {
sails.models.passport
.create({
protocol: "local",
password : passwordToSet,
user : user.id
}).exec(function(err,passport){
if(err) return _cb(err)
return _cb(null)
})
};
})
})

Expand Down
35 changes: 15 additions & 20 deletions api/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var _ = require('lodash');
var async = require('async');
var uuid = require('node-uuid');
var defUserSeedData = require('../../config/default-user-seed-data.js');

/**
* User.js
Expand Down Expand Up @@ -100,26 +101,20 @@ var defaultModel = _.merge(_.cloneDeep(require('../base/Model')), {
}
},

seedData: [
{
"username": "admin",
"email": "admin@some.domain",
"firstName": "Arnold",
"lastName": "Administrator",
"node_id": "http://kong:8001",
"admin": true,
"active" : true
},
{
"username": "demo",
"email": "demo@some.domain",
"firstName": "John",
"lastName": "Doe",
"node_id": "http://kong:8001",
"admin": false,
"active" : true
}
]
//seedData object should now come from a file
// the new object has had the password field added
// we need to remove it
seedData: defUserSeedData.seedData.map( function (orig) {
return {
"username": orig.username,
"email": orig.email,
"firstName": orig.firstName,
"lastName": orig.lastName,
"node_id": orig.node_id,
"admin": orig.admin,
"active" : orig.active
}
})
});

var mongoModel = function () {
Expand Down
70 changes: 70 additions & 0 deletions config/default-user-seed-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* Created by rmetcalf9 on 5/2/2018.
*/
'use strict';
var fs = require('fs');

/*
Return the seed data to be used, either from a file or hardcoded depending
on enviroment variable setting
*/
function getseedData() {
var readEnvVar = process.env.KONGA_SEED_USER_DATA_SOURCE_FILE;
if (typeof(readEnvVar) == 'undefined') {
readEnvVar = undefined;
} else {
readEnvVar = readEnvVar.trim();
if (readEnvVar.length == 0) {
readEnvVar = undefined;
} else {
if (!fs.existsSync(readEnvVar)) {
console.log('Could not find KONGA_SEED_USER_DATA_SOURCE_FILE');
readEnvVar = undefined;
}
try {
var seedUserData = require(readEnvVar);
if (typeof(seedUserData) != 'object') {
readEnvVar = undefined;
} else {
// We may place other checks on file contents here if required
console.log('Sucessfully read in user seed data file');
}
} catch (e) {
console.log(e);
console.log('Failed to load KONGA_SEED_USER_DATA_SOURCE_FILE');
console.log('Reverting to default user seed');
readEnvVar = undefined;
}
}
}
if(typeof(readEnvVar) == 'undefined') {
return [
{
"username": "admin",
"email": "admin@some.domain",
"firstName": "Arnold",
"lastName": "Administrator",
"node_id": "http://kong:8001",
"admin": true,
"active" : true,
"password": "adminadminadmin"
},
{
"username": "demo",
"email": "demo@some.domain",
"firstName": "John",
"lastName": "Doe",
"node_id": "http://kong:8001",
"admin": false,
"active" : true,
"password": "demodemodemo"
}
];
};
return seedUserData
}

module.exports = {
seedData: getseedData()
}

0 comments on commit 182badc

Please sign in to comment.