acme-dns-01-godaddy.js/lib/index.js

158 lines
3.8 KiB
JavaScript
Raw Normal View History

2019-07-28 11:28:13 +00:00
'use strict';
var request; // = require('@root/request');
// request = require('util').promisify(request);
var OTE_ENVIRONMENT = 'https://api.ote-godaddy.com/v1/';
var PRODUCTION_ENVIRONMENT = 'https://api.godaddy.com/v1/';
var defaults = {
baseUrl: PRODUCTION_ENVIRONMENT
};
2021-04-04 16:51:43 +00:00
module.exports.create = function (config) {
2019-07-31 02:00:06 +00:00
var baseUrl = (config.baseUrl || defaults.baseUrl).replace(/\/$/, '');
2019-07-28 11:28:13 +00:00
var apiKey = config.key || config.apiKey;
var apiSecret = config.secret || config.apiSecret;
2019-07-31 01:37:01 +00:00
var auth = 'sso-key ' + apiKey + ':' + apiSecret;
2019-07-31 02:00:06 +00:00
var redacted = 'sso-key ' + apiKey + ':[redacted]';
2019-07-28 11:28:13 +00:00
return {
2021-04-04 16:51:43 +00:00
propagationDelay: 30 * 1000,
2021-04-04 16:51:43 +00:00
init: function (deps) {
2019-07-28 11:28:13 +00:00
request = deps.request;
2021-04-04 16:57:21 +00:00
return Promise.resolve();
2019-07-28 11:28:13 +00:00
},
2021-04-04 16:51:43 +00:00
zones: function (data) {
return api('GET', '/domains?statuses=ACTIVE').then(function (resp) {
return resp.body.map(function (x) {
2019-07-28 11:28:13 +00:00
return x.domain;
});
});
},
2021-04-04 16:51:43 +00:00
set: function (data) {
2019-07-28 11:28:13 +00:00
var ch = data.challenge;
var txt = ch.dnsAuthorization;
// If the domain to be verified is
// optional params commented
var records = [
{
2019-07-31 01:37:01 +00:00
data: txt,
name: ch.dnsPrefix,
2019-07-28 11:28:13 +00:00
// "port": 0,
// "priority": 0,
// "protocol": "string",
// "service": "string",
2019-07-31 01:37:01 +00:00
ttl: 600,
type: 'TXT'
2019-07-28 11:28:13 +00:00
// "weight": 0
}
];
2019-07-31 02:00:06 +00:00
return api('PATCH', '/domains/' + ch.dnsZone + '/records', records).then(
2021-04-04 16:51:43 +00:00
function (resp) {
return null;
2019-07-31 01:37:01 +00:00
}
2021-04-04 16:51:43 +00:00
);
2019-07-28 11:28:13 +00:00
},
2021-04-04 16:51:43 +00:00
remove: function (data) {
2019-07-28 11:28:13 +00:00
var ch = data.challenge;
// get all domain records of type or name
2019-07-31 01:37:01 +00:00
return api(
'GET',
2019-07-31 02:00:06 +00:00
'/domains/' + ch.dnsZone + '/records/TXT/' + ch.dnsPrefix
2019-07-31 01:37:01 +00:00
)
2021-04-04 16:51:43 +00:00
.then(function (resp) {
2019-07-31 02:00:06 +00:00
// keep all TXT records that we don't need to remove
2021-04-04 16:51:43 +00:00
return resp.body.filter(function (el) {
2019-07-31 02:00:06 +00:00
return el.data !== ch.dnsAuthorization;
});
2019-07-31 01:37:01 +00:00
})
2021-04-04 16:51:43 +00:00
.then(function (records) {
2019-07-28 11:28:13 +00:00
// godaddy doesn't provide an endpoint for a single record removal
// but provides this endpoint to replace all records of a given type
// https://developer.godaddy.com/doc/endpoint/domains#/v1/recordReplaceType
// however, calling the endpoint with no records does no changes
// hence when only a single record of type exists and is the one to remove
// we call the endpoint with this dummy record
2019-07-31 02:00:06 +00:00
if (!records.length) {
records.push({
2019-07-31 01:37:01 +00:00
data: 'free_to_delete',
name: 'remove_placeholder',
ttl: 600
2019-07-28 11:28:13 +00:00
});
}
// update - overwrite all type and name records under domain
2019-07-31 01:37:01 +00:00
return api(
'PUT',
2019-07-31 02:00:06 +00:00
'/domains/' + ch.dnsZone + '/records/TXT',
records
2021-04-04 16:51:43 +00:00
).then(function (resp) {
2019-07-31 02:00:06 +00:00
return null;
2019-07-28 11:28:13 +00:00
});
});
},
2021-04-04 16:51:43 +00:00
get: function (data) {
2019-07-28 11:28:13 +00:00
var ch = data.challenge;
2019-07-31 01:37:01 +00:00
return api(
'GET',
2019-07-31 02:00:06 +00:00
'/domains/' + ch.dnsZone + '/records/TXT/' + ch.dnsPrefix
2021-04-04 16:51:43 +00:00
).then(function (resp) {
var entry = (resp.body || []).filter(function (x) {
2019-07-31 02:00:06 +00:00
return x.data === ch.dnsAuthorization;
})[0];
2019-07-28 11:28:13 +00:00
2019-07-31 02:00:06 +00:00
if (entry) {
return { dnsAuthorization: entry.data };
2019-07-28 11:28:13 +00:00
}
2019-07-31 02:00:06 +00:00
return null;
2019-07-28 11:28:13 +00:00
});
}
};
2019-07-31 02:00:06 +00:00
function api(method, path, data) {
var req = {
method: method,
url: baseUrl + path,
headers: { Authorization: auth },
json: data || true
};
2019-08-02 23:03:25 +00:00
2021-04-04 16:51:43 +00:00
return request(req).then(function (resp) {
2019-07-31 02:00:06 +00:00
if (resp.statusCode < 200 || resp.statusCode >= 300) {
req.headers.Authorization = redacted;
console.error();
console.error(req.method + ' ' + req.url);
console.error(req.headers);
if (data) {
console.error(data);
}
console.error();
console.error(resp.statusCode);
console.error(resp.body);
console.error();
throw new Error(
'Request "' +
req.method +
' ' +
req.url +
'" failed: ' +
resp.statusCode +
' ' +
JSON.stringify(resp.body) +
'. Check subdomain, api key, etc'
);
}
return resp;
2019-07-31 02:00:06 +00:00
});
}
2019-07-28 11:28:13 +00:00
};