mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(logs): require the policy type's own permission for PutAccountPolicy (#5956)
This commit is contained in:
1 parent
a57c20943a
commit
e9154fb928
4 files changed
+157
-1
No files matched your search
+122
@@ -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\":\"*\"}";
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user