wip: set zone record

This commit is contained in:
AJ ONeal 2019-07-23 21:50:30 -06:00
parent 564a9d3e20
commit 68b91a27a2
3 changed files with 151 additions and 96 deletions

17
.jshintrc Normal file
View File

@ -0,0 +1,17 @@
{ "node": true
, "browser": true
, "jquery": true
, "globals": { "Promise": true }
, "indent": 2
, "onevar": true
, "laxcomma": true
, "laxbreak": true
, "curly": true
, "nonbsp": true
, "eqeqeq": true
, "immed": true
, "undef": true
, "latedef": "nofunc"
}

17
.prettierrc Normal file
View File

@ -0,0 +1,17 @@
{ "node": true
, "browser": true
, "jquery": true
, "globals": { "Promise": true }
, "indent": 2
, "onevar": true
, "laxcomma": true
, "laxbreak": true
, "curly": true
, "nonbsp": true
, "eqeqeq": true
, "immed": true
, "undef": true
, "latedef": "nofunc"
}

View File

@ -1,107 +1,128 @@
'use strict'; "use strict";
//var auth = require('./auth.js'); //var auth = require('./auth.js');
var defaults = { var defaults = {
baseUrl: 'https://www.googleapis.com/dns/v1/' baseUrl: "https://www.googleapis.com/dns/v1/"
}; };
module.exports.create = function(config) { module.exports.create = function(config) {
var request; var request;
var baseUrl = (config.baseUrl || defaults.baseUrl).replace(/\/$/, ''); var baseUrl = (config.baseUrl || defaults.baseUrl).replace(/\/$/, "");
var token = config.token; var token = config.token;
var sa = getServiceAccount(config); var sa = getServiceAccount(config);
return { var plugin = {
init: function(opts) { init: function(opts) {
request = opts.request; request = opts.request;
return null; return null;
}, },
zones: function(data) { zones: function(data) {
//console.info('List Zones', data); //console.info('List Zones', data);
return api({ return api({
url: baseUrl + '/projects/' + sa.project_id + '/managedZones', url: baseUrl + "/projects/" + sa.project_id + "/managedZones",
json: true json: true
}).then(function(resp) { }).then(function(resp) {
return resp.body.managedZones.map(function(zone) { return resp.body.managedZones.map(function(zone) {
// slice out the leading and trailing single quotes, and the trailing dot // slice out the trailing dot.
// (assuming that all 'dnsName's probably look the same) // ex: "example.com." => "example.com"
var name = zone.dnsName.slice(0, zone.dnsName.length - 1); // (assuming that all 'dnsName's probably look the same)
console.log(`the is name ${name}`); var name = zone.dnsName.slice(0, zone.dnsName.length - 1);
return name; console.log("the is name", name);
}); return name;
}); });
}, });
set: function(data) { },
console.info('Add TXT', data); set: function(data) {
var ch = data.challenge; console.info("Add TXT", data);
return api({ var ch = data.challenge;
method: 'POST', return getZoneNameByDnsName(ch.dnsZone)
url: baseUrl + '/projects/' + sa.project_id + '/managedZones/' + ch.dnsZone + '/changes', .then(function(gZoneName) {
json: { return api({
"kind": "dns#change", method: "POST",
"additions": [ url:
{ baseUrl +
"kind": "dns#resourceRecordSet", "/projects/" +
"name": ch.dnsHost, sa.project_id +
"type": "TXT", "/managedZones/" +
"ttl": 300, // TODO test for lowest allowed value gZoneName +
"rrdatas": [ ch.dnsAuthorization ], "/changes",
"signatureRrdatas": [] json: {
} kind: "dns#change",
], additions: [
"deletions": [], {
//"startTime": "string", kind: "dns#resourceRecordSet",
//"id": "string", name: ch.dnsHost,
//"status": "string", type: "TXT",
//"isServing": true ttl: 300, // TODO test for lowest allowed value
} rrdatas: [ch.dnsAuthorization],
}) signatureRrdatas: []
throw Error('setting TXT not implemented'); }
],
}, deletions: []
remove: function(data) { //"startTime": "string",
// console.info('Remove TXT', data); //"id": "string",
throw Error('removing TXT not implemented'); //"status": "string",
}, //"isServing": true
get: function(data) { }
// console.info('List TXT', data); });
throw Error('listing TXTs not implemented'); })
} .then(function(resp) {
}; if (resp.body.error) {
console.error(resp.headers);
console.error(resp.body);
throw new Error(resp.body.error);
}
});
},
remove: function(data) {
// console.info('Remove TXT', data);
throw Error("removing TXT not implemented");
},
get: function(data) {
// console.info('List TXT', data);
throw Error("listing TXTs not implemented");
}
};
function api(opts) { return plugin;
//return auth.getToken(sa).then(function(token) {
opts.headers = opts.headers || {};
opts.headers.Authorization = 'Bearer ' + token;
return request(opts).then(function(resp){
console.log(resp.headers);
console.log(resp.body);
return resp
}
);
//});
}
function getServiceAccount(config) { function api(opts) {
var saPath = //return auth.getToken(sa).then(function(token) {
config.serviceAccountPath || opts.headers = opts.headers || {};
process.env.GOOGLE_APPLICATION_CREDENTIALS; opts.headers.Authorization = "Bearer " + token;
var sa = config.serviceAccount || require(saPath); return request(opts).then(function(resp) {
console.log(resp.headers);
console.log(resp.body);
return resp;
});
//});
}
if ( function getZoneNameByDnsName(dnsZone) {
!sa || return api({
!( url: baseUrl + "/projects/" + sa.project_id + "/managedZones",
sa.private_key && json: true
sa.private_key_id && }).then(function(resp) {
sa.client_email && // Google Zone Name is NOT the DNS Name, but an arbitrary string
sa.project_id return resp.body.managedZones.filter(function(zone) {
) return dnsZone + "." === zone.dnsName;
) { })[0].name;
throw new Error( });
'missing or incomplete service_account.json: set serviceAccount serviceAccountPath' }
);
} function getServiceAccount(config) {
return sa; var saPath =
} config.serviceAccountPath || process.env.GOOGLE_APPLICATION_CREDENTIALS;
var sa = config.serviceAccount || require(saPath);
if (
!sa ||
!(sa.private_key && sa.private_key_id && sa.client_email && sa.project_id)
) {
throw new Error(
"missing or incomplete service_account.json: set serviceAccount serviceAccountPath"
);
}
return sa;
}
}; };