Skip to content
Permalink
Browse files

run R.test as running R.match can fail

  • Loading branch information...
Schnitzel committed Jun 1, 2019
1 parent ccda4cc commit d89447fa81e2b811aa4fc75f01bf9900e5cacaab
Showing with 37 additions and 33 deletions.
  1. +37 −33 services/api/src/resources/backup/helpers.js
@@ -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 = {

0 comments on commit d89447f

Please sign in to comment.
You can’t perform that action at this time.