Light, fluffy, and always free - The AWS Local Emulator alternative
fix(logs): require the policy type's read permission for DescribeAccountPolicies (#6097)
Niels Kootstra73e890dbc72f
diff --git a/docs/services/iam.md b/docs/services/iam.mdindex 0b1da346dc..5685177c2e 100644--- a/docs/services/iam.md+++ b/docs/services/iam.md@@ -1495,6 +1495,10 @@ for `DATA_PROTECTION_POLICY`, `logs:PutSubscriptionFilter` for `SUBSCRIPTION_FIL`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.+`DescribeAccountPolicies` likewise needs the read action for the type it lists:+`logs:GetDataProtectionPolicy`, `logs:DescribeSubscriptionFilters`, `logs:GetTransformer` or+`logs:DescribeIndexPolicies`. Its reference names none for `METRIC_EXTRACTION_POLICY`, which needs only+`logs:DescribeAccountPolicies`.**Secrets Manager secrets.** A request that names a secret by its name, or by a partial ARN withoutthe random suffix, is evaluated against the stored secret's full ARN, suffix included, so adiff --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 ab42819e15..476d4b6044 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@@ -550,10 +550,11 @@ 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));+ // PutAccountPolicy also needs the action for the type of policy it creates, and+ // DescribeAccountPolicies the one for the type it lists, as their API references say. An+ // unknown type needs nothing more: the handler refuses it.+ if ("logs:PutAccountPolicy".equals(action) || "logs:DescribeAccountPolicies".equals(action)) {+ String typeAction = accountPolicyTypeAction(action, arnBuilder.readJsonBody(ctx));if (typeAction != null && abortIfDenied(ctx, caller, principal, typeAction, action, credentialScope,resources, targetContexts, region, accountId, akid)) {return;@@ -638,11 +639,22 @@ public class IamEnforcementFilter implements ContainerRequestFilter {private static final Set<String> GOVERNANCE_BYPASS_ACTIONS =Set.of("s3:PutObjectRetention", "s3:DeleteObject", "s3:DeleteObjectVersion");- private static String accountPolicyTypeAction(JsonNode body) {+ private static String accountPolicyTypeAction(String action, JsonNode body) {if (body == null) {return null;}- return switch (body.path("policyType").asText()) {+ String policyType = body.path("policyType").asText();+ if ("logs:DescribeAccountPolicies".equals(action)) {+ // The reference names no read action for metric extraction policies.+ return switch (policyType) {+ case "DATA_PROTECTION_POLICY" -> "logs:GetDataProtectionPolicy";+ case "SUBSCRIPTION_FILTER_POLICY" -> "logs:DescribeSubscriptionFilters";+ case "TRANSFORMER_POLICY" -> "logs:GetTransformer";+ case "FIELD_INDEX_POLICY" -> "logs:DescribeIndexPolicies";+ default -> null;+ };+ }+ return switch (policyType) {case "DATA_PROTECTION_POLICY" -> "logs:PutDataProtectionPolicy";case "SUBSCRIPTION_FILTER_POLICY" -> "logs:PutSubscriptionFilter";case "TRANSFORMER_POLICY" -> "logs:PutTransformer";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.javaindex 1ebd434e6c..6e909de06d 100644--- a/src/test/java/io/github/hectorvent/floci/services/iam/CloudWatchLogsAccountPolicyIamEnforcementIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/iam/CloudWatchLogsAccountPolicyIamEnforcementIntegrationTest.java@@ -24,7 +24,8 @@ 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."+ * type of policy that you are creating." DescribeAccountPolicies likewise needs the read action of the+ * type it lists, for the four types its reference names.*/@QuarkusTest@TestProfile(IamEnforcementProfile.class)@@ -96,6 +97,45 @@ class CloudWatchLogsAccountPolicyIamEnforcementIntegrationTest {.body("__type", equalTo("InvalidParameterException"));}+ @ParameterizedTest+ @CsvSource({+ "DATA_PROTECTION_POLICY, GetDataProtectionPolicy",+ "SUBSCRIPTION_FILTER_POLICY, DescribeSubscriptionFilters",+ "TRANSFORMER_POLICY, GetTransformer",+ "FIELD_INDEX_POLICY, DescribeIndexPolicies"})+ void callerAllowedOnlyDescribeAccountPoliciesIsRefusedThePolicyType(String policyType, String typeAction) {+ String caller = user(allow("DescribeAccountPolicies"));++ describeAccountPolicies(caller, policyType)+ .statusCode(400)+ .body("__type", equalTo("AccessDeniedException"))+ .body("message", containsString("logs:" + typeAction));+ }++ @ParameterizedTest+ @CsvSource({+ "DATA_PROTECTION_POLICY, GetDataProtectionPolicy",+ "SUBSCRIPTION_FILTER_POLICY, DescribeSubscriptionFilters",+ "TRANSFORMER_POLICY, GetTransformer",+ "FIELD_INDEX_POLICY, DescribeIndexPolicies"})+ void callerAllowedThePolicyTypeReadActionDescribesThePolicies(String policyType, String typeAction) {+ String caller = user(allow("DescribeAccountPolicies") + "," + allow(typeAction));++ describeAccountPolicies(caller, policyType).statusCode(200);+ }++ @Test+ void metricExtractionPoliciesNeedOnlyDescribeAccountPolicies() {+ String caller = user(allow("DescribeAccountPolicies"));++ describeAccountPolicies(caller, "METRIC_EXTRACTION_POLICY").statusCode(200);+ }++ private ValidatableResponse describeAccountPolicies(String caller, String policyType) {+ return LogsAuthorizationFixtures.call(endpoint.getPort(), caller, REGION, "DescribeAccountPolicies",+ Map.of("policyType", policyType)).then();+ }+private ValidatableResponse putAccountPolicy(String caller, String region, String policyName,String policyType) {return LogsAuthorizationFixtures.call(endpoint.getPort(), caller, region, "PutAccountPolicy",