fix: health endpoint s3 connection (#2136)

This commit is contained in:
Matti Nannt
2024-02-27 13:20:49 +01:00
committed by GitHub
parent b1c2f90feb
commit 32dcb75ef8
2 changed files with 18 additions and 17 deletions

View File

@@ -3,7 +3,7 @@ import { Metadata } from "next";
import { prisma } from "@formbricks/database";
import { IS_S3_CONFIGURED } from "@formbricks/lib/constants";
import { testS3Connection } from "@formbricks/lib/storage/service";
import { testS3BucketAccess } from "@formbricks/lib/storage/service";
export const dynamic = "force-dynamic"; // no caching
@@ -31,8 +31,11 @@ const checkS3Connection = async () => {
// dont try connecting if not in use
return;
}
await testS3Connection();
try {
await testS3BucketAccess();
} catch (e) {
throw new Error("S3 Bucket cannot be accessed");
}
};
export default async function HealthPage() {

View File

@@ -2,7 +2,7 @@ import {
DeleteObjectCommand,
DeleteObjectsCommand,
GetObjectCommand,
ListBucketsCommand,
HeadBucketCommand,
ListObjectsCommand,
PutObjectCommand,
S3Client,
@@ -50,23 +50,21 @@ export const getS3Client = () => {
return s3ClientInstance;
};
export const testS3Connection = async () => {
export const testS3BucketAccess = async () => {
const s3Client = getS3Client();
try {
const s3Client = getS3Client();
const cmd = new ListBucketsCommand({});
const result = await s3Client.send(cmd);
// Attempt to retrieve metadata about the bucket
const headBucketCommand = new HeadBucketCommand({
Bucket: S3_BUCKET_NAME,
});
if (!result.Buckets) {
throw new Error("Access denied to any buckets");
}
await s3Client.send(headBucketCommand);
const bucketNames = result.Buckets.map((b) => b.Name);
if (!bucketNames.includes(S3_BUCKET_NAME)) {
throw new Error("Access denied to bucket");
}
return true;
} catch (error) {
throw new Error(`S3: ${error}`);
console.error("Failed to access S3 bucket:", error);
throw new Error(`S3 Bucket Access Test Failed: ${error}`);
}
};