diff --git a/src/cloudron.js b/src/cloudron.js index f036f9ce7..b3d44648f 100644 --- a/src/cloudron.js +++ b/src/cloudron.js @@ -58,6 +58,7 @@ var appdb = require('./appdb.js'), subdomains = require('./subdomains.js'), superagent = require('superagent'), sysinfo = require('./sysinfo.js'), + tld = require('tldjs'), tokendb = require('./tokendb.js'), updateChecker = require('./updatechecker.js'), user = require('./user.js'), @@ -176,7 +177,8 @@ function dnsSetup(dnsConfig, domain, callback) { 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)); - config.set('fqdn', domain); // set fqdn only after dns config is valid, otherwise cannot re-setup if we failed + config.setFqdn(domain); // set fqdn only after dns config is valid, otherwise cannot re-setup if we failed + config.setZoneName(tld.getDomain(domain)); async.series([ // do not block onDomainConfigured, diff --git a/src/config.js b/src/config.js index b80d5f6ed..ffdc6c0a8 100644 --- a/src/config.js +++ b/src/config.js @@ -17,6 +17,7 @@ exports = module.exports = { apiServerOrigin: apiServerOrigin, webServerOrigin: webServerOrigin, fqdn: fqdn, + setFqdn: setFqdn, token: token, version: version, setVersion: setVersion, @@ -31,6 +32,7 @@ exports = module.exports = { mailFqdn: mailFqdn, appFqdn: appFqdn, zoneName: zoneName, + setZoneName: setZoneName, isDemo: isDemo, @@ -46,6 +48,7 @@ var assert = require('assert'), fs = require('fs'), path = require('path'), safe = require('safetydance'), + tld = require('tldjs'), _ = require('underscore'); var homeDir = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE; @@ -74,6 +77,7 @@ function _reset(callback) { function initConfig() { // setup defaults data.fqdn = 'localhost'; + data.zoneName = ''; data.token = null; data.version = null; @@ -143,10 +147,26 @@ function webServerOrigin() { return get('webServerOrigin'); } +function setFqdn(fqdn) { + set('fqdn', fqdn); +} + function fqdn() { return get('fqdn'); } +function setZoneName(zone) { + set('zoneName', zone); +} + +function zoneName() { + var zone = get('zoneName'); + if (zone) return zone; + + // TODO: move this to migration code path instead + return tld.getDomain(fqdn()) || ''; +} + // keep this in sync with start.sh admin.conf generation code function appFqdn(location) { assert.strictEqual(typeof location, 'string'); @@ -191,13 +211,6 @@ function isCustomDomain() { return get('isCustomDomain'); } -function zoneName() { - if (isCustomDomain()) return fqdn(); // the appstore sets up the custom domain as a zone - - // for shared domain name, strip out the hostname - return fqdn().substr(fqdn().indexOf('.') + 1); -} - function database() { return get('database'); } diff --git a/src/routes/test/apps-test.js b/src/routes/test/apps-test.js index 7af4b1296..d29f0925d 100644 --- a/src/routes/test/apps-test.js +++ b/src/routes/test/apps-test.js @@ -149,7 +149,8 @@ function startBox(done) { safe.fs.unlinkSync(paths.INFRA_VERSION_FILE); child_process.execSync('docker ps -qa | xargs --no-run-if-empty docker rm -f'); - config.set('fqdn', 'foobar.com'); + config.setFqdn('foobar.com'); + config.setZoneName('foobar.com'); awsHostedZones = { HostedZones: [{ diff --git a/src/routes/test/backups-test.js b/src/routes/test/backups-test.js index dfb53aceb..c4497c724 100644 --- a/src/routes/test/backups-test.js +++ b/src/routes/test/backups-test.js @@ -28,7 +28,7 @@ function setup(done) { nock.cleanAll(); config._reset(); config.setVersion('1.2.3'); - config.set('fqdn', 'localhost'); + config.setFqdn('localhost'); async.series([ server.start.bind(server), diff --git a/src/routes/test/cloudron-test.js b/src/routes/test/cloudron-test.js index 943686141..b40892fda 100644 --- a/src/routes/test/cloudron-test.js +++ b/src/routes/test/cloudron-test.js @@ -27,7 +27,7 @@ function setup(done) { nock.cleanAll(); config._reset(); config.set('version', '0.5.0'); - config.set('fqdn', 'localhost'); + config.setFqdn('localhost'); server.start(function (error) { if (error) return done(error); diff --git a/src/routes/test/settings-test.js b/src/routes/test/settings-test.js index 75593d9bf..dbf11ef42 100644 --- a/src/routes/test/settings-test.js +++ b/src/routes/test/settings-test.js @@ -28,7 +28,7 @@ var token = null; var server; function setup(done) { - config.set('fqdn', 'foobar.com'); + config.setFqdn('foobar.com'); async.series([ server.start.bind(server), diff --git a/src/routes/test/ssh-test.js b/src/routes/test/ssh-test.js index 5c04f8504..ebf33fe34 100644 --- a/src/routes/test/ssh-test.js +++ b/src/routes/test/ssh-test.js @@ -28,7 +28,7 @@ var token = null; var server; function setup(done) { - config.set('fqdn', 'foobar.com'); + config.setFqdn('foobar.com'); async.series([ server.start.bind(server), diff --git a/src/test/apptask-test.js b/src/test/apptask-test.js index 02bc1874d..33abb13d1 100644 --- a/src/test/apptask-test.js +++ b/src/test/apptask-test.js @@ -68,7 +68,8 @@ var APP = { describe('apptask', function () { before(function (done) { config.set('version', '0.5.0'); - config.set('fqdn', 'foobar.com'); + config.setFqdn('foobar.com'); + config.setZoneName('foobar.com'); config.set('provider', 'caas'); awsHostedZones = { diff --git a/src/test/config-test.js b/src/test/config-test.js index 8c4849e10..811c9a6cc 100644 --- a/src/test/config-test.js +++ b/src/test/config-test.js @@ -41,7 +41,7 @@ describe('config', function () { expect(config.fqdn()).to.equal('localhost'); expect(config.adminOrigin()).to.equal('https://' + constants.ADMIN_LOCATION + '.localhost'); expect(config.appFqdn('app')).to.equal('app.localhost'); - expect(config.zoneName()).to.equal('localhost'); + expect(config.zoneName()).to.equal(''); }); it('set saves value in file', function (done) { @@ -63,7 +63,7 @@ describe('config', function () { }); it('uses dotted locations with custom domain', function () { - config.set('fqdn', 'example.com'); + config.setFqdn('example.com'); config.set('isCustomDomain', true); expect(config.isCustomDomain()).to.equal(true); @@ -74,7 +74,7 @@ describe('config', function () { }); it('uses hyphen locations with non-custom domain', function () { - config.set('fqdn', 'test.example.com'); + config.setFqdn('test.example.com'); config.set('isCustomDomain', false); expect(config.isCustomDomain()).to.equal(false); @@ -95,4 +95,3 @@ describe('config', function () { }); }); - diff --git a/src/test/dns-test.js b/src/test/dns-test.js index daa7e51b1..f74e0e298 100644 --- a/src/test/dns-test.js +++ b/src/test/dns-test.js @@ -18,10 +18,11 @@ var async = require('async'), describe('dns provider', function () { before(function (done) { + config._reset(); + async.series([ database.initialize, - settings.initialize, - config._reset + settings.initialize ], done); }); @@ -35,6 +36,9 @@ describe('dns provider', function () { provider: 'noop' }; + config.setFqdn('example.com'); + config.setZoneName('example.com'); + settings.setDnsConfig(data, config.fqdn(), done); }); @@ -76,6 +80,9 @@ describe('dns provider', function () { token: TOKEN }; + config.setFqdn('example.com'); + config.setZoneName('example.com'); + settings.setDnsConfig(data, config.fqdn(), done); }); @@ -93,10 +100,10 @@ describe('dns provider', function () { }; var req1 = nock(DIGITALOCEAN_ENDPOINT).filteringRequestBody(function () { return false; }) - .get('/v2/domains/localhost/records') + .get('/v2/domains/' + config.zoneName() + '/records') .reply(200, { domain_records: [] }); var req2 = nock(DIGITALOCEAN_ENDPOINT).filteringRequestBody(function () { return false; }) - .post('/v2/domains/localhost/records') + .post('/v2/domains/' + config.zoneName() + '/records') .reply(201, { domain_record: DOMAIN_RECORD_0 }); subdomains.upsert('test', 'A', [ '1.2.3.4' ], function (error, result) { @@ -143,10 +150,10 @@ describe('dns provider', function () { }; var req1 = nock(DIGITALOCEAN_ENDPOINT).filteringRequestBody(function () { return false; }) - .get('/v2/domains/localhost/records') + .get('/v2/domains/' + config.zoneName() + '/records') .reply(200, { domain_records: [ DOMAIN_RECORD_0, DOMAIN_RECORD_1 ] }); var req2 = nock(DIGITALOCEAN_ENDPOINT).filteringRequestBody(function () { return false; }) - .put('/v2/domains/localhost/records/' + DOMAIN_RECORD_1.id) + .put('/v2/domains/' + config.zoneName() + '/records/' + DOMAIN_RECORD_1.id) .reply(200, { domain_records: DOMAIN_RECORD_1_NEW }); subdomains.upsert('test', 'A', [ DOMAIN_RECORD_1_NEW.data ], function (error, result) { @@ -223,16 +230,16 @@ describe('dns provider', function () { }; var req1 = nock(DIGITALOCEAN_ENDPOINT).filteringRequestBody(function () { return false; }) - .get('/v2/domains/localhost/records') + .get('/v2/domains/' + config.zoneName() + '/records') .reply(200, { domain_records: [ DOMAIN_RECORD_0, DOMAIN_RECORD_1, DOMAIN_RECORD_2 ] }); var req2 = nock(DIGITALOCEAN_ENDPOINT).filteringRequestBody(function () { return false; }) - .put('/v2/domains/localhost/records/' + DOMAIN_RECORD_1.id) + .put('/v2/domains/' + config.zoneName() + '/records/' + DOMAIN_RECORD_1.id) .reply(200, { domain_records: DOMAIN_RECORD_1_NEW }); var req3 = nock(DIGITALOCEAN_ENDPOINT).filteringRequestBody(function () { return false; }) - .put('/v2/domains/localhost/records/' + DOMAIN_RECORD_2.id) + .put('/v2/domains/' + config.zoneName() + '/records/' + DOMAIN_RECORD_2.id) .reply(200, { domain_records: DOMAIN_RECORD_2_NEW }); var req4 = nock(DIGITALOCEAN_ENDPOINT).filteringRequestBody(function () { return false; }) - .post('/v2/domains/localhost/records') + .post('/v2/domains/' + config.zoneName() + '/records') .reply(201, { domain_records: DOMAIN_RECORD_2_NEW }); subdomains.upsert('', 'TXT', [ DOMAIN_RECORD_2_NEW.data, DOMAIN_RECORD_1_NEW.data, DOMAIN_RECORD_3_NEW.data ], function (error, result) { @@ -271,7 +278,7 @@ describe('dns provider', function () { }; var req1 = nock(DIGITALOCEAN_ENDPOINT).filteringRequestBody(function () { return false; }) - .get('/v2/domains/localhost/records') + .get('/v2/domains/' + config.zoneName() + '/records') .reply(200, { domain_records: [ DOMAIN_RECORD_0, DOMAIN_RECORD_1 ] }); subdomains.get('test', 'A', function (error, result) { @@ -309,10 +316,10 @@ describe('dns provider', function () { }; var req1 = nock(DIGITALOCEAN_ENDPOINT).filteringRequestBody(function () { return false; }) - .get('/v2/domains/localhost/records') + .get('/v2/domains/' + config.zoneName() + '/records') .reply(200, { domain_records: [ DOMAIN_RECORD_0, DOMAIN_RECORD_1 ] }); var req2 = nock(DIGITALOCEAN_ENDPOINT).filteringRequestBody(function () { return false; }) - .delete('/v2/domains/localhost/records/' + DOMAIN_RECORD_1.id) + .delete('/v2/domains/' + config.zoneName() + '/records/' + DOMAIN_RECORD_1.id) .reply(204, {}); subdomains.remove('test', 'A', ['1.2.3.4'], function (error) { @@ -326,13 +333,16 @@ describe('dns provider', function () { }); describe('route53', function () { + config.setFqdn('example.com'); + config.setZoneName('example.com'); + // do not clear this with [] but .length = 0 so we don't loose the reference in mockery var awsAnswerQueue = []; var AWS_HOSTED_ZONES = { HostedZones: [{ Id: '/hostedzone/Z34G16B38TNZ9L', - Name: 'localhost.', + Name: config.zoneName() + '.', CallerReference: '305AFD59-9D73-4502-B020-F4E6F889CB30', ResourceRecordSetCount: 2, ChangeInfo: { @@ -470,7 +480,7 @@ describe('dns provider', function () { awsAnswerQueue.push([null, AWS_HOSTED_ZONES]); awsAnswerQueue.push([null, { ResourceRecordSets: [{ - Name: 'test.localhost.', + Name: 'test.' + config.zoneName() + '.', Type: 'A', ResourceRecords: [{ Value: '1.2.3.4'