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'),
|
|
|
|
|
settings = require('../settings.js'),
|
2015-10-30 13:16:07 -07:00
|
|
|
SubdomainError = require('../subdomainerror.js'),
|
2015-10-30 13:45:07 -07:00
|
|
|
util = require('util'),
|
|
|
|
|
_ = require('underscore');
|
2015-10-28 16:02:06 -07:00
|
|
|
|
|
|
|
|
function getDnsCredentials(callback) {
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
settings.getDnsConfig(function (error, dnsConfig) {
|
|
|
|
|
if (error) return callback(new SubdomainError(SubdomainError.INTERNAL_ERROR, error));
|
|
|
|
|
|
|
|
|
|
var credentials = {
|
|
|
|
|
accessKeyId: dnsConfig.accessKeyId,
|
|
|
|
|
secretAccessKey: dnsConfig.secretAccessKey,
|
|
|
|
|
region: dnsConfig.region
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (dnsConfig.endpoint) credentials.endpoint = new AWS.Endpoint(dnsConfig.endpoint);
|
|
|
|
|
|
|
|
|
|
callback(null, credentials);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getZoneByName(zoneName, callback) {
|
|
|
|
|
assert.strictEqual(typeof zoneName, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
debug('getZoneByName: %s', zoneName);
|
|
|
|
|
|
|
|
|
|
getDnsCredentials(function (error, credentials) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
var route53 = new AWS.Route53(credentials);
|
|
|
|
|
route53.listHostedZones({}, function (error, result) {
|
|
|
|
|
if (error) return callback(new SubdomainError(SubdomainError.EXTERNAL_ERROR, new Error(error)));
|
|
|
|
|
|
|
|
|
|
var zone = result.HostedZones.filter(function (zone) {
|
|
|
|
|
return zone.Name.slice(0, -1) === zoneName; // aws zone name contains a '.' at the end
|
|
|
|
|
})[0];
|
|
|
|
|
|
|
|
|
|
if (!zone) return callback(new SubdomainError(SubdomainError.NOT_FOUND, 'no such zone'));
|
|
|
|
|
|
|
|
|
|
debug('getZoneByName: found zone', zone);
|
|
|
|
|
|
|
|
|
|
callback(null, zone);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-30 13:16:07 -07:00
|
|
|
function add(zoneName, subdomain, type, values, callback) {
|
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-10-30 13:04:43 -07:00
|
|
|
|
2015-10-28 16:02:06 -07:00
|
|
|
getZoneByName(zoneName, function (error, zone) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
var fqdn = config.appFqdn(subdomain);
|
2015-10-30 13:04:43 -07:00
|
|
|
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,
|
2015-10-30 13:04:43 -07:00
|
|
|
ResourceRecords: records,
|
2015-10-28 16:02:06 -07:00
|
|
|
Weight: 0,
|
|
|
|
|
SetIdentifier: fqdn,
|
|
|
|
|
TTL: 1
|
|
|
|
|
}
|
|
|
|
|
}]
|
|
|
|
|
},
|
|
|
|
|
HostedZoneId: zone.Id
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
getDnsCredentials(function (error, credentials) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
var route53 = new AWS.Route53(credentials);
|
|
|
|
|
route53.changeResourceRecordSets(params, function(error, result) {
|
|
|
|
|
if (error && error.code === 'PriorRequestNotComplete') {
|
|
|
|
|
return callback(new SubdomainError(SubdomainError.STILL_BUSY, error.message));
|
|
|
|
|
} else if (error) {
|
|
|
|
|
return callback(new SubdomainError(SubdomainError.EXTERNAL_ERROR, error.message));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
debug('addSubdomain: success. changeInfoId:%j', result);
|
|
|
|
|
|
|
|
|
|
callback(null, result.ChangeInfo.Id);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-30 13:45:07 -07:00
|
|
|
function update(zoneName, subdomain, type, values, callback) {
|
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-10-30 13:45:07 -07:00
|
|
|
get(zoneName, subdomain, type, function (error, result) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
if (_.isEqual(values, result)) return callback();
|
|
|
|
|
|
|
|
|
|
add(zoneName, subdomain, type, values, callback);
|
|
|
|
|
});
|
2015-10-30 13:17:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function get(zoneName, subdomain, type, callback) {
|
|
|
|
|
assert.strictEqual(typeof zoneName, 'string');
|
|
|
|
|
assert.strictEqual(typeof subdomain, 'string');
|
|
|
|
|
assert.strictEqual(typeof type, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
getZoneByName(zoneName, function (error, zone) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
var params = {
|
|
|
|
|
HostedZoneId: zone.id,
|
|
|
|
|
MaxItems: '1',
|
2015-10-30 14:41:34 -07:00
|
|
|
StartRecordName: (subdomain ? subdomain + '.' : '') + zoneName + '.',
|
2015-10-30 13:17:33 -07:00
|
|
|
StartRecordType: type
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
getDnsCredentials(function (error, credentials) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
var route53 = new AWS.Route53(credentials);
|
|
|
|
|
route53.listResourceRecordSets(params, function (error, result) {
|
|
|
|
|
if (error) return callback(new SubdomainError(SubdomainError.EXTERNAL_ERROR, new Error(error)));
|
|
|
|
|
if (result.ResourceRecordSets.length === 0) return callback(null, [ ]);
|
2015-10-30 14:41:34 -07:00
|
|
|
if (result.ResourceRecordSets[0].Name !== params.StartRecordName && result.ResourceRecordSets[0].Type !== params.StartRecordType) return callback(null, [ ]);
|
2015-10-30 13:17:33 -07:00
|
|
|
|
|
|
|
|
var values = result.ResourceRecordSets[0].ResourceRecords.map(function (record) { return record.Value; });
|
|
|
|
|
|
|
|
|
|
callback(null, values);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
2015-10-29 15:37:42 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-30 13:30:19 -07:00
|
|
|
function del(zoneName, subdomain, type, values, callback) {
|
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-10-30 13:30:19 -07:00
|
|
|
debug('add: %s for zone %s of type %s with values %j', subdomain, zoneName, type, values);
|
2015-10-30 13:04:43 -07:00
|
|
|
|
2015-10-28 16:02:06 -07:00
|
|
|
getZoneByName(zoneName, function (error, zone) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
var fqdn = config.appFqdn(subdomain);
|
2015-10-30 13:04:43 -07:00
|
|
|
var records = values.map(function (v) { return { Value: v }; });
|
|
|
|
|
|
2015-10-28 16:02:06 -07:00
|
|
|
var resourceRecordSet = {
|
|
|
|
|
Name: fqdn,
|
|
|
|
|
Type: type,
|
2015-10-30 13:04:43 -07:00
|
|
|
ResourceRecords: records,
|
2015-10-28 16:02:06 -07:00
|
|
|
Weight: 0,
|
|
|
|
|
SetIdentifier: fqdn,
|
|
|
|
|
TTL: 1
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var params = {
|
|
|
|
|
ChangeBatch: {
|
|
|
|
|
Changes: [{
|
|
|
|
|
Action: 'DELETE',
|
|
|
|
|
ResourceRecordSet: resourceRecordSet
|
|
|
|
|
}]
|
|
|
|
|
},
|
|
|
|
|
HostedZoneId: zone.Id
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
getDnsCredentials(function (error, credentials) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
var route53 = new AWS.Route53(credentials);
|
|
|
|
|
route53.changeResourceRecordSets(params, function(error, result) {
|
|
|
|
|
if (error && error.message && error.message.indexOf('it was not found') !== -1) {
|
|
|
|
|
debug('delSubdomain: resource record set not found.', error);
|
|
|
|
|
return callback(new SubdomainError(SubdomainError.NOT_FOUND, new Error(error)));
|
|
|
|
|
} else if (error && error.code === 'NoSuchHostedZone') {
|
|
|
|
|
debug('delSubdomain: hosted zone not found.', error);
|
|
|
|
|
return callback(new SubdomainError(SubdomainError.NOT_FOUND, new Error(error)));
|
|
|
|
|
} else if (error && error.code === 'PriorRequestNotComplete') {
|
|
|
|
|
debug('delSubdomain: resource is still busy', error);
|
|
|
|
|
return callback(new SubdomainError(SubdomainError.STILL_BUSY, new Error(error)));
|
|
|
|
|
} else if (error && error.code === 'InvalidChangeBatch') {
|
|
|
|
|
debug('delSubdomain: invalid change batch. No such record to be deleted.');
|
|
|
|
|
return callback(new SubdomainError(SubdomainError.NOT_FOUND, new Error(error)));
|
|
|
|
|
} else if (error) {
|
|
|
|
|
debug('delSubdomain: error', error);
|
|
|
|
|
return callback(new SubdomainError(SubdomainError.EXTERNAL_ERROR, new Error(error)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
debug('delSubdomain: success');
|
|
|
|
|
|
|
|
|
|
callback(null);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getChangeStatus(changeId, callback) {
|
|
|
|
|
assert.strictEqual(typeof changeId, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
if (changeId === '') return callback(null, 'INSYNC');
|
|
|
|
|
|
|
|
|
|
getDnsCredentials(function (error, credentials) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
var route53 = new AWS.Route53(credentials);
|
|
|
|
|
route53.getChange({ Id: changeId }, function (error, result) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
callback(null, result.ChangeInfo.Status);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|