Skip to content

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

Add alias to AWS KMS keys

Carlos Monastyrski407d607f0b30
diff --git a/backend/src/services/certificate-authority/ca-key-source/aws-kms-ca-key-source.test.ts b/backend/src/services/certificate-authority/ca-key-source/aws-kms-ca-key-source.test.ts
index 58c09741e2..5a9a39b25b 100644
--- a/backend/src/services/certificate-authority/ca-key-source/aws-kms-ca-key-source.test.ts
+++ b/backend/src/services/certificate-authority/ca-key-source/aws-kms-ca-key-source.test.ts
@@ -5,6 +5,8 @@ import { tmpdir } from "node:os";
import { join } from "node:path";
import {
+ CreateAliasCommand,
+ CreateAliasCommandInput,
CreateKeyCommand,
GetPublicKeyCommand,
KMSClient,
@@ -81,6 +83,7 @@ const opensslPkeySign = (privateKeyDer: Buffer, input: Buffer, pkeyOpts: string[
const fakeKms = ({ publicKeySpki, sign }: { publicKeySpki: Buffer; sign: (input: TSignInput) => Buffer }) => {
const signCalls: TSignInput[] = [];
const createCalls: Record<string, unknown>[] = [];
+ const aliasCalls: CreateAliasCommandInput[] = [];
const send = async (command: unknown) => {
if (command instanceof CreateKeyCommand) {
@@ -90,6 +93,10 @@ const fakeKms = ({ publicKeySpki, sign }: { publicKeySpki: Buffer; sign: (input:
if (command instanceof GetPublicKeyCommand) {
return { PublicKey: publicKeySpki };
}
+ if (command instanceof CreateAliasCommand) {
+ aliasCalls.push(command.input);
+ return {};
+ }
if (command instanceof SignCommand) {
const input = command.input as TSignInput;
signCalls.push(input);
@@ -99,7 +106,7 @@ const fakeKms = ({ publicKeySpki, sign }: { publicKeySpki: Buffer; sign: (input:
};
vi.spyOn(KMSClient.prototype, "send").mockImplementation(send as never);
- return { signCalls, createCalls, publicKeySpki };
+ return { signCalls, createCalls, aliasCalls, publicKeySpki };
};
const spkiOf = (publicKey: KeyObject) => publicKey.export({ format: "der", type: "spki" });
@@ -350,6 +357,43 @@ describe("AWS KMS CA key source", () => {
expect((command as ScheduleKeyDeletionCommand).input).toEqual({ KeyId: KEY_ARN, PendingWindowInDays: 30 });
});
+ test("names the new key with an alias built from the CA name", async () => {
+ const { aliasCalls } = rsaKms();
+ const provider = awsKmsCaKeySourceFactory(buildDeps());
+
+ await provider.createKey({
+ projectId: "project",
+ caName: "root-ca",
+ keyAlgorithm: CertKeyAlgorithm.RSA_2048,
+ input: { appConnectionId: "conn", region: AWSRegion.US_EAST_1 }
+ });
+
+ expect(aliasCalls).toHaveLength(1);
+ expect(aliasCalls[0].TargetKeyId).toBe(KEY_ARN);
+ expect(aliasCalls[0].AliasName).toMatch(/^alias\/infisical-ca-root-ca-[a-zA-Z0-9]{5}$/);
+ });
+
+ test("still creates the key when the alias cannot be created", async () => {
+ const accessDenied = Object.assign(new Error("denied"), { name: "AccessDeniedException" });
+ const { publicKeySpki } = rsaKms();
+ vi.spyOn(KMSClient.prototype, "send").mockImplementation((async (command: unknown) => {
+ if (command instanceof CreateKeyCommand) return { KeyMetadata: { Arn: KEY_ARN } };
+ if (command instanceof GetPublicKeyCommand) return { PublicKey: publicKeySpki };
+ if (command instanceof CreateAliasCommand) throw accessDenied;
+ return {};
+ }) as never);
+ const provider = awsKmsCaKeySourceFactory(buildDeps());
+
+ const created = await provider.createKey({
+ projectId: "project",
+ caName: "root-ca",
+ keyAlgorithm: CertKeyAlgorithm.RSA_2048,
+ input: { appConnectionId: "conn", region: AWSRegion.US_EAST_1 }
+ });
+
+ expect(created.externalKeyId).toBe(KEY_ARN);
+ });
+
test("schedules deletion of a newly created key whose public key cannot be read", async () => {
const accessDenied = Object.assign(new Error("denied"), { name: "AccessDeniedException" });
const send = vi.spyOn(KMSClient.prototype, "send").mockImplementation((async (command: unknown) => {
diff --git a/backend/src/services/certificate-authority/ca-key-source/aws-kms-ca-key-source.ts b/backend/src/services/certificate-authority/ca-key-source/aws-kms-ca-key-source.ts
index fd1ff9cf75..4088420c59 100644
--- a/backend/src/services/certificate-authority/ca-key-source/aws-kms-ca-key-source.ts
+++ b/backend/src/services/certificate-authority/ca-key-source/aws-kms-ca-key-source.ts
@@ -1,4 +1,5 @@
import {
+ CreateAliasCommand,
CreateKeyCommand,
GetPublicKeyCommand,
KeyUsageType,
@@ -19,6 +20,7 @@ import { getPqcCrypto, isPqcAlgorithm } from "@app/lib/crypto/pqc";
import { bufferToArrayBuffer } from "@app/lib/csr/pkcs11-algorithm-map";
import { BadRequestError, NotFoundError } from "@app/lib/errors";
import { logger } from "@app/lib/logger";
+import { alphaNumericNanoId } from "@app/lib/nanoid";
import { TAppConnectionDALFactory } from "@app/services/app-connection/app-connection-dal";
import { AppConnection, AWSRegion } from "@app/services/app-connection/app-connection-enums";
import { decryptAppConnection } from "@app/services/app-connection/app-connection-fns";
@@ -154,6 +156,16 @@ export const awsKmsCaKeySourceFactory = ({
if (!publicKeySpki) {
throw new BadRequestError({ message: `AWS KMS did not return a public key for '${keyArn}'.` });
}
+ await client
+ .send(
+ new CreateAliasCommand({
+ AliasName: `alias/infisical-ca-${caName}-${alphaNumericNanoId(5)}`,
+ TargetKeyId: keyArn
+ })
+ )
+ .catch((aliasError: unknown) => {
+ logger.warn(aliasError, `Failed to create alias for AWS KMS CA key [keyArn=${keyArn}]`);
+ });
return { appConnectionId, externalKeyId: keyArn, publicKeySpki: Buffer.from(publicKeySpki) };
} catch (error) {
await $scheduleKeyDeletion(client, keyArn).catch((deletionError: unknown) => {
diff --git a/docs/documentation/platform/pki/ca/private-ca.mdx b/docs/documentation/platform/pki/ca/private-ca.mdx
index ba27033d3e..9748a8b821 100644
--- a/docs/documentation/platform/pki/ca/private-ca.mdx
+++ b/docs/documentation/platform/pki/ca/private-ca.mdx
@@ -298,7 +298,7 @@ Both root and intermediate CAs can be AWS KMS-backed. They support the `RSA 2048
"Statement": [
{
"Effect": "Allow",
- "Action": ["kms:CreateKey", "kms:TagResource"],
+ "Action": ["kms:CreateKey", "kms:TagResource", "kms:CreateAlias"],
"Resource": "*"
},
{
@@ -313,7 +313,7 @@ Both root and intermediate CAs can be AWS KMS-backed. They support the `RSA 2048
}
```
-Each CA signing key is created with the `author=infisical` and `purpose=certificate-authority` tags, so the second statement only allows signing with and deleting keys created for CAs.
+Each CA signing key is created with the `author=infisical` and `purpose=certificate-authority` tags, so the second statement only allows signing with and deleting keys created for CAs. Each key also gets an alias named after the CA, such as `alias/infisical-ca-kms-root-ca-x7k2p`, so you can find it in the AWS KMS console.
### Create the CA