2015-08-24 11:13:21 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
2017-04-19 13:30:02 +02:00
|
|
|
backup: backup,
|
|
|
|
|
restore: restore,
|
|
|
|
|
copyBackup: copyBackup,
|
2017-04-23 11:34:46 -07:00
|
|
|
removeBackups: removeBackups,
|
2016-10-11 11:36:25 +02:00
|
|
|
|
2017-01-04 16:22:58 -08:00
|
|
|
backupDone: backupDone,
|
|
|
|
|
|
2017-04-19 13:30:02 +02:00
|
|
|
testConfig: testConfig,
|
2015-08-24 11:13:21 -07:00
|
|
|
};
|
|
|
|
|
|
2017-04-20 20:12:45 -07:00
|
|
|
var assert = require('assert'),
|
2015-08-25 10:01:04 -07:00
|
|
|
AWS = require('aws-sdk'),
|
2017-04-19 13:30:02 +02:00
|
|
|
BackupsError = require('../backups.js').BackupsError,
|
2015-11-06 18:22:29 -08:00
|
|
|
config = require('../config.js'),
|
2017-01-04 16:26:43 -08:00
|
|
|
debug = require('debug')('box:storage/caas'),
|
2017-04-19 13:30:02 +02:00
|
|
|
once = require('once'),
|
2017-04-21 15:28:25 -07:00
|
|
|
PassThrough = require('stream').PassThrough,
|
2017-04-19 13:30:02 +02:00
|
|
|
path = require('path'),
|
|
|
|
|
superagent = require('superagent'),
|
2017-04-21 15:28:25 -07:00
|
|
|
targz = require('./targz.js');
|
2015-08-24 11:13:21 -07:00
|
|
|
|
2017-04-20 16:40:35 +02:00
|
|
|
var FILE_TYPE = '.tar.gz.enc';
|
2017-04-19 13:30:02 +02:00
|
|
|
|
|
|
|
|
// internal only
|
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.token);
|
2015-08-24 11:13:21 -07:00
|
|
|
|
2015-11-06 18:22:29 -08:00
|
|
|
var url = config.apiServerOrigin() + '/api/v1/boxes/' + config.fqdn() + '/awscredentials';
|
2016-09-12 12:53:51 -07:00
|
|
|
superagent.post(url).query({ token: apiConfig.token }).timeout(30 * 1000).end(function (error, result) {
|
2015-12-15 09:12:52 -08:00
|
|
|
if (error && !error.response) return callback(error);
|
2015-11-06 18:22:29 -08:00
|
|
|
if (result.statusCode !== 201) return callback(new Error(result.text));
|
|
|
|
|
if (!result.body || !result.body.credentials) return callback(new Error('Unexpected response'));
|
2015-08-24 12:25:05 -07:00
|
|
|
|
2015-09-09 11:43:50 -07:00
|
|
|
var credentials = {
|
2016-09-16 10:58:34 +02:00
|
|
|
signatureVersion: 'v4',
|
2015-11-06 18:22:29 -08:00
|
|
|
accessKeyId: result.body.credentials.AccessKeyId,
|
|
|
|
|
secretAccessKey: result.body.credentials.SecretAccessKey,
|
2016-04-04 11:23:38 -07:00
|
|
|
sessionToken: result.body.credentials.SessionToken,
|
2016-03-31 09:48:38 -07:00
|
|
|
region: apiConfig.region || 'us-east-1'
|
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
|
|
|
|
|
|
|
|
callback(null, credentials);
|
2015-11-06 18:22:29 -08:00
|
|
|
});
|
2015-08-24 11:13:21 -07:00
|
|
|
}
|
2015-08-25 10:01:04 -07:00
|
|
|
|
2017-04-19 13:30:02 +02:00
|
|
|
function getBackupFilePath(apiConfig, backupId) {
|
2016-09-16 10:58:34 +02:00
|
|
|
assert.strictEqual(typeof apiConfig, 'object');
|
2017-04-19 13:30:02 +02:00
|
|
|
assert.strictEqual(typeof backupId, 'string');
|
|
|
|
|
|
|
|
|
|
return path.join(apiConfig.prefix, backupId.endsWith(FILE_TYPE) ? backupId : backupId+FILE_TYPE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// storage api
|
|
|
|
|
function backup(apiConfig, backupId, sourceDirectories, callback) {
|
|
|
|
|
assert.strictEqual(typeof apiConfig, 'object');
|
|
|
|
|
assert.strictEqual(typeof backupId, 'string');
|
|
|
|
|
assert(Array.isArray(sourceDirectories));
|
2016-09-16 10:58:34 +02:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2017-04-19 13:30:02 +02:00
|
|
|
callback = once(callback);
|
|
|
|
|
|
|
|
|
|
var backupFilePath = getBackupFilePath(apiConfig, backupId);
|
|
|
|
|
|
|
|
|
|
debug('[%s] backup: %j -> %s', backupId, sourceDirectories, backupFilePath);
|
|
|
|
|
|
|
|
|
|
getBackupCredentials(apiConfig, function (error, credentials) {
|
2016-09-16 10:58:34 +02:00
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
2017-04-21 15:28:25 -07:00
|
|
|
var passThrough = new PassThrough();
|
2017-04-19 13:30:02 +02:00
|
|
|
|
|
|
|
|
var params = {
|
|
|
|
|
Bucket: apiConfig.bucket,
|
|
|
|
|
Key: backupFilePath,
|
2017-04-21 15:28:25 -07:00
|
|
|
Body: passThrough
|
2016-09-16 10:58:34 +02:00
|
|
|
};
|
|
|
|
|
|
2017-04-19 13:30:02 +02:00
|
|
|
var s3 = new AWS.S3(credentials);
|
|
|
|
|
s3.upload(params, function (error) {
|
|
|
|
|
if (error) {
|
2017-04-23 22:09:05 -07:00
|
|
|
debug('[%s] backup: s3 upload error.', backupId, error);
|
2017-04-19 13:30:02 +02:00
|
|
|
return callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
callback(null);
|
|
|
|
|
});
|
2017-04-21 15:28:25 -07:00
|
|
|
|
|
|
|
|
targz.create(sourceDirectories, apiConfig.key, passThrough, callback);
|
2016-09-16 10:58:34 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-20 20:12:45 -07:00
|
|
|
function restore(apiConfig, backupId, destination, callback) {
|
2016-09-16 11:21:08 +02:00
|
|
|
assert.strictEqual(typeof apiConfig, 'object');
|
2017-04-19 13:30:02 +02:00
|
|
|
assert.strictEqual(typeof backupId, 'string');
|
2017-04-20 20:12:45 -07:00
|
|
|
assert.strictEqual(typeof destination, 'string');
|
2016-09-16 11:21:08 +02:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2017-04-21 15:28:25 -07:00
|
|
|
callback = once(callback);
|
|
|
|
|
|
2017-04-21 15:06:54 -07:00
|
|
|
var isOldFormat = backupId.endsWith('.tar.gz');
|
|
|
|
|
var backupFilePath = isOldFormat ? path.join(apiConfig.prefix, backupId) : getBackupFilePath(apiConfig, backupId);
|
2017-04-19 13:30:02 +02:00
|
|
|
|
2017-04-20 20:12:45 -07:00
|
|
|
debug('[%s] restore: %s -> %s', backupId, backupFilePath, destination);
|
2017-04-19 13:30:02 +02:00
|
|
|
|
|
|
|
|
getBackupCredentials(apiConfig, function (error, credentials) {
|
2016-09-16 11:21:08 +02:00
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
2017-04-21 15:28:25 -07:00
|
|
|
var params = {
|
|
|
|
|
Bucket: apiConfig.bucket,
|
|
|
|
|
Key: backupFilePath
|
|
|
|
|
};
|
2017-04-21 15:06:54 -07:00
|
|
|
|
2017-04-21 15:28:25 -07:00
|
|
|
var s3 = new AWS.S3(credentials);
|
|
|
|
|
var s3get = s3.getObject(params).createReadStream();
|
2017-04-21 15:06:54 -07:00
|
|
|
|
2017-04-21 15:28:25 -07:00
|
|
|
s3get.on('error', function (error) {
|
|
|
|
|
// TODO ENOENT for the mock, fix upstream!
|
|
|
|
|
if (error.code === 'NoSuchKey' || error.code === 'ENOENT') return callback(new BackupsError(BackupsError.NOT_FOUND));
|
2017-04-21 15:06:54 -07:00
|
|
|
|
2017-04-23 22:09:05 -07:00
|
|
|
debug('[%s] restore: s3 stream error.', backupId, error);
|
2017-04-21 15:28:25 -07:00
|
|
|
callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error.message));
|
2017-04-19 13:30:02 +02:00
|
|
|
});
|
2017-04-21 15:28:25 -07:00
|
|
|
|
|
|
|
|
targz.extract(s3get, isOldFormat, destination, apiConfig.key || '', callback);
|
2016-09-16 11:21:08 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-19 13:30:02 +02:00
|
|
|
function copyBackup(apiConfig, oldBackupId, newBackupId, callback) {
|
2016-03-31 09:48:01 -07:00
|
|
|
assert.strictEqual(typeof apiConfig, 'object');
|
2017-04-19 13:30:02 +02:00
|
|
|
assert.strictEqual(typeof oldBackupId, 'string');
|
|
|
|
|
assert.strictEqual(typeof newBackupId, 'string');
|
2015-08-25 10:01:04 -07:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2016-04-04 15:54:16 -07:00
|
|
|
getBackupCredentials(apiConfig, function (error, credentials) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
var params = {
|
|
|
|
|
Bucket: apiConfig.bucket,
|
2017-04-19 13:30:02 +02:00
|
|
|
Key: getBackupFilePath(apiConfig, newBackupId),
|
|
|
|
|
CopySource: path.join(apiConfig.bucket, getBackupFilePath(apiConfig, oldBackupId))
|
2016-04-04 15:54:16 -07:00
|
|
|
};
|
|
|
|
|
|
2017-04-19 13:30:02 +02:00
|
|
|
var s3 = new AWS.S3(credentials);
|
|
|
|
|
s3.copyObject(params, function (error) {
|
|
|
|
|
if (error && error.code === 'NoSuchKey') return callback(new BackupsError(BackupsError.NOT_FOUND));
|
|
|
|
|
if (error) {
|
2017-04-23 22:09:05 -07:00
|
|
|
debug('copyBackup: s3 copy error.', error);
|
2017-04-19 13:30:02 +02:00
|
|
|
return callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
callback(null);
|
|
|
|
|
});
|
2015-08-25 10:01:04 -07:00
|
|
|
});
|
|
|
|
|
}
|
2015-08-26 16:14:51 -07:00
|
|
|
|
2017-04-23 11:34:46 -07:00
|
|
|
function removeBackups(apiConfig, backupIds, callback) {
|
2016-09-19 15:03:38 +02:00
|
|
|
assert.strictEqual(typeof apiConfig, 'object');
|
2017-04-23 11:34:46 -07:00
|
|
|
assert(Array.isArray(backupIds));
|
2016-09-19 15:03:38 +02:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2017-04-19 13:30:02 +02:00
|
|
|
getBackupCredentials(apiConfig, function (error, credentials) {
|
2016-09-19 15:03:38 +02:00
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
2017-04-19 13:30:02 +02:00
|
|
|
var params = {
|
|
|
|
|
Bucket: apiConfig.bucket,
|
2017-04-23 11:34:46 -07:00
|
|
|
Delete: {
|
|
|
|
|
Objects: [ ] // { Key }
|
|
|
|
|
}
|
2017-04-19 13:30:02 +02:00
|
|
|
};
|
2016-09-19 15:03:38 +02:00
|
|
|
|
2017-04-23 11:34:46 -07:00
|
|
|
backupIds.forEach(function (backupId) {
|
|
|
|
|
params.Delete.Objects.push({ Key: getBackupFilePath(apiConfig, backupId) });
|
|
|
|
|
});
|
|
|
|
|
|
2017-04-19 13:30:02 +02:00
|
|
|
var s3 = new AWS.S3(credentials);
|
2017-04-23 11:34:46 -07:00
|
|
|
s3.deleteObjects(params, function (error) {
|
2017-04-23 22:09:05 -07:00
|
|
|
if (error) debug('Unable to remove %s. Not fatal.', params.Key, error);
|
|
|
|
|
else debug('removeBackups: Deleted: %j Errors: %j', data.Deleted, data.Errors);
|
|
|
|
|
|
2017-04-19 13:30:02 +02:00
|
|
|
callback(null);
|
2016-09-19 15:03:38 +02:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-11 11:36:25 +02:00
|
|
|
function testConfig(apiConfig, callback) {
|
|
|
|
|
assert.strictEqual(typeof apiConfig, 'object');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2017-04-20 17:23:31 -07:00
|
|
|
if (config.provider() !== 'caas') return callback(new BackupsError(BackupsError.BAD_FIELD, 'instance provider must be caas'));
|
2016-10-11 11:36:25 +02:00
|
|
|
|
|
|
|
|
callback();
|
|
|
|
|
}
|
2017-01-04 16:22:58 -08:00
|
|
|
|
2017-04-21 10:31:43 +02:00
|
|
|
function backupDone(backupId, appBackupIds, callback) {
|
|
|
|
|
assert.strictEqual(typeof backupId, 'string');
|
|
|
|
|
assert(Array.isArray(appBackupIds));
|
2017-01-04 16:22:58 -08:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2017-04-21 10:31:43 +02:00
|
|
|
// Caas expects filenames instead of backupIds, this means no prefix but a file type extension
|
|
|
|
|
var boxBackupFilename = backupId + FILE_TYPE;
|
|
|
|
|
var appBackupFilenames = appBackupIds.map(function (id) { return id + FILE_TYPE; });
|
|
|
|
|
|
|
|
|
|
debug('[%s] backupDone: %s apps %j', backupId, boxBackupFilename, appBackupFilenames);
|
2017-01-04 16:22:58 -08:00
|
|
|
|
2017-01-04 16:26:43 -08:00
|
|
|
var url = config.apiServerOrigin() + '/api/v1/boxes/' + config.fqdn() + '/backupDone';
|
|
|
|
|
var data = {
|
|
|
|
|
boxVersion: config.version(),
|
2017-04-21 10:31:43 +02:00
|
|
|
restoreKey: boxBackupFilename,
|
|
|
|
|
appId: null, // now unused
|
|
|
|
|
appVersion: null, // now unused
|
|
|
|
|
appBackupIds: appBackupFilenames
|
2017-01-04 16:26:43 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
superagent.post(url).send(data).query({ token: config.token() }).timeout(30 * 1000).end(function (error, result) {
|
2017-04-21 10:31:43 +02:00
|
|
|
if (error && !error.response) return callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error));
|
|
|
|
|
if (result.statusCode !== 200) return callback(new BackupsError(BackupsError.EXTERNAL_ERROR, result.text));
|
2017-01-04 16:26:43 -08:00
|
|
|
|
|
|
|
|
return callback(null);
|
|
|
|
|
});
|
|
|
|
|
}
|