Skip to content

Commit 12ad715

Browse files
committed
fix(s3): creating public bucket with object ACL enabled
configuring S3 to allow download of verification transcript
1 parent c28e2f0 commit 12ad715

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

packages/backend/src/functions/circuit.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,8 @@ export const verifycontribution = functionsV2.https.onCall(
494494
await uploadFileToBucket(
495495
bucketName,
496496
verificationTranscriptStoragePathAndFilename,
497-
verificationTranscriptTemporaryLocalPath
497+
verificationTranscriptTemporaryLocalPath,
498+
true
498499
)
499500

500501
// Compute verification transcript hash.

packages/backend/src/functions/storage.ts

+37-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import {
66
UploadPartCommand,
77
CompleteMultipartUploadCommand,
88
HeadObjectCommand,
9-
CreateBucketCommand
9+
CreateBucketCommand,
10+
PutPublicAccessBlockCommand,
11+
PutBucketCorsCommand
1012
} from "@aws-sdk/client-s3"
1113
import { getSignedUrl } from "@aws-sdk/s3-request-presigner"
1214
import dotenv from "dotenv"
@@ -146,7 +148,8 @@ export const createBucket = functions
146148
Bucket: data.bucketName,
147149
CreateBucketConfiguration: {
148150
LocationConstraint: String(process.env.AWS_REGION)
149-
}
151+
},
152+
ObjectOwnership: "BucketOwnerPreferred"
150153
})
151154

152155
try {
@@ -156,6 +159,37 @@ export const createBucket = functions
156159
// Check response.
157160
if (response.$metadata.httpStatusCode === 200 && !!response.Location)
158161
printLog(`The AWS S3 bucket ${data.bucketName} has been created successfully`, LogLevel.LOG)
162+
163+
const publicBlockCommand = new PutPublicAccessBlockCommand({
164+
Bucket: data.bucketName,
165+
PublicAccessBlockConfiguration: {
166+
BlockPublicAcls: false,
167+
BlockPublicPolicy: false,
168+
}
169+
})
170+
171+
// Allow objects to be public
172+
const publicBlockResponse = await S3.send(publicBlockCommand)
173+
// Check response.
174+
if (publicBlockResponse.$metadata.httpStatusCode === 200)
175+
printLog(`The AWS S3 bucket ${data.bucketName} has been set with the PublicAccessBlock disabled.`, LogLevel.LOG)
176+
177+
// Set CORS
178+
const corsCommand = new PutBucketCorsCommand({
179+
Bucket: data.bucketName,
180+
CORSConfiguration: {
181+
CORSRules: [
182+
{
183+
AllowedMethods: ["GET"],
184+
AllowedOrigins: ["*"],
185+
}
186+
]
187+
}
188+
})
189+
const corsResponse = await S3.send(corsCommand)
190+
// Check response.
191+
if (corsResponse.$metadata.httpStatusCode === 200)
192+
printLog(`The AWS S3 bucket ${data.bucketName} has been set with the CORS configuration.`, LogLevel.LOG)
159193
} catch (error: any) {
160194
/** * {@link https://docs.aws.amazon.com/simspaceweaver/latest/userguide/troubleshooting_bucket-name-too-long.html | InvalidBucketName} */
161195
if (error.$metadata.httpStatusCode === 400 && error.Code === `InvalidBucketName`)
@@ -308,7 +342,7 @@ export const startMultiPartUpload = functions
308342
const S3 = await getS3Client()
309343

310344
// Prepare S3 command.
311-
const command = new CreateMultipartUploadCommand({ Bucket: bucketName, Key: objectKey })
345+
const command = new CreateMultipartUploadCommand({ Bucket: bucketName, Key: objectKey, ACL: "private" })
312346

313347
try {
314348
// Execute S3 command.

packages/backend/src/lib/utils.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -220,15 +220,15 @@ export const downloadArtifactFromS3Bucket = async (bucketName: string, objectKey
220220
* @param objectKey <string> - the unique key to identify the object inside the given AWS S3 bucket.
221221
* @param localFilePath <string> - the local path where the file to be uploaded is stored.
222222
*/
223-
export const uploadFileToBucket = async (bucketName: string, objectKey: string, localFilePath: string) => {
223+
export const uploadFileToBucket = async (bucketName: string, objectKey: string, localFilePath: string, isPublic: boolean = false) => {
224224
// Prepare AWS S3 client instance.
225225
const client = await getS3Client()
226226

227227
// Extract content type.
228228
const contentType = mime.lookup(localFilePath) || ""
229229

230230
// Prepare command.
231-
const command = new PutObjectCommand({ Bucket: bucketName, Key: objectKey, ContentType: contentType })
231+
const command = new PutObjectCommand({ Bucket: bucketName, Key: objectKey, ContentType: contentType, ACL: isPublic ? "public-read" : "private" })
232232

233233
// Generate a pre-signed url for uploading the file.
234234
const url = await getSignedUrl(client, command, { expiresIn: Number(process.env.AWS_PRESIGNED_URL_EXPIRATION) })

0 commit comments

Comments
 (0)