fix(sts): persist session secret key so RDS/ElastiCache IAM tokens validate (#1266)

* 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
This commit is contained in:
Simon B. Laursen authored and GitHub committed 2026-06-14 19:21:00 -05:00
1 parent ed74698d48
commit 4ac4e5d34b
6 files changed
+138 -6

No files matched your search

@@ -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
// =========================================================================
@@ -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");
}
}
@@ -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<IamService> constructor = IamService.class.getDeclaredConstructor(
StorageBackend.class,
StorageBackend.class,
StorageBackend.class,
StorageBackend.class,
StorageBackend.class,
StorageBackend.class,
StorageBackend.class,
RegionResolver.class
);
constructor.setAccessible(true);
InMemoryStorage<String, SessionCredential> 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);
}
}
}