From 16db087c19fff5a37dcfe8c03b50fe5598aa2fb7 Mon Sep 17 00:00:00 2001 From: Hitesh Date: Thu, 25 Jul 2019 08:56:01 -0700 Subject: [PATCH 1/5] integration tests passed --- AUTHORS | 1 + example.env | 3 -- lib/index.js | 111 ++++++++++++++++++++++++++++++++++++++++++++++----- test.js | 7 ++-- 4 files changed, 106 insertions(+), 16 deletions(-) delete mode 100644 example.env diff --git a/AUTHORS b/AUTHORS index f2496e6..6966d87 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1 +1,2 @@ AJ ONeal (https://coolaj86.com/) +Hitesh Walia \ No newline at end of file diff --git a/example.env b/example.env deleted file mode 100644 index e053b84..0000000 --- a/example.env +++ /dev/null @@ -1,3 +0,0 @@ -ZONE=example.co.uk -USERNAME=johndoe -PASSWORD=xxxxxxxx diff --git a/lib/index.js b/lib/index.js index e25856a..4e62c9b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,29 +1,120 @@ 'use strict'; var request; -var defaults = {}; +var defaults = { + baseUrl: 'https://api.dnsimple.com/v2/' +}; + module.exports.create = function(config) { + var baseUrl = (config.baseUrl || defaults.baseUrl).replace(/\/$/, ''); + var token = config.token; + var account = config.account; + return { init: function(opts) { request = opts.request; return null; }, zones: function(data) { - //console.info('List Zones', data); - throw Error('listing zones not implemented'); + // console.info('List Zones', data); + return api('GET', '/' + account + '/zones') + .then(function(resp) { + return resp['body']['data'].map(function(elem) { + // console.log('DEBUG >>> elem.name: ' + elem.name); + return elem.name; + }); + }); + + }, set: function(data) { // console.info('Add TXT', data); - throw Error('setting TXT not implemented'); - }, - remove: function(data) { - // console.info('Remove TXT', data); - throw Error('removing TXT not implemented'); + var ch = data.challenge; + //console.log('DEBUG >>> ch: ' + JSON.stringify(ch, null, 2)); + var txt = ch.dnsAuthorization; + //console.log('DEBUG >>> txt: ' + txt); + + return api('POST', '/' + account + '/zones/' + ch.dnsZone + '/records', { + name: '', + type: 'TXT', + content: txt + }).then(function(resp) { + //console.log('DEBUG >>> resp: ' + JSON.stringify(resp, null, 2)); + + if (resp.statusCode === 201) { + return true; + } + throw new Error('record did not set. check subdomain, api key, etc'); + + }); }, get: function(data) { // console.info('List TXT', data); - throw Error('listing TXTs not implemented'); + + return api('GET', '/' + account + '/zones/' + data.challenge.dnsZone + '/records') + .then(function(resp) { + + var txtRecord = resp.body.data.filter(function (record) { + return data.challenge.dnsAuthorization === record.content; + })[0]; + + //console.log('DEBUG >>> txtRecord: ' + JSON.stringify(txtRecord,null,2)); + if(txtRecord) return { dnsAuthorization: txtRecord.content }; + else return null; + + }); + + }, + remove: function(data) { + + return api('GET', '/' + account + '/zones/' + data.challenge.dnsZone + '/records') + .then(function(resp) { + + var record = resp.body.data.filter(function (record) { + return data.challenge.dnsAuthorization === record.content; + })[0]; + + if(record) { + return api('DELETE', '/' + account + '/zones/' + data.challenge.dnsZone + '/records/' + record.id) + .then(function(resp) { + // console.info('DEBUG >>> resp: ', JSON.stringify(resp, null, 2)); + return true; + }); + } + else { + throw new Error('Txt Record not found for removal'); + } + }); } - }; + } + + // Authentication and Error handling here + function api(method, path, form) { + var req = { + method: method, + url: baseUrl + path, + headers: { + Authorization: 'Bearer ' + token, + 'Content-Type': 'application/json' + }, + json: true, + form: form + }; + return request(req).then(function(resp) { + if (2 !== Math.floor(resp.statusCode / 100)) { + console.error(resp.statusCode, req.url); + console.error(); + console.error('Request:'); + console.error(req); + console.error(); + console.error('Response:'); + console.error(resp.body); + console.error(); + throw new Error('Error response. Check token, baseUrl, domains, etc.'); + } + return resp; + }); + } + }; diff --git a/test.js b/test.js index a459da9..ca647ba 100755 --- a/test.js +++ b/test.js @@ -5,11 +5,12 @@ var tester = require('acme-challenge-test'); require('dotenv').config(); -// Usage: node ./test.js example.com username xxxxxxxxx +// Usage: node ./test.js example.com token account var zone = process.argv[2] || process.env.ZONE; + var challenger = require('./index.js').create({ - username: process.argv[3] || process.env.USERNAME, - password: process.argv[4] || process.env.PASSWORD + token: process.argv[3] || process.env.TOKEN, + account: process.argv[4] || process.env.ACCOUNT }); // The dry-run tests can pass on, literally, 'example.com' From 477e3dba33d0a1a5f7c803e793f6918b3cd7d892 Mon Sep 17 00:00:00 2001 From: Hitesh Walia Date: Thu, 25 Jul 2019 09:06:56 -0700 Subject: [PATCH 2/5] integration tests passed --- lib/index.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/index.js b/lib/index.js index 4e62c9b..5c71abb 100644 --- a/lib/index.js +++ b/lib/index.js @@ -25,8 +25,6 @@ module.exports.create = function(config) { return elem.name; }); }); - - }, set: function(data) { // console.info('Add TXT', data); @@ -55,12 +53,11 @@ module.exports.create = function(config) { return api('GET', '/' + account + '/zones/' + data.challenge.dnsZone + '/records') .then(function(resp) { - var txtRecord = resp.body.data.filter(function (record) { + var record = resp.body.data.filter(function (record) { return data.challenge.dnsAuthorization === record.content; })[0]; - //console.log('DEBUG >>> txtRecord: ' + JSON.stringify(txtRecord,null,2)); - if(txtRecord) return { dnsAuthorization: txtRecord.content }; + if(txtRecord) return { dnsAuthorization: record.content }; else return null; }); From e1c9c0ef08f2be5d59ae1e41d94555c1994a9cb6 Mon Sep 17 00:00:00 2001 From: Hitesh Walia Date: Thu, 25 Jul 2019 09:13:59 -0700 Subject: [PATCH 3/5] integration tests success --- AUTHORS | 1 - example.env | 3 +++ lib/index.js | 72 ++++++++++++---------------------------------------- test.js | 7 +++-- 4 files changed, 22 insertions(+), 61 deletions(-) create mode 100644 example.env diff --git a/AUTHORS b/AUTHORS index 6966d87..f2496e6 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1,2 +1 @@ AJ ONeal (https://coolaj86.com/) -Hitesh Walia \ No newline at end of file diff --git a/example.env b/example.env new file mode 100644 index 0000000..e053b84 --- /dev/null +++ b/example.env @@ -0,0 +1,3 @@ +ZONE=example.co.uk +USERNAME=johndoe +PASSWORD=xxxxxxxx diff --git a/lib/index.js b/lib/index.js index 5c71abb..9bebbc3 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,22 +1,16 @@ 'use strict'; var request; -var defaults = { - baseUrl: 'https://api.dnsimple.com/v2/' -}; - +var defaults = {}; module.exports.create = function(config) { - var baseUrl = (config.baseUrl || defaults.baseUrl).replace(/\/$/, ''); - var token = config.token; - var account = config.account; - return { init: function(opts) { request = opts.request; return null; }, zones: function(data) { +<<<<<<< HEAD // console.info('List Zones', data); return api('GET', '/' + account + '/zones') .then(function(resp) { @@ -25,30 +19,22 @@ module.exports.create = function(config) { return elem.name; }); }); +======= + //console.info('List Zones', data); + throw Error('listing zones not implemented'); +>>>>>>> parent of 16db087... integration tests passed }, set: function(data) { // console.info('Add TXT', data); - var ch = data.challenge; - //console.log('DEBUG >>> ch: ' + JSON.stringify(ch, null, 2)); - var txt = ch.dnsAuthorization; - //console.log('DEBUG >>> txt: ' + txt); - - return api('POST', '/' + account + '/zones/' + ch.dnsZone + '/records', { - name: '', - type: 'TXT', - content: txt - }).then(function(resp) { - //console.log('DEBUG >>> resp: ' + JSON.stringify(resp, null, 2)); - - if (resp.statusCode === 201) { - return true; - } - throw new Error('record did not set. check subdomain, api key, etc'); - - }); + throw Error('setting TXT not implemented'); + }, + remove: function(data) { + // console.info('Remove TXT', data); + throw Error('removing TXT not implemented'); }, get: function(data) { // console.info('List TXT', data); +<<<<<<< HEAD return api('GET', '/' + account + '/zones/' + data.challenge.dnsZone + '/records') .then(function(resp) { @@ -83,35 +69,9 @@ module.exports.create = function(config) { throw new Error('Txt Record not found for removal'); } }); +======= + throw Error('listing TXTs not implemented'); +>>>>>>> parent of 16db087... integration tests passed } - } - - // Authentication and Error handling here - function api(method, path, form) { - var req = { - method: method, - url: baseUrl + path, - headers: { - Authorization: 'Bearer ' + token, - 'Content-Type': 'application/json' - }, - json: true, - form: form - }; - return request(req).then(function(resp) { - if (2 !== Math.floor(resp.statusCode / 100)) { - console.error(resp.statusCode, req.url); - console.error(); - console.error('Request:'); - console.error(req); - console.error(); - console.error('Response:'); - console.error(resp.body); - console.error(); - throw new Error('Error response. Check token, baseUrl, domains, etc.'); - } - return resp; - }); - } - + }; }; diff --git a/test.js b/test.js index ca647ba..a459da9 100755 --- a/test.js +++ b/test.js @@ -5,12 +5,11 @@ var tester = require('acme-challenge-test'); require('dotenv').config(); -// Usage: node ./test.js example.com token account +// Usage: node ./test.js example.com username xxxxxxxxx var zone = process.argv[2] || process.env.ZONE; - var challenger = require('./index.js').create({ - token: process.argv[3] || process.env.TOKEN, - account: process.argv[4] || process.env.ACCOUNT + username: process.argv[3] || process.env.USERNAME, + password: process.argv[4] || process.env.PASSWORD }); // The dry-run tests can pass on, literally, 'example.com' From 0dac689012d448eb3590fa9264b8768596fdcd9f Mon Sep 17 00:00:00 2001 From: Hitesh Date: Fri, 26 Jul 2019 21:47:54 -0700 Subject: [PATCH 4/5] format code --- lib/index.js | 142 +++++++++++++++++++++++++++++++++------------------ test.js | 6 +-- 2 files changed, 96 insertions(+), 52 deletions(-) diff --git a/lib/index.js b/lib/index.js index 9bebbc3..817e132 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,77 +1,121 @@ 'use strict'; var request; -var defaults = {}; +var defaults = { + baseUrl: 'https://api.dnsimple.com/v2/' +}; module.exports.create = function(config) { + var baseUrl = (config.baseUrl || defaults.baseUrl).replace(/\/$/, ''); + var token = config.token; + var account = config.account; + + function api(method, path, form) { + var req = { + method: method, + url: baseUrl + path, + headers: { + Authorization: 'Bearer ' + token, + 'Content-Type': 'application/json' + }, + json: true, + form: form + }; + return request(req).then(function(resp) { + if (2 !== Math.floor(resp.statusCode / 100)) { + console.error(resp.statusCode, req.url); + console.error(); + console.error('Request:'); + console.error(req); + console.error(); + console.error('Response:'); + console.error(resp.body); + console.error(); + throw new Error( + 'Error response. Check token, baseUrl, domains, etc.' + ); + } + return resp; + }); + } + return { init: function(opts) { request = opts.request; return null; }, zones: function(data) { -<<<<<<< HEAD // console.info('List Zones', data); - return api('GET', '/' + account + '/zones') - .then(function(resp) { - return resp['body']['data'].map(function(elem) { - // console.log('DEBUG >>> elem.name: ' + elem.name); - return elem.name; + return api('GET', '/' + account + '/zones').then(function(resp) { + return resp['body']['data'].map(function(elem) { + //console.log('DEBUG >>> elem.name: ' + elem.name); + return elem.name; }); }); -======= - //console.info('List Zones', data); - throw Error('listing zones not implemented'); ->>>>>>> parent of 16db087... integration tests passed }, set: function(data) { // console.info('Add TXT', data); - throw Error('setting TXT not implemented'); - }, - remove: function(data) { - // console.info('Remove TXT', data); - throw Error('removing TXT not implemented'); + var ch = data.challenge; + var txt = ch.dnsAuthorization; + + return api( + 'POST', + '/' + account + '/zones/' + ch.dnsZone + '/records', + { + name: '', + type: 'TXT', + content: txt + } + ).then(function(resp) { + if (resp.statusCode === 201) { + return true; + } + throw new Error( + 'record did not set. check subdomain, api key, etc' + ); + }); }, get: function(data) { // console.info('List TXT', data); -<<<<<<< HEAD - - return api('GET', '/' + account + '/zones/' + data.challenge.dnsZone + '/records') - .then(function(resp) { - var record = resp.body.data.filter(function (record) { - return data.challenge.dnsAuthorization === record.content; - })[0]; - - if(txtRecord) return { dnsAuthorization: record.content }; - else return null; - - }); + return api( + 'GET', + '/' + account + '/zones/' + data.challenge.dnsZone + '/records' + ).then(function(resp) { + var record = resp.body.data.filter(function(record) { + return data.challenge.dnsAuthorization === record.content; + })[0]; + if (record) return { dnsAuthorization: record.content }; + else return null; + }); }, remove: function(data) { + return api( + 'GET', + '/' + account + '/zones/' + data.challenge.dnsZone + '/records' + ).then(function(resp) { + var record = resp.body.data.filter(function(record) { + return data.challenge.dnsAuthorization === record.content; + })[0]; - return api('GET', '/' + account + '/zones/' + data.challenge.dnsZone + '/records') - .then(function(resp) { - - var record = resp.body.data.filter(function (record) { - return data.challenge.dnsAuthorization === record.content; - })[0]; - - if(record) { - return api('DELETE', '/' + account + '/zones/' + data.challenge.dnsZone + '/records/' + record.id) - .then(function(resp) { - // console.info('DEBUG >>> resp: ', JSON.stringify(resp, null, 2)); - return true; - }); - } - else { - throw new Error('Txt Record not found for removal'); - } - }); -======= - throw Error('listing TXTs not implemented'); ->>>>>>> parent of 16db087... integration tests passed + if (record) { + return api( + 'DELETE', + '/' + + account + + '/zones/' + + data.challenge.dnsZone + + '/records/' + + record.id + ).then(function(resp) { + // console.info('DEBUG >>> resp: ', JSON.stringify(resp, null, 2)); + return true; + }); + } else { + throw new Error('Txt Record not found for removal'); + } + }); } }; }; diff --git a/test.js b/test.js index a459da9..b0accf2 100755 --- a/test.js +++ b/test.js @@ -5,11 +5,11 @@ var tester = require('acme-challenge-test'); require('dotenv').config(); -// Usage: node ./test.js example.com username xxxxxxxxx +// Usage: node ./test.js example.com token account var zone = process.argv[2] || process.env.ZONE; var challenger = require('./index.js').create({ - username: process.argv[3] || process.env.USERNAME, - password: process.argv[4] || process.env.PASSWORD + token: process.argv[3] || process.env.TOKEN, + account: process.argv[4] || process.env.ACCOUNT }); // The dry-run tests can pass on, literally, 'example.com' From b385cb2c8f3c13c0514bfb891c0702b102495305 Mon Sep 17 00:00:00 2001 From: Hitesh Date: Fri, 26 Jul 2019 21:54:32 -0700 Subject: [PATCH 5/5] update authors --- AUTHORS | 1 + README.md | 1 + package.json | 2 +- 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index f2496e6..6966d87 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1 +1,2 @@ AJ ONeal (https://coolaj86.com/) +Hitesh Walia \ No newline at end of file diff --git a/README.md b/README.md index 4dbd414..cac0a4e 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,7 @@ node ./test.js example.com johndoe xxxxxx # Authors - AJ ONeal +- Hitesh Walia See AUTHORS for contact info. diff --git a/package.json b/package.json index 4b3e067..b55ba41 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "acme", "greenlock" ], - "author": "AJ ONeal (https://coolaj86.com/)", + "author": "AJ ONeal (https://coolaj86.com/), Hitesh Walia