139 lines
3.7 KiB
JavaScript
139 lines
3.7 KiB
JavaScript
const fs = require('fs');
|
|
const logger = require('./logger').setup;
|
|
const certificateModel = require('./models/certificate');
|
|
const userModel = require('./models/user');
|
|
const userPermissionModel = require('./models/user_permission');
|
|
const authModel = require('./models/auth');
|
|
const settingModel = require('./models/setting');
|
|
const certbot = require('./lib/certbot');
|
|
|
|
/**
|
|
* Creates a default admin users if one doesn't already exist in the database
|
|
*
|
|
* @returns {Promise}
|
|
*/
|
|
const setupDefaultUser = () => {
|
|
return userModel
|
|
.query()
|
|
.select(userModel.raw('COUNT(`id`) as `count`'))
|
|
.where('is_deleted', 0)
|
|
.first()
|
|
.then((row) => {
|
|
if (!row.count) {
|
|
// Create a new user and set password
|
|
let email = process.env.INITIAL_ADMIN_EMAIL || 'admin@example.org';
|
|
let password = process.env.INITIAL_ADMIN_PASSWORD || 'iArhP1j7p1P6TA92FA2FMbbUGYqwcYzxC4AVEe12Wbi94FY9gNN62aKyF1shrvG4NycjjX9KfmDQiwkLZH1ZDR9xMjiG2QmoHXi';
|
|
|
|
logger.info('Creating a new user: ' + email + ' with password: ' + password);
|
|
|
|
const data = {
|
|
is_deleted: 0,
|
|
email: email,
|
|
name: 'Administrator',
|
|
nickname: 'Admin',
|
|
avatar: '',
|
|
roles: ['admin'],
|
|
};
|
|
|
|
return userModel
|
|
.query()
|
|
.insertAndFetch(data)
|
|
.then((user) => {
|
|
return authModel
|
|
.query()
|
|
.insert({
|
|
user_id: user.id,
|
|
type: 'password',
|
|
secret: password,
|
|
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',
|
|
});
|
|
});
|
|
})
|
|
.then(() => {
|
|
logger.info('Initial admin setup completed');
|
|
});
|
|
}
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Creates default settings if they don't already exist in the database
|
|
*
|
|
* @returns {Promise}
|
|
*/
|
|
const setupDefaultSettings = () => {
|
|
return settingModel
|
|
.query()
|
|
.select(settingModel.raw('COUNT(`id`) as `count`'))
|
|
.where({ id: 'default-site' })
|
|
.first()
|
|
.then((row) => {
|
|
if (!row.count) {
|
|
settingModel
|
|
.query()
|
|
.insert({
|
|
id: 'default-site',
|
|
name: 'Default Site',
|
|
description: 'What to show when Nginx is hit with an unknown Host',
|
|
value: 'congratulations',
|
|
meta: {},
|
|
})
|
|
.then(() => {
|
|
logger.info('Default settings added');
|
|
});
|
|
}
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Installs all Certbot plugins which are required for an installed certificate
|
|
*
|
|
* @returns {Promise}
|
|
*/
|
|
const setupCertbotPlugins = () => {
|
|
return certificateModel
|
|
.query()
|
|
.where('is_deleted', 0)
|
|
.andWhere('provider', 'letsencrypt')
|
|
.then((certificates) => {
|
|
if (certificates && certificates.length) {
|
|
const plugins = [];
|
|
const promises = [];
|
|
|
|
certificates.map(function (certificate) {
|
|
if (certificate.meta && certificate.meta.dns_challenge === true) {
|
|
if (plugins.indexOf(certificate.meta.dns_provider) === -1) {
|
|
plugins.push(certificate.meta.dns_provider);
|
|
}
|
|
|
|
const credentialsLocation = '/data/tls/certbot/credentials/credentials-' + certificate.id;
|
|
fs.mkdirSync('/data/tls/certbot/credentials', { recursive: true });
|
|
fs.writeFileSync(credentialsLocation, certificate.meta.dns_provider_credentials, { mode: 0o600 });
|
|
}
|
|
});
|
|
|
|
return certbot.installPlugins(plugins).then(() => {
|
|
if (promises.length) {
|
|
return Promise.all(promises).then(() => {
|
|
logger.info('Added Certbot plugins ' + plugins.join(', '));
|
|
});
|
|
}
|
|
});
|
|
}
|
|
});
|
|
};
|
|
|
|
module.exports = function () {
|
|
return setupDefaultUser().then(setupDefaultSettings).then(setupCertbotPlugins);
|
|
};
|