2015-07-20 00:09:47 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
2019-01-25 16:33:22 -08:00
|
|
|
set: set,
|
|
|
|
|
get: get,
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2019-05-06 20:01:35 -07:00
|
|
|
getCloudronAvatar: getCloudronAvatar
|
2015-07-20 00:09:47 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var assert = require('assert'),
|
2019-02-09 18:08:10 -08:00
|
|
|
backups = require('../backups.js'),
|
2019-05-07 09:34:23 -07:00
|
|
|
custom = require('../custom.js'),
|
2018-10-12 17:04:04 -07:00
|
|
|
docker = require('../docker.js'),
|
|
|
|
|
DockerError = docker.DockerError,
|
2015-07-20 00:09:47 -07:00
|
|
|
HttpError = require('connect-lastmile').HttpError,
|
|
|
|
|
HttpSuccess = require('connect-lastmile').HttpSuccess,
|
|
|
|
|
safe = require('safetydance'),
|
|
|
|
|
settings = require('../settings.js'),
|
|
|
|
|
SettingsError = settings.SettingsError;
|
|
|
|
|
|
2018-02-06 18:57:30 +01:00
|
|
|
function getAppAutoupdatePattern(req, res, next) {
|
|
|
|
|
settings.getAppAutoupdatePattern(function (error, pattern) {
|
|
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(200, { pattern: pattern }));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setAppAutoupdatePattern(req, res, next) {
|
|
|
|
|
assert.strictEqual(typeof req.body, 'object');
|
|
|
|
|
|
|
|
|
|
if (typeof req.body.pattern !== 'string') return next(new HttpError(400, 'pattern is required'));
|
|
|
|
|
|
|
|
|
|
settings.setAppAutoupdatePattern(req.body.pattern, function (error) {
|
|
|
|
|
if (error && error.reason === SettingsError.BAD_FIELD) return next(new HttpError(400, error.message));
|
|
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
2018-06-07 16:10:47 +02:00
|
|
|
next(new HttpSuccess(200, {}));
|
2018-02-06 18:57:30 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getBoxAutoupdatePattern(req, res, next) {
|
|
|
|
|
settings.getBoxAutoupdatePattern(function (error, pattern) {
|
2015-07-20 00:09:47 -07:00
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(200, { pattern: pattern }));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-06 18:57:30 +01:00
|
|
|
function setBoxAutoupdatePattern(req, res, next) {
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof req.body, 'object');
|
|
|
|
|
|
|
|
|
|
if (typeof req.body.pattern !== 'string') return next(new HttpError(400, 'pattern is required'));
|
|
|
|
|
|
2018-02-06 18:57:30 +01:00
|
|
|
settings.setBoxAutoupdatePattern(req.body.pattern, function (error) {
|
2016-06-02 13:00:23 -07:00
|
|
|
if (error && error.reason === SettingsError.BAD_FIELD) return next(new HttpError(400, error.message));
|
2015-07-20 00:09:47 -07:00
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
2018-06-07 16:10:47 +02:00
|
|
|
next(new HttpSuccess(200, {}));
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setCloudronName(req, res, next) {
|
|
|
|
|
assert.strictEqual(typeof req.body, 'object');
|
|
|
|
|
|
|
|
|
|
if (typeof req.body.name !== 'string') return next(new HttpError(400, 'name is required'));
|
|
|
|
|
|
|
|
|
|
settings.setCloudronName(req.body.name, function (error) {
|
2016-06-02 13:00:23 -07:00
|
|
|
if (error && error.reason === SettingsError.BAD_FIELD) return next(new HttpError(400, error.message));
|
2015-07-20 00:09:47 -07:00
|
|
|
if (error) return next(new HttpError(500, error));
|
2016-06-02 13:00:23 -07:00
|
|
|
|
2018-06-07 16:10:47 +02:00
|
|
|
next(new HttpSuccess(202, {}));
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getCloudronName(req, res, next) {
|
|
|
|
|
settings.getCloudronName(function (error, name) {
|
|
|
|
|
if (error) return next(new HttpError(500, error));
|
2016-06-02 13:00:23 -07:00
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
next(new HttpSuccess(200, { name: name }));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-03 12:10:16 -07:00
|
|
|
function getTimeZone(req, res, next) {
|
|
|
|
|
settings.getTimeZone(function (error, tz) {
|
|
|
|
|
if (error) return next(new HttpError(500, error));
|
2016-06-02 13:00:23 -07:00
|
|
|
|
2016-05-03 12:10:16 -07:00
|
|
|
next(new HttpSuccess(200, { timeZone: tz }));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-02 13:36:47 -07:00
|
|
|
function setTimeZone(req, res, next) {
|
|
|
|
|
assert.strictEqual(typeof req.body, 'object');
|
|
|
|
|
|
|
|
|
|
if (typeof req.body.timeZone !== 'string') return next(new HttpError(400, 'timeZone is required'));
|
|
|
|
|
|
|
|
|
|
settings.setTimeZone(req.body.timeZone, function (error) {
|
|
|
|
|
if (error && error.reason === SettingsError.BAD_FIELD) return next(new HttpError(400, error.message));
|
|
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
2018-06-07 16:10:47 +02:00
|
|
|
next(new HttpSuccess(200, {}));
|
2016-06-02 13:36:47 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
function setCloudronAvatar(req, res, next) {
|
|
|
|
|
assert.strictEqual(typeof req.files, 'object');
|
|
|
|
|
|
|
|
|
|
if (!req.files.avatar) return next(new HttpError(400, 'avatar must be provided'));
|
|
|
|
|
var avatar = safe.fs.readFileSync(req.files.avatar.path);
|
|
|
|
|
|
|
|
|
|
settings.setCloudronAvatar(avatar, function (error) {
|
|
|
|
|
if (error) return next(new HttpError(500, error));
|
2016-06-02 13:00:23 -07:00
|
|
|
|
2018-06-07 16:10:47 +02:00
|
|
|
next(new HttpSuccess(202, {}));
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getCloudronAvatar(req, res, next) {
|
|
|
|
|
settings.getCloudronAvatar(function (error, avatar) {
|
|
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
2015-07-23 14:44:05 +02:00
|
|
|
// avoid caching the avatar on the client to see avatar changes immediately
|
|
|
|
|
res.set('Cache-Control', 'no-cache');
|
|
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
res.set('Content-Type', 'image/png');
|
|
|
|
|
res.status(200).send(avatar);
|
|
|
|
|
});
|
|
|
|
|
}
|
2015-10-26 00:59:20 -07:00
|
|
|
|
2015-11-09 20:34:25 -08:00
|
|
|
function getBackupConfig(req, res, next) {
|
2019-07-26 07:25:40 -07:00
|
|
|
settings.getBackupConfig(function (error, backupConfig) {
|
2015-11-09 20:34:25 -08:00
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
2019-05-10 15:53:34 -07:00
|
|
|
// always send provider as it is used by the UI to figure if backups are disabled ('noop' backend)
|
|
|
|
|
if (!custom.spec().backups.configurable) {
|
2019-07-26 07:25:40 -07:00
|
|
|
return next(new HttpSuccess(200, { provider: backupConfig.provider }));
|
2019-05-07 09:34:23 -07:00
|
|
|
}
|
|
|
|
|
|
2019-07-26 07:25:40 -07:00
|
|
|
next(new HttpSuccess(200, backups.removePrivateFields(backupConfig)));
|
2015-11-09 20:34:25 -08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setBackupConfig(req, res, next) {
|
|
|
|
|
assert.strictEqual(typeof req.body, 'object');
|
|
|
|
|
|
2019-05-10 15:53:34 -07:00
|
|
|
if (!custom.spec().backups.configurable) return next(new HttpError(405, 'feature disabled by admin'));
|
2019-05-07 09:34:23 -07:00
|
|
|
|
2015-11-09 20:34:25 -08:00
|
|
|
if (typeof req.body.provider !== 'string') return next(new HttpError(400, 'provider is required'));
|
2017-04-22 21:46:53 -07:00
|
|
|
if (typeof req.body.retentionSecs !== 'number') return next(new HttpError(400, 'retentionSecs is required'));
|
2018-08-13 22:31:35 -07:00
|
|
|
if (typeof req.body.intervalSecs !== 'number') return next(new HttpError(400, 'intervalSecs is required'));
|
2016-10-13 15:32:18 -07:00
|
|
|
if ('key' in req.body && typeof req.body.key !== 'string') return next(new HttpError(400, 'key must be a string'));
|
2018-03-20 19:18:48 -07:00
|
|
|
if ('syncConcurrency' in req.body) {
|
|
|
|
|
if (typeof req.body.syncConcurrency !== 'number') return next(new HttpError(400, 'syncConcurrency must be a positive integer'));
|
|
|
|
|
if (req.body.syncConcurrency < 1) return next(new HttpError(400, 'syncConcurrency must be a positive integer'));
|
|
|
|
|
}
|
2017-09-25 23:49:49 -07:00
|
|
|
if (typeof req.body.format !== 'string') return next(new HttpError(400, 'format must be a string'));
|
2017-10-05 10:01:09 -07:00
|
|
|
if ('acceptSelfSignedCerts' in req.body && typeof req.body.acceptSelfSignedCerts !== 'boolean') return next(new HttpError(400, 'format must be a boolean'));
|
2015-11-09 20:34:25 -08:00
|
|
|
|
2018-08-05 22:29:27 -07:00
|
|
|
// testing the backup using put/del takes a bit of time at times
|
|
|
|
|
req.clearTimeout();
|
|
|
|
|
|
2015-11-09 20:34:25 -08:00
|
|
|
settings.setBackupConfig(req.body, function (error) {
|
|
|
|
|
if (error && error.reason === SettingsError.BAD_FIELD) return next(new HttpError(400, error.message));
|
2018-09-07 11:27:19 -07:00
|
|
|
if (error && error.reason === SettingsError.EXTERNAL_ERROR) return next(new HttpError(424, error.message));
|
2015-11-09 20:34:25 -08:00
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
2018-06-07 16:10:47 +02:00
|
|
|
next(new HttpSuccess(200, {}));
|
2015-11-09 20:34:25 -08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-25 13:06:38 -07:00
|
|
|
function getPlatformConfig(req, res, next) {
|
|
|
|
|
settings.getPlatformConfig(function (error, config) {
|
|
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(200, config));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setPlatformConfig(req, res, next) {
|
|
|
|
|
assert.strictEqual(typeof req.body, 'object');
|
|
|
|
|
|
|
|
|
|
for (let addon of [ 'mysql', 'postgresql', 'mail', 'mongodb' ]) {
|
|
|
|
|
if (!(addon in req.body)) continue;
|
|
|
|
|
if (typeof req.body[addon] !== 'object') return next(new HttpError(400, 'addon config must be an object'));
|
|
|
|
|
|
|
|
|
|
if (typeof req.body[addon].memory !== 'number') return next(new HttpError(400, 'memory must be a number'));
|
|
|
|
|
if (typeof req.body[addon].memorySwap !== 'number') return next(new HttpError(400, 'memorySwap must be a number'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
settings.setPlatformConfig(req.body, function (error) {
|
|
|
|
|
if (error && error.reason === SettingsError.BAD_FIELD) return next(new HttpError(400, error.message));
|
2018-09-07 11:27:19 -07:00
|
|
|
if (error && error.reason === SettingsError.EXTERNAL_ERROR) return next(new HttpError(424, error.message));
|
2018-07-25 13:06:38 -07:00
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(200, {}));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-29 12:25:10 +02:00
|
|
|
function getExternalLdapConfig(req, res, next) {
|
|
|
|
|
settings.getExternalLdapConfig(function (error, config) {
|
|
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(200, config));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setExternalLdapConfig(req, res, next) {
|
|
|
|
|
assert.strictEqual(typeof req.body, 'object');
|
|
|
|
|
|
|
|
|
|
if (typeof req.body.url !== 'string' || req.body.url === '') return next(new HttpError(400, 'url must be a non empty string'));
|
|
|
|
|
if (typeof req.body.baseDn !== 'string' || req.body.baseDn === '') return next(new HttpError(400, 'baseDn must be a non empty string'));
|
|
|
|
|
if (typeof req.body.filter !== 'string' || req.body.filter === '') return next(new HttpError(400, 'filter must be a non empty string'));
|
|
|
|
|
if ('bindDn' in req.body && (typeof req.body.bindDn !== 'string' || req.body.bindDn === '')) return next(new HttpError(400, 'bindDn must be a non empty string'));
|
|
|
|
|
if ('bindPassword' in req.body && typeof req.body.bindPassword !== 'string') return next(new HttpError(400, 'bindPassword must be a string'));
|
|
|
|
|
|
|
|
|
|
settings.setExternalLdapConfig(req.body, function (error) {
|
|
|
|
|
if (error && error.reason === SettingsError.BAD_FIELD) return next(new HttpError(400, error.message));
|
|
|
|
|
if (error && error.reason === SettingsError.EXTERNAL_ERROR) return next(new HttpError(424, error.message));
|
|
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(200, {}));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-31 16:02:51 +01:00
|
|
|
function getDynamicDnsConfig(req, res, next) {
|
|
|
|
|
settings.getDynamicDnsConfig(function (error, enabled) {
|
|
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(200, { enabled: enabled }));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setDynamicDnsConfig(req, res, next) {
|
|
|
|
|
assert.strictEqual(typeof req.body, 'object');
|
|
|
|
|
|
2019-05-10 15:53:34 -07:00
|
|
|
if (!custom.spec().domains.dynamicDns) return next(new HttpError(405, 'feature disabled by admin'));
|
2019-05-07 09:34:23 -07:00
|
|
|
|
2018-10-31 16:02:51 +01:00
|
|
|
if (typeof req.body.enabled !== 'boolean') return next(new HttpError(400, 'enabled boolean is required'));
|
|
|
|
|
|
|
|
|
|
settings.setDynamicDnsConfig(req.body.enabled, 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, {}));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-27 22:30:32 +02:00
|
|
|
function getUnstableAppsConfig(req, res, next) {
|
|
|
|
|
settings.getUnstableAppsConfig(function (error, enabled) {
|
|
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(200, { enabled: enabled }));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setUnstableAppsConfig(req, res, next) {
|
|
|
|
|
assert.strictEqual(typeof req.body, 'object');
|
|
|
|
|
|
|
|
|
|
if (typeof req.body.enabled !== 'boolean') return next(new HttpError(400, 'enabled boolean is required'));
|
|
|
|
|
|
|
|
|
|
settings.setUnstableAppsConfig(req.body.enabled, 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, {}));
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-10-31 16:02:51 +01:00
|
|
|
|
2018-10-12 17:04:04 -07:00
|
|
|
function setRegistryConfig(req, res, next) {
|
|
|
|
|
assert.strictEqual(typeof req.body, 'object');
|
|
|
|
|
|
|
|
|
|
if (typeof req.body.serveraddress !== 'string') return next(new HttpError(400, 'serveraddress is required'));
|
|
|
|
|
if ('username' in req.body && typeof req.body.username !== 'string') return next(new HttpError(400, 'username is required'));
|
|
|
|
|
if ('password' in req.body && typeof req.body.password !== 'string') return next(new HttpError(400, 'password is required'));
|
|
|
|
|
|
|
|
|
|
docker.setRegistryConfig(req.body, function (error) {
|
|
|
|
|
if (error && error.reason === DockerError.BAD_FIELD) return next(new HttpError(400, error.message));
|
|
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(200));
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-01-25 16:33:22 -08:00
|
|
|
|
|
|
|
|
function get(req, res, next) {
|
|
|
|
|
assert.strictEqual(typeof req.params.setting, 'string');
|
|
|
|
|
|
|
|
|
|
switch (req.params.setting) {
|
|
|
|
|
case settings.DYNAMIC_DNS_KEY: return getDynamicDnsConfig(req, res, next);
|
|
|
|
|
case settings.BACKUP_CONFIG_KEY: return getBackupConfig(req, res, next);
|
|
|
|
|
case settings.PLATFORM_CONFIG_KEY: return getPlatformConfig(req, res, next);
|
2019-08-29 12:25:10 +02:00
|
|
|
case settings.EXTERNAL_LDAP_KEY: return getExternalLdapConfig(req, res, next);
|
2019-04-27 22:30:32 +02:00
|
|
|
case settings.UNSTABLE_APPS_KEY: return getUnstableAppsConfig(req, res, next);
|
2019-01-25 16:33:22 -08:00
|
|
|
|
|
|
|
|
case settings.APP_AUTOUPDATE_PATTERN_KEY: return getAppAutoupdatePattern(req, res, next);
|
|
|
|
|
case settings.BOX_AUTOUPDATE_PATTERN_KEY: return getBoxAutoupdatePattern(req, res, next);
|
|
|
|
|
case settings.TIME_ZONE_KEY: return getTimeZone(req, res, next);
|
|
|
|
|
case settings.CLOUDRON_NAME_KEY: return getCloudronName(req, res, next);
|
|
|
|
|
|
|
|
|
|
case settings.CLOUDRON_AVATAR_KEY: return getCloudronAvatar(req, res, next);
|
|
|
|
|
|
|
|
|
|
default: return next(new HttpError(404, 'No such setting'));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function set(req, res, next) {
|
|
|
|
|
assert.strictEqual(typeof req.body, 'object');
|
|
|
|
|
|
|
|
|
|
switch (req.params.setting) {
|
|
|
|
|
case settings.DYNAMIC_DNS_KEY: return setDynamicDnsConfig(req, res, next);
|
|
|
|
|
case settings.BACKUP_CONFIG_KEY: return setBackupConfig(req, res, next);
|
|
|
|
|
case settings.PLATFORM_CONFIG_KEY: return setPlatformConfig(req, res, next);
|
2019-08-29 12:25:10 +02:00
|
|
|
case settings.EXTERNAL_LDAP_KEY: return setExternalLdapConfig(req, res, next);
|
2019-04-27 22:30:32 +02:00
|
|
|
case settings.UNSTABLE_APPS_KEY: return setUnstableAppsConfig(req, res, next);
|
2019-01-25 16:33:22 -08:00
|
|
|
|
|
|
|
|
case settings.APP_AUTOUPDATE_PATTERN_KEY: return setAppAutoupdatePattern(req, res, next);
|
|
|
|
|
case settings.BOX_AUTOUPDATE_PATTERN_KEY: return setBoxAutoupdatePattern(req, res, next);
|
|
|
|
|
case settings.TIME_ZONE_KEY: return setTimeZone(req, res, next);
|
|
|
|
|
case settings.CLOUDRON_NAME_KEY: return setCloudronName(req, res, next);
|
|
|
|
|
|
2019-01-25 14:57:07 -08:00
|
|
|
case settings.CLOUDRON_AVATAR_KEY: return setCloudronAvatar(req, res, next);
|
|
|
|
|
|
2019-01-25 16:33:22 -08:00
|
|
|
default: return next(new HttpError(404, 'No such setting'));
|
|
|
|
|
}
|
|
|
|
|
}
|