-
Notifications
You must be signed in to change notification settings - Fork 505
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Copy permissions and encrypted to new key
- Loading branch information
Showing
6 changed files
with
119 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { db, schema } from "@/lib/db"; | ||
import { ingestAuditLogs } from "@/lib/tinybird"; | ||
import { TRPCError } from "@trpc/server"; | ||
import { z } from "zod"; | ||
import { auth, t } from "../../trpc"; | ||
|
||
export const updateKeyEncrypted = t.procedure | ||
.use(auth) | ||
.input( | ||
z.object({ | ||
keyId: z.string(), | ||
encrypted: z.string(), | ||
encryptiodKeyId: z.string(), | ||
}), | ||
) | ||
.mutation(async ({ input, ctx }) => { | ||
const key = await db.query.keys.findFirst({ | ||
where: (table, { eq, isNull, and }) => | ||
and(eq(table.id, input.keyId), isNull(table.deletedAt)), | ||
with: { | ||
workspace: true, | ||
}, | ||
}); | ||
if (!key || key.workspace.tenantId !== ctx.tenant.id) { | ||
throw new TRPCError({ | ||
message: | ||
"We are unable to find the correct key. Please contact support using [email protected].", | ||
code: "NOT_FOUND", | ||
}); | ||
} | ||
|
||
const tuple = { | ||
keyId: input.keyId, | ||
encrypted: input.encrypted, | ||
encryptionKeyId: input.encryptiodKeyId, | ||
workspaceId: ctx.tenant.id, | ||
} | ||
await db | ||
.insert(schema.encryptedKeys) | ||
.values({ ...tuple }) | ||
.onDuplicateKeyUpdate({ set: { ...tuple }}) | ||
.catch((_err) => { | ||
throw new TRPCError({ | ||
code: "INTERNAL_SERVER_ERROR", | ||
message: | ||
"We are unable to update key encrypted. Please contact support using [email protected]", | ||
}); | ||
}); | ||
|
||
await ingestAuditLogs({ | ||
workspaceId: key.workspace.id, | ||
actor: { | ||
type: "user", | ||
id: ctx.user.id, | ||
}, | ||
event: "key.update", | ||
description: `Created a encrypted relation to ${key.id}`, | ||
resources: [ | ||
{ | ||
type: "key", | ||
id: key.id, | ||
}, | ||
], | ||
context: { | ||
location: ctx.audit.location, | ||
userAgent: ctx.audit.userAgent, | ||
}, | ||
}); | ||
return true; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,11 +6,11 @@ import { z } from "zod"; | |
import { auth, t } from "../../trpc"; | ||
import { upsertPermissions } from "../rbac"; | ||
|
||
export const addPermissionToRootKey = t.procedure | ||
export const addPermissionToKey = t.procedure | ||
.use(auth) | ||
.input( | ||
z.object({ | ||
rootKeyId: z.string(), | ||
keyId: z.string(), | ||
permission: z.string(), | ||
}), | ||
) | ||
|
@@ -35,9 +35,9 @@ export const addPermissionToRootKey = t.procedure | |
}); | ||
} | ||
|
||
const rootKey = await db.query.keys.findFirst({ | ||
const key = await db.query.keys.findFirst({ | ||
where: (table, { eq, and }) => | ||
and(eq(table.forWorkspaceId, workspace.id), eq(table.id, input.rootKeyId)), | ||
and(eq(table.forWorkspaceId, workspace.id), eq(table.id, input.keyId)), | ||
with: { | ||
permissions: { | ||
with: { | ||
|
@@ -46,22 +46,22 @@ export const addPermissionToRootKey = t.procedure | |
}, | ||
}, | ||
}); | ||
if (!rootKey) { | ||
if (!key) { | ||
throw new TRPCError({ | ||
code: "NOT_FOUND", | ||
message: | ||
"We are unable to find the correct root key. Please contact support using [email protected].", | ||
}); | ||
} | ||
|
||
const { permissions, auditLogs } = await upsertPermissions(ctx, rootKey.workspaceId, [ | ||
const { permissions, auditLogs } = await upsertPermissions(ctx, key.workspaceId, [ | ||
permission.data, | ||
]); | ||
const p = permissions[0]; | ||
await db | ||
.insert(schema.keysPermissions) | ||
.values({ | ||
keyId: rootKey.id, | ||
keyId: key.id, | ||
permissionId: p.id, | ||
workspaceId: p.workspaceId, | ||
}) | ||
|
@@ -80,11 +80,11 @@ export const addPermissionToRootKey = t.procedure | |
workspaceId: workspace.id, | ||
actor: { type: "user", id: ctx.user.id }, | ||
event: "authorization.connect_permission_and_key", | ||
description: `Attached ${p.id} to ${rootKey.id}`, | ||
description: `Attached ${p.id} to ${key.id}`, | ||
resources: [ | ||
{ | ||
type: "key", | ||
id: rootKey.id, | ||
id: key.id, | ||
}, | ||
{ | ||
type: "permission", | ||
|