From 4ac4e5d34b0578f1bd5c7099c83a1b8850e1605d Mon Sep 17 00:00:00 2001 From: "Simon B. Laursen" Date: Mon, 15 Jun 2026 02:21:00 +0200 Subject: [PATCH] fix(sts): persist session secret key so RDS/ElastiCache IAM tokens validate (#1266) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(sts): persist session secret key so RDS/ElastiCache IAM tokens validate STS session credentials (ASIA... keys) issued by AssumeRole, GetSessionToken, AssumeRoleWithWebIdentity, AssumeRoleWithSAML and GetFederationToken were rejected by the RDS and ElastiCache SigV4 validators because the temporary secret access key was never stored. Root cause: - StsQueryHandler generated a random secretKey and returned it to the caller but only passed accessKeyId + roleArn to registerSession — the secret was discarded. - SessionCredential had no secretAccessKey field. - IamService.findSecretKey only checked the accessKeys store (AKIA... keys); the sessions store was never consulted. - RdsSigV4Validator fell back to orElse(accessKeyId), using the key ID itself as the signing secret, so every HMAC comparison failed with 28P01. Fix: 1. Add secretAccessKey field to SessionCredential. 2. Add registerSession overload that accepts the secret and stores it. 3. Update all five STS handlers to call the new overload. 4. Make findSecretKey check sessions as a fallback after accessKeys. 5. Add IamServiceTestHelper.iamServiceWithSessionCredential helper and two new RdsSigV4ValidatorTest cases covering the ASIA key path. The same findSecretKey call is used by the ElastiCache SigV4Validator, so both services benefit from this fix. * fix: avoid IAM enforcement crash for identity sessions --- .../floci/services/iam/IamService.java | 26 +++++++++++- .../floci/services/iam/StsQueryHandler.java | 12 +++--- .../services/iam/model/SessionCredential.java | 13 ++++++ .../floci/services/iam/IamServiceTest.java | 15 +++++++ .../rds/proxy/RdsSigV4ValidatorTest.java | 41 +++++++++++++++++++ .../floci/testutil/IamServiceTestHelper.java | 37 +++++++++++++++++ 6 files changed, 138 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/iam/IamService.java b/src/main/java/io/github/hectorvent/floci/services/iam/IamService.java index 22aa8558f2..82c118082e 100644 --- a/src/main/java/io/github/hectorvent/floci/services/iam/IamService.java +++ b/src/main/java/io/github/hectorvent/floci/services/iam/IamService.java @@ -805,7 +805,11 @@ public class IamService { // ========================================================================= public Optional findSecretKey(String accessKeyId) { - return accessKeys.get(accessKeyId).map(AccessKey::getSecretAccessKey); + Optional fromAccessKey = accessKeys.get(accessKeyId).map(AccessKey::getSecretAccessKey); + if (fromAccessKey.isPresent()) { + return fromAccessKey; + } + return sessions.get(accessKeyId).map(SessionCredential::getSecretAccessKey); } // ========================================================================= @@ -828,6 +832,16 @@ public class IamService { new SessionCredential(sessionAccessKeyId, roleArn, expiration, sessionPolicyDocument)); } + /** + * Stores an assumed-role session including the temporary secret access key so that + * {@link #findSecretKey(String)} can resolve it for RDS/ElastiCache IAM token validation. + */ + public void registerSession(String sessionAccessKeyId, String secretAccessKey, String roleArn, + java.time.Instant expiration, String sessionPolicyDocument) { + sessions.put(sessionAccessKeyId, + new SessionCredential(sessionAccessKeyId, secretAccessKey, roleArn, expiration, sessionPolicyDocument)); + } + /** * Resolves the full caller context for the given access key, including identity policies, * optional session policy, and optional permission boundary. @@ -852,6 +866,10 @@ public class IamService { sessions.delete(accessKeyId); return null; // expired — unknown key → bypass } + + if (session.getRoleArn() == null) { + return null; // identity session without mapped caller context — preserve historical bypass + } List identityPolicies = collectRolePolicies(session.getRoleArn()); String boundaryDoc = resolveRoleBoundaryDocument(session.getRoleArn()); return new CallerContext(identityPolicies, session.getSessionPolicyDocument(), boundaryDoc); @@ -884,6 +902,9 @@ public class IamService { } private String resolveRoleBoundaryDocument(String roleArn) { + if (roleArn == null) { + return null; + } String roleName = roleArn.contains("/") ? roleArn.substring(roleArn.lastIndexOf('/') + 1) : roleArn; return roles.get(roleName) .map(IamRole::getPermissionsBoundaryArn) @@ -970,6 +991,9 @@ public class IamService { } private List collectRolePolicies(String roleArn) { + if (roleArn == null) { + return null; + } String roleName = roleArn.contains("/") ? roleArn.substring(roleArn.lastIndexOf('/') + 1) : roleArn; Optional roleOpt = roles.get(roleName); if (roleOpt.isEmpty()) { diff --git a/src/main/java/io/github/hectorvent/floci/services/iam/StsQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/iam/StsQueryHandler.java index c68e02b12f..fb6b9b903c 100644 --- a/src/main/java/io/github/hectorvent/floci/services/iam/StsQueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/iam/StsQueryHandler.java @@ -73,9 +73,10 @@ public class StsQueryHandler { String assumedRoleArn = AwsArnUtils.Arn.of("sts", "", accountId, "assumed-role/" + roleName + "/" + sessionName).toString(); String assumedRoleId = "AROA" + randomId(16) + ":" + sessionName; - // Register session so IAM enforcement can resolve the role's policies + // Register session so IAM enforcement can resolve the role's policies and so that + // RDS/ElastiCache IAM token validation can find the temporary secret key. String sessionPolicy = getParam(params, "Policy"); - iamService.registerSession(accessKeyId, roleArn, expiration, sessionPolicy); + iamService.registerSession(accessKeyId, secretKey, roleArn, expiration, sessionPolicy); String result = new XmlBuilder() .raw(credentialsXml(accessKeyId, secretKey, sessionToken, expiration)) @@ -106,6 +107,7 @@ public class StsQueryHandler { Instant expiration = Instant.now().plusSeconds(durationSeconds); String result = credentialsXml(accessKeyId, secretKey, sessionToken, expiration); + iamService.registerSession(accessKeyId, secretKey, null, expiration, null); return Response.ok(AwsQueryResponse.envelope("GetSessionToken", AwsNamespaces.STS, result)).build(); } @@ -131,7 +133,7 @@ public class StsQueryHandler { String provider = providerId != null && !providerId.isBlank() ? providerId : "accounts.google.com"; String sessionPolicy = getParam(params, "Policy"); - iamService.registerSession(accessKeyId, roleArn, expiration, sessionPolicy); + iamService.registerSession(accessKeyId, secretKey, roleArn, expiration, sessionPolicy); String result = new XmlBuilder() .raw(credentialsXml(accessKeyId, secretKey, sessionToken, expiration)) @@ -166,7 +168,7 @@ public class StsQueryHandler { String assumedRoleArn = AwsArnUtils.Arn.of("sts", "", accountId, "assumed-role/" + roleName + "/" + sessionName).toString(); String assumedRoleId = "AROA" + randomId(16) + ":" + sessionName; - iamService.registerSession(accessKeyId, roleArn, expiration, null); + iamService.registerSession(accessKeyId, secretKey, roleArn, expiration, null); String result = new XmlBuilder() .raw(credentialsXml(accessKeyId, secretKey, sessionToken, expiration)) @@ -202,7 +204,7 @@ public class StsQueryHandler { String sessionPolicy = getParam(params, "Policy"); // Register federation token so enforcement can scope its policies via session policy - iamService.registerSession(accessKeyId, federatedUserArn, expiration, sessionPolicy); + iamService.registerSession(accessKeyId, secretKey, federatedUserArn, expiration, sessionPolicy); String result = new XmlBuilder() .raw(credentialsXml(accessKeyId, secretKey, sessionToken, expiration)) diff --git a/src/main/java/io/github/hectorvent/floci/services/iam/model/SessionCredential.java b/src/main/java/io/github/hectorvent/floci/services/iam/model/SessionCredential.java index 8952644f0b..9e2c0d4db9 100644 --- a/src/main/java/io/github/hectorvent/floci/services/iam/model/SessionCredential.java +++ b/src/main/java/io/github/hectorvent/floci/services/iam/model/SessionCredential.java @@ -10,6 +10,7 @@ import java.time.Instant; public class SessionCredential { private String accessKeyId; + private String secretAccessKey; private String roleArn; private Instant expiration; /** Inline session policy passed to AssumeRole/GetFederationToken — further restricts role policies. */ @@ -30,9 +31,21 @@ public class SessionCredential { this.sessionPolicyDocument = sessionPolicyDocument; } + public SessionCredential(String accessKeyId, String secretAccessKey, String roleArn, Instant expiration, + String sessionPolicyDocument) { + this.accessKeyId = accessKeyId; + this.secretAccessKey = secretAccessKey; + this.roleArn = roleArn; + this.expiration = expiration; + this.sessionPolicyDocument = sessionPolicyDocument; + } + public String getAccessKeyId() { return accessKeyId; } public void setAccessKeyId(String accessKeyId) { this.accessKeyId = accessKeyId; } + public String getSecretAccessKey() { return secretAccessKey; } + public void setSecretAccessKey(String secretAccessKey) { this.secretAccessKey = secretAccessKey; } + public String getRoleArn() { return roleArn; } public void setRoleArn(String roleArn) { this.roleArn = roleArn; } diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/IamServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/IamServiceTest.java index c3e2a472ce..ecc4d8bace 100644 --- a/src/test/java/io/github/hectorvent/floci/services/iam/IamServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/iam/IamServiceTest.java @@ -13,6 +13,7 @@ import io.github.hectorvent.floci.services.iam.model.PolicyVersion; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import java.time.Instant; import java.util.List; import java.util.Map; @@ -391,6 +392,20 @@ class IamServiceTest { assertTrue(iamService.listAccessKeys("alice").isEmpty()); } + @Test + void getSessionTokenStyleSessionBypassesEnforcementResolution() { + iamService.registerSession( + "ASIAIOSFODNN7EXAMPLE", + "temporary-secret", + null, + Instant.now().plusSeconds(3600), + null + ); + + assertNull(iamService.resolveCallerContext("ASIAIOSFODNN7EXAMPLE")); + assertNull(iamService.resolveCallerPolicies("ASIAIOSFODNN7EXAMPLE")); + } + // ========================================================================= // Instance Profiles // ========================================================================= diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/proxy/RdsSigV4ValidatorTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/proxy/RdsSigV4ValidatorTest.java index 5b705208f3..5849c6f8ad 100644 --- a/src/test/java/io/github/hectorvent/floci/services/rds/proxy/RdsSigV4ValidatorTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/rds/proxy/RdsSigV4ValidatorTest.java @@ -241,4 +241,45 @@ class RdsSigV4ValidatorTest { assertFalse(validator.validate(withoutSignature, "admin")); } + + @Test + void validateAcceptsTokenSignedWithStsSessionCredentials() throws Exception { + String accessKeyId = "ASIAIOSFODNN7EXAMPLE"; + String secretAccessKey = "sts-generated-secret-key"; + IamService iamService = IamServiceTestHelper.iamServiceWithSessionCredential(accessKeyId, secretAccessKey); + + RdsSigV4Validator validator = new RdsSigV4Validator(iamService); + String token = SigV4TokenTestHelper.createRdsToken( + "db.example.local", + 5432, + "admin", + accessKeyId, + secretAccessKey, + Instant.now().minusSeconds(60), + 900 + ); + + assertTrue(validator.validate(token, "admin"), + "Validator must accept RDS IAM tokens signed with STS session credentials (ASIA… keys)"); + } + + @Test + void validateRejectsStsTokenWithWrongSecret() throws Exception { + String accessKeyId = "ASIAIOSFODNN7EXAMPLE"; + IamService iamService = IamServiceTestHelper.iamServiceWithSessionCredential(accessKeyId, "correct-secret"); + + RdsSigV4Validator validator = new RdsSigV4Validator(iamService); + String token = SigV4TokenTestHelper.createRdsToken( + "db.example.local", + 5432, + "admin", + accessKeyId, + "wrong-secret", + Instant.now().minusSeconds(60), + 900 + ); + + assertFalse(validator.validate(token, "admin"), + "Validator must reject STS token signed with wrong secret"); + } } diff --git a/src/test/java/io/github/hectorvent/floci/testutil/IamServiceTestHelper.java b/src/test/java/io/github/hectorvent/floci/testutil/IamServiceTestHelper.java index a9af67ec77..c28db0aae5 100644 --- a/src/test/java/io/github/hectorvent/floci/testutil/IamServiceTestHelper.java +++ b/src/test/java/io/github/hectorvent/floci/testutil/IamServiceTestHelper.java @@ -5,8 +5,10 @@ import io.github.hectorvent.floci.core.storage.InMemoryStorage; import io.github.hectorvent.floci.core.storage.StorageBackend; import io.github.hectorvent.floci.services.iam.IamService; import io.github.hectorvent.floci.services.iam.model.AccessKey; +import io.github.hectorvent.floci.services.iam.model.SessionCredential; import java.lang.reflect.Constructor; +import java.time.Instant; public final class IamServiceTestHelper { @@ -44,4 +46,39 @@ public final class IamServiceTestHelper { throw new IllegalStateException("Failed to construct IamService test fixture", e); } } + + @SuppressWarnings("unchecked") + public static IamService iamServiceWithSessionCredential(String accessKeyId, String secretAccessKey) { + try { + Constructor constructor = IamService.class.getDeclaredConstructor( + StorageBackend.class, + StorageBackend.class, + StorageBackend.class, + StorageBackend.class, + StorageBackend.class, + StorageBackend.class, + StorageBackend.class, + RegionResolver.class + ); + constructor.setAccessible(true); + + InMemoryStorage sessions = new InMemoryStorage<>(); + SessionCredential cred = new SessionCredential(accessKeyId, secretAccessKey, null, + Instant.now().plusSeconds(3600), null); + sessions.put(accessKeyId, cred); + + return constructor.newInstance( + null, + null, + null, + null, + new InMemoryStorage<>(), + null, + sessions, + new RegionResolver("us-east-1", "123456789012") + ); + } catch (ReflectiveOperationException e) { + throw new IllegalStateException("Failed to construct IamService test fixture", e); + } + } }