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

169 lines
3.8 KiB
JavaScript

'use strict';
var request = require('@root/request');
request = require('util').promisify(request);
var defaults = {
baseUrl: 'https://api.vultr.com/'
};
module.exports.create = function(config) {
var baseUrl = config.baseUrl || defaults.baseUrl;
var apiKey = config.apiKey;
return {
zones: function(data) {
var url = baseUrl + 'v1/dns/list';
return request({
method: 'GET',
url: url,
headers: {
'API-Key': apiKey
},
json: true
}).then(function(resp) {
if (resp.statusCode == 200) {
return resp.body.map(function(x) {
return x.domain;
});
} else {
console.error(resp.statusCode);
console.error(resp.body);
throw new Error('Could not get list of zones. Check api key, etc');
}
});
},
set: function(data) {
var ch = data.challenge;
var txt = ch.dnsAuthorization;
// If the domain to be verified is
var url = baseUrl + 'v1/dns/create_record';
//console.debug('adding txt', data);
return request({
method: 'POST',
url: url,
headers: {
'API-Key': apiKey
},
form: {
type: 'TXT',
name: ch.dnsPrefix,
data: '"' + txt + '"', // vultr requires the TXT record wraped in quotes
domain: ch.dnsZone,
ttl: 300
}
}).then(function(resp) {
if (resp.statusCode == 200) {
return true;
} else {
console.error(resp.statusCode);
console.error(resp.body);
throw new Error('record did not set. check subdomain, api key, etc');
}
});
},
remove: function(data) {
var ch = data.challenge;
var url = baseUrl + 'v1/dns/records';
return request({
method: 'GET',
url: url + '?domain=' + ch.dnsZone,
json: true,
headers: {
'API-Key': apiKey
}
})
.then(function(resp) {
if (resp.statusCode == 200) {
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'];
} else {
throw new Error(
"Couldn't remove record. check subdomain, api key, etc"
);
}
} else {
throw new Error(
'record did not set. check subdomain, api key, etc'
);
}
})
.then(function(recordId) {
var url = baseUrl + 'v1/dns/delete_record';
return request({
method: 'POST',
url: url,
headers: {
'API-Key': apiKey
},
form: {
domain: ch.dnsZone,
RECORDID: recordId
}
}).then(function(resp) {
if (resp.statusCode == 200) {
return true;
} else {
console.error(resp.statusCode);
console.error(resp.body);
throw new Error(
'record did not remove. check subdomain, api key, etc'
);
}
});
});
},
get: function(data) {
var ch = data.challenge;
var url = baseUrl + 'v1/dns/records';
//console.debug('getting txt', data);
// 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
return request({
method: 'GET',
url: url + '?domain=' + ch.dnsZone,
json: true,
headers: {
'API-Key': apiKey
}
}).then(function(resp) {
resp = resp.body;
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 {
dnsAuthorization: entry.data.substring(1, entry.data.length - 1)
};
} else {
return null;
}
});
}
};
};