Files
cloudron-box/src/dns/gandi.js

173 lines
6.8 KiB
JavaScript
Raw Normal View History

2018-05-06 18:57:27 -07:00
'use strict';
exports = module.exports = {
2021-08-13 17:22:28 -07:00
removePrivateFields,
injectPrivateFields,
upsert,
get,
del,
wait,
verifyDomainConfig
2018-05-06 18:57:27 -07:00
};
2021-08-13 17:22:28 -07:00
const assert = require('assert'),
2019-10-23 10:02:04 -07:00
BoxError = require('../boxerror.js'),
constants = require('../constants.js'),
2018-05-06 18:57:27 -07:00
debug = require('debug')('box:dns/gandi'),
dig = require('../dig.js'),
2021-08-13 17:22:28 -07:00
dns = require('../dns.js'),
safe = require('safetydance'),
2018-05-06 18:57:27 -07:00
superagent = require('superagent'),
util = require('util'),
waitForDns = require('./waitfordns.js');
2018-05-06 18:57:27 -07:00
2021-08-13 17:22:28 -07:00
const GANDI_API = 'https://dns.api.gandi.net/api/v5';
2018-05-06 18:57:27 -07:00
function formatError(response) {
2018-05-06 20:58:11 -07:00
return util.format(`Gandi DNS error [${response.statusCode}] ${response.body.message}`);
2018-05-06 18:57:27 -07:00
}
function removePrivateFields(domainObject) {
domainObject.config.token = constants.SECRET_PLACEHOLDER;
return domainObject;
}
function injectPrivateFields(newConfig, currentConfig) {
if (newConfig.token === constants.SECRET_PLACEHOLDER) newConfig.token = currentConfig.token;
}
function createRequest(method, url, domainConfig) {
assert.strictEqual(typeof method, 'string');
assert.strictEqual(typeof url, 'string');
assert.strictEqual(typeof domainConfig, 'object');
const request = superagent(method, url).timeout(30 * 1000).ok(() => true);
// https://api.gandi.net/docs/authentication/
if (domainConfig.tokenType === 'ApiKey') {
request.set('X-Api-Key', domainConfig.token);
request.set('Authorization', `Apikey ${domainConfig.token}`);
} else { // PAT
request.set('Authorization', `Bearer ${domainConfig.token}`);
}
return request;
}
async function upsert(domainObject, location, type, values) {
assert.strictEqual(typeof domainObject, 'object');
assert.strictEqual(typeof location, 'string');
2018-05-06 18:57:27 -07:00
assert.strictEqual(typeof type, 'string');
2021-05-02 11:26:08 -07:00
assert(Array.isArray(values));
2018-05-06 18:57:27 -07:00
const domainConfig = domainObject.config,
zoneName = domainObject.zoneName,
2021-08-13 17:22:28 -07:00
name = dns.getName(domainObject, location, type) || '@';
2018-05-06 18:57:27 -07:00
debug(`upsert: ${name} in zone ${zoneName} of type ${type} with values ${JSON.stringify(values)}`);
2018-05-06 18:57:27 -07:00
const data = {
2018-05-06 18:57:27 -07:00
'rrset_ttl': 300, // this is the minimum allowed
2018-05-06 22:14:39 -07:00
'rrset_values': values // for mx records, value is already of the '<priority> <server>' format
2018-05-06 18:57:27 -07:00
};
const [error, response] = await safe(createRequest('PUT', `${GANDI_API}/domains/${zoneName}/records/${name}/${type}`, domainConfig)
.send(data));
if (error) throw new BoxError(BoxError.NETWORK_ERROR, error);
if (response.statusCode === 403 || response.statusCode === 401) throw new BoxError(BoxError.ACCESS_DENIED, formatError(response));
if (response.statusCode === 400) throw new BoxError(BoxError.BAD_FIELD, formatError(response));
if (response.statusCode !== 201) throw new BoxError(BoxError.EXTERNAL_ERROR, formatError(response));
2018-05-06 18:57:27 -07:00
}
async function get(domainObject, location, type) {
assert.strictEqual(typeof domainObject, 'object');
assert.strictEqual(typeof location, 'string');
2018-05-06 18:57:27 -07:00
assert.strictEqual(typeof type, 'string');
const domainConfig = domainObject.config,
zoneName = domainObject.zoneName,
2021-08-13 17:22:28 -07:00
name = dns.getName(domainObject, location, type) || '@';
2018-05-06 18:57:27 -07:00
debug(`get: ${name} in zone ${zoneName} of type ${type}`);
2018-05-06 18:57:27 -07:00
const [error, response] = await safe(createRequest('GET', `${GANDI_API}/domains/${zoneName}/records/${name}/${type}`, domainConfig));
2018-05-06 18:57:27 -07:00
if (error) throw new BoxError(BoxError.NETWORK_ERROR, error);
if (response.statusCode === 403 || response.statusCode === 401) throw new BoxError(BoxError.ACCESS_DENIED, formatError(response));
if (response.statusCode === 404) return [];
if (response.statusCode !== 200) throw new BoxError(BoxError.EXTERNAL_ERROR, formatError(response));
2018-05-06 18:57:27 -07:00
return response.body.rrset_values;
2018-05-06 18:57:27 -07:00
}
async function del(domainObject, location, type, values) {
assert.strictEqual(typeof domainObject, 'object');
assert.strictEqual(typeof location, 'string');
2018-05-06 18:57:27 -07:00
assert.strictEqual(typeof type, 'string');
2021-05-02 11:26:08 -07:00
assert(Array.isArray(values));
2018-05-06 18:57:27 -07:00
const domainConfig = domainObject.config,
zoneName = domainObject.zoneName,
2021-08-13 17:22:28 -07:00
name = dns.getName(domainObject, location, type) || '@';
2018-05-06 18:57:27 -07:00
debug(`del: ${name} in zone ${zoneName} of type ${type} with values ${JSON.stringify(values)}`);
2018-05-06 18:57:27 -07:00
const [error, response] = await safe(createRequest('DELETE', `${GANDI_API}/domains/${zoneName}/records/${name}/${type}`, domainConfig));
2018-05-06 18:57:27 -07:00
if (error) throw new BoxError(BoxError.NETWORK_ERROR, error);
if (response.statusCode === 404) return;
if (response.statusCode === 403 || response.statusCode === 401) throw new BoxError(BoxError.ACCESS_DENIED, formatError(response));
if (response.statusCode !== 204) throw new BoxError(BoxError.EXTERNAL_ERROR, formatError(response));
2018-05-06 18:57:27 -07:00
}
async function wait(domainObject, subdomain, type, value, options) {
assert.strictEqual(typeof domainObject, 'object');
assert.strictEqual(typeof subdomain, 'string');
assert.strictEqual(typeof type, 'string');
assert.strictEqual(typeof value, 'string');
assert(options && typeof options === 'object'); // { interval: 5000, times: 50000 }
const fqdn = dns.fqdn(subdomain, domainObject.domain);
await waitForDns(fqdn, domainObject.zoneName, type, value, options);
}
async function verifyDomainConfig(domainObject) {
assert.strictEqual(typeof domainObject, 'object');
2018-05-06 18:57:27 -07:00
const domainConfig = domainObject.config,
zoneName = domainObject.zoneName;
if (!domainConfig.token || typeof domainConfig.token !== 'string') throw new BoxError(BoxError.BAD_FIELD, 'token must be a non-empty string');
if (domainConfig.tokenType !== 'PAT' && domainConfig.tokenType !== 'ApiKey') throw new BoxError(BoxError.BAD_FIELD, 'tokenType is required');
2018-06-17 21:44:08 -07:00
const credentials = {
token: domainConfig.token,
tokenType: domainConfig.tokenType,
2018-05-06 18:57:27 -07:00
};
const ip = '127.0.0.1';
2023-10-01 13:52:19 +05:30
if (constants.TEST) return credentials; // this shouldn't be here
2018-05-06 18:57:27 -07:00
const [error, nameservers] = await safe(dig.resolve(zoneName, 'NS', { timeout: 5000 }));
if (error && error.code === 'ENOTFOUND') throw new BoxError(BoxError.BAD_FIELD, 'Unable to resolve nameservers for this domain');
if (error || !nameservers) throw new BoxError(BoxError.BAD_FIELD, error ? error.message : 'Unable to get nameservers');
2018-05-06 18:57:27 -07:00
if (!nameservers.every(function (n) { return n.toLowerCase().indexOf('.gandi.net') !== -1; })) {
debug('verifyDomainConfig: %j does not contain Gandi NS', nameservers);
throw new BoxError(BoxError.BAD_FIELD, 'Domain nameservers are not set to Gandi');
}
2018-05-06 18:57:27 -07:00
const location = 'cloudrontestdns';
2018-05-06 18:57:27 -07:00
await upsert(domainObject, location, 'A', [ ip ]);
debug('verifyDomainConfig: Test A record added');
2018-05-06 18:57:27 -07:00
await del(domainObject, location, 'A', [ ip ]);
debug('verifyDomainConfig: Test A record removed again');
2018-05-06 18:57:27 -07:00
return credentials;
2018-05-06 18:57:27 -07:00
}