Files
cloudron-box/src/dashboard.js
T

153 lines
6.3 KiB
JavaScript
Raw Normal View History

import apps from './apps.js';
import * as appstore from './appstore.js';
import assert from 'node:assert';
import BoxError from './boxerror.js';
import * as branding from './branding.js';
import constants from './constants.js';
import debugModule from 'debug';
import * as dns from './dns.js';
import * as externalLdap from './externalldap.js';
import eventlog from './eventlog.js';
import Location from './location.js';
import * as mailServer from './mailserver.js';
import * as platform from './platform.js';
import * as reverseProxy from './reverseproxy.js';
import safe from 'safetydance';
import * as settings from './settings.js';
import * as system from './system.js';
import tasks from './tasks.js';
import * as userDirectory from './user-directory.js';
2023-08-11 19:41:05 +05:30
const debug = debugModule('box:dashboard');
2023-08-12 21:47:24 +05:30
const _setLocation = setLocation;
export {
getLocation,
clearLocation,
startPrepareLocation, // starts the task,
prepareLocation, // the task to setup dns and cert,
setupLocation, // initial setup from setup/restore,
changeLocation, // only on dashboard change (post setup/restore),
2023-08-12 21:47:24 +05:30
getConfig,
_setLocation,
2023-08-11 19:41:05 +05:30
};
2023-08-13 10:29:24 +05:30
async function getLocation() {
2023-08-17 10:44:07 +05:30
const domain = await settings.get(settings.DASHBOARD_DOMAIN_KEY);
const subdomain = await settings.get(settings.DASHBOARD_SUBDOMAIN_KEY);
return new Location(subdomain, domain, Location.TYPE_DASHBOARD);
2023-08-13 10:29:24 +05:30
}
2023-08-16 19:28:04 +05:30
async function setLocation(subdomain, domain) {
assert.strictEqual(typeof subdomain, 'string');
2023-08-11 19:41:05 +05:30
assert.strictEqual(typeof domain, 'string');
2023-08-16 19:28:04 +05:30
await settings.set(settings.DASHBOARD_SUBDOMAIN_KEY, subdomain);
2023-08-11 19:41:05 +05:30
await settings.set(settings.DASHBOARD_DOMAIN_KEY, domain);
2023-08-13 21:36:56 +05:30
debug(`setLocation: ${domain || '<cleared>'}`);
2023-08-13 10:29:24 +05:30
}
2023-08-11 19:41:05 +05:30
2023-08-13 10:29:24 +05:30
async function clearLocation() {
2023-08-16 19:28:04 +05:30
await setLocation('', '');
2023-08-11 19:41:05 +05:30
}
2023-08-12 21:47:24 +05:30
async function getConfig() {
const ubuntuVersion = await system.getUbuntuVersion();
2025-07-09 18:28:55 +02:00
const kernelVersion = await system.getKernelVersion();
const profileConfig = await userDirectory.getProfileConfig();
const externalLdapConfig = await externalLdap.getConfig();
2023-08-12 21:47:24 +05:30
const { fqdn:adminFqdn, domain:adminDomain } = await getLocation();
// be picky about what we send out here since this is sent for 'normal' users as well
return {
apiServerOrigin: await appstore.getApiServerOrigin(),
webServerOrigin: await appstore.getWebServerOrigin(),
consoleServerOrigin: await appstore.getConsoleServerOrigin(),
adminDomain,
adminFqdn,
2023-09-12 18:03:36 +05:30
mailFqdn: (await mailServer.getLocation()).fqdn,
2023-08-12 21:47:24 +05:30
version: constants.VERSION,
ubuntuVersion,
2025-07-09 18:28:55 +02:00
kernelVersion,
2023-08-12 21:47:24 +05:30
isDemo: constants.DEMO,
cloudronName: await branding.getCloudronName(),
footer: await branding.renderFooter(),
features: appstore.getFeatures(),
profileLocked: profileConfig.lockUserProfiles,
mandatory2FA: profileConfig.mandatory2FA,
external2FA: externalLdap.supports2FA(externalLdapConfig),
ldapGroupsSynced: externalLdapConfig.syncGroups
2023-08-12 21:47:24 +05:30
};
}
2023-08-14 09:40:31 +05:30
async function startPrepareLocation(domain, auditSource) {
assert.strictEqual(typeof domain, 'string');
assert.strictEqual(typeof auditSource, 'object');
2023-08-13 10:29:24 +05:30
debug(`prepareLocation: ${domain}`);
2024-01-13 21:15:41 +01:00
if (constants.DEMO) throw new BoxError(BoxError.BAD_STATE, 'Not allowed in demo mode');
const fqdn = dns.fqdn(constants.DASHBOARD_SUBDOMAIN, domain);
for (const app of await apps.list()) {
const appName = `${app.label || app.fqdn} (${app.id})`;
if (app.fqdn === fqdn) throw new BoxError(BoxError.BAD_STATE, `Dashboard location conflicts with primary location of app '${appName}'`);
2025-06-10 13:01:40 +02:00
if (app.secondaryDomains.some(sd => sd.fqdn === fqdn)) throw new BoxError(BoxError.BAD_STATE, `Dashboard location conflicts with secondary location of app '${appName}'`);
if (app.redirectDomains.some(rd => rd.fqdn === fqdn)) throw new BoxError(BoxError.BAD_STATE, `Dashboard location conflicts with redirect location of app '${appName}'`);
if (app.aliasDomains.some(ad => ad.fqdn === fqdn)) throw new BoxError(BoxError.BAD_STATE, `Dashboard location conflicts with alias location of app '${appName}'`);
}
2023-08-14 09:40:31 +05:30
const taskId = await tasks.add(tasks.TASK_PREPARE_DASHBOARD_LOCATION, [ constants.DASHBOARD_SUBDOMAIN, domain, auditSource ]);
2025-06-17 18:54:12 +02:00
safe(tasks.startTask(taskId, {}), { debug }); // background
return taskId;
}
2023-08-14 09:40:31 +05:30
async function prepareLocation(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 location = { subdomain, domain };
const fqdn = dns.fqdn(subdomain, domain);
progressCallback({ percent: 20, message: `Updating DNS of ${fqdn}` });
await dns.registerLocations([location], { overwriteDns: true }, progressCallback);
progressCallback({ percent: 40, message: `Waiting for DNS of ${fqdn}` });
await dns.waitForLocations([location], progressCallback);
progressCallback({ percent: 60, message: `Getting certificate of ${fqdn}` });
await reverseProxy.ensureCertificate(location, {}, auditSource);
}
2023-08-16 19:28:04 +05:30
async function setupLocation(subdomain, domain, auditSource) {
assert.strictEqual(typeof subdomain, 'string');
assert.strictEqual(typeof domain, 'string');
assert.strictEqual(typeof auditSource, 'object');
2023-08-13 21:36:56 +05:30
debug(`setupLocation: ${domain}`);
2023-08-13 10:29:24 +05:30
2024-01-13 21:15:41 +01:00
if (constants.DEMO) throw new BoxError(BoxError.BAD_STATE, 'Not allowed in demo mode');
2023-08-16 19:28:04 +05:30
await setLocation(subdomain, domain);
await platform.onDashboardLocationSet(subdomain, domain);
2023-08-17 13:02:36 +05:30
}
async function changeLocation(subdomain, domain, auditSource) {
assert.strictEqual(typeof subdomain, 'string');
assert.strictEqual(typeof domain, 'string');
assert.strictEqual(typeof auditSource, 'object');
const oldLocation = await getLocation();
2023-08-17 13:02:36 +05:30
await setupLocation(subdomain, domain, auditSource);
2023-08-14 14:20:20 +05:30
2023-08-17 13:02:36 +05:30
debug(`setupLocation: notifying appstore and platform of domain change to ${domain}`);
2023-08-16 19:28:04 +05:30
await eventlog.add(eventlog.ACTION_DASHBOARD_DOMAIN_UPDATE, auditSource, { subdomain, domain });
2023-08-16 11:34:41 +05:30
await safe(appstore.updateCloudron({ domain }), { debug });
await platform.onDashboardLocationChanged(auditSource);
await safe(reverseProxy.removeDashboardConfig(oldLocation.subdomain, oldLocation.domain), { debug });
}