diff --git a/src/apps.js b/src/apps.js index 854fed431..314a46b90 100644 --- a/src/apps.js +++ b/src/apps.js @@ -456,8 +456,6 @@ function configure(appId, data, auditSource, callback) { assert.strictEqual(typeof auditSource, 'object'); assert.strictEqual(typeof callback, 'function'); -console.dir(data); - appdb.get(appId, function (error, app) { if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new AppsError(AppsError.NOT_FOUND, 'No such app')); if (error) return callback(new AppsError(AppsError.INTERNAL_ERROR, error)); @@ -501,11 +499,16 @@ console.dir(data); // save cert to data/box/certs. TODO: move this to apptask when we have a real task queue if ('cert' in data && 'key' in data) { - error = certificates.validateCertificate(data.cert, data.key, config.appFqdn(location)); - if (error) return callback(new AppsError(AppsError.BAD_CERTIFICATE, error.message)); + if (data.cert && data.key) { + error = certificates.validateCertificate(data.cert, data.key, config.appFqdn(location)); + if (error) return callback(new AppsError(AppsError.BAD_CERTIFICATE, error.message)); - if (!safe.fs.writeFileSync(path.join(paths.APP_CERTS_DIR, config.appFqdn(location) + '.cert'), data.cert)) return callback(new AppsError(AppsError.INTERNAL_ERROR, 'Error saving cert: ' + safe.error.message)); - if (!safe.fs.writeFileSync(path.join(paths.APP_CERTS_DIR, config.appFqdn(location) + '.key'), data.key)) return callback(new AppsError(AppsError.INTERNAL_ERROR, 'Error saving key: ' + safe.error.message)); + if (!safe.fs.writeFileSync(path.join(paths.APP_CERTS_DIR, config.appFqdn(location) + '.cert'), data.cert)) return callback(new AppsError(AppsError.INTERNAL_ERROR, 'Error saving cert: ' + safe.error.message)); + if (!safe.fs.writeFileSync(path.join(paths.APP_CERTS_DIR, config.appFqdn(location) + '.key'), data.key)) return callback(new AppsError(AppsError.INTERNAL_ERROR, 'Error saving key: ' + safe.error.message)); + } else { // remove existing cert/key + if (!safe.fs.unlinkSync(path.join(paths.APP_CERTS_DIR, config.appFqdn(location) + '.cert'))) debug('Error removing cert: ' + safe.error.message); + if (!safe.fs.unlinkSync(path.join(paths.APP_CERTS_DIR, config.appFqdn(location) + '.key'))) debug('Error removing key: ' + safe.error.message); + } } values.oldConfig = { diff --git a/src/routes/apps.js b/src/routes/apps.js index c20a5c545..bb0564fb7 100644 --- a/src/routes/apps.js +++ b/src/routes/apps.js @@ -107,12 +107,17 @@ function installApp(req, res, next) { // optional if (('portBindings' in data) && typeof data.portBindings !== 'object') return next(new HttpError(400, 'portBindings must be an object')); if ('icon' in data && typeof data.icon !== 'string') return next(new HttpError(400, 'icon is not a string')); - if ('cert' in data && typeof data.cert !== 'string') return next(new HttpError(400, 'cert must be a string')); - if ('key' in data && typeof data.key !== 'string') return next(new HttpError(400, 'key must be a string')); + + // falsy values in cert and key unset the cert + if (data.key && typeof data.cert !== 'string') return next(new HttpError(400, 'cert must be a string')); + if (data.cert && typeof data.key !== 'string') return next(new HttpError(400, 'key must be a string')); if (data.cert && !data.key) return next(new HttpError(400, 'key must be provided')); if (!data.cert && data.key) return next(new HttpError(400, 'cert must be provided')); + if ('memoryLimit' in data && typeof data.memoryLimit !== 'number') return next(new HttpError(400, 'memoryLimit is not a number')); - if ('altDomain' in data && typeof data.altDomain !== 'string') return next(new HttpError(400, 'altDomain must be a string')); + + // falsy value in altDomain unsets it + if (data.altDomain && typeof data.altDomain !== 'string') return next(new HttpError(400, 'altDomain must be a string')); debug('Installing app id:%s data:%j', data); @@ -138,14 +143,17 @@ function configureApp(req, res, next) { var data = req.body; if ('location' in data && typeof data.location !== 'string') return next(new HttpError(400, 'location must be string')); - if (('portBindings' in data) && typeof data.portBindings !== 'object') return next(new HttpError(400, 'portBindings must be an object')); + if ('portBindings' in data && typeof data.portBindings !== 'object') return next(new HttpError(400, 'portBindings must be an object')); if ('accessRestriction' in data && typeof data.accessRestriction !== 'object') return next(new HttpError(400, 'accessRestriction must be an object')); - if ('cert' in data && typeof data.cert !== 'string') return next(new HttpError(400, 'cert must be a string')); - if ('key' in data && typeof data.key !== 'string') return next(new HttpError(400, 'key must be a string')); + + // falsy values in cert and key unset the cert + if (data.key && typeof data.cert !== 'string') return next(new HttpError(400, 'cert must be a string')); + if (data.cert && typeof data.key !== 'string') return next(new HttpError(400, 'key must be a string')); if (data.cert && !data.key) return next(new HttpError(400, 'key must be provided')); if (!data.cert && data.key) return next(new HttpError(400, 'cert must be provided')); + if ('memoryLimit' in data && typeof data.memoryLimit !== 'number') return next(new HttpError(400, 'memoryLimit is not a number')); - if ('altDomain' in data && typeof data.altDomain !== 'string') return next(new HttpError(400, 'altDomain must be a string')); + if (data.altDomain && typeof data.altDomain !== 'string') return next(new HttpError(400, 'altDomain must be a string')); debug('Configuring app id:%s data:%j', req.params.id, data);