Files
cloudron-box/src/dns/route53.js
T

217 lines
8.4 KiB
JavaScript
Raw Normal View History

2015-10-28 16:02:06 -07:00
/* jslint node:true */
'use strict';
exports = module.exports = {
2015-10-30 13:16:07 -07:00
add: add,
2015-10-30 13:17:33 -07:00
get: get,
2015-10-30 13:30:19 -07:00
del: del,
2015-10-30 13:45:07 -07:00
update: update,
2015-10-28 16:02:06 -07:00
getChangeStatus: getChangeStatus
};
var assert = require('assert'),
AWS = require('aws-sdk'),
config = require('../config.js'),
debug = require('debug')('box:dns/route53'),
SubdomainError = require('../subdomains.js').SubdomainError,
2015-10-30 13:45:07 -07:00
util = require('util'),
_ = require('underscore');
2015-10-28 16:02:06 -07:00
2015-11-08 23:14:39 -08:00
function getDnsCredentials(dnsConfig) {
assert.strictEqual(typeof dnsConfig, 'object');
2015-10-28 16:02:06 -07:00
2015-11-08 23:14:39 -08:00
var credentials = {
accessKeyId: dnsConfig.accessKeyId,
secretAccessKey: dnsConfig.secretAccessKey,
region: dnsConfig.region
};
2015-10-28 16:02:06 -07:00
2015-11-08 23:14:39 -08:00
if (dnsConfig.endpoint) credentials.endpoint = new AWS.Endpoint(dnsConfig.endpoint);
2015-10-28 16:02:06 -07:00
2015-11-08 23:14:39 -08:00
return credentials;
2015-10-28 16:02:06 -07:00
}
2015-11-08 23:14:39 -08:00
function getZoneByName(dnsConfig, zoneName, callback) {
assert.strictEqual(typeof dnsConfig, 'object');
2015-10-28 16:02:06 -07:00
assert.strictEqual(typeof zoneName, 'string');
assert.strictEqual(typeof callback, 'function');
2015-11-08 23:14:39 -08:00
var route53 = new AWS.Route53(getDnsCredentials(dnsConfig));
route53.listHostedZones({}, function (error, result) {
if (error && error.code === 'AccessDenied') return callback(new SubdomainError(SubdomainError.ACCESS_DENIED, error.message));
if (error) return callback(new SubdomainError(SubdomainError.EXTERNAL_ERROR, error.message));
2015-10-28 16:02:06 -07:00
2015-11-08 23:14:39 -08:00
var zone = result.HostedZones.filter(function (zone) {
return zone.Name.slice(0, -1) === zoneName; // aws zone name contains a '.' at the end
})[0];
2015-10-28 16:02:06 -07:00
2015-11-08 23:14:39 -08:00
if (!zone) return callback(new SubdomainError(SubdomainError.NOT_FOUND, 'no such zone'));
2015-10-28 16:02:06 -07:00
2015-11-08 23:14:39 -08:00
callback(null, zone);
2015-10-28 16:02:06 -07:00
});
}
2015-11-08 23:14:39 -08:00
function add(dnsConfig, zoneName, subdomain, type, values, callback) {
assert.strictEqual(typeof dnsConfig, 'object');
2015-10-28 16:02:06 -07:00
assert.strictEqual(typeof zoneName, 'string');
assert.strictEqual(typeof subdomain, 'string');
assert.strictEqual(typeof type, 'string');
2015-10-30 13:16:07 -07:00
assert(util.isArray(values));
2015-10-28 16:02:06 -07:00
assert.strictEqual(typeof callback, 'function');
2015-10-30 13:16:07 -07:00
debug('add: %s for zone %s of type %s with values %j', subdomain, zoneName, type, values);
2015-11-08 23:14:39 -08:00
getZoneByName(dnsConfig, zoneName, function (error, zone) {
2015-10-28 16:02:06 -07:00
if (error) return callback(error);
var fqdn = config.appFqdn(subdomain);
var records = values.map(function (v) { return { Value: v }; });
2015-10-28 16:02:06 -07:00
var params = {
ChangeBatch: {
Changes: [{
Action: 'UPSERT',
ResourceRecordSet: {
Type: type,
Name: fqdn,
ResourceRecords: records,
2015-10-28 16:02:06 -07:00
TTL: 1
}
}]
},
HostedZoneId: zone.Id
};
2015-11-08 23:14:39 -08:00
var route53 = new AWS.Route53(getDnsCredentials(dnsConfig));
route53.changeResourceRecordSets(params, function(error, result) {
if (error && error.code === 'AccessDenied') return callback(new SubdomainError(SubdomainError.ACCESS_DENIED, error.message));
if (error && error.code === 'PriorRequestNotComplete') return callback(new SubdomainError(SubdomainError.STILL_BUSY, error.message));
if (error) return callback(new SubdomainError(SubdomainError.EXTERNAL_ERROR, error.message));
2015-10-28 16:02:06 -07:00
2015-11-08 23:14:39 -08:00
callback(null, result.ChangeInfo.Id);
2015-10-28 16:02:06 -07:00
});
});
}
2015-11-08 23:14:39 -08:00
function update(dnsConfig, zoneName, subdomain, type, values, callback) {
assert.strictEqual(typeof dnsConfig, 'object');
2015-10-29 15:37:42 -07:00
assert.strictEqual(typeof zoneName, 'string');
assert.strictEqual(typeof subdomain, 'string');
assert.strictEqual(typeof type, 'string');
2015-10-30 13:45:07 -07:00
assert(util.isArray(values));
2015-10-29 15:37:42 -07:00
assert.strictEqual(typeof callback, 'function');
2015-11-08 23:57:42 -08:00
get(dnsConfig, zoneName, subdomain, type, function (error, result) {
2015-10-30 13:45:07 -07:00
if (error) return callback(error);
if (_.isEqual(values, result)) return callback();
2015-11-08 23:57:42 -08:00
add(dnsConfig, zoneName, subdomain, type, values, callback);
2015-10-30 13:45:07 -07:00
});
2015-10-30 13:17:33 -07:00
}
2015-11-08 23:14:39 -08:00
function get(dnsConfig, zoneName, subdomain, type, callback) {
assert.strictEqual(typeof dnsConfig, 'object');
2015-10-30 13:17:33 -07:00
assert.strictEqual(typeof zoneName, 'string');
assert.strictEqual(typeof subdomain, 'string');
assert.strictEqual(typeof type, 'string');
assert.strictEqual(typeof callback, 'function');
2015-11-08 23:14:39 -08:00
getZoneByName(dnsConfig, zoneName, function (error, zone) {
2015-10-30 13:17:33 -07:00
if (error) return callback(error);
var params = {
2015-10-30 18:05:08 -07:00
HostedZoneId: zone.Id,
2015-10-30 13:17:33 -07:00
MaxItems: '1',
StartRecordName: (subdomain ? subdomain + '.' : '') + zoneName + '.',
2015-10-30 13:17:33 -07:00
StartRecordType: type
};
2015-11-08 23:14:39 -08:00
var route53 = new AWS.Route53(getDnsCredentials(dnsConfig));
route53.listResourceRecordSets(params, function (error, result) {
if (error && error.code === 'AccessDenied') return callback(new SubdomainError(SubdomainError.ACCESS_DENIED, error.message));
if (error) return callback(new SubdomainError(SubdomainError.EXTERNAL_ERROR, error.message));
2015-11-08 23:14:39 -08:00
if (result.ResourceRecordSets.length === 0) return callback(null, [ ]);
if (result.ResourceRecordSets[0].Name !== params.StartRecordName && result.ResourceRecordSets[0].Type !== params.StartRecordType) return callback(null, [ ]);
2015-10-30 13:17:33 -07:00
2015-11-08 23:14:39 -08:00
var values = result.ResourceRecordSets[0].ResourceRecords.map(function (record) { return record.Value; });
2015-10-30 13:17:33 -07:00
2015-11-08 23:14:39 -08:00
callback(null, values);
2015-10-30 13:17:33 -07:00
});
});
2015-10-29 15:37:42 -07:00
}
2015-11-08 23:14:39 -08:00
function del(dnsConfig, zoneName, subdomain, type, values, callback) {
assert.strictEqual(typeof dnsConfig, 'object');
2015-10-28 16:02:06 -07:00
assert.strictEqual(typeof zoneName, 'string');
assert.strictEqual(typeof subdomain, 'string');
assert.strictEqual(typeof type, 'string');
2015-10-30 13:30:19 -07:00
assert(util.isArray(values));
2015-10-28 16:02:06 -07:00
assert.strictEqual(typeof callback, 'function');
2015-11-08 23:14:39 -08:00
getZoneByName(dnsConfig, zoneName, function (error, zone) {
2015-10-28 16:02:06 -07:00
if (error) return callback(error);
var fqdn = config.appFqdn(subdomain);
var records = values.map(function (v) { return { Value: v }; });
2015-10-28 16:02:06 -07:00
var resourceRecordSet = {
Name: fqdn,
Type: type,
ResourceRecords: records,
2015-10-28 16:02:06 -07:00
TTL: 1
};
var params = {
ChangeBatch: {
Changes: [{
Action: 'DELETE',
ResourceRecordSet: resourceRecordSet
}]
},
HostedZoneId: zone.Id
};
2015-11-08 23:14:39 -08:00
var route53 = new AWS.Route53(getDnsCredentials(dnsConfig));
route53.changeResourceRecordSets(params, function(error, result) {
if (error && error.code === 'AccessDenied') return callback(new SubdomainError(SubdomainError.ACCESS_DENIED, error.message));
2015-11-08 23:14:39 -08:00
if (error && error.message && error.message.indexOf('it was not found') !== -1) {
2015-12-17 20:30:30 -08:00
debug('del: resource record set not found.', error);
return callback(new SubdomainError(SubdomainError.NOT_FOUND, error.message));
2015-11-08 23:14:39 -08:00
} else if (error && error.code === 'NoSuchHostedZone') {
2015-12-17 20:30:30 -08:00
debug('del: hosted zone not found.', error);
return callback(new SubdomainError(SubdomainError.NOT_FOUND, error.message));
2015-11-08 23:14:39 -08:00
} else if (error && error.code === 'PriorRequestNotComplete') {
2015-12-17 20:30:30 -08:00
debug('del: resource is still busy', error);
return callback(new SubdomainError(SubdomainError.STILL_BUSY, error.message));
2015-11-08 23:14:39 -08:00
} else if (error && error.code === 'InvalidChangeBatch') {
2015-12-17 20:30:30 -08:00
debug('del: invalid change batch. No such record to be deleted.');
return callback(new SubdomainError(SubdomainError.NOT_FOUND, error.message));
2015-11-08 23:14:39 -08:00
} else if (error) {
2015-12-17 20:30:30 -08:00
debug('del: error', error);
return callback(new SubdomainError(SubdomainError.EXTERNAL_ERROR, error.message));
2015-11-08 23:14:39 -08:00
}
callback(null);
2015-10-28 16:02:06 -07:00
});
});
}
2015-11-08 23:14:39 -08:00
function getChangeStatus(dnsConfig, changeId, callback) {
assert.strictEqual(typeof dnsConfig, 'object');
2015-10-28 16:02:06 -07:00
assert.strictEqual(typeof changeId, 'string');
assert.strictEqual(typeof callback, 'function');
if (changeId === '') return callback(null, 'INSYNC');
2015-11-08 23:14:39 -08:00
var route53 = new AWS.Route53(getDnsCredentials(dnsConfig));
route53.getChange({ Id: changeId }, function (error, result) {
if (error && error.code === 'AccessDenied') return callback(new SubdomainError(SubdomainError.ACCESS_DENIED, error.message));
2015-10-28 16:02:06 -07:00
if (error) return callback(error);
2015-11-08 23:14:39 -08:00
callback(null, result.ChangeInfo.Status);
2015-10-28 16:02:06 -07:00
});
}
2015-11-08 23:14:39 -08:00