mostly stylistic changes

This commit is contained in:
AJ ONeal 2019-07-30 20:00:06 -06:00
parent 393d25621c
commit 2bdf6ffc71
1 changed files with 58 additions and 66 deletions

View File

@ -11,22 +11,12 @@ var defaults = {
};
module.exports.create = function(config) {
var baseUrl = config.baseUrl || defaults.baseUrl;
var baseUrl = (config.baseUrl || defaults.baseUrl).replace(/\/$/, '');
var apiKey = config.key || config.apiKey;
var apiSecret = config.secret || config.apiSecret;
var auth = 'sso-key ' + apiKey + ':' + apiSecret;
function api(method, path, form) {
return request({
method: method,
url: baseUrl + path,
headers: {
Authorization: auth
},
json: form || true
});
}
var redacted = 'sso-key ' + apiKey + ':[redacted]';
return {
init: function(deps) {
@ -35,13 +25,7 @@ module.exports.create = function(config) {
},
zones: function(data) {
return api('GET', 'domains?statuses=ACTIVE').then(function(resp) {
if (200 !== resp.statusCode) {
console.error(resp.statusCode);
console.error(resp.body);
throw new Error('Could not get list of zones. Check api key, etc');
}
return api('GET', '/domains?statuses=ACTIVE').then(function(resp) {
return resp.body.map(function(x) {
return x.domain;
});
@ -67,17 +51,9 @@ module.exports.create = function(config) {
}
];
return api('PATCH', 'domains/' + ch.dnsZone + '/records', records).then(
return api('PATCH', '/domains/' + ch.dnsZone + '/records', records).then(
function(resp) {
if (200 !== resp.statusCode) {
console.error(resp.statusCode);
console.error(resp.body);
throw new Error(
'record did not set. check subdomain, api key, etc'
);
}
return true;
return null;
}
);
},
@ -87,25 +63,15 @@ module.exports.create = function(config) {
// get all domain records of type or name
return api(
'GET',
'domains/' + ch.dnsZone + '/records/TXT/' + ch.dnsPrefix
'/domains/' + ch.dnsZone + '/records/TXT/' + ch.dnsPrefix
)
.then(function(resp) {
if (200 !== resp.statusCode) {
console.error(resp.statusCode);
console.error(resp.body);
throw new Error('Could not get list of zones. Check api key, etc');
}
var newEntries = [];
// filter all TXT records without record to remove
for (let i = 0; i < resp.body.length; i++) {
if (resp.body[i]['data'] !== ch.dnsAuthorization) {
newEntries.push(resp.body[i]);
}
}
return newEntries;
// keep all TXT records that we don't need to remove
return resp.body.filter(function(el) {
return el.data !== ch.dnsAuthorization;
});
})
.then(function(newRecords) {
.then(function(records) {
// 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
@ -113,8 +79,8 @@ module.exports.create = function(config) {
// hence when only a single record of type exists and is the one to remove
// we call the endpoint with this dummy record
if (!newRecords.length) {
newRecords.push({
if (!records.length) {
records.push({
data: 'free_to_delete',
name: 'remove_placeholder',
ttl: 600
@ -124,17 +90,10 @@ module.exports.create = function(config) {
// update - overwrite all type and name records under domain
return api(
'PUT',
'domains/' + ch.dnsZone + '/records/TXT',
newRecords
'/domains/' + ch.dnsZone + '/records/TXT',
records
).then(function(resp) {
if (200 !== resp.statusCode) {
console.error(resp.statusCode);
console.error(resp.body);
throw new Error(
'record did not remove. check subdomain, api key, etc'
);
}
return true;
return null;
});
});
},
@ -142,20 +101,53 @@ module.exports.create = function(config) {
var ch = data.challenge;
return api(
'GET',
'domains/' + ch.dnsZone + '/records/TXT/' + ch.dnsPrefix
'/domains/' + ch.dnsZone + '/records/TXT/' + ch.dnsPrefix
).then(function(resp) {
resp = resp.body;
var entry = (resp.body || []).filter(function(x) {
return x.data === ch.dnsAuthorization;
})[0];
var entry = null;
if (resp.length) {
entry = resp.filter(function(x) {
return x.data === ch.dnsAuthorization;
})[0];
if (entry) {
return { dnsAuthorization: entry.data };
}
return entry ? { dnsAuthorization: entry.data } : null;
return null;
});
}
};
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'
);
}
});
}
};