2015-08-24 11:13:21 -07:00
|
|
|
/* jslint node:true */
|
|
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
2015-08-25 10:01:04 -07:00
|
|
|
getSignedUploadUrl: getSignedUploadUrl,
|
2015-08-26 16:14:51 -07:00
|
|
|
getSignedDownloadUrl: getSignedDownloadUrl,
|
|
|
|
|
|
|
|
|
|
addSubdomain: addSubdomain,
|
|
|
|
|
delSubdomain: delSubdomain,
|
|
|
|
|
getChangeStatus: getChangeStatus
|
2015-08-24 11:13:21 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var assert = require('assert'),
|
2015-08-25 10:01:04 -07:00
|
|
|
AWS = require('aws-sdk'),
|
2015-08-24 11:13:21 -07:00
|
|
|
config = require('./config.js'),
|
|
|
|
|
debug = require('debug')('box:aws'),
|
2015-08-26 16:14:51 -07:00
|
|
|
SubdomainError = require('./subdomainerror.js'),
|
2015-09-08 11:26:35 -07:00
|
|
|
superagent = require('superagent');
|
2015-08-24 11:13:21 -07:00
|
|
|
|
|
|
|
|
function getAWSCredentials(callback) {
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
// CaaS
|
|
|
|
|
if (config.token()) {
|
|
|
|
|
var url = config.apiServerOrigin() + '/api/v1/boxes/' + config.fqdn() + '/awscredentials';
|
2015-09-08 21:01:04 -07:00
|
|
|
superagent.post(url).query({ token: config.token() }).end(function (error, result) {
|
2015-08-24 11:13:21 -07:00
|
|
|
if (error) return callback(error);
|
2015-08-25 11:12:01 -07:00
|
|
|
if (result.statusCode !== 201) return callback(new Error(result.text));
|
2015-08-27 11:43:02 -07:00
|
|
|
if (!result.body || !result.body.credentials) return callback(new Error('Unexpected response'));
|
2015-08-24 11:13:21 -07:00
|
|
|
|
2015-08-25 11:23:12 -07:00
|
|
|
return callback(null, {
|
|
|
|
|
accessKeyId: result.body.credentials.AccessKeyId,
|
|
|
|
|
secretAccessKey: result.body.credentials.SecretAccessKey,
|
2015-08-25 11:36:40 -07:00
|
|
|
sessionToken: result.body.credentials.SessionToken,
|
|
|
|
|
region: 'us-east-1'
|
2015-08-25 11:23:12 -07:00
|
|
|
});
|
2015-08-24 11:13:21 -07:00
|
|
|
});
|
|
|
|
|
} else {
|
2015-09-08 11:26:35 -07:00
|
|
|
if (!config.aws().accessKeyId || !config.aws().secretAccessKey) return callback(new SubdomainError(SubdomainError.MISSING_CREDENTIALS));
|
2015-08-24 12:25:05 -07:00
|
|
|
|
|
|
|
|
callback(null, {
|
|
|
|
|
accessKeyId: config.aws().accessKeyId,
|
2015-08-25 11:36:40 -07:00
|
|
|
secretAccessKey: config.aws().secretAccessKey,
|
|
|
|
|
region: 'us-east-1'
|
2015-08-24 12:25:05 -07:00
|
|
|
});
|
2015-08-24 11:13:21 -07:00
|
|
|
}
|
|
|
|
|
}
|
2015-08-25 10:01:04 -07:00
|
|
|
|
|
|
|
|
function getSignedUploadUrl(filename, callback) {
|
|
|
|
|
assert.strictEqual(typeof filename, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2015-09-08 21:11:46 -07:00
|
|
|
debug('getSignedUploadUrl: %s', filename);
|
2015-08-25 10:01:04 -07:00
|
|
|
|
|
|
|
|
getAWSCredentials(function (error, credentials) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
var s3 = new AWS.S3(credentials);
|
|
|
|
|
|
|
|
|
|
var params = {
|
|
|
|
|
Bucket: config.aws().backupBucket,
|
|
|
|
|
Key: config.aws().backupPrefix + '/' + filename,
|
|
|
|
|
Expires: 60 * 30 /* 30 minutes */
|
|
|
|
|
};
|
|
|
|
|
|
2015-08-27 09:26:19 -07:00
|
|
|
var url = s3.getSignedUrl('putObject', params);
|
|
|
|
|
|
|
|
|
|
callback(null, { url : url, sessionToken: credentials.sessionToken });
|
2015-08-25 10:01:04 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getSignedDownloadUrl(filename, callback) {
|
|
|
|
|
assert.strictEqual(typeof filename, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2015-09-08 21:11:46 -07:00
|
|
|
debug('getSignedDownloadUrl: %s', filename);
|
2015-08-25 10:01:04 -07:00
|
|
|
|
|
|
|
|
getAWSCredentials(function (error, credentials) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
var s3 = new AWS.S3(credentials);
|
|
|
|
|
|
|
|
|
|
var params = {
|
|
|
|
|
Bucket: config.aws().backupBucket,
|
|
|
|
|
Key: config.aws().backupPrefix + '/' + filename,
|
|
|
|
|
Expires: 60 * 30 /* 30 minutes */
|
|
|
|
|
};
|
|
|
|
|
|
2015-08-27 09:26:19 -07:00
|
|
|
var url = s3.getSignedUrl('getObject', params);
|
|
|
|
|
|
|
|
|
|
callback(null, { url: url, sessionToken: credentials.sessionToken });
|
2015-08-25 10:01:04 -07:00
|
|
|
});
|
|
|
|
|
}
|
2015-08-26 16:14:51 -07:00
|
|
|
|
|
|
|
|
function getZoneByName(zoneName, callback) {
|
|
|
|
|
assert.strictEqual(typeof zoneName, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
debug('getZoneByName: %s', zoneName);
|
|
|
|
|
|
|
|
|
|
getAWSCredentials(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) {
|
2015-08-30 15:37:18 -07:00
|
|
|
return zone.Name.slice(0, -1) === zoneName; // aws zone name contains a '.' at the end
|
2015-08-26 16:14:51 -07:00
|
|
|
})[0];
|
|
|
|
|
|
|
|
|
|
if (!zone) return callback(new SubdomainError(SubdomainError.NOT_FOUND, 'no such zone'));
|
|
|
|
|
|
2015-08-30 15:58:54 -07:00
|
|
|
debug('getZoneByName: found zone', zone);
|
|
|
|
|
|
2015-08-26 16:14:51 -07:00
|
|
|
callback(null, zone);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function addSubdomain(zoneName, subdomain, type, value, callback) {
|
|
|
|
|
assert.strictEqual(typeof zoneName, 'string');
|
|
|
|
|
assert.strictEqual(typeof subdomain, 'string');
|
|
|
|
|
assert.strictEqual(typeof type, 'string');
|
|
|
|
|
assert.strictEqual(typeof value, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
debug('addSubdomain: ' + subdomain + ' for domain ' + zoneName + ' with value ' + value);
|
|
|
|
|
|
|
|
|
|
getZoneByName(zoneName, function (error, zone) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
2015-08-30 16:21:25 -07:00
|
|
|
var fqdn = config.appFqdn(subdomain);
|
2015-08-26 16:14:51 -07:00
|
|
|
var params = {
|
|
|
|
|
ChangeBatch: {
|
|
|
|
|
Changes: [{
|
|
|
|
|
Action: 'UPSERT',
|
|
|
|
|
ResourceRecordSet: {
|
|
|
|
|
Type: type,
|
|
|
|
|
Name: fqdn,
|
|
|
|
|
ResourceRecords: [{
|
|
|
|
|
Value: value
|
|
|
|
|
}],
|
|
|
|
|
Weight: 0,
|
|
|
|
|
SetIdentifier: fqdn,
|
|
|
|
|
TTL: 1
|
|
|
|
|
}
|
|
|
|
|
}]
|
|
|
|
|
},
|
2015-08-30 15:47:08 -07:00
|
|
|
HostedZoneId: zone.Id
|
2015-08-26 16:14:51 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
getAWSCredentials(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, new Error(error)));
|
|
|
|
|
} else if (error) {
|
|
|
|
|
return callback(new SubdomainError(SubdomainError.EXTERNAL_ERROR, new Error(error)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
debug('addSubdomain: success. changeInfoId:%j', result);
|
|
|
|
|
|
|
|
|
|
callback(null, result.ChangeInfo.Id);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function delSubdomain(zoneName, subdomain, type, value, callback) {
|
|
|
|
|
assert.strictEqual(typeof zoneName, 'string');
|
|
|
|
|
assert.strictEqual(typeof subdomain, 'string');
|
|
|
|
|
assert.strictEqual(typeof type, 'string');
|
|
|
|
|
assert.strictEqual(typeof value, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
debug('delSubdomain: %s for domain %s.', subdomain, zoneName);
|
|
|
|
|
|
|
|
|
|
getZoneByName(zoneName, function (error, zone) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
2015-08-30 16:21:25 -07:00
|
|
|
var fqdn = config.appFqdn(subdomain);
|
2015-08-26 16:14:51 -07:00
|
|
|
var resourceRecordSet = {
|
|
|
|
|
Name: fqdn,
|
|
|
|
|
Type: type,
|
|
|
|
|
ResourceRecords: [{
|
|
|
|
|
Value: value
|
|
|
|
|
}],
|
|
|
|
|
Weight: 0,
|
|
|
|
|
SetIdentifier: fqdn,
|
|
|
|
|
TTL: 1
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var params = {
|
|
|
|
|
ChangeBatch: {
|
|
|
|
|
Changes: [{
|
|
|
|
|
Action: 'DELETE',
|
|
|
|
|
ResourceRecordSet: resourceRecordSet
|
|
|
|
|
}]
|
|
|
|
|
},
|
2015-08-30 15:47:08 -07:00
|
|
|
HostedZoneId: zone.Id
|
2015-08-26 16:14:51 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
getAWSCredentials(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');
|
|
|
|
|
|
|
|
|
|
getAWSCredentials(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);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|