Skip to content

Commit

Permalink
feat: allow disabling s3 protocol (#595)
Browse files Browse the repository at this point in the history
  • Loading branch information
fenos authored Dec 4, 2024
1 parent 277de4e commit 77a456f
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 3 deletions.
1 change: 1 addition & 0 deletions migrations/multitenant/0013-s3-protocol-toggle.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE tenants ADD COLUMN IF NOT EXISTS feature_s3_protocol boolean DEFAULT true NOT NULL;
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ type StorageConfigType = {
tusPartSize: number
tusUseFileVersionSeparator: boolean
defaultMetricsEnabled: boolean
s3ProtocolEnabled: boolean
s3ProtocolPrefix: string
s3ProtocolAllowForwardedHeader: boolean
s3ProtocolEnforceRegion: boolean
Expand Down Expand Up @@ -252,6 +253,7 @@ export function getConfig(options?: { reload?: boolean }): StorageConfigType {
getOptionalConfigFromEnv('TUS_USE_FILE_VERSION_SEPARATOR') === 'true',

// S3 Protocol
s3ProtocolEnabled: getOptionalConfigFromEnv('S3_PROTOCOL_ENABLED') !== 'false',
s3ProtocolPrefix: getOptionalConfigFromEnv('S3_PROTOCOL_PREFIX') || '',
s3ProtocolAllowForwardedHeader:
getOptionalConfigFromEnv('S3_ALLOW_FORWARDED_HEADER') === 'true',
Expand Down
25 changes: 23 additions & 2 deletions src/http/routes/admin/tenants.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { FastifyInstance, RequestGenericInterface } from 'fastify'
import { FromSchema } from 'json-schema-to-ts'
import apiKey from '../../plugins/apikey'
import { decrypt, encrypt } from '../../../internal/auth'
import { decrypt, encrypt } from '@internal/auth'
import {
deleteTenantConfig,
TenantMigrationStatus,
multitenantKnex,
lastMigrationName,
runMigrationsOnTenant,
progressiveMigrations,
} from '../../../internal/database'
} from '@internal/database'
import { dbSuperUser, storage } from '../../plugins'

const patchSchema = {
Expand All @@ -35,6 +35,12 @@ const patchSchema = {
maxResolution: { type: 'number', nullable: true },
},
},
s3Protocol: {
type: 'object',
properties: {
enabled: { type: 'boolean' },
},
},
},
},
},
Expand Down Expand Up @@ -75,6 +81,7 @@ interface tenantDBInterface {
} | null
service_key: string
file_size_limit?: number
feature_s3_protocol?: boolean
feature_image_transformation?: boolean
image_transformation_max_resolution?: number
}
Expand All @@ -96,6 +103,7 @@ export default async function routes(fastify: FastifyInstance) {
jwks,
service_key,
feature_image_transformation,
feature_s3_protocol,
image_transformation_max_resolution,
migrations_version,
migrations_status,
Expand All @@ -118,6 +126,9 @@ export default async function routes(fastify: FastifyInstance) {
enabled: feature_image_transformation,
maxResolution: image_transformation_max_resolution,
},
s3Protocol: {
enabled: feature_s3_protocol,
},
},
})
)
Expand All @@ -137,6 +148,7 @@ export default async function routes(fastify: FastifyInstance) {
jwt_secret,
jwks,
service_key,
feature_s3_protocol,
feature_image_transformation,
image_transformation_max_resolution,
migrations_version,
Expand All @@ -163,6 +175,9 @@ export default async function routes(fastify: FastifyInstance) {
enabled: feature_image_transformation,
maxResolution: image_transformation_max_resolution,
},
s3Protocol: {
enabled: feature_s3_protocol,
},
},
migrationVersion: migrations_version,
migrationStatus: migrations_status,
Expand Down Expand Up @@ -197,6 +212,7 @@ export default async function routes(fastify: FastifyInstance) {
jwks,
service_key: encrypt(serviceKey),
feature_image_transformation: features?.imageTransformation?.enabled ?? false,
feature_s3_protocol: features?.s3Protocol?.enabled ?? true,
migrations_version: null,
migrations_status: null,
tracing_mode: tracingMode,
Expand Down Expand Up @@ -250,6 +266,7 @@ export default async function routes(fastify: FastifyInstance) {
jwks,
service_key: serviceKey !== undefined ? encrypt(serviceKey) : undefined,
feature_image_transformation: features?.imageTransformation?.enabled,
feature_s3_protocol: features?.s3Protocol?.enabled,
image_transformation_max_resolution:
features?.imageTransformation?.maxResolution === null
? null
Expand Down Expand Up @@ -315,6 +332,10 @@ export default async function routes(fastify: FastifyInstance) {
?.image_transformation_max_resolution as number | undefined
}

if (typeof features?.s3Protocol?.enabled !== 'undefined') {
tenantInfo.feature_s3_protocol = features?.s3Protocol?.enabled
}

if (databasePoolUrl) {
tenantInfo.database_pool_url = encrypt(databasePoolUrl)
}
Expand Down
11 changes: 10 additions & 1 deletion src/http/routes/s3/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { FastifyInstance, RouteHandlerMethod } from 'fastify'
import { JSONSchema } from 'json-schema-to-ts'
import { trace } from '@opentelemetry/api'
import { db, jsonToXml, signatureV4, storage } from '../../plugins'
import { db, jsonToXml, requireTenantFeature, signatureV4, storage } from '../../plugins'
import { findArrayPathsInSchemas, getRouter, RequestInput } from './router'
import { s3ErrorHandler } from './error-handler'
import { getConfig } from '../../../config'

const { s3ProtocolEnabled } = getConfig()

export default async function routes(fastify: FastifyInstance) {
if (!s3ProtocolEnabled) {
return
}

fastify.register(async (fastify) => {
const s3Router = getRouter()
const s3Routes = s3Router.routes()
Expand Down Expand Up @@ -97,6 +104,8 @@ export default async function routes(fastify: FastifyInstance) {
}

fastify.register(async (localFastify) => {
localFastify.register(requireTenantFeature('s3Protocol'))

const disableContentParser = routesByMethod?.some(
(route) => route.disableContentTypeParser
)
Expand Down
7 changes: 7 additions & 0 deletions src/internal/database/tenant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ export interface Features {
enabled: boolean
maxResolution?: number
}
s3Protocol: {
enabled: boolean
}
}

export enum TenantMigrationStatus {
Expand Down Expand Up @@ -203,6 +206,7 @@ export async function getTenantConfig(tenantId: string): Promise<TenantConfig> {
jwks,
service_key,
feature_image_transformation,
feature_s3_protocol,
image_transformation_max_resolution,
database_pool_url,
max_connections,
Expand Down Expand Up @@ -231,6 +235,9 @@ export async function getTenantConfig(tenantId: string): Promise<TenantConfig> {
enabled: feature_image_transformation,
maxResolution: image_transformation_max_resolution,
},
s3Protocol: {
enabled: feature_s3_protocol,
},
},
migrationVersion: migrations_version,
migrationStatus: migrations_status,
Expand Down
6 changes: 6 additions & 0 deletions src/test/tenant.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ const payload = {
enabled: true,
maxResolution: null,
},
s3Protocol: {
enabled: true,
},
},
}

Expand All @@ -43,6 +46,9 @@ const payload2 = {
enabled: false,
maxResolution: null,
},
s3Protocol: {
enabled: true,
},
},
}

Expand Down
3 changes: 3 additions & 0 deletions src/test/x-forwarded-host.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ beforeAll(async () => {
imageTransformation: {
enabled: true,
},
s3Protocol: {
enabled: true,
},
},
}))

Expand Down

0 comments on commit 77a456f

Please sign in to comment.