diff --git a/docs/services/iam.md b/docs/services/iam.md index 88901902ce..6731acb2f3 100644 --- a/docs/services/iam.md +++ b/docs/services/iam.md @@ -1391,6 +1391,14 @@ ARNs take precedence over plain-name interpretation and must identify a local gr is accepted on a canonical local group ARN. These compatibility forms are not a claim that AWS accepts them for these APIs. Other Logs actions retain their existing wildcard resource evaluation. +**CloudWatch Logs account policies.** `PutAccountPolicy` also needs the action for the type of +policy it creates, checked against `*` like `logs:PutAccountPolicy` itself: `logs:PutDataProtectionPolicy` +for `DATA_PROTECTION_POLICY`, `logs:PutSubscriptionFilter` for `SUBSCRIPTION_FILTER_POLICY`, +`logs:PutTransformer` for `TRANSFORMER_POLICY`, `logs:PutIndexPolicy` for `FIELD_INDEX_POLICY` and +`logs:PutMetricExtractionPolicy` for `METRIC_EXTRACTION_POLICY`. Without it the request is refused with +`AccessDeniedException` and no policy is stored. The `iam:PassRole` a subscription filter policy with a +`RoleArn` also needs is not checked. + **Secrets Manager secrets.** A request that names a secret by its name, or by a partial ARN without the random suffix, is evaluated against the stored secret's full ARN, suffix included, so a `secret:-??????` resource, the pattern the Secrets Manager User Guide documents, matches it as 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.java index ddb1f07326..b3682d5a28 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 @@ -1,5 +1,6 @@ package io.github.hectorvent.floci.core.common; +import com.fasterxml.jackson.databind.JsonNode; import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.services.cloudtrail.CloudTrailService; import io.github.hectorvent.floci.services.iam.IamActionRegistry; @@ -540,6 +541,16 @@ public class IamEnforcementFilter implements ContainerRequestFilter { } } + // PutAccountPolicy also needs the action for the type of policy it creates, as its API + // reference says. An unknown type needs nothing more: the handler refuses it. + if ("logs:PutAccountPolicy".equals(action)) { + String typeAction = accountPolicyTypeAction(arnBuilder.readJsonBody(ctx)); + if (typeAction != null && abortIfDenied(ctx, caller, principal, typeAction, action, credentialScope, + resources, targetContexts, region, accountId, akid)) { + return; + } + } + // A PutObject carrying If-Match compares against the object it replaces, and S3 authorizes // that read as s3:GetObject, WITHOUT the object's tags in the request context. Measured // against real AWS: under a GetObject allow conditioned on s3:ExistingObjectTag the @@ -618,6 +629,20 @@ public class IamEnforcementFilter implements ContainerRequestFilter { private static final Set GOVERNANCE_BYPASS_ACTIONS = Set.of("s3:PutObjectRetention", "s3:DeleteObject", "s3:DeleteObjectVersion"); + private static String accountPolicyTypeAction(JsonNode body) { + if (body == null) { + return null; + } + return switch (body.path("policyType").asText()) { + case "DATA_PROTECTION_POLICY" -> "logs:PutDataProtectionPolicy"; + case "SUBSCRIPTION_FILTER_POLICY" -> "logs:PutSubscriptionFilter"; + case "TRANSFORMER_POLICY" -> "logs:PutTransformer"; + case "FIELD_INDEX_POLICY" -> "logs:PutIndexPolicy"; + case "METRIC_EXTRACTION_POLICY" -> "logs:PutMetricExtractionPolicy"; + default -> null; + }; + } + // PutObject and CopyObject are any PUT S3Controller does not run as UploadPart or UploadPartCopy, // which name both an uploadId and a partNumber; CompleteMultipartUpload is a POST naming its // upload, without ?uploads. diff --git a/src/main/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilder.java b/src/main/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilder.java index c494220c00..9dff1b9bae 100644 --- a/src/main/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilder.java +++ b/src/main/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilder.java @@ -1065,7 +1065,8 @@ public class ResourceArnBuilder { // ── Helpers ────────────────────────────────────────────────────────────────── - private JsonNode readJsonBody(ContainerRequestContext ctx) { + /** The request's JSON or CBOR body, buffered so the handler still reads it, or null when it has none. */ + public JsonNode readJsonBody(ContainerRequestContext ctx) { Object cached = ctx.getProperty("floci.bufferedJsonBody"); if (cached instanceof JsonNode node) { return node; diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/CloudWatchLogsAccountPolicyIamEnforcementIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/CloudWatchLogsAccountPolicyIamEnforcementIntegrationTest.java new file mode 100644 index 0000000000..1ebd434e6c --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/iam/CloudWatchLogsAccountPolicyIamEnforcementIntegrationTest.java @@ -0,0 +1,122 @@ +package io.github.hectorvent.floci.services.iam; + +import io.github.hectorvent.floci.testing.IamEnforcementProfile; +import io.github.hectorvent.floci.testing.LogsAuthorizationFixtures; +import io.github.hectorvent.floci.testing.PartitionCleanup; +import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; +import io.quarkus.test.common.http.TestHTTPResource; +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.TestProfile; +import io.restassured.response.ValidatableResponse; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import java.net.URI; +import java.util.Map; +import java.util.UUID; + +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; + +/** + * PutAccountPolicy also needs the action for the type of policy it creates, as its API reference + * says: "To use this operation, you must be signed on with the correct permissions depending on the + * type of policy that you are creating." + */ +@QuarkusTest +@TestProfile(IamEnforcementProfile.class) +class CloudWatchLogsAccountPolicyIamEnforcementIntegrationTest { + + private static final String ACCOUNT = "111111111111"; + private static final String REGION = "us-east-1"; + /** + * Where the allowed cases create their policies. Logs has no DeleteAccountPolicy, and a region + * holds one data protection and one subscription filter policy, so the refused cases, which + * create nothing, keep {@link #REGION} to themselves. + */ + private static final String CREATE_REGION = "eu-west-1"; + + @RegisterExtension + final PartitionCleanup cleanup = new PartitionCleanup(); + + @TestHTTPResource + URI endpoint; + + @BeforeAll + static void configureContentTypes() { + RestAssuredJsonUtils.configureAwsContentTypes(); + } + + @ParameterizedTest + @CsvSource({ + "DATA_PROTECTION_POLICY, PutDataProtectionPolicy", + "SUBSCRIPTION_FILTER_POLICY, PutSubscriptionFilter", + "TRANSFORMER_POLICY, PutTransformer", + "FIELD_INDEX_POLICY, PutIndexPolicy", + "METRIC_EXTRACTION_POLICY, PutMetricExtractionPolicy"}) + void callerAllowedOnlyPutAccountPolicyIsRefusedThePolicyType(String policyType, String typeAction) { + String caller = user(allow("PutAccountPolicy")); + String policyName = policyName(); + + putAccountPolicy(caller, REGION, policyName, policyType) + .statusCode(400) + .body("__type", equalTo("AccessDeniedException")) + .body("message", containsString("logs:" + typeAction)); + describeAccountPolicy(REGION, policyName, policyType) + .statusCode(400) + .body("__type", equalTo("ResourceNotFoundException")); + } + + @ParameterizedTest + @CsvSource({ + "DATA_PROTECTION_POLICY, PutDataProtectionPolicy", + "SUBSCRIPTION_FILTER_POLICY, PutSubscriptionFilter", + "TRANSFORMER_POLICY, PutTransformer", + "FIELD_INDEX_POLICY, PutIndexPolicy", + "METRIC_EXTRACTION_POLICY, PutMetricExtractionPolicy"}) + void callerAllowedThePolicyTypeActionCreatesThePolicy(String policyType, String typeAction) { + String caller = user(allow("PutAccountPolicy") + "," + allow(typeAction)); + String policyName = policyName(); + + putAccountPolicy(caller, CREATE_REGION, policyName, policyType).statusCode(200); + describeAccountPolicy(CREATE_REGION, policyName, policyType) + .statusCode(200) + .body("accountPolicies[0].policyName", equalTo(policyName)); + } + + @Test + void unknownPolicyTypeIsRejectedAsInvalidRatherThanDenied() { + String caller = user(allow("PutAccountPolicy")); + + putAccountPolicy(caller, REGION, policyName(), "UNKNOWN_POLICY") + .statusCode(400) + .body("__type", equalTo("InvalidParameterException")); + } + + private ValidatableResponse putAccountPolicy(String caller, String region, String policyName, + String policyType) { + return LogsAuthorizationFixtures.call(endpoint.getPort(), caller, region, "PutAccountPolicy", + Map.of("policyName", policyName, "policyType", policyType, "policyDocument", "{}")).then(); + } + + private ValidatableResponse describeAccountPolicy(String region, String policyName, String policyType) { + return LogsAuthorizationFixtures.call(endpoint.getPort(), ACCOUNT, region, "DescribeAccountPolicies", + Map.of("policyName", policyName, "policyType", policyType)).then(); + } + + private static String policyName() { + return "account-policy-auth-" + UUID.randomUUID(); + } + + private String user(String statements) { + return LogsAuthorizationFixtures.user(cleanup, endpoint.getPort(), ACCOUNT, "account-policy-auth-", + "account-policies", statements); + } + + private static String allow(String action) { + return "{\"Effect\":\"Allow\",\"Action\":\"logs:" + action + "\",\"Resource\":\"*\"}"; + } +}