diff --git a/src/cloudron.js b/src/cloudron.js index 595ecfb8a..99d8eff54 100644 --- a/src/cloudron.js +++ b/src/cloudron.js @@ -40,6 +40,7 @@ var appdb = require('./appdb.js'), debug = require('debug')('box:cloudron'), df = require('@sindresorhus/df'), domains = require('./domains.js'), + DomainError = domains.DomainError, eventlog = require('./eventlog.js'), fs = require('fs'), locker = require('./locker.js'), @@ -176,8 +177,8 @@ function dnsSetup(dnsConfig, domain, zoneName, callback) { debug('dnsSetup: Setting up Cloudron with domain %s and zone %s', domain, zoneName); - settings.setDnsConfig(dnsConfig, domain, zoneName, function (error) { - if (error && error.reason === SettingsError.BAD_FIELD) return callback(new CloudronError(CloudronError.BAD_FIELD, error.message)); + function done(error) { + if (error && error.reason === DomainError.BAD_FIELD) return callback(new CloudronError(CloudronError.BAD_FIELD, error.message)); if (error) return callback(new CloudronError(CloudronError.INTERNAL_ERROR, error)); config.setFqdn(domain); // set fqdn only after dns config is valid, otherwise cannot re-setup if we failed @@ -189,6 +190,13 @@ function dnsSetup(dnsConfig, domain, zoneName, callback) { ], NOOP_CALLBACK); callback(); + } + + domains.get(domain, function (error, result) { + if (error && error.reason !== DomainError.NOT_FOUND) return callback(new SettingsError(SettingsError.INTERNAL_ERROR, error)); + + if (!result) domains.add(domain, zoneName, dnsConfig, done); + else domains.update(domain, dnsConfig, done); }); } @@ -846,12 +854,20 @@ function migrate(options, callback) { var dnsConfig = _.pick(options, 'domain', 'provider', 'accessKeyId', 'secretAccessKey', 'region', 'endpoint', 'token', 'zoneName'); - settings.setDnsConfig(dnsConfig, options.domain, options.zoneName || tld.getDomain(options.domain), function (error) { - if (error && error.reason === SettingsError.BAD_FIELD) return callback(new CloudronError(CloudronError.BAD_FIELD, error.message)); - if (error) return callback(new CloudronError(CloudronError.INTERNAL_ERROR, error)); + domains.get(options.domain, function (error, result) { + if (error && error.reason !== DomainError.NOT_FOUND) return callback(new CloudronError(CloudronError.INTERNAL_ERROR, error)); - // TODO: should probably rollback dns config if migrate fails - doMigrate(options, callback); + var func; + if (!result) func = domains.add.bind(null, options.domain, options.zoneName, dnsConfig); + else func = domains.update.bind(null, options.domain, dnsConfig); + + func(function (error) { + if (error && error.reason === DomainError.BAD_FIELD) return callback(new CloudronError(CloudronError.BAD_FIELD, error.message)); + if (error) return callback(new SettingsError(CloudronError.INTERNAL_ERROR, error)); + + // TODO: should probably rollback dns config if migrate fails + doMigrate(options, callback); + }); }); } diff --git a/src/routes/settings.js b/src/routes/settings.js index 7a64b39e4..64648cc0d 100644 --- a/src/routes/settings.js +++ b/src/routes/settings.js @@ -12,9 +12,6 @@ exports = module.exports = { getEmailStatus: getEmailStatus, - getDnsConfig: getDnsConfig, - setDnsConfig: setDnsConfig, - getBackupConfig: getBackupConfig, setBackupConfig: setBackupConfig, @@ -239,27 +236,6 @@ function getEmailStatus(req, res, next) { }); } -function getDnsConfig(req, res, next) { - settings.getDnsConfig(function (error, config) { - if (error) return next(new HttpError(500, error)); - - next(new HttpSuccess(200, config)); - }); -} - -function setDnsConfig(req, res, next) { - assert.strictEqual(typeof req.body, 'object'); - - if (typeof req.body.provider !== 'string') return next(new HttpError(400, 'provider is required')); - - settings.setDnsConfig(req.body, config.fqdn(), config.zoneName(), function (error) { - if (error && error.reason === SettingsError.BAD_FIELD) return next(new HttpError(400, error.message)); - if (error) return next(new HttpError(500, error)); - - next(new HttpSuccess(200)); - }); -} - function getBackupConfig(req, res, next) { settings.getBackupConfig(function (error, config) { if (error) return next(new HttpError(500, error)); diff --git a/src/server.js b/src/server.js index 3e4eb8893..e18b03df9 100644 --- a/src/server.js +++ b/src/server.js @@ -206,8 +206,6 @@ function initializeExpressSync() { router.get ('/api/v1/settings/cloudron_avatar', settingsScope, routes.user.requireAdmin, routes.settings.getCloudronAvatar); router.post('/api/v1/settings/cloudron_avatar', settingsScope, routes.user.requireAdmin, multipart, routes.settings.setCloudronAvatar); router.get ('/api/v1/settings/email_status', settingsScope, routes.user.requireAdmin, routes.settings.getEmailStatus); - router.get ('/api/v1/settings/dns_config', settingsScope, routes.user.requireAdmin, routes.settings.getDnsConfig); - router.post('/api/v1/settings/dns_config', settingsScope, routes.user.requireAdmin, routes.settings.setDnsConfig); router.get ('/api/v1/settings/backup_config', settingsScope, routes.user.requireAdmin, routes.settings.getBackupConfig); router.post('/api/v1/settings/backup_config', settingsScope, routes.user.requireAdmin, routes.settings.setBackupConfig); router.post('/api/v1/settings/certificate', settingsScope, routes.user.requireAdmin, routes.settings.setFallbackCertificate); diff --git a/src/settings.js b/src/settings.js index e55721e7e..53cef8d23 100644 --- a/src/settings.js +++ b/src/settings.js @@ -21,10 +21,6 @@ exports = module.exports = { getDeveloperMode: getDeveloperMode, setDeveloperMode: setDeveloperMode, - // DEPRECATED in favor of domains table via domains.js - getDnsConfig: getDnsConfig, - setDnsConfig: setDnsConfig, - getDynamicDnsConfig: getDynamicDnsConfig, setDynamicDnsConfig: setDynamicDnsConfig, @@ -296,44 +292,6 @@ function setDeveloperMode(enabled, callback) { }); } -function getDnsConfig(callback) { - assert.strictEqual(typeof callback, 'function'); - - domains.get(config.fqdn(), function (error, result) { - if (error && error.reason === DomainError.NOT_FOUND) return callback(null, { provider: 'manual' }); - if (error) return callback(new SettingsError(SettingsError.INTERNAL_ERROR, error)); - - callback(null, result); - }); -} - -function setDnsConfig(dnsConfig, domain, zoneName, callback) { - assert.strictEqual(typeof dnsConfig, 'object'); - assert.strictEqual(typeof domain, 'string'); - assert.strictEqual(typeof zoneName, 'string'); - assert.strictEqual(typeof callback, 'function'); - - function done(error) { - if (error && error.reason === DomainError.ACCESS_DENIED) return callback(new SettingsError(SettingsError.BAD_FIELD, 'Error adding A record. Access denied')); - if (error && error.reason === DomainError.NOT_FOUND) return callback(new SettingsError(SettingsError.BAD_FIELD, 'Zone not found')); - if (error && error.reason === DomainError.EXTERNAL_ERROR) return callback(new SettingsError(SettingsError.BAD_FIELD, 'Error adding A record:' + error.message)); - if (error && error.reason === DomainError.BAD_FIELD) return callback(new SettingsError(SettingsError.BAD_FIELD, error.message)); - if (error && error.reason === DomainError.INVALID_PROVIDER) return callback(new SettingsError(SettingsError.BAD_FIELD, error.message)); - if (error) return callback(new SettingsError(SettingsError.INTERNAL_ERROR, error)); - - cloudron.configureWebadmin(NOOP_CALLBACK); // do not block - - callback(null, dnsConfig); - } - - domains.get(domain, function (error, result) { - if (error && error.reason !== DomainError.NOT_FOUND) return callback(new SettingsError(SettingsError.INTERNAL_ERROR, error)); - - if (!result) domains.add(domain, zoneName, dnsConfig, done); - else domains.update(domain, dnsConfig, done); - }); -} - function getDynamicDnsConfig(callback) { assert.strictEqual(typeof callback, 'function'); diff --git a/webadmin/src/js/client.js b/webadmin/src/js/client.js index ea40dbc1d..8296ae91c 100644 --- a/webadmin/src/js/client.js +++ b/webadmin/src/js/client.js @@ -467,20 +467,6 @@ angular.module('Application').service('Client', ['$http', 'md5', 'Notification', }).error(defaultErrorHandler(callback)); }; - Client.prototype.setDnsConfig = function (dnsConfig, callback) { - post('/api/v1/settings/dns_config', dnsConfig).success(function(data, status) { - if (status !== 200) return callback(new ClientError(status, data)); - callback(null); - }).error(defaultErrorHandler(callback)); - }; - - Client.prototype.getDnsConfig = function (callback) { - get('/api/v1/settings/dns_config').success(function(data, status) { - if (status !== 200) return callback(new ClientError(status, data)); - callback(null, data); - }).error(defaultErrorHandler(callback)); - }; - Client.prototype.setAutoupdatePattern = function (pattern, callback) { post('/api/v1/settings/autoupdate_pattern', { pattern: pattern }).success(function(data, status) { if (status !== 200) return callback(new ClientError(status, data)); diff --git a/webadmin/src/js/main.js b/webadmin/src/js/main.js index 199310138..5d66ba357 100644 --- a/webadmin/src/js/main.js +++ b/webadmin/src/js/main.js @@ -114,47 +114,41 @@ angular.module('Application').controller('MainController', ['$scope', '$route', }; function runConfigurationChecks() { - Client.getDnsConfig(function (error, result) { + var actionScope; + + // warn user if dns config is not working (the 'configuring' flag detects if configureWebadmin is 'active') + if (!$scope.status.webadminStatus.configuring && !$scope.status.webadminStatus.dns) { + actionScope = $scope.$new(true); + actionScope.action = '/#/certs'; + Client.notify('Invalid Domain Config', 'Unable to update DNS. Click here to update it.', true, 'error', actionScope); + } + + Client.getBackupConfig(function (error, backupConfig) { if (error) return console.error(error); - var actionScope; + if (backupConfig.provider === 'noop') { + var actionScope = $scope.$new(true); + actionScope.action = '/#/settings'; - // warn user if dns config is not working (the 'configuring' flag detects if configureWebadmin is 'active') - if (!$scope.status.webadminStatus.configuring && !$scope.status.webadminStatus.dns) { - actionScope = $scope.$new(true); - actionScope.action = '/#/certs'; - Client.notify('Invalid Domain Config', 'Unable to update DNS. Click here to update it.', true, 'error', actionScope); + Client.notify('Backup Configuration', 'Cloudron backups are disabled. Ensure the server is backed up using alternate means.', false, 'info', actionScope); } - if (result.provider === 'caas') return; - - Client.getBackupConfig(function (error, backupConfig) { + Client.getMailRelay(function (error, result) { if (error) return console.error(error); - if (backupConfig.provider === 'noop') { - var actionScope = $scope.$new(true); - actionScope.action = '/#/settings'; + // the email status checks are currently only useful when using Cloudron itself for relaying + if (result.provider !== 'cloudron-smtp') return; - Client.notify('Backup Configuration', 'Cloudron backups are disabled. Ensure the server is backed up using alternate means.', false, 'info', actionScope); - } - - Client.getMailRelay(function (error, result) { + // Check if all email DNS records are set up properly only for non external DNS API + Client.getEmailStatus(function (error, result) { if (error) return console.error(error); - // the email status checks are currently only useful when using Cloudron itself for relaying - if (result.provider !== 'cloudron-smtp') return; + if (!result.dns.spf.status || !result.dns.dkim.status || !result.dns.ptr.status || !result.relay.status) { + var actionScope = $scope.$new(true); + actionScope.action = '/#/email'; - // Check if all email DNS records are set up properly only for non external DNS API - Client.getEmailStatus(function (error, result) { - if (error) return console.error(error); - - if (!result.dns.spf.status || !result.dns.dkim.status || !result.dns.ptr.status || !result.relay.status) { - var actionScope = $scope.$new(true); - actionScope.action = '/#/email'; - - Client.notify('DNS Configuration', 'Please setup all required DNS records to guarantee correct mail delivery', false, 'info', actionScope); - } - }); + Client.notify('DNS Configuration', 'Please setup all required DNS records to guarantee correct mail delivery', false, 'info', actionScope); + } }); }); }); diff --git a/webadmin/src/views/email.js b/webadmin/src/views/email.js index a52e3642a..59050db74 100644 --- a/webadmin/src/views/email.js +++ b/webadmin/src/views/email.js @@ -239,11 +239,12 @@ angular.module('Application').controller('EmailController', ['$scope', '$locatio }); } + // TODO this currently assumes the config.fqdn is the mail domain function getDnsConfig() { - Client.getDnsConfig(function (error, dnsConfig) { + Client.getDomain($scope.config.fqdn, function (error, result) { if (error) return console.error(error); - $scope.dnsConfig = dnsConfig; + $scope.dnsConfig = result.config; }); }