domain: split the config and wellknown routes

we want to add more stuff to the UI like the jitsi URL
This commit is contained in:
Girish Ramakrishnan
2021-12-03 13:46:54 -08:00
parent 5592dc8a42
commit 39807e6ba4
8 changed files with 107 additions and 41 deletions

View File

@@ -4,7 +4,8 @@ exports = module.exports = {
add,
get,
list,
update,
setConfig,
setWellKnown,
del,
checkDnsRecords,
@@ -76,7 +77,7 @@ async function list(req, res, next) {
next(new HttpSuccess(200, { domains: results }));
}
async function update(req, res, next) {
async function setConfig(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
assert.strictEqual(typeof req.body, 'object');
@@ -98,26 +99,33 @@ async function update(req, res, next) {
if (!req.body.tlsConfig.provider || typeof req.body.tlsConfig.provider !== 'string') return next(new HttpError(400, 'tlsConfig.provider must be a string'));
}
if ('wellKnown' in req.body) {
if (typeof req.body.wellKnown !== 'object') return next(new HttpError(400, 'wellKnown must be an object'));
if (req.body.wellKnown) {
if (Object.keys(req.body.wellKnown).some(k => typeof req.body.wellKnown[k] !== 'string')) return next(new HttpError(400, 'wellKnown is a map of strings'));
}
}
// some DNS providers like DigitalOcean take a really long time to verify credentials (https://github.com/expressjs/timeout/issues/26)
req.clearTimeout();
let data = {
const data = {
zoneName: req.body.zoneName || '',
provider: req.body.provider,
config: req.body.config,
fallbackCertificate: req.body.fallbackCertificate || null,
tlsConfig: req.body.tlsConfig || { provider: 'letsencrypt-prod' },
wellKnown: req.body.wellKnown || null
tlsConfig: req.body.tlsConfig || { provider: 'letsencrypt-prod' }
};
const [error] = await safe(domains.update(req.params.domain, data, AuditSource.fromRequest(req)));
const [error] = await safe(domains.setConfig(req.params.domain, data, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(204, {}));
}
async function setWellKnown(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
assert.strictEqual(typeof req.body, 'object');
if (typeof req.body.wellKnown !== 'object') return next(new HttpError(400, 'wellKnown must be an object'));
if (req.body.wellKnown) {
if (Object.keys(req.body.wellKnown).some(k => typeof req.body.wellKnown[k] !== 'string')) return next(new HttpError(400, 'wellKnown is a map of strings'));
}
const [error] = await safe(domains.setWellKnown(req.params.domain, req.body.wellKnown || null, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(204, {}));

View File

@@ -152,6 +152,40 @@ describe('Domains API', function () {
});
});
describe('update', function () {
it('config fails for non-existing domain', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/domains/whatever/update`)
.query({ access_token: owner.token })
.ok(() => true);
expect(response.statusCode).to.equal(404);
});
it('config succeeds', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/domains/${DOMAIN_0.domain}/config`)
.query({ access_token: owner.token })
.send(DOMAIN_0);
expect(response.statusCode).to.equal(204);
});
it('wellknown succeeds', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/domains/${DOMAIN_0.domain}/wellknown`)
.query({ access_token: owner.token })
.send({ wellKnown: null });
expect(response.statusCode).to.equal(204);
});
it('wellknown succeeds', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/domains/${DOMAIN_0.domain}/wellknown`)
.query({ access_token: owner.token })
.send({ wellKnown: { service: 'some.service' } });
expect(response.statusCode).to.equal(204);
});
});
describe('Certificates API', function () {
let validCert0, validKey0, // example.com
validCert1, validKey1; // *.example.com
@@ -170,7 +204,7 @@ describe('Domains API', function () {
let d = Object.assign({}, DOMAIN_0);
d.fallbackCertificate = { key: validKey1 };
const response = await superagent.put(`${serverUrl}/api/v1/domains/${DOMAIN_0.domain}`)
const response = await superagent.post(`${serverUrl}/api/v1/domains/${DOMAIN_0.domain}/config`)
.query({ access_token: owner.token })
.send(d)
.ok(() => true);
@@ -182,7 +216,7 @@ describe('Domains API', function () {
let d = Object.assign({}, DOMAIN_0);
d.fallbackCertificate = { cert: validCert1 };
const response = await superagent.put(`${serverUrl}/api/v1/domains/${DOMAIN_0.domain}`)
const response = await superagent.post(`${serverUrl}/api/v1/domains/${DOMAIN_0.domain}/config`)
.query({ access_token: owner.token })
.send(d)
.ok(() => true);
@@ -194,7 +228,7 @@ describe('Domains API', function () {
let d = Object.assign({}, DOMAIN_0);
d.fallbackCertificate = { cert: 1234, key: validKey1 };
const response = await superagent.put(`${serverUrl}/api/v1/domains/${DOMAIN_0.domain}`)
const response = await superagent.post(`${serverUrl}/api/v1/domains/${DOMAIN_0.domain}/config`)
.query({ access_token: owner.token })
.send(d)
.ok(() => true);
@@ -206,7 +240,7 @@ describe('Domains API', function () {
let d = Object.assign({}, DOMAIN_0);
d.fallbackCertificate = { cert: validCert1, key: true };
const response = await superagent.put(`${serverUrl}/api/v1/domains/${DOMAIN_0.domain}`)
const response = await superagent.post(`${serverUrl}/api/v1/domains/${DOMAIN_0.domain}/config`)
.query({ access_token: owner.token })
.send(d)
.ok(() => true);
@@ -218,7 +252,7 @@ describe('Domains API', function () {
let d = Object.assign({}, DOMAIN_0);
d.fallbackCertificate = { cert: validCert0, key: validKey0 };
const response = await superagent.put(`${serverUrl}/api/v1/domains/${DOMAIN_0.domain}`)
const response = await superagent.post(`${serverUrl}/api/v1/domains/${DOMAIN_0.domain}/config`)
.query({ access_token: owner.token })
.send(d)
.ok(() => true);
@@ -230,7 +264,7 @@ describe('Domains API', function () {
let d = Object.assign({}, DOMAIN_0);
d.fallbackCertificate = { cert: validCert1, key: validKey1 };
const response = await superagent.put(`${serverUrl}/api/v1/domains/${DOMAIN_0.domain}`)
const response = await superagent.post(`${serverUrl}/api/v1/domains/${DOMAIN_0.domain}/config`)
.query({ access_token: owner.token })
.send(d);