Skip to content

Commit

Permalink
run R.test as running R.match can fail
Browse files Browse the repository at this point in the history
  • Loading branch information
Schnitzel committed Jun 1, 2019
1 parent ccda4cc commit d89447f
Showing 1 changed file with 37 additions and 33 deletions.
70 changes: 37 additions & 33 deletions services/api/src/resources/backup/helpers.js
Expand Up @@ -6,42 +6,46 @@ const S3 = require('aws-sdk/clients/s3');
const makeS3TempLink = async (restore /* : Object */) => {
const restoreLocation = R.prop('restoreLocation', restore);

const accessKeyId = R.propOr(
'XXXXXXXXXXXXXXXXXXXX',
'S3_BAAS_ACCESS_KEY_ID',
process.env,
);
const secretAccessKey = R.propOr(
'XXXXXXXXXXXXXXXXXXXX',
'S3_BAAS_SECRET_ACCESS_KEY',
process.env,
);

// https://{endpoint}/{bucket}/{key}
const s3LinkMatch = /([^/]+)\/([^/]+)\/([^/]+)/;

const s3Parts = R.match(s3LinkMatch, restoreLocation);

// We have to generate a new client every time because the endpoint is parsed
// from the s3 url.
const s3Client = new S3({
accessKeyId,
secretAccessKey,
s3ForcePathStyle: true,
signatureVersion: 'v4',
endpoint: `https://${R.prop(1, s3Parts)}`,
});

const tempUrl = s3Client.getSignedUrl('getObject', {
Bucket: R.prop(2, s3Parts),
Key: R.prop(3, s3Parts),
Expires: 300, // 5 minutes
});

return {
...restore,
restoreLocation: tempUrl,
};
if (R.test(s3LinkMatch, restoreLocation)) {
const s3Parts = R.match(s3LinkMatch, restoreLocation);

const accessKeyId = R.propOr(
'XXXXXXXXXXXXXXXXXXXX',
'S3_BAAS_ACCESS_KEY_ID',
process.env,
);
const secretAccessKey = R.propOr(
'XXXXXXXXXXXXXXXXXXXX',
'S3_BAAS_SECRET_ACCESS_KEY',
process.env,
);

// We have to generate a new client every time because the endpoint is parsed
// from the s3 url.
const s3Client = new S3({
accessKeyId,
secretAccessKey,
s3ForcePathStyle: true,
signatureVersion: 'v4',
endpoint: `https://${R.prop(1, s3Parts)}`,
});

const tempUrl = s3Client.getSignedUrl('getObject', {
Bucket: R.prop(2, s3Parts),
Key: R.prop(3, s3Parts),
Expires: 300, // 5 minutes
});

return {
...restore,
restoreLocation: tempUrl,
};
}

return restore;
};

const Helpers = {
Expand Down

0 comments on commit d89447f

Please sign in to comment.