|
| 1 | +import type { APIGatewayProxyHandler } from 'aws-lambda'; |
| 2 | +import { GetObjectCommand, PutObjectCommand } from '@aws-sdk/client-s3'; |
| 3 | +import { S3RequestPresigner } from '@aws-sdk/s3-request-presigner'; |
| 4 | +import { Hash } from '@aws-sdk/hash-node'; |
| 5 | +import { parseUrl } from '@aws-sdk/url-parser'; |
| 6 | +import { HttpRequest } from '@aws-sdk/protocol-http'; |
| 7 | +import { formatUrl } from '@aws-sdk/util-format-url'; |
| 8 | + |
| 9 | +/** |
| 10 | + * Preflight request for the artifact resource |
| 11 | + * |
| 12 | + * @param event lambda event |
| 13 | + */ |
| 14 | +export const handler: APIGatewayProxyHandler = async (event) => { |
| 15 | + const requestMethod = event.headers['access-control-request-method']; |
| 16 | + const signableHeaders = event.headers['access-control-request-headers']?.split(',').map(header => header.trim()).filter(header => header !== 'Authorization'); |
| 17 | + const requestHeaders = signableHeaders?.reduce((acc: Record<string, string>, header) => { |
| 18 | + if (event.headers[header]) { |
| 19 | + acc[header] = event.headers[header]; |
| 20 | + } |
| 21 | + return acc; |
| 22 | + }, {}); |
| 23 | + |
| 24 | + if (!requestMethod) { |
| 25 | + return { |
| 26 | + statusCode: 400, |
| 27 | + body: JSON.stringify({ error: 'Missing required headers' }), |
| 28 | + }; |
| 29 | + } |
| 30 | + |
| 31 | + const teamId = event.requestContext.authorizer?.teamId; |
| 32 | + |
| 33 | + if (!teamId) { |
| 34 | + return { |
| 35 | + statusCode: 400, |
| 36 | + body: JSON.stringify({ error: 'Missing teamId' }), |
| 37 | + }; |
| 38 | + } |
| 39 | + |
| 40 | + let command: GetObjectCommand | PutObjectCommand | undefined; |
| 41 | + if (requestMethod === 'GET') { |
| 42 | + command = new GetObjectCommand({ |
| 43 | + Bucket: process.env.ARTIFACTS_BUCKET, |
| 44 | + Key: `${teamId}/${event.pathParameters?.hash}`, |
| 45 | + }); |
| 46 | + } else if (requestMethod === 'PUT') { |
| 47 | + command = new PutObjectCommand({ |
| 48 | + Bucket: process.env.ARTIFACTS_BUCKET, |
| 49 | + Key: `${teamId}/${event.pathParameters?.hash}`, |
| 50 | + ContentType: event.headers['content-type'], |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + if (!command) { |
| 55 | + return { |
| 56 | + statusCode: 400, |
| 57 | + body: JSON.stringify({ error: 'Invalid request method' }), |
| 58 | + }; |
| 59 | + } |
| 60 | + |
| 61 | + if (!process.env.AWS_ACCESS_KEY_ID || !process.env.AWS_SECRET_ACCESS_KEY || !process.env.AWS_REGION) { |
| 62 | + throw new Error('AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_REGION must be set'); |
| 63 | + } |
| 64 | + |
| 65 | + const presigner = new S3RequestPresigner({ |
| 66 | + credentials: { |
| 67 | + accessKeyId: process.env.AWS_ACCESS_KEY_ID, |
| 68 | + secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, |
| 69 | + sessionToken: process.env.AWS_SESSION_TOKEN, |
| 70 | + }, |
| 71 | + region: process.env.AWS_REGION, |
| 72 | + sha256: Hash.bind(null, 'sha256'), |
| 73 | + }); |
| 74 | + |
| 75 | + const url = parseUrl(`https://${process.env.ARTIFACTS_BUCKET}.s3.${process.env.AWS_REGION}.amazonaws.com/${teamId}/${event.pathParameters?.hash}`); |
| 76 | + const request = new HttpRequest({ |
| 77 | + ...url, |
| 78 | + method: requestMethod, |
| 79 | + query: { |
| 80 | + teamId: teamId, |
| 81 | + }, |
| 82 | + headers: requestHeaders, |
| 83 | + }); |
| 84 | + const presignedUrl = await presigner.presign(request, { |
| 85 | + expiresIn: 60 * 60 * 24, |
| 86 | + signableHeaders: signableHeaders ? new Set(signableHeaders) : undefined, |
| 87 | + }); |
| 88 | + const formattedUrl = formatUrl(presignedUrl); |
| 89 | + |
| 90 | + // turborepo cli appends teamId to the url, so we need to remove it to ensure valid signature |
| 91 | + const responseUrl = new URL(formattedUrl); |
| 92 | + responseUrl.searchParams.delete('teamId'); |
| 93 | + |
| 94 | + return { |
| 95 | + statusCode: 200, |
| 96 | + headers: { |
| 97 | + location: responseUrl.href, |
| 98 | + allow_authorization_header: false, |
| 99 | + }, |
| 100 | + body: '', |
| 101 | + }; |
| 102 | +}; |
0 commit comments