Files
cloudron-box/src/storage/caas.js

162 lines
5.9 KiB
JavaScript
Raw Normal View History

2015-08-24 11:13:21 -07:00
'use strict';
exports = module.exports = {
getBoxBackupDetails: getBoxBackupDetails,
2016-09-16 11:21:08 +02:00
getAppBackupDetails: getAppBackupDetails,
2016-09-16 10:59:17 +02:00
getRestoreUrl: getRestoreUrl,
getAppRestoreConfig: getAppRestoreConfig,
getLocalFilePath: getLocalFilePath,
2016-09-16 10:59:17 +02:00
copyObject: copyObject
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-11-06 18:22:29 -08:00
config = require('../config.js'),
safe = require('safetydance'),
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.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';
superagent.post(url).query({ token: apiConfig.token }).timeout(30 * 1000).end(function (error, result) {
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
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,
sessionToken: result.body.credentials.SessionToken,
2016-03-31 09:48:38 -07:00
region: apiConfig.region || 'us-east-1'
};
2016-03-31 09:48:01 -07:00
if (apiConfig.endpoint) credentials.endpoint = new AWS.Endpoint(apiConfig.endpoint);
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
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');
getBackupCredentials(apiConfig, function (error, result) {
if (error) return callback(error);
var s3Url = 's3://' + apiConfig.bucket + '/' + apiConfig.prefix + '/' + id;
var region = apiConfig.region || 'us-east-1';
var details = {
backupScriptArguments: [ 's3', s3Url, result.accessKeyId, result.secretAccessKey, region, apiConfig.key, result.sessionToken ]
2016-09-16 10:58:34 +02:00
};
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 12:07:24 +02:00
assert.strictEqual(typeof configId, 'string');
2016-09-16 11:21:08 +02:00
assert.strictEqual(typeof callback, 'function');
getBackupCredentials(apiConfig, function (error, result) {
if (error) return callback(error);
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, result.accessKeyId, result.secretAccessKey, region, apiConfig.key, result.sessionToken ]
2016-09-16 11:21:08 +02:00
};
callback(null, details);
});
}
function getRestoreUrl(apiConfig, filename, callback) {
2016-03-31 09:48:01 -07:00
assert.strictEqual(typeof apiConfig, 'object');
assert.strictEqual(typeof filename, 'string');
2015-08-25 10:01:04 -07:00
assert.strictEqual(typeof callback, 'function');
if (!apiConfig.bucket || !apiConfig.prefix) return new Error('Invalid configuration'); // prevent error in s3
2015-08-25 10:01:04 -07:00
getBackupCredentials(apiConfig, function (error, credentials) {
if (error) return callback(error);
credentials.region = apiConfig.region; // use same region as where we uploaded
var s3 = new AWS.S3(credentials);
var params = {
Bucket: apiConfig.bucket,
Key: apiConfig.prefix + '/' + filename,
Expires: 60 * 60 * 24 /* 1 day */
};
var url = s3.getSignedUrl('getObject', params);
callback(null, { url: url });
2015-08-25 10:01:04 -07: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);
});
});
}
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
if (!apiConfig.bucket || !apiConfig.prefix) return new Error('Invalid configuration'); // prevent error in s3
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
});
}