diff --git a/src/database.js b/src/database.js index b9ef8dd61..d5c150603 100644 --- a/src/database.js +++ b/src/database.js @@ -101,6 +101,7 @@ function clear(callback) { async.series([ child_process.exec.bind(null, cmd), require('./clientdb.js')._addDefaultClients, + require('./domaindb.js')._addDefaultDomain, require('./groupdb.js')._addDefaultGroups ], callback); } diff --git a/src/domaindb.js b/src/domaindb.js index 9f238064b..9b7e01277 100644 --- a/src/domaindb.js +++ b/src/domaindb.js @@ -9,12 +9,14 @@ exports = module.exports = { update: update, del: del, - _clear: clear + _clear: clear, + _addDefaultDomain: addDefaultDomain }; var assert = require('assert'), database = require('./database.js'), DatabaseError = require('./databaseerror'), + config = require('./config.js'), safe = require('safetydance'); function postProcess(data) { @@ -93,3 +95,12 @@ function clear(callback) { callback(error); }); } + +function addDefaultDomain(callback) { + assert(config.fqdn(), 'no fqdn set in config, cannot continue'); + + add(config.fqdn(), config.zoneName(), {}, function (error) { + if (error && error.reason !== DatabaseError.ALREADY_EXISTS) return callback(error); + callback(); + }); +} \ No newline at end of file diff --git a/src/groupdb.js b/src/groupdb.js index 51b25361c..19b9e07ae 100644 --- a/src/groupdb.js +++ b/src/groupdb.js @@ -24,6 +24,7 @@ exports = module.exports = { var assert = require('assert'), constants = require('./constants.js'), + config = require('./config.js'), database = require('./database.js'), DatabaseError = require('./databaseerror'), mailboxdb = require('./mailboxdb.js'); @@ -88,10 +89,8 @@ function add(id, name, callback) { assert.strictEqual(typeof name, 'string'); assert.strictEqual(typeof callback, 'function'); - var data = [ id, name ]; - var queries = []; - queries.push({ query: 'INSERT INTO mailboxes (name, ownerId, ownerType) VALUES (?, ?, ?)', args: [ name, id, mailboxdb.TYPE_GROUP ] }); + queries.push({ query: 'INSERT INTO mailboxes (name, domain, ownerId, ownerType) VALUES (?, ?, ?, ?)', args: [ name, config.fqdn(), id, mailboxdb.TYPE_GROUP ] }); queries.push({ query: 'INSERT INTO groups (id, name) VALUES (?, ?)', args: [ id, name ] }); database.transaction(queries, function (error, result) { diff --git a/src/test/apps-test.js b/src/test/apps-test.js index 16b68f14f..79760af68 100644 --- a/src/test/apps-test.js +++ b/src/test/apps-test.js @@ -127,11 +127,12 @@ describe('Apps', function () { }; before(function (done) { + config.setFqdn(DOMAIN_0.domain); async.series([ database.initialize, database._clear, - domaindb.add.bind(null, DOMAIN_0.domain, DOMAIN_0.zoneName, DOMAIN_0.config), + // DOMAIN_0 already added for test through domaindb.addDefaultDomain() domaindb.add.bind(null, DOMAIN_1.domain, DOMAIN_1.zoneName, DOMAIN_1.config), userdb.add.bind(null, ADMIN_0.id, ADMIN_0), userdb.add.bind(null, USER_0.id, USER_0), diff --git a/src/test/apptask-test.js b/src/test/apptask-test.js index 31bf55a26..dad36f629 100644 --- a/src/test/apptask-test.js +++ b/src/test/apptask-test.js @@ -11,6 +11,7 @@ var addons = require('../addons.js'), async = require('async'), config = require('../config.js'), database = require('../database.js'), + domains = require('../domains.js'), expect = require('expect.js'), fs = require('fs'), js2xml = require('js2xmlparser').parse, @@ -48,12 +49,24 @@ var MANIFEST = { } }; +const DOMAIN_0 = { + domain: 'example.com', + zoneName: 'example.com', + config: { + provider: 'route53', + accessKeyId: 'accessKeyId', + secretAccessKey: 'secretAccessKey', + endpoint: 'http://localhost:5353' + } +}; + var APP = { id: 'appid', appStoreId: 'appStoreId', installationState: appdb.ISTATE_PENDING_INSTALL, runState: null, location: 'applocation', + domain: DOMAIN_0.domain, manifest: MANIFEST, containerId: null, httpPort: 4567, @@ -63,13 +76,12 @@ var APP = { memoryLimit: 0 }; - var awsHostedZones; +var awsHostedZones; describe('apptask', function () { before(function (done) { config.set('version', '0.5.0'); - config.setFqdn('foobar.com'); - config.setZoneName('foobar.com'); + config.setFqdn(DOMAIN_0.domain); config.set('provider', 'caas'); awsHostedZones = { @@ -89,9 +101,9 @@ describe('apptask', function () { async.series([ database.initialize, - appdb.add.bind(null, APP.id, APP.appStoreId, APP.manifest, APP.location, APP.portBindings, APP), + domains.update.bind(null, DOMAIN_0.domain, DOMAIN_0.config, null), + appdb.add.bind(null, APP.id, APP.appStoreId, APP.manifest, APP.location, APP.domain, APP.portBindings, APP), settings.initialize, - settings.setDnsConfig.bind(null, { provider: 'route53', accessKeyId: 'accessKeyId', secretAccessKey: 'secretAccessKey', endpoint: 'http://localhost:5353' }, config.fqdn(), config.zoneName()), settings.setTlsConfig.bind(null, { provider: 'caas' }) ], done); }); @@ -246,7 +258,7 @@ describe('apptask', function () { .post('/2013-04-01/hostedzone/ZONEID/rrset/') .reply(200, js2xml('ChangeResourceRecordSetsResponse', { ChangeInfo: { Id: 'RRID', Status: 'INSYNC' } })); - apptask._unregisterSubdomain(APP, APP.location, function (error) { + apptask._unregisterSubdomain(APP, APP.location, APP.domain, function (error) { expect(error).to.be(null); expect(awsScope.isDone()).to.be.ok(); done(); diff --git a/src/test/digest-test.js b/src/test/digest-test.js index a45991c24..e232b2b73 100644 --- a/src/test/digest-test.js +++ b/src/test/digest-test.js @@ -9,6 +9,7 @@ var async = require('async'), config = require('../config.js'), database = require('../database.js'), digest = require('../digest.js'), + domaindb = require('../domaindb.js'), eventlog = require('../eventlog.js'), expect = require('expect.js'), mailer = require('../mailer.js'), @@ -27,6 +28,12 @@ var USER_0 = { displayName: 'User 0' }; +const DOMAIN_0 = { + domain: 'example.com', + zoneName: 'example.com', + config: { provider: 'manual' } +}; + var AUDIT_SOURCE = { ip: '1.2.3.4' }; @@ -62,6 +69,7 @@ describe('digest', function () { config.set('version', '1.0.0'); config.set('apiServerOrigin', 'http://localhost:4444'); config.set('provider', 'notcaas'); + config.setFqdn(DOMAIN_0.domain); safe.fs.unlinkSync(paths.UPDATE_CHECKER_FILE); async.series([ @@ -124,7 +132,7 @@ describe('digest', function () { digest.maybeSend(function (error) { if (error) return done(error); - checkMails(1, `${USER_0.email}, ${USER_0.username}@${config.fqdn()}`, done); + checkMails(1, `${USER_0.email}, ${USER_0.username}@${DOMAIN_0.domain}`, done); }); }); }); diff --git a/src/test/groups-test.js b/src/test/groups-test.js index 052fc078d..ecb3440c5 100644 --- a/src/test/groups-test.js +++ b/src/test/groups-test.js @@ -7,6 +7,7 @@ 'use strict'; var async = require('async'), + config = require('../config.js'), constants = require('../constants.js'), database = require('../database.js'), DatabaseError = require('../databaseerror.js'), @@ -23,6 +24,12 @@ var GROUP0_NAME = 'administrators', var GROUP1_NAME = 'externs', group1Object; +const DOMAIN_0 = { + domain: 'example.com', + zoneName: 'example.com', + config: { provider: 'manual' } +}; + var USER_0 = { id: 'uuid213', username: 'uuid213', @@ -50,12 +57,13 @@ var USER_1 = { // this user has not signed up yet }; function setup(done) { - // ensure data/config/mount paths - database.initialize(function (error) { - expect(error).to.be(null); + config.setFqdn(DOMAIN_0.domain); - database._clear(done); - }); + // ensure data/config/mount paths + async.series([ + database.initialize, + database._clear + ], done); } function cleanup(done) { @@ -118,7 +126,7 @@ describe('Groups', function () { }); it('did create mailbox', function (done) { - mailboxdb.getGroup(GROUP0_NAME.toLowerCase(), function (error, mailbox) { + mailboxdb.getGroup(GROUP0_NAME.toLowerCase(), DOMAIN_0.domain, function (error, mailbox) { expect(error).to.be(null); expect(mailbox.ownerType).to.be(mailboxdb.TYPE_GROUP); done(); @@ -162,7 +170,7 @@ describe('Groups', function () { }); it('did delete mailbox', function (done) { - mailboxdb.getGroup(GROUP0_NAME.toLowerCase(), function (error) { + mailboxdb.getGroup(GROUP0_NAME.toLowerCase(), DOMAIN_0.domain, function (error) { expect(error.reason).to.be(DatabaseError.NOT_FOUND); done(); }); @@ -241,7 +249,7 @@ describe('Group membership', function () { }); it('can get list members', function (done) { - mailboxdb.getGroup(GROUP0_NAME.toLowerCase(), function (error, result) { + mailboxdb.getGroup(GROUP0_NAME.toLowerCase(), DOMAIN_0.domain, function (error, result) { expect(error).to.be(null); expect(result.name).to.be(GROUP0_NAME.toLowerCase()); expect(result.ownerType).to.be(mailboxdb.TYPE_GROUP); diff --git a/src/test/updatechecker-test.js b/src/test/updatechecker-test.js index 5c5ff2814..25969c1cb 100644 --- a/src/test/updatechecker-test.js +++ b/src/test/updatechecker-test.js @@ -28,6 +28,12 @@ var USER_0 = { displayName: 'User 0' }; +const DOMAIN_0 = { + domain: 'example.com', + zoneName: 'example.com', + config: { provider: 'manual' } +}; + var AUDIT_SOURCE = { ip: '1.2.3.4' }; @@ -54,6 +60,7 @@ function cleanup(done) { describe('updatechecker - box - manual (email)', function () { before(function (done) { config._reset(); + config.setFqdn(DOMAIN_0.domain); config.set('version', '1.0.0'); config.set('apiServerOrigin', 'http://localhost:4444'); config.set('provider', 'notcaas'); @@ -157,9 +164,11 @@ describe('updatechecker - box - manual (email)', function () { describe('updatechecker - box - automatic (no email)', function () { before(function (done) { + config.setFqdn(DOMAIN_0.domain); config.set('version', '1.0.0'); config.set('apiServerOrigin', 'http://localhost:4444'); config.set('provider', 'notcaas'); + async.series([ database.initialize, settings.initialize, @@ -198,9 +207,11 @@ describe('updatechecker - box - automatic (no email)', function () { describe('updatechecker - box - automatic free (email)', function () { before(function (done) { + config.setFqdn(DOMAIN_0.domain); config.set('version', '1.0.0'); config.set('apiServerOrigin', 'http://localhost:4444'); config.set('provider', 'notcaas'); + async.series([ database.initialize, settings.initialize, @@ -244,6 +255,7 @@ describe('updatechecker - app - manual (email)', function () { installationProgress: null, runState: null, location: 'some-location-0', + domain: DOMAIN_0.domain, manifest: { version: '1.0.0', dockerImage: 'docker/app0', healthCheckPath: '/', httpPort: 80, title: 'app0', tcpPorts: { @@ -262,6 +274,7 @@ describe('updatechecker - app - manual (email)', function () { }; before(function (done) { + config.setFqdn(DOMAIN_0.domain); config.set('version', '1.0.0'); config.set('apiServerOrigin', 'http://localhost:4444'); config.set('provider', 'notcaas'); @@ -271,7 +284,7 @@ describe('updatechecker - app - manual (email)', function () { database._clear, settings.initialize, mailer._clearMailQueue, - appdb.add.bind(null, APP_0.id, APP_0.appStoreId, APP_0.manifest, APP_0.location, APP_0.portBindings, APP_0), + appdb.add.bind(null, APP_0.id, APP_0.appStoreId, APP_0.manifest, APP_0.location, APP_0.domain, APP_0.portBindings, APP_0), user.createOwner.bind(null, USER_0.username, USER_0.password, USER_0.email, USER_0.displayName, AUDIT_SOURCE), settings.setAutoupdatePattern.bind(null, constants.AUTOUPDATE_PATTERN_NEVER), settingsdb.set.bind(null, settings.APPSTORE_CONFIG_KEY, JSON.stringify({ userId: 'uid', cloudronId: 'cid', token: 'token' })) @@ -356,6 +369,7 @@ describe('updatechecker - app - automatic (no email)', function () { installationProgress: null, runState: null, location: 'some-location-0', + domain: DOMAIN_0.domain, manifest: { version: '1.0.0', dockerImage: 'docker/app0', healthCheckPath: '/', httpPort: 80, title: 'app0', tcpPorts: { @@ -374,6 +388,7 @@ describe('updatechecker - app - automatic (no email)', function () { }; before(function (done) { + config.setFqdn(DOMAIN_0.domain); config.set('version', '1.0.0'); config.set('apiServerOrigin', 'http://localhost:4444'); config.set('provider', 'notcaas'); @@ -383,7 +398,7 @@ describe('updatechecker - app - automatic (no email)', function () { database._clear, settings.initialize, mailer._clearMailQueue, - appdb.add.bind(null, APP_0.id, APP_0.appStoreId, APP_0.manifest, APP_0.location, APP_0.portBindings, APP_0), + appdb.add.bind(null, APP_0.id, APP_0.appStoreId, APP_0.manifest, APP_0.location, APP_0.domain, APP_0.portBindings, APP_0), user.createOwner.bind(null, USER_0.username, USER_0.password, USER_0.email, USER_0.displayName, AUDIT_SOURCE), settingsdb.set.bind(null, settings.APPSTORE_CONFIG_KEY, JSON.stringify({ userId: 'uid', cloudronId: 'cid', token: 'token' })) ], done); @@ -417,6 +432,7 @@ describe('updatechecker - app - automatic free (email)', function () { installationProgress: null, runState: null, location: 'some-location-0', + domain: DOMAIN_0.domain, manifest: { version: '1.0.0', dockerImage: 'docker/app0', healthCheckPath: '/', httpPort: 80, title: 'app0', tcpPorts: { @@ -435,6 +451,7 @@ describe('updatechecker - app - automatic free (email)', function () { }; before(function (done) { + config.setFqdn(DOMAIN_0.domain); config.set('version', '1.0.0'); config.set('apiServerOrigin', 'http://localhost:4444'); config.set('provider', 'notcaas'); @@ -444,7 +461,7 @@ describe('updatechecker - app - automatic free (email)', function () { database._clear, settings.initialize, mailer._clearMailQueue, - appdb.add.bind(null, APP_0.id, APP_0.appStoreId, APP_0.manifest, APP_0.location, APP_0.portBindings, APP_0), + appdb.add.bind(null, APP_0.id, APP_0.appStoreId, APP_0.manifest, APP_0.location, APP_0.domain, APP_0.portBindings, APP_0), user.createOwner.bind(null, USER_0.username, USER_0.password, USER_0.email, USER_0.displayName, AUDIT_SOURCE), settingsdb.set.bind(null, settings.APPSTORE_CONFIG_KEY, JSON.stringify({ userId: 'uid', cloudronId: 'cid', token: 'token' })) ], done); diff --git a/src/test/user-test.js b/src/test/user-test.js index 37f6643d6..b9df2d306 100644 --- a/src/test/user-test.js +++ b/src/test/user-test.js @@ -39,6 +39,12 @@ var EMAIL_1 = 'second@user.com'; var PASSWORD_1 = 'Sup2345$@strong'; var DISPLAY_NAME_1 = 'Second User'; +const DOMAIN_0 = { + domain: 'example.com', + zoneName: 'example.com', + config: { provider: 'manual' } +}; + function cleanupUsers(done) { async.series([ groupdb._clear, @@ -62,9 +68,12 @@ function createOwner(done) { } function setup(done) { + config.setFqdn(DOMAIN_0.domain); + async.series([ database.initialize, database._clear, + mailer.start, mailer._clearMailQueue ], done); } @@ -205,7 +214,7 @@ describe('User', function () { }); it('did create mailbox', function (done) { - mailboxdb.getMailbox(USERNAME.toLowerCase(), function (error, mailbox) { + mailboxdb.getMailbox(USERNAME.toLowerCase(), DOMAIN_0.domain, function (error, mailbox) { expect(error).to.be(null); expect(mailbox.ownerType).to.be(mailboxdb.TYPE_USER); done(); @@ -712,10 +721,10 @@ describe('User', function () { }); it('updated the mailbox', function (done) { - mailboxdb.getMailbox(USERNAME, function (error) { + mailboxdb.getMailbox(USERNAME, DOMAIN_0.domain, function (error) { expect(error.reason).to.be(DatabaseError.NOT_FOUND); - mailboxdb.getMailbox(USERNAME_NEW.toLowerCase(), function (error, mailbox) { + mailboxdb.getMailbox(USERNAME_NEW.toLowerCase(), DOMAIN_0.domain, function (error, mailbox) { expect(error).to.be(null); expect(mailbox.ownerId).to.be(userObject.id); done(); @@ -998,7 +1007,7 @@ describe('User', function () { user.setAliases(userObject.id, [ 'everything', 'is', 'awesome' ], function (error) { expect(error).to.be(null); - mailboxdb.getAliasesForName(USERNAME.toLowerCase(), function (error, results) { + mailboxdb.getAliasesForName(USERNAME.toLowerCase(), DOMAIN_0.domain, function (error, results) { expect(error).to.be(null); expect(results.length).to.be(3); done(); @@ -1014,10 +1023,10 @@ describe('User', function () { }); it('did delete mailbox and aliases', function (done) { - mailboxdb.getMailbox(userObject.username.toLowerCase(), function (error, mailbox) { + mailboxdb.getMailbox(userObject.username.toLowerCase(), DOMAIN_0.domain, function (error, mailbox) { expect(error.reason).to.be(DatabaseError.NOT_FOUND); - mailboxdb.getAliasesForName(USERNAME.toLowerCase(), function (error, results) { + mailboxdb.getAliasesForName(USERNAME.toLowerCase(), DOMAIN_0.domain, function (error, results) { expect(error).to.be(null); expect(results.length).to.be(0); done(); diff --git a/src/userdb.js b/src/userdb.js index 7e0ad714e..19d1702de 100644 --- a/src/userdb.js +++ b/src/userdb.js @@ -19,6 +19,7 @@ exports = module.exports = { var assert = require('assert'), constants = require('./constants.js'), + config = require('./config.js'), database = require('./database.js'), debug = require('debug')('box:userdb'), DatabaseError = require('./databaseerror'), @@ -145,8 +146,8 @@ function add(userId, user, callback) { }); if (user.username) { queries.push({ - query: 'INSERT INTO mailboxes (name, ownerId, ownerType) VALUES (?, ?, ?)', - args: [ user.username, userId, mailboxdb.TYPE_USER ] + query: 'INSERT INTO mailboxes (name, domain, ownerId, ownerType) VALUES (?, ?, ?, ?)', + args: [ user.username, config.fqdn(), userId, mailboxdb.TYPE_USER ] }); } @@ -239,8 +240,8 @@ function update(userId, user, callback) { queries.push({ query: 'DELETE FROM mailboxes WHERE ownerId = ? AND aliasTarget IS NULL', args: [ userId ] }); // add new mailbox queries.push({ - query: 'INSERT INTO mailboxes (name, ownerId, ownerType) VALUES (?, ?, ?)', - args: [ user.username, userId, mailboxdb.TYPE_USER ] + query: 'INSERT INTO mailboxes (name, domain, ownerId, ownerType) VALUES (?, ?, ?, ?)', + args: [ user.username, config.fqdn(), userId, mailboxdb.TYPE_USER ] }); }