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

141 lines
3.3 KiB
JavaScript
Raw Permalink Normal View History

2019-06-10 13:54:39 +00:00
'use strict';
var request = require('@root/request');
request = require('util').promisify(request);
var defaults = {
2019-06-13 19:51:01 +00:00
baseUrl: 'https://api.vultr.com/v1/dns'
2019-06-10 13:54:39 +00:00
};
module.exports.create = function(config) {
2019-06-13 19:51:01 +00:00
var baseUrl = (config.baseUrl || defaults.baseUrl).replace(/\/$/, '');
2019-06-10 13:54:39 +00:00
var apiKey = config.apiKey;
2019-06-13 19:51:01 +00:00
function api(method, path, form) {
return request({
method: method,
url: baseUrl + path,
headers: {
'API-Key': apiKey
},
json: true,
form: form
});
}
2019-06-10 13:54:39 +00:00
return {
2019-06-13 19:32:42 +00:00
zones: function(data) {
2019-06-13 19:51:01 +00:00
return api('GET', '/list').then(function(resp) {
if (200 !== resp.statusCode) {
2019-06-13 19:32:42 +00:00
console.error(resp.statusCode);
console.error(resp.body);
throw new Error('Could not get list of zones. Check api key, etc');
}
2019-06-13 19:51:01 +00:00
return resp.body.map(function(x) {
return x.domain;
});
2019-06-13 19:32:42 +00:00
});
},
2019-06-10 13:54:39 +00:00
set: function(data) {
var ch = data.challenge;
var txt = ch.dnsAuthorization;
// If the domain to be verified is
2019-06-13 19:32:42 +00:00
//console.debug('adding txt', data);
2019-06-13 19:51:01 +00:00
return api('POST', '/create_record', {
type: 'TXT',
name: ch.dnsPrefix,
data: '"' + txt + '"', // vultr requires the TXT record wraped in quotes
domain: ch.dnsZone,
ttl: 300
2019-06-10 13:54:39 +00:00
}).then(function(resp) {
2019-06-13 19:51:01 +00:00
if (200 !== resp.statusCode) {
2019-06-13 19:32:42 +00:00
console.error(resp.statusCode);
console.error(resp.body);
2019-06-10 13:54:39 +00:00
throw new Error('record did not set. check subdomain, api key, etc');
}
2019-06-13 19:51:01 +00:00
return true;
2019-06-10 13:54:39 +00:00
});
},
remove: function(data) {
var ch = data.challenge;
2019-06-13 19:51:01 +00:00
return api('GET', '/records?domain=' + ch.dnsZone)
2019-06-13 09:53:54 +00:00
.then(function(resp) {
2019-06-13 19:51:01 +00:00
if (200 !== resp.statusCode) {
throw new Error(
'record did not set. check subdomain, api key, etc'
);
}
2019-06-10 13:54:39 +00:00
2019-06-13 19:51:01 +00:00
resp = resp.body;
//console.debug(resp);
var entries = resp.filter(function(x) {
return x.type === 'TXT';
});
var entry = entries.filter(function(x) {
// vultr wraps the TXT record in double quotes
return (
x.data.substring(1, x.data.length - 1) === ch.dnsAuthorization
);
})[0];
if (entry) {
return entry.RECORDID;
2019-06-10 13:54:39 +00:00
} else {
throw new Error(
2019-06-13 19:51:01 +00:00
"Couldn't remove record. check subdomain, api key, etc"
2019-06-10 13:54:39 +00:00
);
}
})
.then(function(recordId) {
2019-06-13 19:51:01 +00:00
return api('POST', '/delete_record', {
domain: ch.dnsZone,
RECORDID: recordId
2019-06-10 13:54:39 +00:00
}).then(function(resp) {
2019-06-13 19:51:01 +00:00
if (200 !== resp.statusCode) {
2019-06-13 19:32:42 +00:00
console.error(resp.statusCode);
console.error(resp.body);
2019-06-13 09:53:54 +00:00
throw new Error(
'record did not remove. check subdomain, api key, etc'
);
2019-06-10 13:54:39 +00:00
}
2019-06-13 19:51:01 +00:00
return true;
2019-06-10 13:54:39 +00:00
});
});
},
get: function(data) {
var ch = data.challenge;
2019-06-13 19:32:42 +00:00
//console.debug('getting txt', data);
2019-06-10 13:54:39 +00:00
// Digital ocean provides the api to fetch records by ID. Since we do not have id, we fetch all the records,
// filter the required TXT record
2019-06-13 19:51:01 +00:00
return api('GET', '/records?domain=' + ch.dnsZone).then(function(resp) {
2019-06-10 13:54:39 +00:00
resp = resp.body;
var entries = resp.filter(function(x) {
2019-06-13 09:53:54 +00:00
return x.type === 'TXT';
2019-06-10 13:54:39 +00:00
});
var entry = entries.filter(function(x) {
// vultr wraps the TXT record in double quotes
return x.data.substring(1, x.data.length - 1) === ch.dnsAuthorization;
})[0];
if (entry) {
2019-06-13 09:53:54 +00:00
return {
dnsAuthorization: entry.data.substring(1, entry.data.length - 1)
};
2019-06-10 13:54:39 +00:00
} else {
2019-06-13 09:53:54 +00:00
return null;
2019-06-10 13:54:39 +00:00
}
});
}
};
};