Skip to content

Commit

Permalink
fix: roundabout raw cid does not fallback to car when raw
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Apr 26, 2024
1 parent 5ac61d3 commit 3c8dafd
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 68 deletions.
27 changes: 2 additions & 25 deletions roundabout/index.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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
Expand All @@ -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 })
Expand Down
43 changes: 0 additions & 43 deletions roundabout/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3c8dafd

Please sign in to comment.