From 3c8dafd7a3fa7b3e858a0a82a31be2384050a1d3 Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Fri, 26 Apr 2024 18:18:56 +0100 Subject: [PATCH] fix: roundabout raw cid does not fallback to car when raw --- roundabout/index.js | 27 ++-------------------- roundabout/test/index.test.js | 43 ----------------------------------- 2 files changed, 2 insertions(+), 68 deletions(-) diff --git a/roundabout/index.js b/roundabout/index.js index 952d56d4..59b80472 100644 --- a/roundabout/index.js +++ b/roundabout/index.js @@ -1,8 +1,5 @@ import { getSignedUrl as getR2SignedUrl } from '@aws-sdk/s3-request-presigner' -import { - GetObjectCommand, - HeadObjectCommand -} from '@aws-sdk/client-s3' +import { GetObjectCommand } from '@aws-sdk/client-s3' import { base58btc } from 'multiformats/bases/base58' import { RAW_CODE } from './constants.js' @@ -45,9 +42,7 @@ export function getSigner (s3Client, bucketName) { * Blobs are stored as `b58btc(multihash)/b58btc(multihash).blob` and requested to * Roundabout via a RAW CID. * Store protocol SHOULD receive CAR files that are stored as - * `carCid/carCid.car`, but in practise there is not validation that these CIDs are - * really a CAR CID. There is non CAR CIDs in this key format, and we MUST fallback - * to this format if a Blob is non existent for a RAW CID. + * `carCid/carCid.car`. * * @param {object} config * @param {S3Client} config.s3Client @@ -65,24 +60,6 @@ export function contentLocationResolver ({ s3Client, bucket, expiresIn }) { if (cid.code === RAW_CODE) { const encodedMultihash = base58btc.encode(cid.multihash.bytes) const blobKey = `${encodedMultihash}/${encodedMultihash}.blob` - // We MUST double check blob key actually exists before returning - // a presigned URL for it. - // This is required because `store/add` accepts to store data that - // did not have a CAR CID, and was still stored as `${CID}/${CID}.car` - const headCommand = new HeadObjectCommand({ - Bucket: bucket, - Key: blobKey, - }) - try { - await s3Client.send(headCommand) - } catch (err) { - if (err?.$metadata?.httpStatusCode === 404) { - // Fallback to attempt CAR CID - return signer.getUrl(carKey, { expiresIn }) - } - throw new Error(`Failed to HEAD object in bucket for: ${blobKey}`) - } - return signer.getUrl(blobKey, { expiresIn }) } return signer.getUrl(carKey, { expiresIn }) diff --git a/roundabout/test/index.test.js b/roundabout/test/index.test.js index f0141fdd..e6631d73 100644 --- a/roundabout/test/index.test.js +++ b/roundabout/test/index.test.js @@ -73,28 +73,6 @@ test('can create signed url for Blob in bucket and get it', async t => { t.assert(fetchResponse.ok) }) -test('fallsback to create signed url for CAR in bucket if Blob not found', async t => { - const bucketName = await createBucket(t.context.s3Client) - const rawCid = await putBlobAsCarToBucket(t.context.s3Client, bucketName) - const expiresIn = 3 * 24 * 60 * 60 // 3 days in seconds - - const locateContent = contentLocationResolver({ - bucket: bucketName, - s3Client: t.context.s3Client, - expiresIn - }) - - const signedUrl = await locateContent(rawCid) - if (!signedUrl) { - throw new Error('presigned url must be received') - } - t.truthy(signedUrl?.includes(`X-Amz-Expires=${expiresIn}`)) - t.truthy(signedUrl?.includes(`${rawCid}/${rawCid}.car`)) - - const fetchResponse = await fetch(signedUrl) - t.assert(fetchResponse.ok) -}) - test('fails to fetch from signed url for object not in bucket', async t => { const bucketName = await createBucket(t.context.s3Client) const carCid = CID.parse('bagbaiera222226db4v4oli5fldqghzgbv5rqv3n4ykyfxk7shfr42bfnqwua') @@ -176,27 +154,6 @@ async function getContent () { }) } -/** - * @param {import('@aws-sdk/client-s3').S3Client} s3Client - * @param {string} bucketName - */ -async function putBlobAsCarToBucket (s3Client, bucketName) { - // Write non CAR CID as .car - const content = await getContent() - const rawCid = new CID(1, RAW_CODE, content.cid.multihash, content.cid.multihash.bytes) - const key = `${rawCid.toString()}/${rawCid.toString()}.car` - await s3Client.send( - new PutObjectCommand({ - Bucket: bucketName, - Key: key, - Body: content.bytes, - }) - ) - - // Return RAW CID - return rawCid -} - /** * @param {import('@aws-sdk/client-s3').S3Client} s3Client * @param {string} bucketName