Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

refactor: unify key metadata structure in audit log and cmek service; enhance CmekTable for better key selection handling

Victor Hugo dos Santosed105f8d94bd
diff --git a/backend/src/ee/services/audit-log/audit-log-types.ts b/backend/src/ee/services/audit-log/audit-log-types.ts
index 872ea08448..f2b56ec2d6 100644
--- a/backend/src/ee/services/audit-log/audit-log-types.ts
+++ b/backend/src/ee/services/audit-log/audit-log-types.ts
@@ -3662,8 +3662,7 @@ interface CmekGetPrivateKeyEvent {
interface CmekBulkGetPrivateKeysEvent {
type: EventType.CMEK_BULK_EXPORT_PRIVATE_KEYS;
metadata: {
- keyIds: string[];
- keyNames: string[];
+ keys: { keyId: string; name: string }[];
};
}
diff --git a/backend/src/server/routes/v1/cmek-router.ts b/backend/src/server/routes/v1/cmek-router.ts
index 9c7ee9a5a9..0a6ded8679 100644
--- a/backend/src/server/routes/v1/cmek-router.ts
+++ b/backend/src/server/routes/v1/cmek-router.ts
@@ -563,8 +563,7 @@ export const registerCmekRouter = async (server: FastifyZodProvider) => {
event: {
type: EventType.CMEK_BULK_EXPORT_PRIVATE_KEYS,
metadata: {
- keyIds: keys.map((k) => k.keyId),
- keyNames: keys.map((k) => k.name)
+ keys: keys.map((k) => ({ keyId: k.keyId, name: k.name }))
}
}
});
diff --git a/backend/src/services/cmek/cmek-service.ts b/backend/src/services/cmek/cmek-service.ts
index e5b83158ac..0a176ebf21 100644
--- a/backend/src/services/cmek/cmek-service.ts
+++ b/backend/src/services/cmek/cmek-service.ts
@@ -359,38 +359,35 @@ export const cmekServiceFactory = ({ kmsService, kmsDAL, permissionService }: TC
ProjectPermissionSub.Cmek
);
- const bulkMaterials = await kmsService.getBulkKeyMaterial({ kmsIds: uniqueKeyIds });
+ const bulkMaterials = await kmsService.getBulkKeyMaterial({ kmsIds: keys.map((k) => k.id) });
- const result = await Promise.all(
- keys.map(async (key) => {
- const materialEntry = bulkMaterials.find((m) => m.kmsId === key.id);
+ const materialByKmsId = new Map(bulkMaterials.map((m) => [m.kmsId, m]));
+ const asymmetricAlgorithms = new Set<string>(Object.values(AsymmetricKeyAlgorithm));
- if (!materialEntry) {
- throw new NotFoundError({ message: `Key material not found for key ID "${key.id}"` });
- }
+ const result = keys.map((key) => {
+ const materialEntry = materialByKmsId.get(key.id);
- const isAsymmetric = Object.values(AsymmetricKeyAlgorithm).includes(
+ if (!materialEntry) {
+ throw new NotFoundError({ message: `Key material not found for key ID "${key.id}"` });
+ }
+
+ let publicKey: string | undefined;
+ if (asymmetricAlgorithms.has(key.encryptionAlgorithm)) {
+ const pubKeyBuffer = signingService(
key.encryptionAlgorithm as AsymmetricKeyAlgorithm
- );
-
- let publicKey: string | undefined;
- if (isAsymmetric) {
- const pubKeyBuffer = signingService(
- key.encryptionAlgorithm as AsymmetricKeyAlgorithm
- ).getPublicKeyFromPrivateKey(materialEntry.keyMaterial);
- publicKey = pubKeyBuffer.toString("base64");
- }
-
- return {
- keyId: key.id,
- name: key.name,
- keyUsage: key.keyUsage,
- algorithm: key.encryptionAlgorithm,
- privateKey: materialEntry.keyMaterial.toString("base64"),
- ...(publicKey ? { publicKey } : {})
- };
- })
- );
+ ).getPublicKeyFromPrivateKey(materialEntry.keyMaterial);
+ publicKey = pubKeyBuffer.toString("base64");
+ }
+
+ return {
+ keyId: key.id,
+ name: key.name,
+ keyUsage: key.keyUsage,
+ algorithm: key.encryptionAlgorithm,
+ privateKey: materialEntry.keyMaterial.toString("base64"),
+ ...(publicKey ? { publicKey } : {})
+ };
+ });
return { keys: result, projectId };
};
diff --git a/frontend/src/pages/kms/OverviewPage/components/CmekTable.tsx b/frontend/src/pages/kms/OverviewPage/components/CmekTable.tsx
index b4e9d5fc87..509ec44d79 100644
--- a/frontend/src/pages/kms/OverviewPage/components/CmekTable.tsx
+++ b/frontend/src/pages/kms/OverviewPage/components/CmekTable.tsx
@@ -1,4 +1,4 @@
-import { useState } from "react";
+import { useEffect, useState } from "react";
import {
faCancel,
faCheck,
@@ -144,8 +144,15 @@ export const CmekTable = () => {
const [selectedKeyIds, setSelectedKeyIds] = useState<string[]>([]);
- const isPageSelected = keys.length > 0 && keys.every((k) => selectedKeyIds.includes(k.id));
- const isPageIndeterminate = !isPageSelected && keys.some((k) => selectedKeyIds.includes(k.id));
+ useEffect(() => {
+ setSelectedKeyIds([]);
+ }, [page]);
+
+ const selectableKeys = keys.filter((k) => !k.isDisabled);
+ const isPageSelected =
+ selectableKeys.length > 0 && selectableKeys.every((k) => selectedKeyIds.includes(k.id));
+ const isPageIndeterminate =
+ !isPageSelected && selectableKeys.some((k) => selectedKeyIds.includes(k.id));
const [, isCopyingCiphertext, setCopyCipherText] = useTimedReset<string>({
initialState: "",
@@ -353,15 +360,18 @@ export const CmekTable = () => {
id="cmek-page-select"
isChecked={isPageSelected || isPageIndeterminate}
isIndeterminate={isPageIndeterminate}
+ isDisabled={selectableKeys.length === 0}
variant="project"
onCheckedChange={() => {
if (isPageSelected) {
setSelectedKeyIds((prev) =>
- prev.filter((id) => !keys.find((k) => k.id === id))
+ prev.filter((id) => !selectableKeys.find((k) => k.id === id))
);
} else {
setSelectedKeyIds((prev) => {
- const merged = [...new Set([...prev, ...keys.map((k) => k.id)])];
+ const merged = [
+ ...new Set([...prev, ...selectableKeys.map((k) => k.id)])
+ ];
return merged.slice(0, 100);
});
}
@@ -427,24 +437,40 @@ export const CmekTable = () => {
onMouseLeave={() => setCopyCipherText("")}
>
<TableCell>
- <Checkbox
- id={`select-cmek-${id}`}
- isChecked={isSelected}
- variant="project"
- onClick={(e) => {
- e.stopPropagation();
- if (!isSelected && selectedKeyIds.length >= 100) {
- createNotification({
- text: "Cannot select more than 100 keys at once",
- type: "error"
- });
- return;
- }
- setSelectedKeyIds((prev) =>
- isSelected ? prev.filter((k) => k !== id) : [...prev, id]
- );
- }}
- />
+ {isDisabled ? (
+ <Tooltip>
+ <TooltipTrigger asChild>
+ <span className="inline-flex">
+ <Checkbox
+ id={`select-cmek-${id}`}
+ isChecked={false}
+ isDisabled
+ variant="project"
+ />
+ </span>
+ </TooltipTrigger>
+ <TooltipContent>Disabled keys cannot be exported</TooltipContent>
+ </Tooltip>
+ ) : (
+ <Checkbox
+ id={`select-cmek-${id}`}
+ isChecked={isSelected}
+ variant="project"
+ onClick={(e) => {
+ e.stopPropagation();
+ if (!isSelected && selectedKeyIds.length >= 100) {
+ createNotification({
+ text: "Cannot select more than 100 keys at once",
+ type: "error"
+ });
+ return;
+ }
+ setSelectedKeyIds((prev) =>
+ isSelected ? prev.filter((k) => k !== id) : [...prev, id]
+ );
+ }}
+ />
+ )}
</TableCell>
<TableCell>
<div className="flex items-center gap-2">