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