Skip to content

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

fix(dynamic-secrets): apply the IRSA setting to AWS IAM temporary credentials

Vlad Ligaid257ffd88fdb
diff --git a/backend/src/ee/services/dynamic-secret/providers/aws-iam.test.ts b/backend/src/ee/services/dynamic-secret/providers/aws-iam.test.ts
new file mode 100644
index 0000000000..eb274339a3
--- /dev/null
+++ b/backend/src/ee/services/dynamic-secret/providers/aws-iam.test.ts
@@ -0,0 +1,88 @@
+import { beforeEach, describe, expect, test, vi } from "vitest";
+
+import { TDynamicSecrets } from "@app/db/schemas";
+
+import { AwsIamProvider } from "./aws-iam";
+import { AwsIamAuthType, AwsIamCredentialType } from "./models";
+
+const aws = vi.hoisted(() => {
+ const state = {
+ irsaEnabled: false,
+ calls: 0,
+ FakeClient: class {
+ // eslint-disable-next-line class-methods-use-this
+ async send() {
+ state.calls += 1;
+ return { Credentials: { AccessKeyId: "access-key", SecretAccessKey: "secret-key", SessionToken: "token" } };
+ }
+ }
+ };
+ return state;
+});
+
+vi.mock("@app/lib/config/env", () => ({
+ getConfig: () => ({ KUBERNETES_AUTO_FETCH_SERVICE_ACCOUNT_TOKEN: aws.irsaEnabled })
+}));
+vi.mock("@app/lib/crypto/cryptography", () => ({ crypto: { isFipsModeEnabled: () => false } }));
+vi.mock("@app/lib/aws/hashing", () => ({ CustomAWSHasher: function CustomAWSHasher() {} }));
+vi.mock("@aws-sdk/client-sts", async (importOriginal) => ({
+ ...(await importOriginal<typeof import("@aws-sdk/client-sts")>()),
+ STSClient: aws.FakeClient
+}));
+vi.mock("@aws-sdk/client-iam", async (importOriginal) => ({
+ ...(await importOriginal<typeof import("@aws-sdk/client-iam")>()),
+ IAMClient: aws.FakeClient
+}));
+
+const irsaInputs = (credentialType: AwsIamCredentialType) => ({
+ method: AwsIamAuthType.IRSA,
+ credentialType,
+ region: "us-east-1"
+});
+
+const createLease = (credentialType: AwsIamCredentialType) =>
+ AwsIamProvider().create({
+ inputs: irsaInputs(credentialType),
+ expireAt: Date.now() + 60 * 60 * 1000,
+ identity: { name: "[email protected]" },
+ dynamicSecret: { id: "dynamic-secret-1" } as TDynamicSecrets,
+ metadata: { projectId: "proj-1" }
+ });
+
+const IRSA_DISABLED_MESSAGE =
+ "Failed to get AWS credentials via IRSA: KUBERNETES_AUTO_FETCH_SERVICE_ACCOUNT_TOKEN is not enabled.";
+
+describe("AwsIamProvider IRSA", () => {
+ beforeEach(() => {
+ aws.irsaEnabled = false;
+ aws.calls = 0;
+ });
+
+ test.each([
+ [
+ "connection check for temporary credentials",
+ () =>
+ AwsIamProvider().validateConnection(irsaInputs(AwsIamCredentialType.TemporaryCredentials), {
+ projectId: "proj-1"
+ })
+ ],
+ ["lease for temporary credentials", () => createLease(AwsIamCredentialType.TemporaryCredentials)],
+ ["lease for an IAM user", () => createLease(AwsIamCredentialType.IamUser)]
+ ])("refuses the %s when service account token fetching is off", async (_, run) => {
+ await expect(run()).rejects.toThrow(IRSA_DISABLED_MESSAGE);
+ expect(aws.calls).toBe(0);
+ });
+
+ test("issues temporary credentials when service account token fetching is on", async () => {
+ aws.irsaEnabled = true;
+
+ await expect(
+ AwsIamProvider().validateConnection(irsaInputs(AwsIamCredentialType.TemporaryCredentials), {
+ projectId: "proj-1"
+ })
+ ).resolves.toBe(true);
+ const lease = await createLease(AwsIamCredentialType.TemporaryCredentials);
+
+ expect(lease.data).toEqual({ ACCESS_KEY: "access-key", SECRET_ACCESS_KEY: "secret-key", SESSION_TOKEN: "token" });
+ });
+});
diff --git a/backend/src/ee/services/dynamic-secret/providers/aws-iam.ts b/backend/src/ee/services/dynamic-secret/providers/aws-iam.ts
index bbdfc1c6d3..743116533f 100644
--- a/backend/src/ee/services/dynamic-secret/providers/aws-iam.ts
+++ b/backend/src/ee/services/dynamic-secret/providers/aws-iam.ts
@@ -50,6 +50,15 @@ const getAssumeRoleSessionPolicyParams = (providerInputs: {
};
};
+// Allow instances to disable automatic service account token fetching (e.g. for shared cloud)
+const assertIrsaEnabled = () => {
+ if (!getConfig().KUBERNETES_AUTO_FETCH_SERVICE_ACCOUNT_TOKEN) {
+ throw new UnauthorizedError({
+ message: "Failed to get AWS credentials via IRSA: KUBERNETES_AUTO_FETCH_SERVICE_ACCOUNT_TOKEN is not enabled."
+ });
+ }
+};
+
export const AwsIamProvider = (): TDynamicProviderFns => {
const validateProviderInputs = async (inputs: unknown) => {
const providerInputs = await DynamicSecretAwsIamSchema.parseAsync(inputs);
@@ -98,12 +107,7 @@ export const AwsIamProvider = (): TDynamicProviderFns => {
}
if (providerInputs.method === AwsIamAuthType.IRSA) {
- // Allow instances to disable automatic service account token fetching (e.g. for shared cloud)
- if (!appCfg.KUBERNETES_AUTO_FETCH_SERVICE_ACCOUNT_TOKEN) {
- throw new UnauthorizedError({
- message: "Failed to get AWS credentials via IRSA: KUBERNETES_AUTO_FETCH_SERVICE_ACCOUNT_TOKEN is not enabled."
- });
- }
+ assertIrsaEnabled();
// The SDK will automatically pick up credentials from the environment
const client = new IAMClient({
@@ -173,6 +177,7 @@ export const AwsIamProvider = (): TDynamicProviderFns => {
return true;
}
if (providerInputs.method === AwsIamAuthType.IRSA) {
+ assertIrsaEnabled();
const stsClient = new STSClient({
region: providerInputs.region,
useFipsEndpoint: crypto.isFipsModeEnabled(),
@@ -325,6 +330,7 @@ export const AwsIamProvider = (): TDynamicProviderFns => {
};
}
if (providerInputs.method === AwsIamAuthType.IRSA) {
+ assertIrsaEnabled();
durationSeconds = requestedDuration;
stsClient = new STSClient({
region: providerInputs.region,
diff --git a/tests/suites/secretmanager/dynamicsecrets/config_test.go b/tests/suites/secretmanager/dynamicsecrets/config_test.go
index fee0b4d6ec..8cb29accd9 100644
--- a/tests/suites/secretmanager/dynamicsecrets/config_test.go
+++ b/tests/suites/secretmanager/dynamicsecrets/config_test.go
@@ -7,6 +7,7 @@ import (
"github.com/Infisical/infisical/tests/fixture"
"github.com/Infisical/infisical/tests/harness"
"github.com/Infisical/infisical/tests/internal/apierr"
+ "github.com/Infisical/infisical/tests/internal/spec"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
)
@@ -120,6 +121,25 @@ func TestDynamicSecret_Create(t *testing.T) {
requireStatus(t, http.StatusUnprocessableEntity, res.StatusCode(), res.Body)
require.Empty(t, names(t, proj, "/"))
})
+
+ t.Run("should refuse naming the setting and create nothing when AWS IAM temporary credentials use IRSA on an instance without it", func(t *testing.T) {
+ t.Parallel()
+ spec.Why(t, `The harness leaves KUBERNETES_AUTO_FETCH_SERVICE_ACCOUNT_TOKEN at its default, off,
+ and the refusal comes before any call to AWS, so no fake is needed.`)
+
+ // Setup
+ tn := h.NewTenant(t)
+ proj := fixture.NewProject(t, tn, fixture.WithProjectType("secret-manager"))
+ inputs := map[string]any{"method": "irsa", "credentialType": "temporary-credentials", "region": "us-east-1"}
+
+ // Action
+ res, _ := createDynamicSecret(t, tn.Admin.API, proj, provider{"type": "aws-iam", "inputs": inputs})
+
+ // Assert
+ requireStatus(t, http.StatusBadRequest, res.StatusCode(), res.Body)
+ require.Contains(t, apierr.Message(res.Body), "KUBERNETES_AUTO_FETCH_SERVICE_ACCOUNT_TOKEN is not enabled")
+ require.Empty(t, names(t, proj, "/"))
+ })
}
func TestDynamicSecret_Inputs(t *testing.T) {