Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: address organization storage review

Richie McIlroy31f08a405c64
diff --git a/apps/web/actions/organization/storage.ts b/apps/web/actions/organization/storage.ts
index 6533cb4fb..3af710940 100644
--- a/apps/web/actions/organization/storage.ts
+++ b/apps/web/actions/organization/storage.ts
@@ -22,7 +22,7 @@ import {
getGoogleDriveUserEmail,
} from "@cap/web-backend";
import { type Organisation, S3Bucket, Storage } from "@cap/web-domain";
-import { and, desc, eq } from "drizzle-orm";
+import { and, desc, eq, isNull } from "drizzle-orm";
import { revalidatePath } from "next/cache";
import { runPromise } from "@/lib/server";
@@ -95,7 +95,12 @@ const requireOrganizationOwner = async (
ownerId: organizations.ownerId,
})
.from(organizations)
- .where(eq(organizations.id, organizationId))
+ .where(
+ and(
+ eq(organizations.id, organizationId),
+ isNull(organizations.tombstoneAt),
+ ),
+ )
.limit(1);
if (!organization) throw new Error("Organization not found");
@@ -106,11 +111,14 @@ const requireOrganizationOwner = async (
return { user, organization };
};
-const decryptS3Config = async (bucket: typeof s3Buckets.$inferSelect) => ({
+const decryptS3Config = async (
+ bucket: typeof s3Buckets.$inferSelect,
+ exposeSecrets: boolean,
+) => ({
configured: true,
provider: bucket.provider,
- accessKeyId: await decrypt(bucket.accessKeyId),
- secretAccessKey: await decrypt(bucket.secretAccessKey),
+ accessKeyId: exposeSecrets ? await decrypt(bucket.accessKeyId) : "",
+ secretAccessKey: exposeSecrets ? await decrypt(bucket.secretAccessKey) : "",
endpoint: bucket.endpoint
? await decrypt(bucket.endpoint)
: "https://s3.amazonaws.com",
@@ -226,6 +234,34 @@ const getOrganizationBucket = async (
return bucket ?? null;
};
+const getS3InputCredentials = async (input: S3ConfigInput) => {
+ const hasAccessKeyId = input.accessKeyId.trim().length > 0;
+ const hasSecretAccessKey = input.secretAccessKey.trim().length > 0;
+
+ if (hasAccessKeyId && hasSecretAccessKey) {
+ return {
+ accessKeyId: input.accessKeyId,
+ secretAccessKey: input.secretAccessKey,
+ };
+ }
+
+ const existingBucket = await getOrganizationBucket(input.organizationId);
+ if (!existingBucket) {
+ throw new Error("Access key ID and secret access key are required");
+ }
+
+ if (hasAccessKeyId || hasSecretAccessKey) {
+ throw new Error(
+ "Enter both access key ID and secret access key to change credentials",
+ );
+ }
+
+ return {
+ accessKeyId: await decrypt(existingBucket.accessKeyId),
+ secretAccessKey: await decrypt(existingBucket.secretAccessKey),
+ };
+};
+
const revalidateStorageSettings = () => {
revalidatePath(settingsPath);
revalidatePath("/dashboard/settings/organization");
@@ -272,7 +308,7 @@ export async function getOrganizationStorageSettings(
activeProvider,
googleOAuthClientId: process.env.GOOGLE_CLIENT_ID ?? null,
googlePickerApiKey: process.env.NEXT_PUBLIC_GOOGLE_PICKER_API_KEY ?? null,
- s3: bucket ? await decryptS3Config(bucket) : null,
+ s3: bucket ? await decryptS3Config(bucket, false) : null,
googleDrive:
drive && driveConfig
? {
@@ -293,10 +329,11 @@ export async function getOrganizationStorageSettings(
export async function saveOrganizationS3Config(input: S3ConfigInput) {
const { user } = await requireOrganizationOwner(input.organizationId);
+ const credentials = await getS3InputCredentials(input);
const encryptedConfig = {
provider: input.provider,
- accessKeyId: await encrypt(input.accessKeyId),
- secretAccessKey: await encrypt(input.secretAccessKey),
+ accessKeyId: await encrypt(credentials.accessKeyId),
+ secretAccessKey: await encrypt(credentials.secretAccessKey),
endpoint: input.endpoint ? await encrypt(input.endpoint) : null,
bucketName: await encrypt(input.bucketName),
region: await encrypt(input.region),
@@ -334,14 +371,15 @@ export async function removeOrganizationS3Config(
export async function testOrganizationS3Config(input: S3ConfigInput) {
await requireOrganizationOwner(input.organizationId);
+ const credentials = await getS3InputCredentials(input);
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 5000);
const s3Client = new S3Client({
endpoint: input.endpoint || undefined,
region: input.region,
credentials: {
- accessKeyId: input.accessKeyId,
- secretAccessKey: input.secretAccessKey,
+ accessKeyId: credentials.accessKeyId,
+ secretAccessKey: credentials.secretAccessKey,
},
});
diff --git a/apps/web/app/(org)/dashboard/settings/organization/integrations/page.tsx b/apps/web/app/(org)/dashboard/settings/organization/integrations/page.tsx
index 474b86de9..abb7c13fe 100644
--- a/apps/web/app/(org)/dashboard/settings/organization/integrations/page.tsx
+++ b/apps/web/app/(org)/dashboard/settings/organization/integrations/page.tsx
@@ -21,7 +21,17 @@ export default async function OrganizationIntegrationsPage() {
const settings = await getOrganizationStorageSettings(
user.activeOrganizationId,
- );
+ ).catch((error: unknown) => {
+ if (
+ error instanceof Error &&
+ (error.message === "Only the owner can manage organization storage" ||
+ error.message === "Organization not found")
+ ) {
+ redirect("/dashboard/caps");
+ }
+
+ throw error;
+ });
return <OrganizationStorageIntegrations initialSettings={settings} />;
}
diff --git a/apps/web/app/(org)/dashboard/settings/organization/integrations/storage-integrations.tsx b/apps/web/app/(org)/dashboard/settings/organization/integrations/storage-integrations.tsx
index f1a6513e6..f6fb0678e 100644
--- a/apps/web/app/(org)/dashboard/settings/organization/integrations/storage-integrations.tsx
+++ b/apps/web/app/(org)/dashboard/settings/organization/integrations/storage-integrations.tsx
@@ -275,7 +275,7 @@ export function OrganizationStorageIntegrations({
<InfoIcon className="size-3.5 text-gray-9 shrink-0" />
<p className="text-[12px] text-gray-9">
Storage applies to all members of {settings.organization.name}. Only
- owners and admins can manage integrations.
+ owners can manage integrations.
</p>
</div>
@@ -368,7 +368,9 @@ export function OrganizationStorageIntegrations({
id={accessKeyId}
type="password"
value={s3Config.accessKeyId}
- placeholder="PL31OADSQNK"
+ placeholder={
+ settings.s3?.configured ? "Stored securely" : "PL31OADSQNK"
+ }
autoComplete="off"
onChange={(event) =>
setS3Config((current) => ({
@@ -386,7 +388,9 @@ export function OrganizationStorageIntegrations({
id={secretKeyId}
type="password"
value={s3Config.secretAccessKey}
- placeholder="PL31OADSQNK"
+ placeholder={
+ settings.s3?.configured ? "Stored securely" : "PL31OADSQNK"
+ }
autoComplete="off"
onChange={(event) =>
setS3Config((current) => ({
diff --git a/apps/web/app/(org)/dashboard/settings/organization/layout.tsx b/apps/web/app/(org)/dashboard/settings/organization/layout.tsx
index ecd5459b2..7f3089e12 100644
--- a/apps/web/app/(org)/dashboard/settings/organization/layout.tsx
+++ b/apps/web/app/(org)/dashboard/settings/organization/layout.tsx
@@ -1,7 +1,7 @@
import { db } from "@cap/database";
import { getCurrentUser } from "@cap/database/auth/session";
import { organizationMembers, organizations } from "@cap/database/schema";
-import { and, eq } from "drizzle-orm";
+import { and, eq, isNull } from "drizzle-orm";
import { redirect } from "next/navigation";
import { SettingsNav } from "./_components/SettingsNav";
@@ -29,6 +29,7 @@ export default async function OrganizationSettingsLayout({
and(
eq(organizationMembers.userId, user.id),
eq(organizations.id, user.activeOrganizationId),
+ isNull(organizations.tombstoneAt),
),
)
.limit(1);