move dashboard change routes under dashboard/

This commit is contained in:
Girish Ramakrishnan
2023-08-13 10:06:01 +05:30
parent 7b32cb16f3
commit e723c3c19b
9 changed files with 134 additions and 135 deletions
+98 -1
View File
@@ -4,16 +4,33 @@ exports = module.exports = {
getLocation,
setLocation,
setupDnsAndCert,
prepareDomain,
setDashboardDomain,
switchDomain,
getConfig,
};
const appstore = require('./appstore.js'),
const apps = require('./apps.js'),
appstore = require('./appstore.js'),
assert = require('assert'),
BoxError = require('./boxerror.js'),
branding = require('./branding.js'),
constants = require('./constants.js'),
debug = require('debug')('box:dashboard'),
eventlog = require('./eventlog.js'),
dns = require('./dns.js'),
mailServer = require('./mailserver.js'),
network = require('./network.js'),
oidc = require('./oidc.js'),
reverseProxy = require('./reverseproxy.js'),
safe = require('safetydance'),
services = require('./services.js'),
settings = require('./settings.js'),
system = require('./system.js'),
tasks = require('./tasks.js'),
users = require('./users.js');
async function setLocation(domain, fqdn) {
@@ -55,3 +72,83 @@ async function getConfig() {
mandatory2FA: profileConfig.mandatory2FA,
};
}
async function prepareDomain(domain, auditSource) {
assert.strictEqual(typeof domain, 'string');
assert.strictEqual(typeof auditSource, 'object');
debug(`prepareDomain: ${domain}`);
if (constants.DEMO) throw new BoxError(BoxError.CONFLICT, 'Not allowed in demo mode');
const fqdn = dns.fqdn(constants.DASHBOARD_SUBDOMAIN, domain);
const result = await apps.list();
if (result.some(app => app.fqdn === fqdn)) throw new BoxError(BoxError.BAD_STATE, 'Dashboard location conflicts with an existing app');
const taskId = await tasks.add(tasks.TASK_SETUP_DNS_AND_CERT, [ constants.DASHBOARD_SUBDOMAIN, domain, auditSource ]);
tasks.startTask(taskId, {});
return taskId;
}
// call this only pre activation since it won't start mail server
async function setDashboardDomain(domain, auditSource) {
assert.strictEqual(typeof domain, 'string');
assert.strictEqual(typeof auditSource, 'object');
debug(`setDashboardDomain: ${domain}`);
await reverseProxy.writeDashboardConfig(domain);
const fqdn = dns.fqdn(constants.DASHBOARD_SUBDOMAIN, domain);
await setLocation(domain, fqdn);
await safe(appstore.updateCloudron({ domain }), { debug });
await eventlog.add(eventlog.ACTION_DASHBOARD_DOMAIN_UPDATE, auditSource, { domain, fqdn });
}
// call this only post activation because it will restart mail server
async function switchDomain(domain, auditSource) {
assert.strictEqual(typeof domain, 'string');
assert.strictEqual(typeof auditSource, 'object');
debug(`switchDomain: ${domain}`);
if (constants.DEMO) throw new BoxError(BoxError.CONFLICT, 'Not allowed in demo mode');
await setDashboardDomain(domain, auditSource);
// mark apps using oidc addon to be reconfigured
const [, installedApps] = await safe(apps.list());
await safe(apps.configureInstalledApps(installedApps.filter((a) => !!a.manifest.addons.oidc), auditSource));
await safe(services.rebuildService('turn', auditSource), { debug }); // to update the realm variable
await oidc.stop();
await oidc.start();
}
async function setupDnsAndCert(subdomain, domain, auditSource, progressCallback) {
assert.strictEqual(typeof subdomain, 'string');
assert.strictEqual(typeof domain, 'string');
assert.strictEqual(typeof auditSource, 'object');
assert.strictEqual(typeof progressCallback, 'function');
const dashboardFqdn = dns.fqdn(subdomain, domain);
const ipv4 = await network.getIPv4();
const ipv6 = await network.getIPv6();
progressCallback({ percent: 20, message: `Updating DNS of ${dashboardFqdn}` });
await dns.upsertDnsRecords(subdomain, domain, 'A', [ ipv4 ]);
if (ipv6) await dns.upsertDnsRecords(subdomain, domain, 'AAAA', [ ipv6 ]);
progressCallback({ percent: 40, message: `Waiting for DNS of ${dashboardFqdn}` });
await dns.waitForDnsRecord(subdomain, domain, 'A', ipv4, { interval: 30000, times: 50000 });
if (ipv6) await dns.waitForDnsRecord(subdomain, domain, 'AAAA', ipv6, { interval: 30000, times: 50000 });
progressCallback({ percent: 60, message: `Getting certificate of ${dashboardFqdn}` });
const location = { subdomain, domain, fqdn: dashboardFqdn, type: apps.LOCATION_TYPE_DASHBOARD, certificate: null };
await reverseProxy.ensureCertificate(location, {}, auditSource);
}