From c7cda58cc1f9096a6149d678273e41f583c899da Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Thu, 13 Jun 2019 13:32:42 -0600 Subject: [PATCH] update to list zones --- .gitignore | 2 ++ lib/index.js | 55 ++++++++++++++++++++++++++++------------------- package-lock.json | 28 ++++++++++++++++++++++++ package.json | 2 +- test.js | 30 ++++++-------------------- 5 files changed, 70 insertions(+), 47 deletions(-) create mode 100644 .gitignore create mode 100644 package-lock.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..407a14c --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*secret* +node_modules diff --git a/lib/index.js b/lib/index.js index 99f92fa..fff0ba8 100644 --- a/lib/index.js +++ b/lib/index.js @@ -11,16 +11,34 @@ 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 domainname = ch.identifier.value; - var zone = domainname; - var dnsPrefix = ch.dnsHost.replace(new RegExp('.' + zone + '$'), ''); var txt = ch.dnsAuthorization; // If the domain to be verified is var url = baseUrl + 'v1/dns/create_record'; - console.log('adding txt', data); + //console.debug('adding txt', data); return request({ method: 'POST', url: url, @@ -29,32 +47,28 @@ module.exports.create = function(config) { }, form: { type: 'TXT', - name: dnsPrefix, + name: ch.dnsPrefix, data: '"' + txt + '"', // vultr requires the TXT record wraped in quotes - domain: config.domain, + domain: ch.dnsZone, ttl: 300 } }).then(function(resp) { if (resp.statusCode == 200) { return true; } else { - console.log(resp.statusCode); - console.log(resp.body); + 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 domainname = data.challenge.altname; - var zone = domainname; - var url = baseUrl + 'v1/dns/records'; return request({ method: 'GET', - url: url + '?domain=' + config.domain, - // PROBLEM (fixed): Remember to set json: true (not need to JSON.parse) + url: url + '?domain=' + ch.dnsZone, json: true, headers: { 'API-Key': apiKey @@ -63,7 +77,7 @@ module.exports.create = function(config) { .then(function(resp) { if (resp.statusCode == 200) { resp = resp.body; - console.log(resp); + //console.debug(resp); var entries = resp.filter(function(x) { return x.type === 'TXT'; }); @@ -89,8 +103,6 @@ module.exports.create = function(config) { } }) .then(function(recordId) { - var domainname = data.challenge.altname; - var zone = domainname; var url = baseUrl + 'v1/dns/delete_record'; return request({ @@ -100,15 +112,15 @@ module.exports.create = function(config) { 'API-Key': apiKey }, form: { - domain: config.domain, + domain: ch.dnsZone, RECORDID: recordId } }).then(function(resp) { if (resp.statusCode == 200) { return true; } else { - console.log(resp.statusCode); - console.log(resp.body); + console.error(resp.statusCode); + console.error(resp.body); throw new Error( 'record did not remove. check subdomain, api key, etc' ); @@ -118,16 +130,15 @@ module.exports.create = function(config) { }, get: function(data) { var ch = data.challenge; - var domainname = data.challenge.altname; var url = baseUrl + 'v1/dns/records'; - console.log('getting txt', data); + //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=' + config.domain, + url: url + '?domain=' + ch.dnsZone, json: true, headers: { 'API-Key': apiKey diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..529ab68 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,28 @@ +{ + "name": "acme-dns-01-vultr", + "version": "3.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "@root/request": { + "version": "1.3.11", + "resolved": "https://registry.npmjs.org/@root/request/-/request-1.3.11.tgz", + "integrity": "sha512-3a4Eeghcjsfe6zh7EJ+ni1l8OK9Fz2wL1OjP4UCa0YdvtH39kdXB9RGWuzyNv7dZi0+Ffkc83KfH0WbPMiuJFw==" + }, + "acme-challenge-test": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/acme-challenge-test/-/acme-challenge-test-3.2.1.tgz", + "integrity": "sha512-8MwL2oWx7vM/SBIeEQfeoRyW0kYCtLFS4FfgIx3lsQmSKhbDo9J88Ud6DejdupRp2T+DlEkWIBVI3qOCVViUaQ==", + "dev": true + }, + "acme-dns-01-test": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/acme-dns-01-test/-/acme-dns-01-test-3.2.1.tgz", + "integrity": "sha512-m4UxltZzTNbQTK30iQQ6BQ99oRJA9p69+eg/u/Plxiw10bD+qsmRZR9rsqZuiSc62wfng/upGvXWMQZ/csn3lA==", + "dev": true, + "requires": { + "acme-challenge-test": "^3.2.0" + } + } + } +} diff --git a/package.json b/package.json index f2cdb01..049302b 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,6 @@ "@root/request": "^1.3.11" }, "devDependencies": { - "acme-challenge-test": "^3.1.2" + "acme-dns-01-test": "^3.2.1" } } diff --git a/test.js b/test.js index 8d5fb25..ca6de26 100644 --- a/test.js +++ b/test.js @@ -1,42 +1,24 @@ #!/usr/bin/env node 'use strict'; -// https://git.coolaj86.com/coolaj86/acme-challenge-test.js -var tester = require('acme-challenge-test'); +// https://git.rootprojects.org/root/acme-dns-01-test.js +var tester = require('acme-dns-01-test'); // Usage: node ./test.js example.com xxxxxxxxx var zone = process.argv[2]; var challenger = require('./index.js').create({ - apiKey: process.argv[3], - domain: zone + apiKey: process.argv[3] }); // The dry-run tests can pass on, literally, 'example.com' // but the integration tests require that you have control over the domain -var domain = zone; - tester - .test('dns-01', domain, challenger) + .testZone('dns-01', zone, challenger) .then(function() { - console.info('PASS', domain); - ///* - domain = 'foo.' + zone; - - return tester - .test('dns-01', domain, challenger) - .then(function() { - console.info('PASS', domain); - }) - .then(function() { - domain = '*.foo.' + zone; - - return tester.test('dns-01', domain, challenger).then(function() { - console.info('PASS', domain); - }); - }); - //*/ + console.info('PASS', zone); }) .catch(function(e) { + console.info('FAIL', zone); console.error(e.message); console.error(e.stack); });