Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: s3 post handler #594

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,405 changes: 745 additions & 660 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"xml2js": "^0.6.2"
},
"devDependencies": {
"@aws-sdk/s3-presigned-post": "3.654.0",
"@types/async-retry": "^1.4.5",
"@types/busboy": "^1.3.0",
"@types/crypto-js": "^4.1.1",
Expand Down
32 changes: 30 additions & 2 deletions src/http/plugins/signature-v4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { signJWT, verifyJWT } from '@internal/auth'
import { ERRORS } from '@internal/errors'

import { getConfig } from '../../config'
import { MultipartFile } from '@fastify/multipart'

const {
anonKey,
Expand All @@ -25,10 +26,16 @@ const {

type AWSRequest = FastifyRequest<{ Querystring: { 'X-Amz-Credential'?: string } }>

declare module 'fastify' {
interface FastifyRequest {
multiPartFileStream?: MultipartFile
}
}

export const signatureV4 = fastifyPlugin(
async function (fastify: FastifyInstance) {
fastify.addHook('preHandler', async (request: AWSRequest) => {
const clientSignature = extractSignature(request)
const clientSignature = await extractSignature(request)

const sessionToken = clientSignature.sessionToken

Expand Down Expand Up @@ -101,7 +108,7 @@ export const signatureV4 = fastifyPlugin(
{ name: 'auth-signature-v4' }
)

function extractSignature(req: AWSRequest) {
async function extractSignature(req: AWSRequest) {
if (typeof req.headers.authorization === 'string') {
return SignatureV4.parseAuthorizationHeader(req.headers)
}
Expand All @@ -110,6 +117,27 @@ function extractSignature(req: AWSRequest) {
return SignatureV4.parseQuerySignature(req.query)
}

if (typeof req.isMultipart === 'function' && req.isMultipart()) {
const formData = new FormData()
const data = await req.file({
limits: {
files: 1,
},
})

const fields = data?.fields
if (fields) {
for (const key in fields) {
if (fields.hasOwnProperty(key) && (fields[key] as any).fieldname !== 'file') {
formData.append(key, (fields[key] as any).value)
}
}
}
// Assign the multipartFileStream for later use
req.multiPartFileStream = data
return SignatureV4.parseMultipartSignature(formData)
}

throw ERRORS.AccessDenied('Missing signature')
}

Expand Down
2 changes: 1 addition & 1 deletion src/http/plugins/xml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const jsonToXml = fastifyPlugin(

if (!opts.disableContentParser) {
fastify.register(xmlBodyParser, {
contentType: ['text/xml', 'application/xml', '*'],
contentType: ['text/xml', 'application/xml'],
isArray: (_: string, jpath: string) => {
return opts.parseAsArray?.includes(jpath)
},
Expand Down
9 changes: 5 additions & 4 deletions src/http/routes/object/createObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { FromSchema } from 'json-schema-to-ts'
import { createDefaultSchema } from '../../routes-helper'
import { ROUTE_OPERATIONS } from '../operations'
import fastifyMultipart from '@fastify/multipart'
import { fileUploadFromRequest } from '@storage/uploader'

const createObjectParamsSchema = {
type: 'object',
Expand Down Expand Up @@ -72,15 +73,15 @@ export default async function routes(fastify: FastifyInstance) {
const objectName = request.params['*']

const isUpsert = request.headers['x-upsert'] === 'true'
const owner = request.owner as string
const owner = request.owner

const { objectMetadata, path, id } = await request.storage
.from(bucketName)
.uploadNewObject(request, {
.uploadFromRequest(request, {
objectName,
owner,
isUpsert,
signal: request.signals.body.signal,
owner: owner,
isUpsert,
})

return response.status(objectMetadata?.httpStatusCode ?? 200).send({
Expand Down
8 changes: 5 additions & 3 deletions src/http/routes/object/updateObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { FromSchema } from 'json-schema-to-ts'
import { createDefaultSchema } from '../../routes-helper'
import { ROUTE_OPERATIONS } from '../operations'
import fastifyMultipart from '@fastify/multipart'
import { fileUploadFromRequest } from '@storage/uploader'

const updateObjectParamsSchema = {
type: 'object',
Expand Down Expand Up @@ -74,10 +75,11 @@ export default async function routes(fastify: FastifyInstance) {

const { objectMetadata, path, id } = await request.storage
.from(bucketName)
.uploadOverridingObject(request, {
owner,
objectName: objectName,
.uploadFromRequest(request, {
objectName,
signal: request.signals.body.signal,
owner: owner,
isUpsert: true,
})

return response.status(objectMetadata?.httpStatusCode ?? 200).send({
Expand Down
2 changes: 1 addition & 1 deletion src/http/routes/object/uploadSignedObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export default async function routes(fastify: FastifyInstance) {
const { objectMetadata, path } = await request.storage
.asSuperUser()
.from(bucketName)
.uploadNewObject(request, {
.uploadFromRequest(request, {
owner,
objectName,
isUpsert: upsert,
Expand Down
153 changes: 153 additions & 0 deletions src/http/routes/s3/commands/put-object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import { S3ProtocolHandler } from '@storage/protocols/s3/s3-handler'
import { S3Router } from '../router'
import { ROUTE_OPERATIONS } from '../../operations'
import { Multipart, MultipartValue } from '@fastify/multipart'
import { Policy } from '@storage/protocols/s3'
import { fileUploadFromRequest } from '@storage/uploader'
import { ERRORS } from '@internal/errors'

const PutObjectInput = {
summary: 'Put Object',
Params: {
type: 'object',
properties: {
Bucket: { type: 'string' },
'*': { type: 'string' },
},
required: ['Bucket', '*'],
},
Querystring: {
type: 'object',
},
Headers: {
type: 'object',
properties: {
authorization: { type: 'string' },
host: { type: 'string' },
'x-amz-content-sha256': { type: 'string' },
'x-amz-date': { type: 'string' },
'content-type': { type: 'string' },
'content-length': { type: 'integer' },
'cache-control': { type: 'string' },
'content-disposition': { type: 'string' },
'content-encoding': { type: 'string' },
expires: { type: 'string' },
},
required: ['content-length'],
},
} as const

const PostFormInput = {
summary: 'PostForm Object',
Params: {
type: 'object',
properties: {
Bucket: { type: 'string' },
},
required: ['Bucket'],
},
} as const

export default function PutObject(s3Router: S3Router) {
s3Router.put(
'/:Bucket/*',
{
schema: PutObjectInput,
operation: ROUTE_OPERATIONS.S3_UPLOAD,
disableContentTypeParser: true,
},
async (req, ctx) => {
const s3Protocol = new S3ProtocolHandler(ctx.storage, ctx.tenantId, ctx.owner)

const metadata = s3Protocol.parseMetadataHeaders(req.Headers)
const contentLength = req.Headers['content-length']
let key = req.Params['*']

if (key.endsWith('/') && contentLength === 0) {
// Consistent with how supabase Storage handles empty folders
key += '.emptyFolderPlaceholder'
}

const bucket = await ctx.storage
.asSuperUser()
.findBucket(req.Params.Bucket, 'id,file_size_limit,allowed_mime_types', {})

const uploadRequest = await fileUploadFromRequest(ctx.req, {
objectName: key,
allowedMimeTypes: bucket.allowed_mime_types || [],
fileSizeLimit: bucket.file_size_limit || undefined,
})

return s3Protocol.putObject(
{
Body: uploadRequest.body,
Bucket: req.Params.Bucket,
Key: key,
CacheControl: uploadRequest.cacheControl,
ContentType: uploadRequest.mimeType,
Expires: req.Headers?.['expires'] ? new Date(req.Headers?.['expires']) : undefined,
ContentEncoding: req.Headers?.['content-encoding'],
Metadata: metadata,
},
{ signal: ctx.signals.body, isTruncated: uploadRequest.isTruncated }
)
}
)

s3Router.post(
'/:Bucket|content-type=multipart/form-data',
{
schema: PostFormInput,
operation: ROUTE_OPERATIONS.S3_UPLOAD,
disableContentTypeParser: true,
acceptMultiformData: true,
},
async (req, ctx) => {
const s3Protocol = new S3ProtocolHandler(ctx.storage, ctx.tenantId, ctx.owner)

const file = ctx.req.multiPartFileStream

if (!file) {
throw ERRORS.InvalidParameter('Missing file')
}

const metadata = s3Protocol.parseMetadataHeaders(file?.fields || {})
const policy = normaliseFormDataField(file?.fields?.Policy)
const xPolicy: Policy = JSON.parse(
Buffer.from(policy as string, 'base64').toString('utf-8') as string
)

// const key = xPolicy.conditions.policies.find()

return s3Protocol.putObject(
{
Body: file?.file,
Bucket: req.Params.Bucket,
Key: normaliseFormDataField(file?.fields?.key) as string,
CacheControl: normaliseFormDataField(file?.fields?.['Cache-Control']) as string,
ContentType: normaliseFormDataField(file?.fields?.['Content-Type']) as string,
// Expires: req.Headers?.['expires'] ? new Date(req.Headers?.['expires']) : undefined,
ContentEncoding: normaliseFormDataField(file?.fields?.['Content-Encoding']) as string,
Metadata: metadata,
},
{ signal: ctx.signals.body, isTruncated: () => file.file.truncated }
)
}
)
}

function normaliseFormDataField(value: Multipart | Multipart[] | undefined) {
if (!value) {
return undefined
}

if (Array.isArray(value)) {
return (value[0] as MultipartValue).value as string
}

if (value.type === 'field') {
return value.value
}

return value.file
}
58 changes: 0 additions & 58 deletions src/http/routes/s3/commands/upload-part.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,6 @@ import { S3ProtocolHandler } from '@storage/protocols/s3/s3-handler'
import { S3Router } from '../router'
import { ROUTE_OPERATIONS } from '../../operations'

const PutObjectInput = {
summary: 'Put Object',
Params: {
type: 'object',
properties: {
Bucket: { type: 'string' },
'*': { type: 'string' },
},
required: ['Bucket', '*'],
},
Querystring: {
type: 'object',
},
Headers: {
type: 'object',
properties: {
authorization: { type: 'string' },
host: { type: 'string' },
'x-amz-content-sha256': { type: 'string' },
'x-amz-date': { type: 'string' },
'content-type': { type: 'string' },
'content-length': { type: 'integer' },
'cache-control': { type: 'string' },
'content-disposition': { type: 'string' },
'content-encoding': { type: 'string' },
expires: { type: 'string' },
},
},
} as const

const UploadPartInput = {
summary: 'Upload Part',
Params: {
Expand Down Expand Up @@ -84,32 +54,4 @@ export default function UploadPart(s3Router: S3Router) {
})
}
)

s3Router.put(
'/:Bucket/*',
{
schema: PutObjectInput,
operation: ROUTE_OPERATIONS.S3_UPLOAD,
disableContentTypeParser: true,
},
(req, ctx) => {
const s3Protocol = new S3ProtocolHandler(ctx.storage, ctx.tenantId, ctx.owner)

const metadata = s3Protocol.parseMetadataHeaders(req.Headers)

return s3Protocol.putObject(
{
Body: ctx.req as any,
Bucket: req.Params.Bucket,
Key: req.Params['*'],
CacheControl: req.Headers?.['cache-control'],
ContentType: req.Headers?.['content-type'],
Expires: req.Headers?.['expires'] ? new Date(req.Headers?.['expires']) : undefined,
ContentEncoding: req.Headers?.['content-encoding'],
Metadata: metadata,
},
ctx.signals.body
)
}
)
}
Loading
Loading