Light, fluffy, and always free - The AWS Local Emulator alternative
fix(sts): issue GetSessionToken without IAM permissions (#6051)
zgy5f75b7dff07b
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java b/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.javaindex 1531304c65..fccaf84bc7 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java@@ -67,6 +67,8 @@ import java.util.regex.Pattern;* <li>The action cannot be resolved (unknown mapping → permissive)</li>* <li>The action is {@code sts:GetCallerIdentity}, which AWS allows without permissions. The* key itself is still checked.</li>+ * <li>The action is {@code sts:GetSessionToken}, which AWS also allows without permissions:+ * policies cannot control authentication operations. The key itself is still checked.</li>* </ul>** <p>Authenticated temporary callers of {@code sts:GetFederationToken} are refused here with@@ -383,6 +385,11 @@ public class IamEnforcementFilter implements ContainerRequestFilter {if ("sts:GetCallerIdentity".equals(action)) {return; // AWS returns caller identity even when an identity policy explicitly denies it}+ if ("sts:GetSessionToken".equals(action)) {+ // AWS: "No permissions are required for users to perform this operation... You cannot+ // use policies to control authentication operations." An explicit deny does not stop it.+ return;+ }if ("sts:GetFederationToken".equals(action) && IamService.isTemporaryAccessKey(akid)) {ctx.abortWith(queryXmlAccessDenied(IamService.FEDERATION_TOKEN_SESSION_MESSAGE));diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/StsFederationCallerEnforcementIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/StsFederationCallerEnforcementIntegrationTest.javaindex 603f80d0e8..15def3a5de 100644--- a/src/test/java/io/github/hectorvent/floci/services/iam/StsFederationCallerEnforcementIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/iam/StsFederationCallerEnforcementIntegrationTest.java@@ -36,6 +36,11 @@ class StsFederationCallerEnforcementIntegrationTest {private static final String ACCOUNT = "000000000000";private static final String ALLOW_STS = "{\"Version\":\"2012-10-17\",\"Statement\":["+ "{\"Effect\":\"Allow\",\"Action\":\"sts:*\",\"Resource\":\"*\"}]}";+ private static final String DENY_SESSION_TOKEN = "{\"Version\":\"2012-10-17\",\"Statement\":["+ + "{\"Effect\":\"Deny\",\"Action\":\"sts:GetSessionToken\",\"Resource\":\"*\"}]}";+ private static final String ALLOW_STS_DENY_FEDERATION_TOKEN = "{\"Version\":\"2012-10-17\",\"Statement\":["+ + "{\"Effect\":\"Allow\",\"Action\":\"sts:*\",\"Resource\":\"*\"},"+ + "{\"Effect\":\"Deny\",\"Action\":\"sts:GetFederationToken\",\"Resource\":\"*\"}]}";@Inject IamService iam;@RegisterExtension final PartitionCleanup cleanup = new PartitionCleanup();@@ -90,6 +95,27 @@ class StsFederationCallerEnforcementIntegrationTest {}}+ @Test+ void getSessionTokenIsIssuedEvenWhenAPolicyExplicitlyDeniesIt() throws Exception {+ Credentials user = userWithPolicy(DENY_SESSION_TOKEN);+ remember(request(user, "GetSessionToken", Map.of()), "GetSessionToken");+ }++ @Test+ void getSessionTokenIsIssuedWithoutAPolicyThatAllowsIt() throws Exception {+ Credentials user = user(false);+ remember(request(user, "GetSessionToken", Map.of()), "GetSessionToken");+ }++ @Test+ void otherStsActionsStayDecidedByPolicies() throws Exception {+ Credentials user = userWithPolicy(ALLOW_STS_DENY_FEDERATION_TOKEN);+ request(user, "GetFederationToken", Map.of("Name", "still-denied")).then().statusCode(403)+ .body("ErrorResponse.Error.Code", equalTo("AccessDenied"))+ .body("ErrorResponse.Error.Message",+ equalTo("User is not authorized to perform: sts:GetFederationToken"));+ }+@Testvoid federationSessionKeepsOtherIamAndStsRestrictions() throws Exception {Credentials issued = issue("GetFederationToken");@@ -118,13 +144,17 @@ class StsFederationCallerEnforcementIntegrationTest {}private Credentials user(boolean allowed) {+ return userWithPolicy(allowed ? ALLOW_STS : null);+ }++ private Credentials userWithPolicy(String policyDocument) {String name = "fed-enforced-" + UUID.randomUUID();iam.createUser(name, "/");cleanup.register(() -> iam.deleteUser(name));AccessKey key = iam.createAccessKey(name);cleanup.register(() -> iam.deleteAccessKey(name, key.getAccessKeyId()));- if (allowed) {- iam.putUserPolicy(name, "sts", ALLOW_STS);+ if (policyDocument != null) {+ iam.putUserPolicy(name, "sts", policyDocument);cleanup.register(() -> iam.deleteUserPolicy(name, "sts"));}return new Credentials(key.getAccessKeyId(), key.getSecretAccessKey(), null);