diff --git a/src/dashboard.js b/src/dashboard.js index c4750de9b..c63bfffd6 100644 --- a/src/dashboard.js +++ b/src/dashboard.js @@ -4,9 +4,10 @@ exports = module.exports = { getLocation, clearLocation, - startPrepareLocation, - prepareLocation, - setupLocation, + 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) getConfig, @@ -120,12 +121,17 @@ async function setupLocation(subdomain, domain, auditSource) { if (constants.DEMO) throw new BoxError(BoxError.CONFLICT, 'Not allowed in demo mode'); await reverseProxy.writeDashboardConfig(domain); - - const currentLocation = await getLocation(); await setLocation(subdomain, domain); - if (!currentLocation.domain) return; // first time location is set, no change notification needed +} - debug('setupLocation: notifying platform of domain change'); +async function changeLocation(subdomain, domain, auditSource) { + assert.strictEqual(typeof subdomain, 'string'); + assert.strictEqual(typeof domain, 'string'); + assert.strictEqual(typeof auditSource, 'object'); + + await setupLocation(subdomain, domain, auditSource); + + debug(`setupLocation: notifying appstore and platform of domain change to ${domain}`); await eventlog.add(eventlog.ACTION_DASHBOARD_DOMAIN_UPDATE, auditSource, { subdomain, domain }); await safe(appstore.updateCloudron({ domain }), { debug }); await platform.onDashboardLocationChanged(auditSource); diff --git a/src/location.js b/src/location.js index 09fbb5ad9..29b385a72 100644 --- a/src/location.js +++ b/src/location.js @@ -1,7 +1,13 @@ 'use strict'; +const assert = require('assert'); + class Location { constructor(subdomain, domain, type, certificate) { + assert(subdomain === null || typeof subdomain === 'string'); + assert(domain === null || typeof domain === 'string'); + assert.strictEqual(typeof type, 'string'); + this.subdomain = subdomain; this.domain = domain; this.type = type; diff --git a/src/reverseproxy.js b/src/reverseproxy.js index 78814c459..08a7fc8e4 100644 --- a/src/reverseproxy.js +++ b/src/reverseproxy.js @@ -248,9 +248,9 @@ function getAppLocationsSync(app) { assert.strictEqual(typeof app, 'object'); return [new Location(app.subdomain, app.domain, Location.TYPE_PRIMARY, app.certificate)] - .concat(app.secondaryDomains.map(sd => new Location(sd.subdomin, sd.domain, Location.TYPE_SECONDARY, sd.certificate))) - .concat(app.redirectDomains.map(rd => new Location(rd.subdomin, rd.domain, Location.TYPE_REDIRECT, rd.certificate))) - .concat(app.aliasDomains.map(ad => new Location(ad.subdomin, ad.domain, Location.TYPE_ALIAS, ad.certificate))); + .concat(app.secondaryDomains.map(sd => new Location(sd.subdomain, sd.domain, Location.TYPE_SECONDARY, sd.certificate))) + .concat(app.redirectDomains.map(rd => new Location(rd.subdomain, rd.domain, Location.TYPE_REDIRECT, rd.certificate))) + .concat(app.aliasDomains.map(ad => new Location(ad.subdomain, ad.domain, Location.TYPE_ALIAS, ad.certificate))); } function getAcmeCertificateNameSync(fqdn, domainObject) { diff --git a/src/routes/dashboard.js b/src/routes/dashboard.js index ca5ffb53c..66c607379 100644 --- a/src/routes/dashboard.js +++ b/src/routes/dashboard.js @@ -4,7 +4,7 @@ exports = module.exports = { getConfig, startPrepareLocation, - setupLocation + changeLocation }; const AuditSource = require('../auditsource.js'), @@ -31,10 +31,10 @@ async function startPrepareLocation(req, res, next) { next(new HttpSuccess(202, { taskId })); } -async function setupLocation(req, res, next) { +async function changeLocation(req, res, next) { if (!req.body.domain || typeof req.body.domain !== 'string') return next(new HttpError(400, 'domain must be a string')); - const [error] = await safe(dashboard.setupLocation(constants.DASHBOARD_SUBDOMAIN, req.body.domain, AuditSource.fromRequest(req))); + const [error] = await safe(dashboard.changeLocation(constants.DASHBOARD_SUBDOMAIN, req.body.domain, AuditSource.fromRequest(req))); if (error) return next(BoxError.toHttpError(error)); next(new HttpSuccess(204, {})); diff --git a/src/server.js b/src/server.js index 0deadbe6e..5db25674a 100644 --- a/src/server.js +++ b/src/server.js @@ -117,7 +117,7 @@ async function initializeExpressSync() { // config route for dashboard that any auth user (not just admin) can access router.get ('/api/v1/dashboard/config', token, authorizeUser, routes.dashboard.getConfig); router.post('/api/v1/dashboard/prepare_location', json, token, authorizeAdmin, routes.dashboard.startPrepareLocation); - router.post('/api/v1/dashboard/location', json, token, authorizeAdmin, routes.dashboard.setupLocation); + router.post('/api/v1/dashboard/location', json, token, authorizeAdmin, routes.dashboard.changeLocation); // system (vm/server) router.get ('/api/v1/system/reboot', token, authorizeAdmin, routes.system.isRebootRequired);