Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

chore(iam): report enforcement state at startup and cover IAM management actions (#3935)

Matheus André5ac07733e92b
diff --git a/docs/services/iam.md b/docs/services/iam.md
index 96ed462100..8829e0aea8 100644
--- a/docs/services/iam.md
+++ b/docs/services/iam.md
@@ -332,7 +332,9 @@ Requests signed with the seeded access key return the deployer user ARN from `st
By default Floci accepts any credentials without enforcing IAM policies — all requests are allowed through regardless of what policies are attached to the calling identity. This preserves backward compatibility and keeps the default setup frictionless.
-Setting `enforcement-enabled: true` activates the policy evaluator as a JAX-RS request filter. Every inbound request is then evaluated against the identity-based policies of the calling IAM user or assumed role before it reaches the service handler.
+Setting `enforcement-enabled: true` activates the policy evaluator as a JAX-RS request filter. Every inbound request is then evaluated against the identity-based policies of the calling IAM user or assumed role before it reaches the service handler. This includes IAM's own management actions (`iam:CreateUser`, `iam:CreateGroup`, `iam:AttachUserPolicy`, `iam:DeleteUser`, ...): a user whose policies only grant, say, `s3:*` receives `AccessDenied` when calling them.
+
+The startup banner reports the effective state (`IAM: policy enforcement enabled` / `disabled`). If requests you expect to be denied keep succeeding, check that line first: the flag is only read under the name below, and any other spelling (for example `FLOCI_IAM_STRICT_VALIDATION`, which does not exist) is silently ignored, leaving the permissive default in place.
### Enable enforcement
diff --git a/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycle.java b/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycle.java
index 3aebdc1cab..20670f2f55 100644
--- a/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycle.java
+++ b/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycle.java
@@ -179,6 +179,11 @@ public class EmulatorLifecycle {
LOG.infof("Region: %s Account: %s", config.defaultRegion(), config.defaultAccountId());
LOG.infov("Storage: {0} Path: {1}", config.storage().mode(), config.storage().persistentPath());
LOG.infov("TLS: {0}", config.tls().enabled() ? "enabled (HTTPS + HTTP dual mode)" : "disabled (HTTP only)");
+ // Surfaced in the banner because a mistyped flag name (issue #3931) silently leaves
+ // enforcement off, and nothing else in the logs reveals that policies are not evaluated.
+ LOG.infov("IAM: {0}", config.services().iam().enforcementEnabled()
+ ? "policy enforcement enabled"
+ : "policy enforcement disabled (set FLOCI_SERVICES_IAM_ENFORCEMENT_ENABLED=true to enforce)");
// BOOT hooks run before service initialization — scripts cannot use AWS APIs yet.
try {
diff --git a/src/test/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycleTest.java b/src/test/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycleTest.java
index 254ba748d1..f72e6585c5 100644
--- a/src/test/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycleTest.java
+++ b/src/test/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycleTest.java
@@ -44,6 +44,7 @@ import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import java.io.IOException;
+import java.text.MessageFormat;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CopyOnWriteArrayList;
@@ -100,6 +101,7 @@ class EmulatorLifecycleTest {
@Mock private InitLifecycleState initLifecycleState;
@Mock private PersistentPathValidator persistentPathValidator;
@Mock private EmulatorConfig.TlsConfig tlsConfig;
+ @Mock private EmulatorConfig.IamServiceConfig iamServiceConfig;
@Mock private SchemaCreationWorker schemaCreationWorker;
@Mock private StepFunctionsService stepFunctionsService;
@Mock private Instance<ContainerTeardown> containerTeardowns;
@@ -121,6 +123,8 @@ class EmulatorLifecycleTest {
Mockito.lenient().when(eksServiceConfig.enabled()).thenReturn(false);
Mockito.lenient().when(servicesConfig.timestreamInfluxdb()).thenReturn(timestreamInfluxDbServiceConfig);
Mockito.lenient().when(timestreamInfluxDbServiceConfig.enabled()).thenReturn(true);
+ Mockito.lenient().when(servicesConfig.iam()).thenReturn(iamServiceConfig);
+ Mockito.lenient().when(iamServiceConfig.enforcementEnabled()).thenReturn(false);
Mockito.lenient().when(config.tls()).thenReturn(tlsConfig);
Mockito.lenient().when(tlsConfig.enabled()).thenReturn(false);
Mockito.lenient().when(config.port()).thenReturn(4566);
@@ -522,7 +526,10 @@ class EmulatorLifecycleTest {
java.util.logging.Handler handler = new java.util.logging.Handler() {
@Override
public void publish(java.util.logging.LogRecord logRecord) {
- messages.add(logRecord.getMessage());
+ Object[] parameters = logRecord.getParameters();
+ messages.add(parameters == null || parameters.length == 0
+ ? logRecord.getMessage()
+ : MessageFormat.format(logRecord.getMessage(), parameters));
}
@Override
@@ -577,6 +584,36 @@ class EmulatorLifecycleTest {
"The Floci ready banner must still be emitted");
}
+ @Test
+ @DisplayName("Should report in the banner that IAM enforcement is disabled by default")
+ void shouldReportIamEnforcementDisabledInBanner() {
+ stubStorageConfig();
+ when(initializationHooksRunner.hasHooks(InitializationHook.START)).thenReturn(false);
+ when(initializationHooksRunner.hasHooks(InitializationHook.READY)).thenReturn(false);
+
+ List<String> messages =
+ lifecycleLogMessages(() -> emulatorLifecycle.onStart(Mockito.mock(StartupEvent.class)));
+
+ assertTrue(messages.contains("IAM: policy enforcement disabled"
+ + " (set FLOCI_SERVICES_IAM_ENFORCEMENT_ENABLED=true to enforce)"),
+ "The banner must say enforcement is off and name the flag that turns it on");
+ }
+
+ @Test
+ @DisplayName("Should report in the banner that IAM enforcement is enabled when configured")
+ void shouldReportIamEnforcementEnabledInBanner() {
+ stubStorageConfig();
+ when(iamServiceConfig.enforcementEnabled()).thenReturn(true);
+ when(initializationHooksRunner.hasHooks(InitializationHook.START)).thenReturn(false);
+ when(initializationHooksRunner.hasHooks(InitializationHook.READY)).thenReturn(false);
+
+ List<String> messages =
+ lifecycleLogMessages(() -> emulatorLifecycle.onStart(Mockito.mock(StartupEvent.class)));
+
+ assertTrue(messages.contains("IAM: policy enforcement enabled"),
+ "The banner must say enforcement is on");
+ }
+
@Test
@DisplayName("Should emit the parity \"Ready.\" line on the deferred onHttpStart ready path too")
void shouldEmitParityReadyLineOnHttpStartPath() throws IOException, InterruptedException {
diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/IamManagementActionsEnforcementIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/IamManagementActionsEnforcementIntegrationTest.java
new file mode 100644
index 0000000000..50d7f475d7
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/iam/IamManagementActionsEnforcementIntegrationTest.java
@@ -0,0 +1,126 @@
+package io.github.hectorvent.floci.services.iam;
+
+import io.quarkus.test.junit.QuarkusTest;
+import io.quarkus.test.junit.QuarkusTestProfile;
+import io.quarkus.test.junit.TestProfile;
+import io.restassured.response.ValidatableResponse;
+import io.restassured.specification.RequestSpecification;
+import org.junit.jupiter.api.Test;
+
+import java.util.Map;
+import java.util.UUID;
+
+import static io.restassured.RestAssured.given;
+import static org.hamcrest.Matchers.containsString;
+import static org.hamcrest.Matchers.not;
+
+/**
+ * Regression guard for issue #3931: an IAM user whose only policy grants S3 permissions must
+ * receive AccessDenied for IAM management actions (CreateUser, CreateGroup,
+ * AttachUserPolicy, DeleteUser) when enforcement is enabled.
+ */
+@QuarkusTest
+@TestProfile(IamManagementActionsEnforcementIntegrationTest.IamEnforcementProfile.class)
+class IamManagementActionsEnforcementIntegrationTest {
+
+ private static final String ACCOUNT_ID = "000000000000";
+ private static final String REGION = "us-east-1";
+
+ private static final String S3_ONLY_POLICY = """
+ {"Version":"2012-10-17","Statement":[
+ {"Effect":"Allow","Action":["s3:ListAllMyBuckets","s3:CreateBucket"],"Resource":"*"},
+ {"Effect":"Allow","Action":["s3:ListBucket","s3:GetObject","s3:PutObject"],"Resource":"*"}
+ ]}""";
+
+ @Test
+ void s3OnlyUserIsDeniedIamManagementActions() {
+ String suffix = UUID.randomUUID().toString().substring(0, 8);
+ String developer = "test-developer-" + suffix;
+ String group = "Developers-" + suffix;
+
+ adminIam("CreateUser", Map.of("UserName", developer)).statusCode(200);
+ adminIam("CreateGroup", Map.of("GroupName", group)).statusCode(200);
+ adminIam("PutGroupPolicy", Map.of("GroupName", group, "PolicyName", "DeveloperS3Policy",
+ "PolicyDocument", S3_ONLY_POLICY)).statusCode(200);
+ adminIam("AddUserToGroup", Map.of("GroupName", group, "UserName", developer)).statusCode(200);
+ String akid = adminIam("CreateAccessKey", Map.of("UserName", developer))
+ .statusCode(200)
+ .extract().path("CreateAccessKeyResponse.CreateAccessKeyResult.AccessKey.AccessKeyId");
+
+ // Granted S3 action still works.
+ given().header("Authorization", auth(akid, "s3"))
+ .when().get("/")
+ .then().statusCode(200);
+
+ userIam(akid, "CreateUser", Map.of("UserName", "should-fail-" + suffix))
+ .statusCode(403)
+ .body(containsString("AccessDenied"))
+ .body(containsString("iam:CreateUser"));
+ userIam(akid, "CreateGroup", Map.of("GroupName", "should-fail-" + suffix))
+ .statusCode(403)
+ .body(containsString("AccessDenied"));
+ userIam(akid, "AttachUserPolicy", Map.of("UserName", developer,
+ "PolicyArn", "arn:aws:iam::aws:policy/AdministratorAccess"))
+ .statusCode(403)
+ .body(containsString("AccessDenied"));
+ userIam(akid, "DeleteUser", Map.of("UserName", developer))
+ .statusCode(403)
+ .body(containsString("AccessDenied"));
+
+ // The user was not created and the policy was not attached.
+ adminIam("GetUser", Map.of("UserName", "should-fail-" + suffix)).statusCode(404);
+ adminIam("ListAttachedUserPolicies", Map.of("UserName", developer))
+ .statusCode(200)
+ .body(not(containsString("AdministratorAccess")));
+ }
+
+ @Test
+ void explicitDenyOnIamActionsIsHonoured() {
+ String suffix = UUID.randomUUID().toString().substring(0, 8);
+ String user = "deny-user-" + suffix;
+ adminIam("CreateUser", Map.of("UserName", user)).statusCode(200);
+ adminIam("PutUserPolicy", Map.of("UserName", user, "PolicyName", "S3AllowIamDeny",
+ "PolicyDocument", """
+ {"Version":"2012-10-17","Statement":[
+ {"Effect":"Allow","Action":["s3:*"],"Resource":"*"},
+ {"Effect":"Deny","Action":["iam:*"],"Resource":"*"}
+ ]}""")).statusCode(200);
+ String akid = adminIam("CreateAccessKey", Map.of("UserName", user))
+ .statusCode(200)
+ .extract().path("CreateAccessKeyResponse.CreateAccessKeyResult.AccessKey.AccessKeyId");
+
+ userIam(akid, "CreateUser", Map.of("UserName", "deny-direct-" + suffix))
+ .statusCode(403)
+ .body(containsString("AccessDenied"));
+ }
+
+ private static ValidatableResponse adminIam(String action, Map<String, String> params) {
+ return iamCall(ACCOUNT_ID, action, params);
+ }
+
+ private static ValidatableResponse userIam(String akid, String action, Map<String, String> params) {
+ return iamCall(akid, action, params);
+ }
+
+ private static ValidatableResponse iamCall(String akid, String action, Map<String, String> params) {
+ RequestSpecification spec = given()
+ .header("Authorization", auth(akid, "iam"))
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", action)
+ .formParam("Version", "2010-05-08");
+ params.forEach(spec::formParam);
+ return spec.when().post("/").then();
+ }
+
+ private static String auth(String accessKeyId, String service) {
+ return "AWS4-HMAC-SHA256 Credential=" + accessKeyId + "/20260919/" + REGION + "/" + service
+ + "/aws4_request, SignedHeaders=host, Signature=abc";
+ }
+
+ public static final class IamEnforcementProfile implements QuarkusTestProfile {
+ @Override
+ public Map<String, String> getConfigOverrides() {
+ return Map.of("floci.services.iam.enforcement-enabled", "true");
+ }
+ }
+}