2015-08-24 11:13:21 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
2016-09-16 11:22:04 +02:00
|
|
|
getBoxBackupDetails: getBoxBackupDetails,
|
2016-09-16 11:21:08 +02:00
|
|
|
getAppBackupDetails: getAppBackupDetails,
|
|
|
|
|
|
2016-09-16 10:59:17 +02:00
|
|
|
getRestoreUrl: getRestoreUrl,
|
2016-09-19 15:03:38 +02:00
|
|
|
getAppRestoreConfig: getAppRestoreConfig,
|
2016-09-16 18:14:36 +02:00
|
|
|
getLocalFilePath: getLocalFilePath,
|
2016-09-16 10:59:17 +02:00
|
|
|
|
2016-10-10 15:04:28 +02:00
|
|
|
copyObject: copyObject,
|
2016-10-11 11:36:25 +02:00
|
|
|
removeBackup: removeBackup,
|
|
|
|
|
|
|
|
|
|
testConfig: testConfig
|
2015-08-24 11:13:21 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var assert = require('assert'),
|
2016-09-19 15:03:38 +02:00
|
|
|
AWS = require('aws-sdk'),
|
|
|
|
|
safe = require('safetydance'),
|
2016-10-11 11:36:25 +02:00
|
|
|
SettingsError = require('../settings.js').SettingsError,
|
2016-09-19 15:03:38 +02:00
|
|
|
superagent = require('superagent');
|
2015-08-24 11:13:21 -07:00
|
|
|
|
2016-03-31 09:48:01 -07:00
|
|
|
function getBackupCredentials(apiConfig, callback) {
|
|
|
|
|
assert.strictEqual(typeof apiConfig, 'object');
|
2015-08-24 11:13:21 -07:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2016-03-31 09:48:01 -07:00
|
|
|
assert(apiConfig.accessKeyId && apiConfig.secretAccessKey);
|
2015-11-06 18:22:29 -08:00
|
|
|
|
|
|
|
|
var credentials = {
|
2016-09-12 17:31:52 +02:00
|
|
|
signatureVersion: 'v4',
|
2016-03-31 09:48:01 -07:00
|
|
|
accessKeyId: apiConfig.accessKeyId,
|
|
|
|
|
secretAccessKey: apiConfig.secretAccessKey,
|
2016-03-31 09:48:38 -07:00
|
|
|
region: apiConfig.region || 'us-east-1'
|
2015-11-06 18:22:29 -08:00
|
|
|
};
|
2015-09-09 11:43:50 -07:00
|
|
|
|
2016-03-31 09:48:01 -07:00
|
|
|
if (apiConfig.endpoint) credentials.endpoint = new AWS.Endpoint(apiConfig.endpoint);
|
2015-09-09 11:43:50 -07:00
|
|
|
|
2015-11-06 18:22:29 -08:00
|
|
|
callback(null, credentials);
|
2015-08-24 11:13:21 -07:00
|
|
|
}
|
2015-08-25 10:01:04 -07:00
|
|
|
|
2016-09-16 11:22:04 +02:00
|
|
|
function getBoxBackupDetails(apiConfig, id, callback) {
|
2016-09-16 10:58:34 +02:00
|
|
|
assert.strictEqual(typeof apiConfig, 'object');
|
|
|
|
|
assert.strictEqual(typeof id, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
var s3Url = 's3://' + apiConfig.bucket + '/' + apiConfig.prefix + '/' + id;
|
|
|
|
|
var region = apiConfig.region || 'us-east-1';
|
|
|
|
|
|
|
|
|
|
var details = {
|
|
|
|
|
backupScriptArguments: [ 's3', s3Url, apiConfig.accessKeyId, apiConfig.secretAccessKey, region, apiConfig.key ]
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
callback(null, details);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-16 11:21:08 +02:00
|
|
|
function getAppBackupDetails(apiConfig, appId, dataId, configId, callback) {
|
|
|
|
|
assert.strictEqual(typeof apiConfig, 'object');
|
|
|
|
|
assert.strictEqual(typeof appId, 'string');
|
|
|
|
|
assert.strictEqual(typeof dataId, 'string');
|
2016-09-16 16:54:34 +02:00
|
|
|
assert.strictEqual(typeof configId, 'string');
|
2016-09-16 11:21:08 +02:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
var s3DataUrl = 's3://' + apiConfig.bucket + '/' + apiConfig.prefix + '/' + dataId;
|
|
|
|
|
var s3ConfigUrl = 's3://' + apiConfig.bucket + '/' + apiConfig.prefix + '/' + configId;
|
|
|
|
|
var region = apiConfig.region || 'us-east-1';
|
|
|
|
|
|
|
|
|
|
var details = {
|
|
|
|
|
backupScriptArguments: [ 's3', appId, s3ConfigUrl, s3DataUrl, apiConfig.accessKeyId, apiConfig.secretAccessKey, region, apiConfig.key ]
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
callback(null, details);
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-04 11:44:24 -07:00
|
|
|
function getRestoreUrl(apiConfig, filename, callback) {
|
2016-03-31 09:48:01 -07:00
|
|
|
assert.strictEqual(typeof apiConfig, 'object');
|
2016-03-31 09:53:56 -07:00
|
|
|
assert.strictEqual(typeof filename, 'string');
|
2015-08-25 10:01:04 -07:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2016-03-31 09:48:01 -07:00
|
|
|
getBackupCredentials(apiConfig, function (error, credentials) {
|
2015-08-25 10:01:04 -07:00
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
var s3 = new AWS.S3(credentials);
|
|
|
|
|
|
|
|
|
|
var params = {
|
2016-04-04 11:44:24 -07:00
|
|
|
Bucket: apiConfig.bucket,
|
|
|
|
|
Key: apiConfig.prefix + '/' + filename,
|
2016-08-24 17:53:28 -07:00
|
|
|
Expires: 60 * 60 * 24 /* 1 day */
|
2015-08-25 10:01:04 -07:00
|
|
|
};
|
|
|
|
|
|
2015-08-27 09:26:19 -07:00
|
|
|
var url = s3.getSignedUrl('getObject', params);
|
|
|
|
|
|
2016-04-03 23:23:23 -07:00
|
|
|
callback(null, { url: url });
|
2015-08-25 10:01:04 -07:00
|
|
|
});
|
|
|
|
|
}
|
2015-08-26 16:14:51 -07:00
|
|
|
|
2016-09-19 15:03:38 +02:00
|
|
|
function getAppRestoreConfig(apiConfig, backupId, callback) {
|
|
|
|
|
assert.strictEqual(typeof apiConfig, 'object');
|
|
|
|
|
assert.strictEqual(typeof backupId, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
var configFilename = backupId.replace(/\.tar\.gz$/, '.json');
|
|
|
|
|
|
|
|
|
|
getRestoreUrl(apiConfig, configFilename, function (error, result) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
superagent.get(result.url).buffer(true).timeout(30 * 1000).end(function (error, response) {
|
|
|
|
|
if (error && !error.response) return callback(new Error(error.message));
|
|
|
|
|
if (response.statusCode !== 200) return callback(new Error('Invalid response code when getting config.json : ' + response.statusCode));
|
|
|
|
|
|
|
|
|
|
var config = safe.JSON.parse(response.text);
|
|
|
|
|
if (!config) return callback(new Error('Error in config:' + safe.error.message));
|
|
|
|
|
|
|
|
|
|
return callback(null, config);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-16 18:14:36 +02:00
|
|
|
function getLocalFilePath(apiConfig, filename, callback) {
|
|
|
|
|
assert.strictEqual(typeof apiConfig, 'object');
|
|
|
|
|
assert.strictEqual(typeof filename, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
callback(new Error('not supported'));
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-31 09:48:01 -07:00
|
|
|
function copyObject(apiConfig, from, to, callback) {
|
|
|
|
|
assert.strictEqual(typeof apiConfig, 'object');
|
2015-09-21 14:02:00 -07:00
|
|
|
assert.strictEqual(typeof from, 'string');
|
|
|
|
|
assert.strictEqual(typeof to, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2016-03-31 09:48:01 -07:00
|
|
|
getBackupCredentials(apiConfig, function (error, credentials) {
|
2015-09-21 14:02:00 -07:00
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
var params = {
|
2016-03-31 09:48:01 -07:00
|
|
|
Bucket: apiConfig.bucket, // target bucket
|
|
|
|
|
Key: apiConfig.prefix + '/' + to, // target file
|
|
|
|
|
CopySource: apiConfig.bucket + '/' + apiConfig.prefix + '/' + from, // source
|
2015-09-21 14:02:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var s3 = new AWS.S3(credentials);
|
2015-09-21 14:14:21 -07:00
|
|
|
s3.copyObject(params, callback);
|
2015-09-21 14:02:00 -07:00
|
|
|
});
|
|
|
|
|
}
|
2016-10-10 15:04:28 +02:00
|
|
|
|
2016-10-10 15:45:12 +02:00
|
|
|
function removeBackup(apiConfig, backupId, appBackupIds, callback) {
|
2016-10-10 15:04:28 +02:00
|
|
|
assert.strictEqual(typeof apiConfig, 'object');
|
|
|
|
|
assert.strictEqual(typeof backupId, 'string');
|
2016-10-10 15:45:12 +02:00
|
|
|
assert(Array.isArray(appBackupIds));
|
2016-10-10 15:04:28 +02:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
// Result: none
|
|
|
|
|
|
|
|
|
|
callback(new Error('not implemented'));
|
|
|
|
|
}
|
2016-10-11 11:36:25 +02:00
|
|
|
|
|
|
|
|
function testConfig(apiConfig, callback) {
|
|
|
|
|
assert.strictEqual(typeof apiConfig, 'object');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
if (typeof apiConfig.accessKeyId !== 'string') return callback(new SettingsError(SettingsError.BAD_FIELD, 'accessKeyId must be a string'));
|
|
|
|
|
if (typeof apiConfig.secretAccessKey !== 'string') return callback(new SettingsError(SettingsError.BAD_FIELD, 'secretAccessKey must be a string'));
|
|
|
|
|
if (typeof apiConfig.bucket !== 'string') return callback(new SettingsError(SettingsError.BAD_FIELD, 'bucket must be a string'));
|
|
|
|
|
if (typeof apiConfig.prefix !== 'string') return callback(new SettingsError(SettingsError.BAD_FIELD, 'prefix must be a string'));
|
|
|
|
|
|
2016-10-11 11:46:28 +02:00
|
|
|
// attempt to upload and delete a file with new credentials
|
|
|
|
|
getBackupCredentials(apiConfig, function (error, credentials) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
var params = {
|
|
|
|
|
Bucket: apiConfig.bucket,
|
|
|
|
|
Key: apiConfig.prefix + '/testfile',
|
|
|
|
|
Body: 'testcontent'
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var s3 = new AWS.S3(credentials);
|
|
|
|
|
s3.putObject(params, function (error) {
|
|
|
|
|
if (error) return callback(new SettingsError(SettingsError.EXTERNAL_ERROR, error.message));
|
|
|
|
|
|
|
|
|
|
var params = {
|
|
|
|
|
Bucket: apiConfig.bucket,
|
|
|
|
|
Key: apiConfig.prefix + '/testfile'
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
s3.deleteObject(params, function (error) {
|
|
|
|
|
if (error) return callback(new SettingsError(SettingsError.EXTERNAL_ERROR, error.message));
|
|
|
|
|
|
|
|
|
|
callback();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
2016-10-11 11:36:25 +02:00
|
|
|
}
|