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

154 lines
3.7 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
};
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 {
init: function(deps) {
request = deps.request;
return null;
},
zones: function(data) {
2019-07-31 02:00:06 +00:00
return api('GET', '/domains?statuses=ACTIVE').then(function(resp) {
2019-07-28 11:28:13 +00:00
return resp.body.map(function(x) {
return x.domain;
});
});
},
set: function(data) {
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(
2019-07-31 01:37:01 +00:00
function(resp) {
2019-07-31 02:00:06 +00:00
return null;
2019-07-31 01:37:01 +00:00
}
);
2019-07-28 11:28:13 +00:00
},
remove: function(data) {
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
)
2019-07-28 11:28:13 +00:00
.then(function(resp) {
2019-07-31 02:00:06 +00:00
// keep all TXT records that we don't need to remove
return resp.body.filter(function(el) {
return el.data !== ch.dnsAuthorization;
});
2019-07-31 01:37:01 +00:00
})
2019-07-31 02:00:06 +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
2019-07-31 01:37:01 +00:00
).then(function(resp) {
2019-07-31 02:00:06 +00:00
return null;
2019-07-28 11:28:13 +00:00
});
});
},
get: function(data) {
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
2019-07-31 01:37:01 +00:00
).then(function(resp) {
2019-07-31 02:00:06 +00:00
var entry = (resp.body || []).filter(function(x) {
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
};
return request(req).then(function(resp) {
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'
);
}
});
}
2019-07-28 11:28:13 +00:00
};