From d80562fbf7be59a3ef5d950591d601477c2b6ef8 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Thu, 11 Jul 2019 22:20:52 -0600 Subject: [PATCH] v3.0.0: Gandi + Let's Encrypt for Node.js --- .gitignore | 2 + README.md | 29 +++++++------ lib/index.js | 103 +++++++++++++++++++++++++--------------------- package-lock.json | 29 +++++++++++++ package.json | 3 +- test.js | 1 + 6 files changed, 106 insertions(+), 61 deletions(-) create mode 100644 package-lock.json diff --git a/.gitignore b/.gitignore index 144585f..6f876b6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +.env + # ---> Node # Logs logs diff --git a/README.md b/README.md index ac3294b..bcf8610 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # [acme-dns-01-gandi.js](https://git.rootprojects.org/root/acme-dns-01-gandi.js) | a [Root](https://rootprojects.org/) project -###### Gandi LiveDNS + Let's Encrypt for Node.js - ACME dns-01 challenges w/ ACME.js and Greenlock.js +Gandi LiveDNS + Let's Encrypt for Node.js - ACME dns-01 challenges w/ ACME.js and Greenlock.js -###### This handles ACME dns-01 challenges, compatible with ACME.js and Greenlock.js. Passes acme-dns-01-test. +This handles ACME dns-01 challenges, compatible with ACME.js and Greenlock.js. Passes acme-dns-01-test. # Features @@ -17,18 +17,20 @@ # Install -`npm install --save acme-dns-01-gandi` +```js +npm install --save acme-dns-01-gandi +``` Generate Gandi LiveDNS API Token: -- Login to your account at: https://account.gandi.net/ -- Under the Security section, click the link next to 'Production API key' to generate a token. +- Login to your account at: https://account.gandi.net/ +- Under the Security section, click the link next to 'Production API key' to generate a token. # Usage First you create an instance with your credentials: -``` +```js var dns01 = require('acme-dns-01-gandi').create({ baseUrl: 'https://dns.api.gandi.net/api/v5/', // default token: 'xxxx' @@ -39,7 +41,7 @@ Then you can use it with any compatible ACME library, such as Greenlock.js or AC ## Greenlock.js -``` +```js var Greenlock = require('greenlock-express'); var greenlock = Greenlock.create({ challenges: { @@ -53,10 +55,8 @@ See [Greenlock Express](https://git.rootprojects.org/root/greenlock-express.js) ## ACME.js -``` - +```js // TODO - ``` See the [ACME.js](https://git.rootprojects.org/root/acme-v2.js) for more details. @@ -71,7 +71,7 @@ There are only 5 methods: - `get(opts)` - `remove(opts)` -``` +```js dns01 .set({ identifier: { value: 'foo.example.co.uk' }, @@ -92,7 +92,7 @@ See acme-dns-01-test for more implementation details. # Tests -``` +```bash # node ./test.js domain-zone api-token node ./test.js example.com xxxxxx ``` @@ -101,10 +101,13 @@ node ./test.js example.com xxxxxx - Jarom Bridges - AJ ONeal - See AUTHORS for contact info. + +See AUTHORS for contact info. # Legal [acme-dns-01-gandi.js](https://git.coolaj86.com/coolaj86/acme-dns-01-gandi.js) | MPL-2.0 | [Terms of Use](https://therootcompany.com/legal/#terms) | [Privacy Policy](https://therootcompany.com/legal/#privacy) +Copyright 2019 Jarom Bridges +Copyright 2019 AJ ONeal Copyright 2019 The Root Group LLC diff --git a/lib/index.js b/lib/index.js index a0d57ec..2e39dc4 100644 --- a/lib/index.js +++ b/lib/index.js @@ -16,8 +16,7 @@ module.exports.create = function(config) { }, zones: function(opts) { - console.log(opts); - + //console.log(opts); return request({ method: 'GET', url: baseUrl + '/zones', @@ -33,8 +32,7 @@ module.exports.create = function(config) { }, set: function(opts) { - console.log(opts); - + //console.log(opts); return request({ method: 'GET', url: @@ -49,7 +47,7 @@ module.exports.create = function(config) { }, json: true }).then(function(resp) { - if (resp.body.cause === 'Not Found') { + function create() { return request({ method: 'POST', url: @@ -67,38 +65,49 @@ module.exports.create = function(config) { rrset_values: [opts.challenge.dnsAuthorization] } }); - } else { - const body = resp.body; - let value = body.rrset_values.map(x => JSON.parse(x)); + } - if (body.rrset_values) { - return request({ - method: 'PUT', - url: - baseUrl + - '/domains/' + - opts.challenge.dnsZone + - '/records/' + - opts.challenge.dnsPrefix + - '/TXT', - headers: { - 'X-Api-Key': authtoken - }, - json: { - rrset_ttl: 300, - rrset_values: value.concat([ - opts.challenge.dnsAuthorization - ]) - } - }); + function replace() { + var body = resp.body; + var value = body.rrset_values.map(function(x) { + return JSON.parse(x); + }); + + if (!body.rrset_values) { + return null; } + + return request({ + method: 'PUT', + url: + baseUrl + + '/domains/' + + opts.challenge.dnsZone + + '/records/' + + opts.challenge.dnsPrefix + + '/TXT', + headers: { + 'X-Api-Key': authtoken + }, + json: { + rrset_ttl: 300, + rrset_values: value.concat([ + opts.challenge.dnsAuthorization + ]) + } + }); + } + + if (resp.body.cause === 'Not Found') { + return create(); + } else { + return replace(); } }); }, remove: function(opts) { - console.log(opts); - + //console.log(opts); return request({ method: 'DELETE', url: @@ -116,8 +125,7 @@ module.exports.create = function(config) { }, get: function(opts) { - console.log(opts); - + //console.log(opts); return request({ method: 'GET', url: @@ -131,23 +139,24 @@ module.exports.create = function(config) { }, json: true }).then(function(resp) { - const body = resp.body; - if (body.length > 0) { - let value = body[0].rrset_values - .map(x => JSON.parse(x)) - .filter( - field => field === opts.challenge.dnsAuthorization - ); - if (value !== []) { - return { - dnsAuthorization: value[0] - }; - } else { - return null; - } - } else { + var body = resp.body; + if (!(body.length > 0)) { return null; } + + var value = body[0].rrset_values + .map(function(x) { + return JSON.parse(x); + }) + .filter(function(field) { + return field === opts.challenge.dnsAuthorization; + })[0]; + if (!value) { + return null; + } + return { + dnsAuthorization: value + }; }); } }; diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..2e15465 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,29 @@ +{ + "name": "acme-dns-01-gandi", + "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==", + "dev": true + }, + "acme-challenge-test": { + "version": "3.3.2", + "resolved": "https://registry.npmjs.org/acme-challenge-test/-/acme-challenge-test-3.3.2.tgz", + "integrity": "sha512-0AbMcaON20wpI5vzFDAqwcv2VerY4xIlNCqX0w1xEJUIu/EQtQNmkje+rKNuy2TUl2KBMdIaR6YBbJUdaEiC4w==", + "dev": true, + "requires": { + "@root/request": "^1.3.11" + } + }, + "dotenv": { + "version": "8.0.0", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.0.0.tgz", + "integrity": "sha512-30xVGqjLjiUOArT4+M5q9sYdvuR4riM6yK9wMcas9Vbp6zZa+ocC9dp6QoftuhTPhFAiLK/0C5Ni2nou/Bk8lg==", + "dev": true + } + } +} diff --git a/package.json b/package.json index 1542047..8eb3c21 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "acme-dns-01-gandi", - "version": "0.0.1", + "version": "3.0.0", "description": "Gandi + Let's Encrypt for Node.js - ACME dns-01 challenges w/ ACME.js and Greenlock.js", "main": "index.js", "scripts": { @@ -22,6 +22,7 @@ "author": "AJ ONeal (https://coolaj86.com/)", "license": "MPL-2.0", "devDependencies": { + "acme-challenge-test": "^3.3.2", "dotenv": "^8.0.0" } } diff --git a/test.js b/test.js index b794bdf..d8b9412 100755 --- a/test.js +++ b/test.js @@ -3,6 +3,7 @@ // See https://git.coolaj86.com/coolaj86/acme-challenge-test.js var tester = require('acme-challenge-test'); +require('dotenv').config(); // Usage: node ./test.js example.com xxxxxxxxx var zone = process.argv[2] || process.env.ZONE; -- 2.38.5