Skip to content

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

refactor to use aws specific types

Matheus Nogueira9b62344883d5
diff --git a/backend/src/ee/routes/v1/honey-token-router.ts b/backend/src/ee/routes/v1/honey-token-router.ts
index f4cd68cafb..c0d0bfdb9e 100644
--- a/backend/src/ee/routes/v1/honey-token-router.ts
+++ b/backend/src/ee/routes/v1/honey-token-router.ts
@@ -8,6 +8,7 @@ import { readLimit, writeLimit } from "@app/server/config/rateLimiter";
import { slugSchema } from "@app/server/lib/schemas";
import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
import { AuthMode } from "@app/services/auth/auth-type";
+import { AwsHoneyTokenCredentialsSchema } from "@app/ee/services/honey-token/honey-token-aws-types";
const SanitizedHoneyTokenConfigSchema = HoneyTokenConfigsSchema.pick({
id: true,
@@ -64,6 +65,13 @@ const HoneyTokenResetResponseSchema = HoneyTokensSchema.pick({
lastResetAt: z.date().nullable()
});
+const HoneyTokenCredentialsResponseSchema = z.discriminatedUnion("type", [
+ z.object({
+ type: z.literal(HoneyTokenType.AWS),
+ credentials: AwsHoneyTokenCredentialsSchema
+ })
+]);
+
export const registerHoneyTokenRouter = async (server: FastifyZodProvider) => {
server.route({
url: "/limits",
@@ -277,9 +285,7 @@ export const registerHoneyTokenRouter = async (server: FastifyZodProvider) => {
projectId: z.string().trim()
}),
response: {
- 200: z.object({
- credentials: z.record(z.string(), z.string())
- })
+ 200: HoneyTokenCredentialsResponseSchema
}
},
handler: async (req) => {
@@ -290,7 +296,10 @@ export const registerHoneyTokenRouter = async (server: FastifyZodProvider) => {
},
req.permission
);
- return { credentials };
+ return {
+ type: HoneyTokenType.AWS,
+ credentials
+ };
}
});
diff --git a/backend/src/ee/services/honey-token/honey-token-aws-types.ts b/backend/src/ee/services/honey-token/honey-token-aws-types.ts
new file mode 100644
index 0000000000..90395150ce
--- /dev/null
+++ b/backend/src/ee/services/honey-token/honey-token-aws-types.ts
@@ -0,0 +1,28 @@
+import { z } from "zod";
+
+import { BadRequestError } from "@app/lib/errors";
+
+export const AwsHoneyTokenCredentialsSchema = z.object({
+ accessKeyId: z.string(),
+ secretAccessKey: z.string()
+});
+
+export type TAwsHoneyTokenCredentials = z.infer<typeof AwsHoneyTokenCredentialsSchema>;
+
+export const AwsHoneyTokenDecryptedCredentialsSchema = AwsHoneyTokenCredentialsSchema.extend({
+ iamUserName: z.string()
+});
+
+export type TAwsHoneyTokenDecryptedCredentials = z.infer<typeof AwsHoneyTokenDecryptedCredentialsSchema>;
+
+export const parseAwsHoneyTokenDecryptedCredentials = (
+ value: unknown
+): TAwsHoneyTokenDecryptedCredentials => {
+ const result = AwsHoneyTokenDecryptedCredentialsSchema.safeParse(value);
+
+ if (!result.success) {
+ throw new BadRequestError({ message: "Invalid AWS honey token credentials" });
+ }
+
+ return result.data;
+};
diff --git a/backend/src/ee/services/honey-token/honey-token-service.ts b/backend/src/ee/services/honey-token/honey-token-service.ts
index 5e000308be..b5f0994cf3 100644
--- a/backend/src/ee/services/honey-token/honey-token-service.ts
+++ b/backend/src/ee/services/honey-token/honey-token-service.ts
@@ -41,6 +41,10 @@ import { THoneyTokenDALFactory } from "./honey-token-dal";
import { HoneyTokenStatus, HoneyTokenType } from "./honey-token-enums";
import { THoneyTokenEventDALFactory } from "./honey-token-event-dal";
import { AwsHoneyTokenConfigSchema } from "./honey-token-types";
+import {
+ parseAwsHoneyTokenDecryptedCredentials,
+ TAwsHoneyTokenCredentials
+} from "./honey-token-aws-types";
const HONEY_TOKEN_IAM_USER_PREFIX = "inf_ht_";
const CF_COMPLETE_STATUSES = new Set(["CREATE_COMPLETE", "UPDATE_COMPLETE", "IMPORT_COMPLETE"]);
@@ -792,7 +796,7 @@ export const honeyTokenServiceFactory = ({
const getCredentials = async (
{ honeyTokenId, projectId }: { honeyTokenId: string; projectId: string },
actor: OrgServiceActor
- ) => {
+ ): Promise<{ credentials: TAwsHoneyTokenCredentials }> => {
const { permission } = await permissionService.getProjectPermission({
actor: actor.type,
actorId: actor.id,
@@ -815,18 +819,16 @@ export const honeyTokenServiceFactory = ({
orgId: actor.orgId
});
- const decryptedCredentials = JSON.parse(
- decryptor({ cipherTextBlob: honeyToken.encryptedCredentials }).toString()
- ) as Record<string, string>;
-
- const mapping = honeyToken.secretsMapping as Record<string, string>;
-
- const credentials: Record<string, string> = {};
- for (const [credentialKey, secretName] of Object.entries(mapping)) {
- credentials[secretName] = decryptedCredentials[credentialKey] ?? "";
- }
+ const decryptedCredentials = parseAwsHoneyTokenDecryptedCredentials(
+ JSON.parse(decryptor({ cipherTextBlob: honeyToken.encryptedCredentials }).toString()) as unknown
+ );
- return { credentials };
+ return {
+ credentials: {
+ accessKeyId: decryptedCredentials.accessKeyId,
+ secretAccessKey: decryptedCredentials.secretAccessKey
+ }
+ };
};
const getHoneyTokenById = async (