Skip to content

Commit

Permalink
Fixes #11 - After creating keys the app needs to completely restart d…
Browse files Browse the repository at this point in the history
…ue to aggressive module caching
  • Loading branch information
Jamie Curnow committed Sep 12, 2018
1 parent 4fe26ec commit efa1424
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 45 deletions.
7 changes: 6 additions & 1 deletion rootfs/etc/services.d/manager/run
Expand Up @@ -3,4 +3,9 @@
mkdir -p /data/letsencrypt-acme-challenge

cd /app
node --abort_on_uncaught_exception --max_old_space_size=250 /app/src/backend/index.js

while :
do
node --abort_on_uncaught_exception --max_old_space_size=250 /app/src/backend/index.js
sleep 1
done
13 changes: 9 additions & 4 deletions src/backend/importer.js
@@ -1,9 +1,10 @@
'use strict';

const fs = require('fs');
const logger = require('./logger').import;
const utils = require('./lib/utils');
const batchflow = require('batchflow');
const fs = require('fs');
const logger = require('./logger').import;
const utils = require('./lib/utils');
const batchflow = require('batchflow');
const debug_mode = process.env.NODE_ENV !== 'production';

const internalProxyHost = require('./internal/proxy-host');
const internalRedirectionHost = require('./internal/redirection-host');
Expand Down Expand Up @@ -534,6 +535,10 @@ module.exports = function () {
);

} else {
if (debug_mode) {
logger.debug('Importer skipped');
}

resolve();
}
});
Expand Down
1 change: 1 addition & 0 deletions src/backend/logger.js
Expand Up @@ -8,4 +8,5 @@ module.exports = {
nginx: new Signale({scope: 'Nginx '}),
ssl: new Signale({scope: 'SSL '}),
import: new Signale({scope: 'Importer'}),
setup: new Signale({scope: 'Setup '})
};
95 changes: 55 additions & 40 deletions src/backend/setup.js
Expand Up @@ -3,10 +3,11 @@
const fs = require('fs');
const NodeRSA = require('node-rsa');
const config = require('config');
const logger = require('./logger').global;
const logger = require('./logger').setup;
const userModel = require('./models/user');
const userPermissionModel = require('./models/user_permission');
const authModel = require('./models/auth');
const debug_mode = process.env.NODE_ENV !== 'production';

module.exports = function () {
return new Promise((resolve, reject) => {
Expand All @@ -22,6 +23,9 @@ module.exports = function () {
config_data = require(filename);
} catch (err) {
// do nothing
if (debug_mode) {
logger.debug(filename + ' config file could not be required');
}
}

// Now create the keys and save them in the config.
Expand All @@ -40,12 +44,18 @@ module.exports = function () {
reject(err);
} else {
logger.info('Wrote JWT key pair to config file: ' + filename);
config.util.loadFileConfigs();
resolve();

logger.warn('Restarting interface to apply new configuration');
process.exit(0);
}
});

} else {
// JWT key pair exists
if (debug_mode) {
logger.debug('JWT Keypair already exists');
}

resolve();
}
})
Expand All @@ -54,49 +64,54 @@ module.exports = function () {
.query()
.select(userModel.raw('COUNT(`id`) as `count`'))
.where('is_deleted', 0)
.first('count')
.then(row => {
if (!row.count) {
// Create a new user and set password
logger.info('Creating a new user: admin@example.com with password: changeme');
.first();
})
.then(row => {
if (!row.count) {
// Create a new user and set password
logger.info('Creating a new user: admin@example.com with password: changeme');

let data = {
is_deleted: 0,
email: 'admin@example.com',
name: 'Administrator',
nickname: 'Admin',
avatar: '',
roles: ['admin']
};
let data = {
is_deleted: 0,
email: 'admin@example.com',
name: 'Administrator',
nickname: 'Admin',
avatar: '',
roles: ['admin']
};

return userModel
return userModel
.query()
.insertAndFetch(data)
.then(user => {
return authModel
.query()
.insertAndFetch(data)
.then(user => {
return authModel
.insert({
user_id: user.id,
type: 'password',
secret: 'changeme',
meta: {}
})
.then(() => {
return userPermissionModel
.query()
.insert({
user_id: user.id,
type: 'password',
secret: 'changeme',
meta: {}
})
.then(() => {
return userPermissionModel
.query()
.insert({
user_id: user.id,
visibility: 'all',
proxy_hosts: 'manage',
redirection_hosts: 'manage',
dead_hosts: 'manage',
streams: 'manage',
access_lists: 'manage',
certificates: 'manage'
});
user_id: user.id,
visibility: 'all',
proxy_hosts: 'manage',
redirection_hosts: 'manage',
dead_hosts: 'manage',
streams: 'manage',
access_lists: 'manage',
certificates: 'manage'
});
});
}
});
})
.then(() => {
logger.info('Initial setup completed');
});
} else if (debug_mode) {
logger.debug('Admin user setup not required');
}
});
};

0 comments on commit efa1424

Please sign in to comment.