cloudron-e2e-test/cloudron.js

810 lines
31 KiB
JavaScript
Raw Normal View History

2015-06-14 04:33:42 +00:00
#!/usr/bin/env node
'use strict';
var assert = require('assert'),
2017-03-30 23:31:22 +00:00
async = require('async'),
2015-09-16 19:31:00 +00:00
common = require('./common.js'),
2015-06-14 04:33:42 +00:00
debug = require('debug')('e2e:cloudron'),
dns = require('dns'),
2016-06-24 03:06:36 +00:00
ImapProbe = require('./imap-probe.js'),
2015-06-14 04:33:42 +00:00
querystring = require('querystring'),
2017-03-31 03:03:07 +00:00
net = require('net'),
2016-06-24 05:03:26 +00:00
nodemailer = require('nodemailer'),
2016-05-19 00:46:42 +00:00
once = require('once'),
2015-06-14 04:33:42 +00:00
request = require('superagent-sync'),
2016-06-24 05:03:26 +00:00
smtpTransport = require('nodemailer-smtp-transport'),
sleep = require('./shell.js').sleep,
2017-03-31 03:03:07 +00:00
superagent = require('superagent'),
2017-03-31 02:26:29 +00:00
tcpBomb = require('./tcpbomb.js'),
2016-05-19 02:34:12 +00:00
tls = require('tls'),
2016-04-06 01:33:11 +00:00
url = require('url'),
util = require('util');
2015-06-14 04:33:42 +00:00
exports = module.exports = Cloudron;
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
function Cloudron(box) {
this._box = box;
2016-07-08 22:36:54 +00:00
this._setDomain(box.domain);
2015-06-14 04:33:42 +00:00
this._credentials = {
password: null,
accessToken: null
};
}
2016-07-08 22:22:30 +00:00
Cloudron.prototype._setDomain = function (domain) {
this._isCustomDomain = domain === process.env.CUSTOM_DOMAIN || domain === process.env.EC2_SELFHOST_DOMAIN || domain === process.env.DO_SELFHOST_DOMAIN;
2016-07-08 22:22:30 +00:00
this._adminFqdn = this._isCustomDomain ? 'my.' + domain : 'my-' + domain;
this._origin = this._isCustomDomain ? 'https://my.' + domain : 'https://my-' + domain;
};
2016-06-24 05:29:34 +00:00
Cloudron.prototype.fqdn = function () {
return this._box.domain;
};
Cloudron.prototype.adminFqdn = function () {
return 'my' + (this._isCustomDomain ? '.' : '-') + this._box.domain;
};
2015-12-18 04:58:50 +00:00
Cloudron.prototype.appFqdn = function (location) {
return location + (this._isCustomDomain ? '.' : '-') + this._box.domain;
};
2015-06-14 04:33:42 +00:00
// get oauth token for logged in as certain user { username, password, email }
Cloudron.prototype.getOauthToken = function (user) {
var username = user.username;
var password = user.password;
////////// try to authorize without a session
var res = request.get(this._origin + '/api/v1/oauth/dialog/authorize').query({ redirect_uri: 'https://self', client_id: 'cid-webadmin', response_type: 'token', scope: 'root,profile,apps,roleAdmin' }).end();
var sessionCookies = res.headers['set-cookie']; // always an array
///////// should get redirected to login form with a script tag (to workaround chrome issue with redirects+cookies)
var redirectUrl = res.text.match(/window.location.href = "(.*)"/);
if (!redirectUrl) {
2015-07-29 16:16:10 +00:00
debug('Could not determine redirected url', res.text, res.headers);
assert(false);
}
var urlp = url.parse(redirectUrl[1]);
2015-06-14 04:33:42 +00:00
////////// get the login form (api/v1/session/login)
res = request.get(this._origin + urlp.pathname).set('cookie', sessionCookies[0]).query(urlp.query).end();
2016-02-26 05:27:34 +00:00
var csrfs = res.text.match(/name="_csrf" value="(.*)"/);
if (!csrfs) {
debug('Could not determine csrf', res.text, res.headers);
assert(false);
}
var csrf = csrfs[1];
2015-06-14 04:33:42 +00:00
sessionCookies = res.headers['set-cookie']; // always an array
2015-06-15 06:11:07 +00:00
assert.notStrictEqual(sessionCookies.length, 0);
2015-06-14 04:33:42 +00:00
////////// submit the login form with credentials
res = request.post(this._origin + urlp.pathname).set('cookie', sessionCookies[0]).send({ _csrf: csrf, username: username, password: password }).redirects(0).end();
if (res.statusCode !== 302) {
debug('Failed to submit the login for.', res.statusCode, res.text);
assert(false);
}
2015-06-14 04:33:42 +00:00
sessionCookies = res.headers['set-cookie']; // always an array
2015-06-15 06:11:07 +00:00
assert.notStrictEqual(sessionCookies.length, 0);
2015-06-14 04:33:42 +00:00
////////// authorize now with cookies
res = request.get(this._origin + '/api/v1/oauth/dialog/authorize').set('cookie', sessionCookies[0]).query({ redirect_uri: 'https://self', client_id: 'cid-webadmin', response_type: 'token', scope: 'root,profile,apps,roleAdmin' }).redirects(0).end();
2016-01-27 17:21:41 +00:00
common.verifyResponse(res, 'Unable to authorize');
2015-06-15 06:11:07 +00:00
assert.strictEqual(res.statusCode, 302);
2015-06-14 04:33:42 +00:00
sessionCookies = res.headers['set-cookie']; // always an array
2015-06-15 06:11:07 +00:00
assert.notStrictEqual(sessionCookies.length, 0);
2015-06-14 04:33:42 +00:00
////////// success will get redirect to callback?redirectURI=xx#access_token=yy&token_type=Bearer' (content is a <script>)
urlp = url.parse(res.headers.location);
res = request.get(this._origin + urlp.pathname).set('cookie', sessionCookies[0]).query(urlp.query).redirects(0).end();
2015-06-15 06:11:07 +00:00
assert.strictEqual(res.statusCode, 200);
2015-06-14 04:33:42 +00:00
////////// simulate what the the script of callback call does
var accessToken = querystring.parse(urlp.hash.substr(1)).access_token;
return accessToken;
2015-09-29 01:43:51 +00:00
};
2015-06-14 04:33:42 +00:00
2017-02-02 16:08:51 +00:00
// setup dns for selfhosters
Cloudron.prototype.setupDns = function (dnsConfig) {
var res = request.post('https://' + this._box.ip + '/api/v1/cloudron/dns_setup').send(dnsConfig).end();
common.verifyResponse2xx(res, 'Could not setup Cloudron dns');
for (var i = 0; i < 60; ++i) {
sleep(5);
res = request.get(this._origin + '/api/v1/cloudron/status').end();
common.verifyResponse2xx(res, 'Could not get Cloudron status');
if (res.body.adminFqdn) return;
}
};
2015-06-14 04:33:42 +00:00
// activate the box
Cloudron.prototype.activate = function (user) {
var setupToken = this._box.setupToken;
////////// activation
var res;
2016-05-03 20:47:03 +00:00
for (var i = 0; i < 60; ++i) {
sleep(20);
res = request.post(this._origin + '/api/v1/cloudron/activate').query({ setupToken: setupToken }).send(user).end();
if (res.statusCode === 201) break;
if (res.statusCode === 307) continue;
if (res.statusCode === 409) {
debug('Response error statusCode:%s error:%s body:%j', res.statusCode, res.error, res.body);
throw new Error('Cloudron already activated! This should not happen');
2016-09-21 15:29:25 +00:00
} else {
debug('Response error will retry. statusCode:%s error:%s body:%j', res.statusCode, res.error, res.body);
}
}
// final verification will fail if retries do not succeed
common.verifyResponse2xx(res, 'Could not activate the box');
2015-06-14 04:33:42 +00:00
res = request.get(this._origin + '/api/v1/cloudron/status').end();
2016-01-27 17:21:41 +00:00
common.verifyResponse2xx(res, 'Could not get Cloudron status');
2015-06-14 04:33:42 +00:00
assert.strictEqual(res.body.version, this._box.version);
2015-09-29 01:43:51 +00:00
};
2015-06-14 04:33:42 +00:00
2016-06-07 23:09:46 +00:00
Cloudron.prototype.waitForApp = function (appId, version) {
2015-06-14 04:33:42 +00:00
// wait for app to come up
process.stdout.write('Waiting for app to come up.');
2015-08-31 04:48:33 +00:00
var res;
2015-06-20 19:50:28 +00:00
for (var i = 0; i < 60; i++) {
2016-06-18 18:33:03 +00:00
sleep(30);
2015-06-14 04:33:42 +00:00
process.stdout.write('.');
2015-08-31 04:48:33 +00:00
res = request.get(this._origin + '/api/v1/apps/'+ appId).query({ access_token: this._credentials.accessToken }).end();
// if app still redirects, wait a bit
if (res.statusCode === 307) continue;
2016-01-27 17:21:41 +00:00
common.verifyResponse2xx(res, 'Could not query app status');
2015-06-14 04:33:42 +00:00
2015-07-24 05:48:08 +00:00
if (res.body.installationState === 'installed' && res.body.runState === 'running' && res.body.health === 'healthy') {
2015-06-14 04:33:42 +00:00
console.log();
break;
}
}
2015-06-16 05:28:48 +00:00
assert.strictEqual(res.body.installationState, 'installed');
assert.strictEqual(res.body.runState, 'running');
2016-06-07 23:09:46 +00:00
assert.strictEqual(res.body.runState, 'running');
if (version) assert.strictEqual(res.body.manifest.version, version);
2015-12-17 21:58:49 +00:00
return res.body;
2015-06-14 04:33:42 +00:00
};
2017-02-02 16:08:51 +00:00
Cloudron.prototype.waitForBox = function (byIp) {
2015-08-31 04:48:33 +00:00
process.stdout.write('Waiting for box.');
var res;
2016-05-03 20:47:03 +00:00
for (var i = 0; i < 60; i++) {
sleep(20);
2017-02-02 16:08:51 +00:00
res = request.get((byIp ? ('https://' + this._box.ip) : this._origin) + '/api/v1/cloudron/status').end();
2016-01-26 23:04:59 +00:00
if (res.statusCode === 200) {
2015-08-31 04:48:33 +00:00
console.log();
2016-01-26 23:04:59 +00:00
return;
2015-08-31 04:48:33 +00:00
}
process.stdout.write('.');
}
2016-01-26 23:04:59 +00:00
2016-01-27 02:30:02 +00:00
throw new Error('waitForBox failed');
2015-08-31 04:48:33 +00:00
};
2015-06-14 04:33:42 +00:00
Cloudron.prototype.setCredentials = function (password, accessToken) {
this._credentials = {
password: password,
accessToken: accessToken
};
};
2016-06-07 23:04:15 +00:00
Cloudron.prototype.installApp = function (location, manifestOrAppstoreId, portBindings) {
2016-05-05 22:03:17 +00:00
portBindings = portBindings || null; // null binds nothing
2016-06-07 23:09:46 +00:00
var version = typeof manifestOrAppstoreId === 'object' ? manifestOrAppstoreId.version : manifestOrAppstoreId.split('@')[1];
2016-06-07 23:04:15 +00:00
var data = {
manifest: typeof manifestOrAppstoreId === 'object' ? manifestOrAppstoreId : null,
appStoreId: typeof manifestOrAppstoreId === 'string' ? manifestOrAppstoreId : '',
2016-06-07 23:04:15 +00:00
location: location,
accessRestriction: null,
oauthProxy: false,
portBindings: portBindings
};
2015-06-14 04:33:42 +00:00
var res = request.post(this._origin + '/api/v1/apps/install')
.query({ access_token: this._credentials.accessToken })
2016-06-07 23:04:15 +00:00
.send(data)
2015-06-14 04:33:42 +00:00
.end();
2016-01-27 17:21:41 +00:00
common.verifyResponse2xx(res, 'Cannot install app');
2015-06-14 04:33:42 +00:00
debug('App installed at %s'.green, location);
var appId = res.body.id;
2016-06-07 23:09:46 +00:00
var app = this.waitForApp(appId, version);
2015-06-14 04:33:42 +00:00
debug('App is running'.green);
2015-12-17 21:58:49 +00:00
res = request.get('https://' + app.fqdn).end();
2016-01-27 17:21:41 +00:00
common.verifyResponse2xx(res, 'App is unreachable');
2015-06-14 04:33:42 +00:00
console.log('App is reachable'.green);
return appId;
};
2016-05-05 23:53:39 +00:00
Cloudron.prototype.configureApp = function (appId, newLocation, altDomain /* optional */, portBindings) {
portBindings = portBindings || null;
var data = { location: newLocation, accessRestriction: null, oauthProxy: false, password: this._credentials.password, portBindings: portBindings };
2016-04-26 17:07:47 +00:00
if (altDomain) data.altDomain = altDomain;
var res = request.post(this._origin + '/api/v1/apps/' + appId + '/configure').query({ access_token: this._credentials.accessToken }).send(data).end();
2016-01-27 17:21:41 +00:00
common.verifyResponse2xx(res, 'App could not be configured');
2015-06-14 04:33:42 +00:00
console.log('App moved to different location'.green);
2015-12-17 21:58:49 +00:00
var app = this.waitForApp(appId);
2015-06-14 04:33:42 +00:00
2015-12-17 21:58:49 +00:00
res = request.get('https://' + app.fqdn).end();
2016-01-27 17:21:41 +00:00
common.verifyResponse2xx(res, 'App is unreachable');
2015-06-14 04:33:42 +00:00
console.log('App is reachable'.green);
};
2016-10-03 20:38:55 +00:00
Cloudron.prototype.cloneApp = function (appId, newLocation, portBindings) {
portBindings = portBindings || null;
2016-10-03 21:24:57 +00:00
var backups = this.listAppBackups(appId);
2016-10-03 21:31:24 +00:00
console.log('Backups are ', backups);
2016-10-03 21:24:57 +00:00
var data = { location: newLocation, password: this._credentials.password, portBindings: portBindings, backupId: backups[0].id };
2016-10-03 20:38:55 +00:00
var res = request.post(this._origin + '/api/v1/apps/' + appId + '/clone').query({ access_token: this._credentials.accessToken }).send(data).end();
common.verifyResponse2xx(res, 'App could not be clone');
console.log('App cloned to different location'.green);
2016-10-03 20:40:14 +00:00
var app = this.waitForApp(res.body.id);
2016-10-03 20:38:55 +00:00
res = request.get('https://' + app.fqdn).end();
common.verifyResponse2xx(res, 'App is unreachable');
console.log('App is reachable'.green);
};
2015-06-14 04:33:42 +00:00
Cloudron.prototype.uninstallApp = function (appId) {
process.stdout.write('Uninstalling app');
var res = request.post(this._origin + '/api/v1/apps/' + appId + '/uninstall').query({ access_token: this._credentials.accessToken }).send({ password: this._credentials.password }).end();
2016-01-27 17:21:41 +00:00
common.verifyResponse2xx(res, 'Cannot uninstall app');
2015-06-14 04:33:42 +00:00
2016-05-03 20:47:03 +00:00
for (var i = 0; i < 60; i++) {
sleep(20);
2015-06-14 04:33:42 +00:00
process.stdout.write('.');
res = request.get(this._origin + '/api/v1/apps/'+ appId).query({ access_token: this._credentials.accessToken }).retry(0).end();
if (res.statusCode === 404) {
console.log();
2016-01-26 23:04:59 +00:00
debug('App is uninstalled'.green);
return;
2015-06-14 04:33:42 +00:00
}
}
2016-01-26 23:04:59 +00:00
assert(false, 'uninstallApp failed');
};
2015-06-14 04:33:42 +00:00
Cloudron.prototype.updateApp = function (appId, manifestOrAppstoreId) {
2016-04-16 02:28:33 +00:00
process.stdout.write('Trying to update');
2016-06-07 23:09:46 +00:00
var version = typeof manifestOrAppstoreId === 'object' ? manifestOrAppstoreId.version : manifestOrAppstoreId.split('@')[1];
var data = {
password: this._credentials.password,
manifest: typeof manifestOrAppstoreId === 'object' ? manifestOrAppstoreId : null,
2016-06-08 01:52:04 +00:00
appStoreId: typeof manifestOrAppstoreId === 'string' ? manifestOrAppstoreId : ''
};
var res = request.post(this._origin + '/api/v1/apps/' + appId + '/update')
.query({ access_token: this._credentials.accessToken })
.send(data)
.end();
2016-04-16 02:28:33 +00:00
common.verifyResponse2xx(res, 'Could not update');
console.log('Update started'.green);
2016-06-07 23:09:46 +00:00
var app = this.waitForApp(appId, version);
2016-04-16 02:28:33 +00:00
debug('App is running'.green);
res = request.get('https://' + app.fqdn).end();
common.verifyResponse2xx(res, 'App is unreachable');
2016-06-14 06:40:08 +00:00
console.log('App updated'.green);
};
Cloudron.prototype.restoreApp = function (appId, backup) {
2016-06-14 19:31:48 +00:00
process.stdout.write('Trying to restore to ' + JSON.stringify(backup));
2016-06-14 06:40:08 +00:00
var data = {
2016-06-14 17:11:38 +00:00
backupId: backup.id,
password: this._credentials.password
2016-06-14 06:40:08 +00:00
};
var res = request.post(this._origin + '/api/v1/apps/' + appId + '/restore')
.query({ access_token: this._credentials.accessToken })
.send(data)
.end();
common.verifyResponse2xx(res, 'Could not restore');
console.log('Restore started'.green);
var app = this.waitForApp(appId, backup.version);
debug('App is running'.green);
res = request.get('https://' + app.fqdn).end();
common.verifyResponse2xx(res, 'App is unreachable');
console.log('App restored'.green);
};
Cloudron.prototype.listAppBackups = function (appId) {
var res = request.get(this._origin + '/api/v1/apps/' + appId + '/backups').query({ access_token: this._credentials.accessToken }).end();
2016-06-14 06:40:08 +00:00
common.verifyResponse2xx(res, 'Could not list backups');
return res.body.backups;
2016-04-16 02:28:33 +00:00
};
2015-06-14 04:33:42 +00:00
Cloudron.prototype.update = function (toVersion) {
2015-07-16 22:05:06 +00:00
process.stdout.write('Trying to update');
2015-08-31 04:48:33 +00:00
var res;
2016-05-03 20:47:03 +00:00
for (var i = 0; i < 60; i++) {
sleep(20);
2015-07-16 22:05:06 +00:00
process.stdout.write('.');
2015-08-31 04:48:33 +00:00
res = request.post(this._origin + '/api/v1/cloudron/update').query({ access_token: this._credentials.accessToken }).send({ password: this._credentials.password }).end();
2015-07-16 22:05:06 +00:00
if (res.statusCode === 422) continue; // box has not seen the update yet
if (res.statusCode === 409) break; // update is in progress, lock was acquired
2016-01-27 17:21:41 +00:00
common.verifyResponse2xx(res, 'Could not update');
2015-07-16 22:05:06 +00:00
break;
}
2015-06-14 04:33:42 +00:00
2015-07-16 22:05:06 +00:00
console.log('Update started'.green);
2016-06-07 21:35:30 +00:00
this.waitForUpdate(toVersion);
};
Cloudron.prototype.waitForUpdate = function (toVersion) {
2015-06-14 04:33:42 +00:00
process.stdout.write('Waiting for update.');
2016-06-07 21:35:30 +00:00
var res;
for (var i = 0; i < 60; i++) {
2016-06-18 18:33:03 +00:00
sleep(30);
2015-06-14 04:33:42 +00:00
res = request.get(this._origin + '/api/v1/cloudron/status').end();
if (res.statusCode === 200 && res.body.version === toVersion) {
console.log();
break;
}
process.stdout.write('.');
}
assert.strictEqual(res.body.version, toVersion);
assert.strictEqual(res.body.activated, true);
console.log('Updated successfully'.green);
2015-08-31 04:48:33 +00:00
};
2015-06-14 04:33:42 +00:00
Cloudron.prototype.addUser = function (username, email) {
var res = request.post(this._origin + '/api/v1/users').query({ access_token: this._credentials.accessToken }).send({ username: username, email: email, invite: true }).end();
2016-01-27 17:21:41 +00:00
common.verifyResponse2xx(res, 'Could not add user');
2016-04-06 19:27:11 +00:00
return res.body;
};
2016-09-29 04:02:38 +00:00
Cloudron.prototype.setAliases = function (aliases) {
var res = request.get(this._origin + '/api/v1/profile').query({ access_token: this._credentials.accessToken }).end();
common.verifyResponse2xx(res, 'Could not get profile');
console.log('user id is ', res.body.id);
res = request.put(this._origin + '/api/v1/users/' + res.body.id + '/aliases')
2016-06-24 15:39:34 +00:00
.query({ access_token: this._credentials.accessToken })
.send({ aliases: aliases })
.end();
common.verifyResponse2xx(res, 'Could not set aliases');
return res.body;
};
Cloudron.prototype.resetPassword = function (resetToken, password) {
var res = request.get(this._origin + '/api/v1/session/password/reset.html').query({ reset_token: resetToken }).end();
2016-01-27 17:21:41 +00:00
common.verifyResponse2xx(res, 'Could not get password setup site');
2015-06-15 06:11:07 +00:00
var sessionCookies = res.headers['set-cookie']; // always an array
var csrf = res.text.match(/name="_csrf" value="(.*)"/)[1];
2015-06-15 06:11:07 +00:00
res = request.post(this._origin + '/api/v1/session/password/reset')
.set('cookie', sessionCookies[0])
.type('form').send({ _csrf: csrf, resetToken: resetToken, password: password, passwordRepeat: password }).end();
2016-01-27 17:44:45 +00:00
common.verifyResponse(res, 'Could not setup password for user');
assert.strictEqual(res.statusCode, 302);
};
2016-10-03 21:30:47 +00:00
Cloudron.prototype.backupApp = function (appId) {
var res = request.post(this._origin + '/api/v1/apps/' + appId + '/backup').query({ access_token: this._credentials.accessToken }).end();
2016-10-03 21:31:24 +00:00
common.verifyResponse2xx(res, 'Could not schedule a backup');
2016-10-03 21:30:47 +00:00
this.waitForApp(appId);
};
2015-06-16 19:00:46 +00:00
Cloudron.prototype.backup = function () {
var res = request.get(this._origin + '/api/v1/backups').query({ access_token: this._credentials.accessToken }).end();
2016-01-27 17:21:41 +00:00
common.verifyResponse2xx(res, 'Could not get backups');
2015-06-16 19:00:46 +00:00
var existingBackups = res.body.backups;
2015-06-17 15:06:01 +00:00
res = request.post(this._origin + '/api/v1/backups').query({ access_token: this._credentials.accessToken }).end();
2016-01-27 17:21:41 +00:00
common.verifyResponse2xx(res, 'Could not schedule backup');
2015-06-16 19:00:46 +00:00
2016-05-03 20:47:03 +00:00
for (var i = 0; i < 60; i++) {
sleep(20); // backup sometimes takes a while to start
2015-06-16 19:00:46 +00:00
res = request.get(this._origin + '/api/v1/cloudron/progress').end();
if (res.body.backup === null || res.body.backup.percent === 100) {
debug('backup done');
break;
}
debug('Backing up: %s %s', res.body.backup.percent, res.body.backup.message);
}
2016-05-03 20:47:03 +00:00
if (res.body.backup !== null && res.body.backup.percent !== 100) throw new Error('backup: timedout');
2016-01-27 02:30:02 +00:00
2015-06-16 19:00:46 +00:00
res = request.get(this._origin + '/api/v1/backups').query({ access_token: this._credentials.accessToken }).end();
2016-01-27 17:21:41 +00:00
common.verifyResponse2xx(res, 'Could not get backups');
2015-06-16 19:00:46 +00:00
var latestBackups = res.body.backups;
assert.strictEqual(latestBackups.length, existingBackups.length + 1);
2016-04-05 00:02:21 +00:00
return latestBackups[0]; // { creationTime, boxVersion, id, dependsOn }
2015-06-16 19:00:46 +00:00
};
2015-08-31 04:48:33 +00:00
Cloudron.prototype.reboot = function () {
2015-08-31 04:49:09 +00:00
var res = request.post(this._origin + '/api/v1/cloudron/reboot').query({ access_token: this._credentials.accessToken }).send({ }).end();
2016-01-27 17:21:41 +00:00
common.verifyResponse2xx(res, 'Box could not be rebooted');
2015-08-31 04:48:33 +00:00
this.waitForBox();
};
2016-07-08 22:22:30 +00:00
Cloudron.prototype.migrate = function (options) {
2016-07-08 22:36:54 +00:00
options.password = this._credentials.password;
2016-07-08 22:22:30 +00:00
var res = request.post(this._origin + '/api/v1/cloudron/migrate')
.query({ access_token: this._credentials.accessToken })
.send(options)
.end();
common.verifyResponse2xx(res, 'Box could not be migrated');
if (options.domain) this._setDomain(options.domain);
};
2016-09-01 15:59:16 +00:00
Cloudron.prototype.setEmailEnabled = function (enabled) {
var res = request
.post(this._origin + '/api/v1/settings/mail_config').query({ access_token: this._credentials.accessToken })
.send({ enabled: enabled })
.end();
common.verifyResponse2xx(res, 'Could not enable email');
2016-09-02 03:55:07 +00:00
sleep(60); // generously wait for the mail server to restart. it takes 10 seconds to stop it... and then there is DNS propagation
2016-09-01 15:59:16 +00:00
};
2016-10-14 02:48:48 +00:00
Cloudron.prototype.setCloudronName = function (name) {
var res = request
.post(this._origin + '/api/v1/settings/cloudron_name').query({ access_token: this._credentials.accessToken })
.send({ name: name})
.end();
common.verifyResponse2xx(res, 'Could not enable email');
};
2016-05-03 19:23:34 +00:00
Cloudron.prototype.checkTimeZone = function (tz) {
var res = request.get(this._origin + '/api/v1/settings/time_zone').query({ access_token: this._credentials.accessToken }).end();
common.verifyResponse2xx(res, 'Could not query timezone');
2016-05-30 16:33:39 +00:00
if (tz !== res.body.timeZone) throw new Error('timezone does not match. expecting: ' + tz + ' got ' + res.body.timeZone);
2016-05-03 19:23:34 +00:00
};
2016-06-07 21:35:30 +00:00
Cloudron.prototype.setAutoupdatePattern = function (pattern) {
var res = request.post(this._origin + '/api/v1/settings/autoupdate_pattern')
.query({ access_token: this._credentials.accessToken })
.send({ pattern: pattern })
.end();
common.verifyResponse2xx(res, 'Could not set autoupdate pattern');
};
2015-09-30 00:32:21 +00:00
Cloudron.prototype.checkA = function (callback) {
2016-09-20 09:09:14 +00:00
var expectedIp = this._box.ip;
2016-09-09 09:58:39 +00:00
2015-09-30 00:32:21 +00:00
dns.resolve4(this._box.domain, function (error, records) {
if (error) return callback(error);
if (records.length !== 1) return callback(new Error('Got ' + JSON.stringify(records) + ' A records. Expecting 1 length array'));
2016-09-09 09:58:39 +00:00
if (records[0] !== expectedIp) return callback(new Error('Bad A record. ' + records[0] + '. Expecting ' + expectedIp));
2015-09-30 00:32:21 +00:00
2015-09-30 04:39:11 +00:00
callback(null, records);
2015-09-30 00:32:21 +00:00
});
};
2016-04-06 01:43:30 +00:00
Cloudron.prototype._checkGraphs = function (targets, from) {
2016-04-06 01:33:11 +00:00
var params = {
2016-04-06 05:17:40 +00:00
target: targets.length === 1 ? targets[0] : targets,
2016-04-06 01:33:11 +00:00
format: 'json',
2016-04-06 01:43:30 +00:00
from: from,
2016-04-06 01:33:11 +00:00
access_token: this._credentials.accessToken
};
var res;
2016-05-03 20:47:03 +00:00
for (var i = 0; i < 60; i++) {
sleep(20);
2016-04-06 01:33:11 +00:00
res = request.get(this._origin + '/api/v1/cloudron/graphs').query(params).end();
process.stdout.write('.');
2016-04-06 01:43:30 +00:00
if (res.statusCode !== 200) continue;
2016-04-06 04:39:40 +00:00
if (!util.isArray(res.body) || res.body.length !== targets.length) continue;
2016-04-06 01:43:30 +00:00
for (var j = 0; j < res.body.length; j++) {
if (res.body[j].datapoints.length === 0) break; // no data
console.log();
console.log(res.body);
return; // success
}
2016-04-06 01:33:11 +00:00
}
assert(false, 'Graphs are not populated');
};
2016-04-06 01:43:30 +00:00
Cloudron.prototype.checkAppGraphs = function (appId) {
var timePeriod = 2 * 60; // in minutes
var timeBucketSize = 30; // in minutes
var target = 'summarize(collectd.localhost.table-' + appId + '-memory.gauge-rss, "' + timeBucketSize + 'min", "avg")';
this._checkGraphs([target], '-' + timePeriod + 'min');
};
Cloudron.prototype.checkDiskGraphs = function () {
var targets = [
'averageSeries(collectd.localhost.df-loop*.df_complex-free)',
'averageSeries(collectd.localhost.df-loop*.df_complex-reserved)',
'averageSeries(collectd.localhost.df-loop*.df_complex-used)'
];
this._checkGraphs(targets, '-1min');
};
Cloudron.prototype.checkSPF = function (callback) {
2015-09-29 03:36:11 +00:00
var that = this;
dns.resolveTxt(this._box.domain, function (error, records) {
if (error) return callback(error);
2015-09-29 03:36:11 +00:00
if (records.length !== 1 || records[0].length !== 1) return callback(new Error('Got ' + JSON.stringify(records) + ' TXT records. Expecting 1 length 2d array'));
2016-03-28 18:25:15 +00:00
if (records[0][0].search(new RegExp('^v=spf1 a:' + that._adminFqdn + ' ~all$')) !== 0) return callback(new Error('Bad SPF record. ' + records[0][0]));
2015-09-30 04:39:11 +00:00
callback(null, records);
});
};
2016-05-18 06:31:50 +00:00
Cloudron.prototype.checkMX = function (callback) {
var that = this;
dns.resolveMx(this._box.domain, function (error, records) {
if (error) return callback(error);
if (records.length !== 1) return callback(new Error('Got ' + JSON.stringify(records) + ' MX records. Expecting 1 length array'));
2016-05-18 08:36:58 +00:00
if (records[0].exchange !== that._adminFqdn) return callback(new Error('Bad MX record. ' + JSON.stringify(records[0])));
2016-05-18 06:31:50 +00:00
callback(null, records);
});
};
Cloudron.prototype.checkDKIM = function (callback) {
2015-10-31 04:52:00 +00:00
dns.resolveTxt('cloudron._domainkey.' + this._box.domain, function (error, records) {
if (error) return callback(error);
2015-09-29 03:36:11 +00:00
if (records.length !== 1 || records[0].length !== 1) return callback(new Error('Got ' + JSON.stringify(records) + ' TXT records. Expecting 1 length 2d array'));
// node removes the quotes or maybe this is why a 2d-array?
2015-09-29 04:10:39 +00:00
if (records[0][0].search(/^v=DKIM1; t=s; p=.*$/) !== 0) return callback(new Error('Bad DKIM record. ' + records[0][0]));
2015-09-30 04:39:11 +00:00
callback(null, records);
});
};
Cloudron.prototype.checkDMARC = function (callback) {
dns.resolveTxt('_dmarc.' + this._box.domain, function (error, records) {
if (error) return callback(error);
2015-09-29 03:36:11 +00:00
if (records.length !== 1 || records[0].length !== 1) return callback(new Error('Got ' + JSON.stringify(records) + ' TXT records. Expecting 1 length 2d array'));
// node removes the quotes or maybe this is why a 2d-array?
2016-09-20 16:06:40 +00:00
if (records[0][0].search(/^v=DMARC1; p=reject; pct=100$/) !== 0) return callback(new Error('Bad DMARC record. ' + records[0][0]));
2015-09-30 04:39:11 +00:00
callback(null, records);
});
};
Cloudron.prototype.populateAddons = function (domain) {
var res = request.post('https://' + domain + '/populate_addons').end();
2016-04-26 16:44:45 +00:00
assert.strictEqual(res.statusCode, 200);
for (var addon in res.body) {
assert.strictEqual(res.body[addon], 'OK');
}
};
Cloudron.prototype.checkAddons = function (domain, owner) {
2015-10-21 00:53:57 +00:00
var lastError;
// try many times because the scheduler takes sometime to run
for (var i = 0; i < 100; i++) {
var res = request.post('https://' + domain + '/check_addons').query({ username: owner.username, password: owner.password }).end();
2015-10-21 00:53:57 +00:00
try {
assert.strictEqual(res.statusCode, 200);
for (var addon in res.body) {
assert.strictEqual(res.body[addon], 'OK');
}
2015-10-21 00:53:57 +00:00
return;
} catch (e) {
lastError = e;
console.error(e);
console.log('Attempt %s failed. Trying again in 10 seconds', i);
sleep(10);
}
}
throw lastError;
};
2015-10-31 04:12:41 +00:00
2017-03-30 23:31:22 +00:00
Cloudron.prototype.checkAppRateLimit = function (domain) {
var res = request.post('https://' + domain + '/ratelimit').end();
assert.strictEqual(res.statusCode, 200);
for (var addon in res.body) {
assert.strictEqual(res.body[addon], 'OK');
}
};
2017-01-13 03:18:12 +00:00
Cloudron.prototype.checkDnsbl = function (domain) {
var res = request.post('https://' + domain + '/check_dnsbl').end();
common.verifyResponse2xx(res, 'Could not query dnsbl');
assert.strictEqual(res.body.status, 'OK');
};
2015-10-31 04:12:41 +00:00
Cloudron.prototype.setDnsConfig = function (dnsConfig) {
var res = request.post(this._origin + '/api/v1/settings/dns_config').query({ access_token: this._credentials.accessToken }).send(dnsConfig).end();
2016-01-27 17:21:41 +00:00
common.verifyResponse2xx(res, 'Could not set dns config');
2015-10-31 04:12:41 +00:00
};
2016-05-05 22:14:46 +00:00
Cloudron.prototype.setBackupConfig = function (backupConfig) {
var res = request.post(this._origin + '/api/v1/settings/backup_config').query({ access_token: this._credentials.accessToken }).send(backupConfig).end();
common.verifyResponse2xx(res, 'Could not set backup config');
};
2016-05-19 02:34:12 +00:00
Cloudron.prototype.saveSieveScript = function (owner, callback) {
var authString = 'AUTHENTICATE "PLAIN" "' + new Buffer('\0' + owner.username + '\0' + owner.password).toString('base64') + '"';
2016-05-19 05:03:38 +00:00
var data = '';
2016-05-19 02:34:12 +00:00
callback = once(callback);
var socket = tls.connect(4190, this._adminFqdn, { rejectUnauthorized: false }, function (error) {
if (error) return callback(error);
socket.write(authString + '\n');
2016-05-19 06:46:22 +00:00
socket.write('PUTSCRIPT "hutsefluts" {6+}\nkeep;\n\nLOGOUT\n');
2016-05-19 02:34:12 +00:00
2016-05-19 05:03:38 +00:00
setTimeout(function () {
socket.end();
2016-05-19 06:12:52 +00:00
callback(new Error('Could not auth with sieve with ' + authString + '.\n' + data));
2016-05-19 05:03:38 +00:00
}, 5000); // give it 5 seconds
2016-05-19 02:34:12 +00:00
});
socket.on('data', function (chunk) {
2016-05-19 05:03:38 +00:00
data += chunk.toString('utf8');
2016-05-19 06:46:22 +00:00
if (data.toString('utf8').indexOf('OK "PUTSCRIPT completed."') !== -1) return callback();
2016-05-19 02:34:12 +00:00
});
socket.on('end', function () { });
socket.on('error', callback);
};
2016-05-19 03:28:23 +00:00
Cloudron.prototype.checkSieveScript = function (owner, callback) {
2016-05-19 02:34:12 +00:00
var authString = 'AUTHENTICATE "PLAIN" "' + new Buffer('\0' + owner.username + '\0' + owner.password).toString('base64') + '"';
2016-05-19 05:03:38 +00:00
var data = '';
2016-05-19 02:34:12 +00:00
callback = once(callback);
var socket = tls.connect(4190, this._adminFqdn, { rejectUnauthorized: false }, function (error) {
if (error) return callback(error);
socket.write(authString + '\n');
2016-05-19 06:46:22 +00:00
socket.write('LISTSCRIPTS\nLOGOUT\n');
2016-05-19 02:34:12 +00:00
2016-05-19 05:03:38 +00:00
setTimeout(function () {
socket.end();
2016-05-19 06:12:52 +00:00
callback(new Error('Could not auth with sieve with ' + authString + '.\n' + data));
2016-05-19 05:03:38 +00:00
}, 5000); // give it 5 seconds
2016-05-19 02:34:12 +00:00
});
socket.on('data', function (chunk) {
2016-05-19 05:03:38 +00:00
data += chunk.toString('utf8');
if (data.toString('utf8').indexOf('"hutsefluts"') !== -1) return callback();
2016-05-19 02:34:12 +00:00
});
socket.on('end', function () { });
socket.on('error', callback);
};
2016-06-08 03:31:02 +00:00
Cloudron.prototype.checkForUpdates = function () {
request.post(this._origin + '/api/v1/cloudron/check_for_updates')
.query({ access_token: this._credentials.accessToken })
.end();
};
2016-06-24 05:03:26 +00:00
2016-06-24 15:39:34 +00:00
Cloudron.prototype.sendMail = function (account, to, callback) {
2016-06-24 05:03:26 +00:00
var transport = nodemailer.createTransport(smtpTransport({
host: this._adminFqdn,
port: 587,
auth: {
2016-06-24 15:39:34 +00:00
user: account.username,
pass: account.password
2016-06-24 05:03:26 +00:00
}
}));
var mailOptions = {
2016-06-24 15:39:34 +00:00
from: (account.from || account.username) + '@'+ this._box.domain,
2016-06-24 05:03:26 +00:00
to: to,
2016-06-24 05:29:34 +00:00
subject: 'Hi from e2e test - ' + this._box.domain,
2016-06-24 16:46:08 +00:00
text: 'This release depends on you' // keep in sync with mailer.sendMailToCloudronUser
2016-06-24 05:03:26 +00:00
};
2016-06-24 16:02:48 +00:00
debug('Sending mail with options %j', mailOptions);
2016-06-24 05:03:26 +00:00
transport.sendMail(mailOptions, callback);
};
2016-06-24 05:29:34 +00:00
2016-06-24 15:39:34 +00:00
Cloudron.prototype.checkMail = function (account, callback) {
2016-06-24 05:29:34 +00:00
var imap = new ImapProbe({
2016-06-24 15:39:34 +00:00
user: account.username,
password: account.password,
2016-06-24 05:29:34 +00:00
host: this._adminFqdn,
port: 993, // imap port
2016-06-24 05:29:34 +00:00
tls: true,
tlsOptions: { rejectUnauthorized: false },
readOnly: true
});
imap.probe({
2016-06-24 18:01:16 +00:00
subject: common.regexp('Hi from e2e test - ' + this._box.domain),
to: common.regexp((account.to || account.username) + '@' + this._box.domain),
body: common.regexp('This release depends on you')
2016-06-24 05:29:34 +00:00
}, callback);
};
2017-03-30 23:31:22 +00:00
Cloudron.prototype.checkTcpRateLimit = function (port, times, callback) {
tcpBomb(this._adminFqdn, port, times, 900 /* timeout */, function (error, result) {
2017-03-31 02:44:32 +00:00
if (error) return callback(new Error(port + ':' + error.message));
2017-03-30 23:31:22 +00:00
if (result.timeout) return callback(null, { status: 'OK' }); // something timedout, this is good enough
2017-03-31 02:44:32 +00:00
return callback(new Error(port + ':' + JSON.stringify(result, null, 4)));
2017-03-30 23:31:22 +00:00
});
};
2017-03-31 03:03:07 +00:00
Cloudron.prototype.checkNginxRateLimit = function (owner, times, callback) {
2017-03-30 23:31:22 +00:00
var ok = 0;
2017-03-31 03:03:07 +00:00
var that = this;
2017-03-30 23:31:22 +00:00
async.times(times, function (n, done) {
2017-03-31 03:03:07 +00:00
superagent.post('https://' + that._adminFqdn + '/api/v1/developer/login').send({ username: owner.username, password: owner.password }).end(function (error, result) {
if (!error && result.statusCode === 200) ++ok;
2017-03-30 23:31:22 +00:00
2017-03-31 03:03:07 +00:00
done();
});
}, function () {
callback(ok == times ? new Error('All requests succeeded') : null);
2017-03-30 23:31:22 +00:00
});
};
2017-04-15 16:20:47 +00:00
Cloudron.prototype.setAppstoreConfig = function (id, token) {
2017-04-15 16:04:31 +00:00
var res = request.post('https://' + this._adminFqdn + '/api/v1/settings/appstore_config')
.send({ userId: id, token: token })
.query({ access_token: this._credentials.accessToken })
.end();
2017-04-15 15:00:57 +00:00
common.verifyResponse2xx(res, 'Could not set appstore config');
2017-04-15 14:32:45 +00:00
};