diff --git a/README.md b/README.md index 65d1ee5..68147bd 100644 --- a/README.md +++ b/README.md @@ -30,11 +30,10 @@ First you create an instance with your credentials: ```js var http01 = require('acme-http-01-s3').create({ - awsAccessKey: process.env., - awsSecretAccessKey: process.env., - awsRegion: process.env., - awsBucket: process.env., - awsHost:process.env. + awsAccessKey: process.env.AWS_ACCESS_KEY, + awsSecretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, + awsRegion: process.env.AWS_REGION, + awsBucket: process.env.AWS_BUCKET }); ``` diff --git a/example.env b/example.env index 2625abd..489c3d2 100644 --- a/example.env +++ b/example.env @@ -1,9 +1,13 @@ RECORD=example.co.uk -awsAccessKey='' -awsSecretAccessKey='' -awsRegion='' -awsBucket='' -awsHost='' +AWS_ACCESS_KEY='xx' +AWS_SECRET_ACCESS_KEY='xx' + +# default us-east-1 +AWS_REGION='us-east-1' +AWS_BUCKET='xx' + +# default s3.[AWS_REGION].amazonaws.com +AWS_HOST='xx' diff --git a/lib/index.js b/lib/index.js index 8b1b8ce..6152e9b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -5,42 +5,42 @@ request = promisify(request); var aws4 = require('aws4'); var defaults = { - awsAccessKey: 'TQ6UOPROD626SWV5KIP5', - awsSecretAccessKey: 'Tm3dNht5QMKxu7EU44ZLflDeE7sFbqQy3Q+XE+MY', awsRegion: 'us-east-1', - awsBucket: 'hy8', - awsHost: 's3.switch.lxc' }; module.exports.create = function(config) { - var awsAccessKey = config.awsAccessKey || defaults.awsAccessKey; - var awsSecretAccessKey = config.awsSecretAccessKey || defaults.awsSecretAccessKey; + var awsAccessKey = config.awsAccessKey || null; + var awsSecretAccessKey = config.awsSecretAccessKey || null; var awsRegion = config.awsRegion || defaults.awsRegion; - var awsBucket = config.awsBucket || defaults.awsBucket; - var awsHost = config.awsHost || defaults.awsHost; + var awsBucket = config.awsBucket || null; + var awsHost = config.awsHost || 's3.'+awsRegion+'.amazonaws.com'; + if (awsAccessKey===null||awsSecretAccessKey===null){ + throw new Error('awsAccessKey and awsSecretAccessKey are required'); + } var AWSCredentials = { accessKeyId: awsAccessKey, secretAccessKey: awsSecretAccessKey }; + if (awsBucket===null){ + throw new Error('awsBucket is required'); + } + return { init: function(opts) { - console.log('init'); return null; }, set: function(data) { console.log('Add Key Auth URL'); var ch = data.challenge; - var signed = aws4.sign({ host: awsHost, service: 's3', region: awsRegion, path: '/' + awsBucket + '/' + ch.identifier.value + '/' + ch.token, - headers: { 'Content-Type': 'text/plain;charset=UTF-8' }, @@ -50,11 +50,10 @@ module.exports.create = function(config) { }, AWSCredentials ); - console.log(signed); return request({ // debug: true, method: 'PUT', - url: 'http://' + signed.host + signed.path, + url: 'https://' + signed.host + signed.path, headers: { 'Content-Type': 'text/plain;charset=UTF-8' }, body: ch.keyAuthorization @@ -67,16 +66,14 @@ module.exports.create = function(config) { } return true; }) - .catch(function(err) { - throw err; - // return null; - }); + .catch(function(err) { + throw err; + // return null; + }); }, get: function(data) { console.log('List Key Auth URL'); - var ch = data.challenge; - var signed = aws4.sign({ host: awsHost, service: 's3', @@ -90,18 +87,18 @@ module.exports.create = function(config) { }, AWSCredentials ); - console.log(signed); + return request({ // debug: true, method: 'GET', - url: 'http://' + signed.host + signed.path, + url: 'https://' + signed.host + signed.path, headers: { 'Content-Type': 'text/plain;charset=UTF-8' } }).then(function(resp) { if (200 === resp.statusCode) { return { keyAuthorization: resp.body }; - }else if (404 === resp.statusCode){ + } else if (404 === resp.statusCode) { return null; } // wrong sign returns 403 @@ -130,11 +127,10 @@ module.exports.create = function(config) { }, AWSCredentials ); - console.log(signed); return request({ // debug: true, method: 'DELETE', - url: 'http://' + signed.host + signed.path + url: 'https://' + signed.host + signed.path }).then(function(resp) { // console.log(resp.statusCode); if (204 !== resp.statusCode) { @@ -144,10 +140,10 @@ module.exports.create = function(config) { } return true; }) - .catch(function(err) { - throw err; - // return null; - }); + .catch(function(err) { + throw err; + // return null; + }); } }; diff --git a/test.js b/test.js index 5c5e07e..f42e069 100755 --- a/test.js +++ b/test.js @@ -8,10 +8,10 @@ require('dotenv').config(); // Usage: node ./test.js example.com username xxxxxxxxx var record = process.argv[2] || process.env.RECORD; var challenger = require('./index.js').create({ - databaseUrl: process.env.DATABASE_URL, - tableName: process.env.TABLE_NAME, - dialect: process.env.DIALECT, - storage: process.env.STORAGE + awsAccessKey: process.env.AWS_ACCESS_KEY, + awsSecretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, + awsRegion: process.env.AWS_REGION, + awsBucket: process.env.AWS_BUCKET }); // The dry-run tests can pass on, literally, 'example.com'