Skip to content

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

feat: enhance KMS service and documentation for bulk key export

Victor Hugo dos Santos5405fd081b0a
diff --git a/backend/src/services/cmek/cmek-service.ts b/backend/src/services/cmek/cmek-service.ts
index c525eb15bc..e5b83158ac 100644
--- a/backend/src/services/cmek/cmek-service.ts
+++ b/backend/src/services/cmek/cmek-service.ts
@@ -3,7 +3,7 @@ import { ForbiddenError } from "@casl/ability";
import { ActionProjectType } from "@app/db/schemas";
import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service-types";
import { ProjectPermissionCmekActions, ProjectPermissionSub } from "@app/ee/services/permission/project-permission";
-import { AsymmetricKeyAlgorithm, SigningAlgorithm } from "@app/lib/crypto/sign";
+import { AsymmetricKeyAlgorithm, SigningAlgorithm, signingService } from "@app/lib/crypto/sign";
import { DatabaseErrorCode } from "@app/lib/error-codes";
import { BadRequestError, DatabaseError, NotFoundError } from "@app/lib/errors";
import { OrgServiceActor } from "@app/lib/types";
@@ -322,13 +322,14 @@ export const cmekServiceFactory = ({ kmsService, kmsDAL, permissionService }: TC
const bulkGetPrivateKeys = async ({ keyIds }: TCmekBulkGetPrivateKeysDTO, actor: OrgServiceActor) => {
if (keyIds.length === 0) throw new BadRequestError({ message: "At least one key ID is required" });
- const keys = await kmsDAL.findCmeksByIds(keyIds);
+ const uniqueKeyIds = [...new Set(keyIds)];
+ const keys = await kmsDAL.findCmeksByIds(uniqueKeyIds);
if (keys.length === 0) throw new NotFoundError({ message: "No keys found for the provided IDs" });
- if (keys.length !== keyIds.length) {
+ if (keys.length !== uniqueKeyIds.length) {
const foundIds = new Set(keys.map((k) => k.id));
- const missingIds = keyIds.filter((id) => !foundIds.has(id));
+ const missingIds = uniqueKeyIds.filter((id) => !foundIds.has(id));
throw new NotFoundError({ message: `Keys not found for IDs: ${missingIds.join(", ")}` });
}
@@ -358,25 +359,28 @@ export const cmekServiceFactory = ({ kmsService, kmsDAL, permissionService }: TC
ProjectPermissionSub.Cmek
);
- const bulkMaterials = await kmsService.getBulkKeyMaterial({ kmsIds: keyIds });
+ const bulkMaterials = await kmsService.getBulkKeyMaterial({ kmsIds: uniqueKeyIds });
const result = await Promise.all(
keys.map(async (key) => {
const materialEntry = bulkMaterials.find((m) => m.kmsId === key.id);
+
+ if (!materialEntry) {
+ throw new NotFoundError({ message: `Key material not found for key ID "${key.id}"` });
+ }
+
const isAsymmetric = Object.values(AsymmetricKeyAlgorithm).includes(
key.encryptionAlgorithm as AsymmetricKeyAlgorithm
);
let publicKey: string | undefined;
if (isAsymmetric) {
- const pubKeyBuffer = await kmsService.getPublicKey({ kmsId: key.id });
+ const pubKeyBuffer = signingService(
+ key.encryptionAlgorithm as AsymmetricKeyAlgorithm
+ ).getPublicKeyFromPrivateKey(materialEntry.keyMaterial);
publicKey = pubKeyBuffer.toString("base64");
}
- if (!materialEntry) {
- throw new NotFoundError({ message: `Key material not found for key ID "${key.id}"` });
- }
-
return {
keyId: key.id,
name: key.name,
diff --git a/docs/api-reference/endpoints/kms/keys/bulk-export-private-keys.mdx b/docs/api-reference/endpoints/kms/keys/bulk-export-private-keys.mdx
new file mode 100644
index 0000000000..e6725cea20
--- /dev/null
+++ b/docs/api-reference/endpoints/kms/keys/bulk-export-private-keys.mdx
@@ -0,0 +1,4 @@
+---
+title: "Bulk Export Private Keys"
+openapi: "POST /api/v1/kms/keys/bulk-export-private-keys"
+---
diff --git a/docs/docs.json b/docs/docs.json
index f00db502ad..c6e91b1f6f 100644
--- a/docs/docs.json
+++ b/docs/docs.json
@@ -3239,7 +3239,8 @@
"api-reference/endpoints/kms/keys/update",
"api-reference/endpoints/kms/keys/delete",
"api-reference/endpoints/kms/keys/public-key",
- "api-reference/endpoints/kms/keys/private-key"
+ "api-reference/endpoints/kms/keys/private-key",
+ "api-reference/endpoints/kms/keys/bulk-export-private-keys"
]
},
{
diff --git a/frontend/src/pages/kms/OverviewPage/components/CmekTable.tsx b/frontend/src/pages/kms/OverviewPage/components/CmekTable.tsx
index fdb0d48d31..4f3e1454ed 100644
--- a/frontend/src/pages/kms/OverviewPage/components/CmekTable.tsx
+++ b/frontend/src/pages/kms/OverviewPage/components/CmekTable.tsx
@@ -8,7 +8,6 @@ import {
faEdit,
faEllipsis,
faFileSignature,
- faInfoCircle,
faLock,
faLockOpen,
faPlus,
@@ -16,7 +15,7 @@ import {
} from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { motion } from "framer-motion";
-import { ChevronDownIcon, SearchIcon } from "lucide-react";
+import { ChevronDownIcon, InfoIcon, SearchIcon } from "lucide-react";
import { twMerge } from "tailwind-merge";
import { createNotification } from "@app/components/notifications";
@@ -425,6 +424,7 @@ export const CmekTable = () => {
return (
<UnstableTableRow
key={`cmek-${id}`}
+ className="group"
data-state={isSelected ? "selected" : undefined}
onMouseLeave={() => setCopyCipherText("")}
>
@@ -454,10 +454,7 @@ export const CmekTable = () => {
{description && (
<Tooltip>
<TooltipTrigger asChild>
- <FontAwesomeIcon
- icon={faInfoCircle}
- className="text-mineshaft-400"
- />
+ <InfoIcon className="size-3.5 text-muted opacity-0 transition-all group-hover:opacity-100" />
</TooltipTrigger>
<TooltipContent>{description}</TooltipContent>
</Tooltip>
@@ -486,10 +483,7 @@ export const CmekTable = () => {
{kmsKeyUsageOptions[keyUsage].label}
<Tooltip>
<TooltipTrigger asChild>
- <FontAwesomeIcon
- icon={faInfoCircle}
- className="text-mineshaft-400"
- />
+ <InfoIcon className="size-3.5 text-muted opacity-0 transition-all group-hover:opacity-100" />
</TooltipTrigger>
<TooltipContent>
{kmsKeyUsageOptions[keyUsage].tooltip}