2020-02-18 23:55:06 -05:00
|
|
|
const fs = require('fs');
|
|
|
|
const NodeRSA = require('node-rsa');
|
|
|
|
const config = require('config');
|
|
|
|
const logger = require('./logger').setup;
|
2020-10-17 06:25:36 -04:00
|
|
|
const certificateModel = require('./models/certificate');
|
2020-02-18 23:55:06 -05:00
|
|
|
const userModel = require('./models/user');
|
|
|
|
const userPermissionModel = require('./models/user_permission');
|
2020-10-17 06:25:36 -04:00
|
|
|
const utils = require('./lib/utils');
|
2020-02-18 23:55:06 -05:00
|
|
|
const authModel = require('./models/auth');
|
2020-07-19 10:35:13 -04:00
|
|
|
const settingModel = require('./models/setting');
|
2020-10-17 06:13:08 -04:00
|
|
|
const dns_plugins = require('./global/certbot-dns-plugins');
|
2020-02-18 23:55:06 -05:00
|
|
|
const debug_mode = process.env.NODE_ENV !== 'production' || !!process.env.DEBUG;
|
|
|
|
|
2020-08-05 18:58:20 -04:00
|
|
|
/**
|
|
|
|
* Creates a new JWT RSA Keypair if not alread set on the config
|
|
|
|
*
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
const setupJwt = () => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// Now go and check if the jwt gpg keys have been created and if not, create them
|
|
|
|
if (!config.has('jwt') || !config.has('jwt.key') || !config.has('jwt.pub')) {
|
|
|
|
logger.info('Creating a new JWT key pair...');
|
2020-02-18 23:55:06 -05:00
|
|
|
|
2020-08-05 18:58:20 -04:00
|
|
|
// jwt keys are not configured properly
|
|
|
|
const filename = config.util.getEnv('NODE_CONFIG_DIR') + '/' + (config.util.getEnv('NODE_ENV') || 'default') + '.json';
|
|
|
|
let config_data = {};
|
2020-02-18 23:55:06 -05:00
|
|
|
|
2020-08-05 18:58:20 -04:00
|
|
|
try {
|
|
|
|
config_data = require(filename);
|
|
|
|
} catch (err) {
|
|
|
|
// do nothing
|
|
|
|
if (debug_mode) {
|
|
|
|
logger.debug(filename + ' config file could not be required');
|
|
|
|
}
|
2020-02-18 23:55:06 -05:00
|
|
|
}
|
|
|
|
|
2020-08-05 18:58:20 -04:00
|
|
|
// Now create the keys and save them in the config.
|
|
|
|
let key = new NodeRSA({ b: 2048 });
|
|
|
|
key.generateKeyPair();
|
2020-02-18 23:55:06 -05:00
|
|
|
|
2020-08-05 18:58:20 -04:00
|
|
|
config_data.jwt = {
|
|
|
|
key: key.exportKey('private').toString(),
|
|
|
|
pub: key.exportKey('public').toString(),
|
|
|
|
};
|
2020-02-18 23:55:06 -05:00
|
|
|
|
2020-08-05 18:58:20 -04:00
|
|
|
// Write config
|
|
|
|
fs.writeFile(filename, JSON.stringify(config_data, null, 2), (err) => {
|
|
|
|
if (err) {
|
|
|
|
logger.error('Could not write JWT key pair to config file: ' + filename);
|
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
logger.info('Wrote JWT key pair to config file: ' + filename);
|
2021-02-08 16:46:35 -05:00
|
|
|
delete require.cache[require.resolve('config')];
|
|
|
|
resolve();
|
2020-08-05 18:58:20 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// JWT key pair exists
|
|
|
|
if (debug_mode) {
|
|
|
|
logger.debug('JWT Keypair already exists');
|
2020-02-18 23:55:06 -05:00
|
|
|
}
|
|
|
|
|
2020-08-05 18:58:20 -04:00
|
|
|
resolve();
|
2020-02-18 23:55:06 -05:00
|
|
|
}
|
2020-08-05 18:58:20 -04:00
|
|
|
});
|
|
|
|
};
|
2020-07-19 10:35:13 -04:00
|
|
|
|
2020-08-05 18:58:20 -04:00
|
|
|
/**
|
|
|
|
* Creates a default admin users if one doesn't already exist in the database
|
|
|
|
*
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
const setupDefaultUser = () => {
|
|
|
|
return userModel
|
2020-07-19 10:35:13 -04:00
|
|
|
.query()
|
|
|
|
.select(userModel.raw('COUNT(`id`) as `count`'))
|
|
|
|
.where('is_deleted', 0)
|
|
|
|
.first()
|
2020-08-05 18:58:20 -04:00
|
|
|
.then((row) => {
|
|
|
|
if (!row.count) {
|
|
|
|
// Create a new user and set password
|
|
|
|
logger.info('Creating a new user: admin@example.com with password: changeme');
|
2020-07-19 10:35:13 -04:00
|
|
|
|
2020-08-05 18:58:20 -04:00
|
|
|
let data = {
|
|
|
|
is_deleted: 0,
|
|
|
|
email: 'admin@example.com',
|
|
|
|
name: 'Administrator',
|
|
|
|
nickname: 'Admin',
|
|
|
|
avatar: '',
|
|
|
|
roles: ['admin'],
|
|
|
|
};
|
2020-07-19 10:35:13 -04:00
|
|
|
|
2020-08-05 18:58:20 -04:00
|
|
|
return userModel
|
|
|
|
.query()
|
|
|
|
.insertAndFetch(data)
|
|
|
|
.then((user) => {
|
|
|
|
return authModel
|
|
|
|
.query()
|
|
|
|
.insert({
|
|
|
|
user_id: user.id,
|
|
|
|
type: 'password',
|
|
|
|
secret: 'changeme',
|
|
|
|
meta: {},
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
return userPermissionModel.query().insert({
|
2020-07-19 10:35:13 -04:00
|
|
|
user_id: user.id,
|
|
|
|
visibility: 'all',
|
|
|
|
proxy_hosts: 'manage',
|
|
|
|
redirection_hosts: 'manage',
|
|
|
|
dead_hosts: 'manage',
|
|
|
|
streams: 'manage',
|
|
|
|
access_lists: 'manage',
|
2020-08-05 18:58:20 -04:00
|
|
|
certificates: 'manage',
|
2020-07-19 10:35:13 -04:00
|
|
|
});
|
2020-08-05 18:58:20 -04:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
logger.info('Initial admin setup completed');
|
|
|
|
});
|
|
|
|
} else if (debug_mode) {
|
|
|
|
logger.debug('Admin user setup not required');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2020-02-18 23:55:06 -05:00
|
|
|
|
2020-08-05 18:58:20 -04:00
|
|
|
/**
|
|
|
|
* Creates default settings if they don't already exist in the database
|
|
|
|
*
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
const setupDefaultSettings = () => {
|
2020-07-19 10:35:13 -04:00
|
|
|
return settingModel
|
|
|
|
.query()
|
2020-08-05 18:58:20 -04:00
|
|
|
.select(settingModel.raw('COUNT(`id`) as `count`'))
|
|
|
|
.where({id: 'default-site'})
|
2020-07-19 10:35:13 -04:00
|
|
|
.first()
|
2020-08-05 18:58:20 -04:00
|
|
|
.then((row) => {
|
2020-07-19 10:35:13 -04:00
|
|
|
if (!row.count) {
|
|
|
|
settingModel
|
2020-02-18 23:55:06 -05:00
|
|
|
.query()
|
2020-07-19 10:35:13 -04:00
|
|
|
.insert({
|
|
|
|
id: 'default-site',
|
|
|
|
name: 'Default Site',
|
|
|
|
description: 'What to show when Nginx is hit with an unknown Host',
|
|
|
|
value: 'congratulations',
|
2020-08-05 18:58:20 -04:00
|
|
|
meta: {},
|
|
|
|
})
|
|
|
|
.then(() => {
|
2020-07-19 10:35:13 -04:00
|
|
|
logger.info('Default settings added');
|
2020-02-18 23:55:06 -05:00
|
|
|
});
|
2020-08-05 18:58:20 -04:00
|
|
|
}
|
|
|
|
if (debug_mode) {
|
2020-07-19 10:35:13 -04:00
|
|
|
logger.debug('Default setting setup not required');
|
2020-02-18 23:55:06 -05:00
|
|
|
}
|
|
|
|
});
|
2020-08-05 18:58:20 -04:00
|
|
|
};
|
2020-07-19 10:35:13 -04:00
|
|
|
|
2020-10-17 06:13:08 -04:00
|
|
|
/**
|
|
|
|
* Installs all Certbot plugins which are required for an installed certificate
|
|
|
|
*
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
const setupCertbotPlugins = () => {
|
|
|
|
return certificateModel
|
2020-10-17 06:25:36 -04:00
|
|
|
.query()
|
|
|
|
.where('is_deleted', 0)
|
|
|
|
.andWhere('provider', 'letsencrypt')
|
|
|
|
.then((certificates) => {
|
|
|
|
if (certificates && certificates.length) {
|
2022-11-14 16:54:48 -05:00
|
|
|
let plugins = [];
|
|
|
|
let promises = [];
|
2022-11-14 16:48:57 -05:00
|
|
|
let install_cloudflare_plugin = false;
|
2020-10-17 06:25:36 -04:00
|
|
|
|
|
|
|
certificates.map(function (certificate) {
|
|
|
|
if (certificate.meta && certificate.meta.dns_challenge === true) {
|
2022-11-14 16:54:48 -05:00
|
|
|
const dns_plugin = dns_plugins[certificate.meta.dns_provider];
|
|
|
|
|
2022-11-14 16:48:57 -05:00
|
|
|
if (dns_plugin.package_name === 'certbot-dns-cloudflare') {
|
|
|
|
install_cloudflare_plugin = true;
|
|
|
|
} else {
|
|
|
|
const packages_to_install = `${dns_plugin.package_name}${dns_plugin.version_requirement || ''} ${dns_plugin.dependencies}`;
|
|
|
|
if (plugins.indexOf(packages_to_install) === -1) plugins.push(packages_to_install);
|
|
|
|
}
|
2020-10-17 06:25:36 -04:00
|
|
|
|
|
|
|
// Make sure credentials file exists
|
2022-11-14 16:48:57 -05:00
|
|
|
const credentials_loc = '/etc/letsencrypt/credentials/credentials-' + certificate.id;
|
2021-12-29 10:30:49 -05:00
|
|
|
// Escape single quotes and backslashes
|
|
|
|
const escapedCredentials = certificate.meta.dns_provider_credentials.replaceAll('\'', '\\\'').replaceAll('\\', '\\\\');
|
|
|
|
const credentials_cmd = '[ -f \'' + credentials_loc + '\' ] || { mkdir -p /etc/letsencrypt/credentials 2> /dev/null; echo \'' + escapedCredentials + '\' > \'' + credentials_loc + '\' && chmod 600 \'' + credentials_loc + '\'; }';
|
2020-10-17 06:25:36 -04:00
|
|
|
promises.push(utils.exec(credentials_cmd));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-11-05 18:17:52 -05:00
|
|
|
if (plugins.length) {
|
2021-05-06 21:34:11 -04:00
|
|
|
const install_cmd = 'pip install ' + plugins.join(' ');
|
2020-11-05 18:17:52 -05:00
|
|
|
promises.push(utils.exec(install_cmd));
|
|
|
|
}
|
|
|
|
|
2022-11-14 16:48:57 -05:00
|
|
|
if (install_cloudflare_plugin) {
|
|
|
|
promises.push(utils.exec('pip install certbot-dns-cloudflare --index-url https://www.piwheels.org/simple --prefer-binary'));
|
|
|
|
}
|
|
|
|
|
2020-11-05 18:17:52 -05:00
|
|
|
if (promises.length) {
|
|
|
|
return Promise.all(promises)
|
2022-11-14 16:48:57 -05:00
|
|
|
.then(() => {
|
|
|
|
logger.info('Added Certbot plugins ' + plugins.join(', '));
|
2020-11-05 18:17:52 -05:00
|
|
|
});
|
|
|
|
}
|
2020-10-17 06:25:36 -04:00
|
|
|
}
|
|
|
|
});
|
2020-10-17 06:13:08 -04:00
|
|
|
};
|
|
|
|
|
2021-06-29 14:40:56 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts a timer to call run the logrotation binary every two days
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
const setupLogrotation = () => {
|
|
|
|
const intervalTimeout = 1000 * 60 * 60 * 24 * 2; // 2 days
|
|
|
|
|
|
|
|
const runLogrotate = async () => {
|
2021-07-22 08:05:21 -04:00
|
|
|
try {
|
|
|
|
await utils.exec('logrotate /etc/logrotate.d/nginx-proxy-manager');
|
|
|
|
logger.info('Logrotate completed.');
|
|
|
|
} catch (e) { logger.warn(e); }
|
2021-06-29 14:40:56 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
logger.info('Logrotate Timer initialized');
|
|
|
|
setInterval(runLogrotate, intervalTimeout);
|
|
|
|
// And do this now as well
|
|
|
|
return runLogrotate();
|
|
|
|
};
|
|
|
|
|
2020-07-19 10:35:13 -04:00
|
|
|
module.exports = function () {
|
2020-08-05 18:58:20 -04:00
|
|
|
return setupJwt()
|
|
|
|
.then(setupDefaultUser)
|
2020-10-17 06:13:08 -04:00
|
|
|
.then(setupDefaultSettings)
|
2021-06-29 14:40:56 -04:00
|
|
|
.then(setupCertbotPlugins)
|
|
|
|
.then(setupLogrotation);
|
2020-02-18 23:55:06 -05:00
|
|
|
};
|