mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(eks): populate cluster request IAM conditions (#8336)
* fix(eks): populate cluster request IAM conditions * fix(eks): read cluster conditions through the generated table and refuse mistyped flags
This commit is contained in:
1 parent
aa2adac677
commit
259a0737e7
18 files changed
+1747
-14
No files matched your search
@@ -0,0 +1,205 @@
|
||||
package io.floci.aws.core.common;
|
||||
|
||||
import io.floci.aws.testing.AwsTestClients;
|
||||
import io.floci.aws.testing.EnforcementFixtures;
|
||||
import io.floci.aws.testing.EnforcementFixtures.UserCredentials;
|
||||
import io.floci.aws.testing.IamEnforcementProfile;
|
||||
import io.floci.aws.testing.PartitionCleanup;
|
||||
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.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.eks.EksClient;
|
||||
import software.amazon.awssdk.services.eks.model.Cluster;
|
||||
import software.amazon.awssdk.services.eks.model.CreateClusterRequest;
|
||||
import software.amazon.awssdk.services.eks.model.EksException;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.floci.aws.testing.EnforcementFixtures.auth;
|
||||
import static io.floci.aws.testing.IamTestPolicies.EMPTY_POLICY_DOCUMENT;
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@QuarkusTest
|
||||
@TestProfile(IamEnforcementProfile.class)
|
||||
class EksClusterConditionsIntegrationTest {
|
||||
private static final String ACCOUNT = "000000000000";
|
||||
private static final String CONDITIONS = """
|
||||
{"StringEquals":{"eks:authenticationMode":"API_AND_CONFIG_MAP","aws:RequestTag/owner":"team"},
|
||||
"Bool":{"eks:endpointPublicAccess":"true","eks:endpointPrivateAccess":"true",
|
||||
"eks:bootstrapClusterCreatorAdminPermissions":"false"}}""";
|
||||
|
||||
@TestHTTPResource("/")
|
||||
URI endpoint;
|
||||
@RegisterExtension
|
||||
final PartitionCleanup cleanup = new PartitionCleanup();
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"authenticationMode", "endpointPublicAccess", "endpointPrivateAccess", "bootstrap"})
|
||||
void createRequiresEveryExplicitAttributeAndRetainsRequestTagConditions(String field) {
|
||||
try (Fixture f = new Fixture()) {
|
||||
f.policy(createGrant());
|
||||
CreateClusterRequest request = f.request();
|
||||
Cluster created = f.create(request);
|
||||
assertEquals("API_AND_CONFIG_MAP", created.accessConfig().authenticationModeAsString());
|
||||
assertFalse(created.accessConfig().bootstrapClusterCreatorAdminPermissions());
|
||||
assertTrue(created.resourcesVpcConfig().endpointPublicAccess());
|
||||
assertTrue(created.resourcesVpcConfig().endpointPrivateAccess());
|
||||
assertEquals("team", created.tags().get("owner"));
|
||||
denied(() -> f.create(changed(f.request(), field, false)));
|
||||
denied(() -> f.create(changed(f.request(), field, true)));
|
||||
denied(() -> f.create(f.request().toBuilder().tags(Map.of("owner", "other")).build()));
|
||||
// All the positive attributes still require PassRole, not just the EKS grant.
|
||||
f.policy(createGrant(), false);
|
||||
denied(() -> f.create(f.request()));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void explicitDenySeesFalseAndMissingFieldsRemainAbsent() {
|
||||
try (Fixture f = new Fixture()) {
|
||||
f.policy("""
|
||||
{"Effect":"Allow","Action":"eks:CreateCluster","Resource":"*"},
|
||||
{"Effect":"Deny","Action":"eks:CreateCluster","Resource":"*",
|
||||
"Condition":{"Bool":{"eks:bootstrapClusterCreatorAdminPermissions":"false"}}}""");
|
||||
denied(() -> f.create(f.request()));
|
||||
f.create(changed(f.request(), "bootstrap", true));
|
||||
f.policy("""
|
||||
{"Effect":"Allow","Action":"eks:CreateCluster","Resource":"*"},
|
||||
{"Effect":"Deny","Action":"eks:CreateCluster","Resource":"*",
|
||||
"Condition":{"Null":{"eks:authenticationMode":"true"}}}""");
|
||||
denied(() -> f.create(changed(f.request(), "authenticationMode", true)));
|
||||
f.create(f.request());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A number for a boolean member is refused rather than read as {@code true}, which the condition key of the
|
||||
* same member would not carry; the boolean and its text both carry it, so the deny holds for either.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"1", "true", "\"true\""})
|
||||
void aBooleanMemberIsCheckedAsTheClusterReadsIt(String value) {
|
||||
try (Fixture f = new Fixture()) {
|
||||
f.policy("""
|
||||
{"Effect":"Allow","Action":"eks:CreateCluster","Resource":"*"},
|
||||
{"Effect":"Deny","Action":"eks:CreateCluster","Resource":"*",
|
||||
"Condition":{"Bool":{"eks:endpointPublicAccess":"true"}}}""");
|
||||
String name = "condition-" + UUID.randomUUID();
|
||||
ValidatableResponse response = given().port(endpoint.getPort())
|
||||
.header("Authorization", auth(f.accessKeyId, "eks")).contentType("application/json")
|
||||
.body("{\"name\":\"%s\",\"roleArn\":\"%s\",\"resourcesVpcConfig\":{\"endpointPublicAccess\":%s}}"
|
||||
.formatted(name, f.role, value))
|
||||
.post("/clusters").then();
|
||||
if ("1".equals(value)) {
|
||||
response.statusCode(400).body(containsString("SerializationException"));
|
||||
} else {
|
||||
response.statusCode(403);
|
||||
}
|
||||
assertFalse(f.admin.listClusters(r -> { }).clusters().contains(name));
|
||||
}
|
||||
}
|
||||
|
||||
private static CreateClusterRequest changed(CreateClusterRequest request, String field, boolean omit) {
|
||||
return switch (field) {
|
||||
case "authenticationMode" -> request.toBuilder().accessConfig(request.accessConfig().toBuilder()
|
||||
.authenticationMode(omit ? null : "API").build()).build();
|
||||
case "bootstrap" -> request.toBuilder().accessConfig(request.accessConfig().toBuilder()
|
||||
.bootstrapClusterCreatorAdminPermissions(omit ? null : true).build()).build();
|
||||
case "endpointPublicAccess" -> request.toBuilder().resourcesVpcConfig(request.resourcesVpcConfig().toBuilder()
|
||||
.endpointPublicAccess(omit ? null : false).build()).build();
|
||||
case "endpointPrivateAccess" -> request.toBuilder().resourcesVpcConfig(request.resourcesVpcConfig().toBuilder()
|
||||
.endpointPrivateAccess(omit ? null : false).build()).build();
|
||||
default -> throw new IllegalArgumentException(field);
|
||||
};
|
||||
}
|
||||
|
||||
private static String createGrant() {
|
||||
return """
|
||||
{"Effect":"Allow","Action":"eks:CreateCluster","Resource":"*","Condition":%s}"""
|
||||
.formatted(CONDITIONS);
|
||||
}
|
||||
|
||||
private static void denied(Runnable call) {
|
||||
EksException error = assertThrows(EksException.class, call::run);
|
||||
assertEquals(403, error.statusCode());
|
||||
assertEquals("AccessDeniedException", error.awsErrorDetails().errorCode());
|
||||
}
|
||||
|
||||
private final class Fixture implements AutoCloseable {
|
||||
final String user = "eks-condition-" + UUID.randomUUID();
|
||||
final String role;
|
||||
final String accessKeyId;
|
||||
final EksClient admin;
|
||||
final EksClient client;
|
||||
final List<String> clusters = new ArrayList<>();
|
||||
|
||||
Fixture() {
|
||||
UserCredentials credentials = EnforcementFixtures.createUser(cleanup, endpoint.getPort(),
|
||||
auth(ACCOUNT, "iam"), user);
|
||||
accessKeyId = credentials.accessKeyId();
|
||||
role = EnforcementFixtures.createRole(cleanup, endpoint.getPort(), ACCOUNT, "/", user,
|
||||
EMPTY_POLICY_DOCUMENT);
|
||||
admin = AwsTestClients.create(EksClient.builder(), ACCOUNT, Region.US_EAST_1, endpoint);
|
||||
try {
|
||||
client = AwsTestClients.create(EksClient.builder(), credentials.accessKeyId(),
|
||||
credentials.secretAccessKey(), Region.US_EAST_1, endpoint);
|
||||
} catch (RuntimeException e) {
|
||||
admin.close();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
void policy(String statements) {
|
||||
policy(statements, true);
|
||||
}
|
||||
|
||||
void policy(String statements, boolean passRole) {
|
||||
EnforcementFixtures.putUserPolicy(endpoint.getPort(), auth(ACCOUNT, "iam"), user, "conditions",
|
||||
"""
|
||||
{"Version":"2012-10-17","Statement":[%s,
|
||||
{"Effect":"Allow","Action":"eks:TagResource","Resource":"*"}%s]}"""
|
||||
.formatted(statements, passRole ? ",{\"Effect\":\"Allow\",\"Action\":\"iam:PassRole\","
|
||||
+ "\"Resource\":\"" + role + "\"}" : ""));
|
||||
}
|
||||
|
||||
CreateClusterRequest request() {
|
||||
return CreateClusterRequest.builder().name("condition-" + UUID.randomUUID()).roleArn(role)
|
||||
.resourcesVpcConfig(v -> v.endpointPublicAccess(true).endpointPrivateAccess(true))
|
||||
.accessConfig(a -> a.authenticationMode("API_AND_CONFIG_MAP").bootstrapClusterCreatorAdminPermissions(false))
|
||||
.tags(Map.of("owner", "team")).build();
|
||||
}
|
||||
|
||||
Cluster create(CreateClusterRequest request) {
|
||||
Cluster cluster = client.createCluster(request).cluster();
|
||||
clusters.add(cluster.name());
|
||||
return cluster;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
try {
|
||||
for (String name : clusters) {
|
||||
admin.deleteCluster(r -> r.name(name));
|
||||
}
|
||||
} finally {
|
||||
client.close();
|
||||
admin.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -79,6 +79,26 @@ class IamConditionContextResolverTest {
|
||||
private IamConditionContextResolver resolver;
|
||||
private ResolvedServiceCatalog serviceCatalog;
|
||||
|
||||
@Test
|
||||
void eksRequestConditionsUseModeledPathsAndLeaveOmissionsAbsent() {
|
||||
ContainerRequestContext request = mock(ContainerRequestContext.class);
|
||||
when(request.getProperty("floci.bufferedJsonBody")).thenReturn(json("""
|
||||
{"accessConfig":{"authenticationMode":"API","bootstrapClusterCreatorAdminPermissions":false},
|
||||
"resourcesVpcConfig":{"endpointPrivateAccess":true,"endpointPublicAccess":false}}"""));
|
||||
Map<String, List<String>> create = resolver.resolve("eks", "eks:CreateCluster", request);
|
||||
assertEquals(Map.of("eks:authenticationMode", List.of("API"),
|
||||
"eks:bootstrapClusterCreatorAdminPermissions", List.of("false"),
|
||||
"eks:endpointPrivateAccess", List.of("true"), "eks:endpointPublicAccess", List.of("false")), create);
|
||||
Map<String, List<String>> update = resolver.resolve("eks", "eks:UpdateClusterConfig", request);
|
||||
assertEquals(3, update.size());
|
||||
assertFalse(update.containsKey("eks:bootstrapClusterCreatorAdminPermissions"));
|
||||
assertNull(resolver.resolve("eks", "eks:DescribeCluster", request));
|
||||
when(request.getProperty("floci.bufferedJsonBody")).thenReturn(json("""
|
||||
{"accessConfig":{"authenticationMode":null},"resourcesVpcConfig":{"endpointPrivateAccess":[]}}"""));
|
||||
assertNull(resolver.resolve("eks", "eks:CreateCluster", request));
|
||||
assertNull(resolver.resolve("eks", "eks:UpdateClusterConfig", request));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
|
||||
@@ -54,7 +54,6 @@ import io.floci.aws.services.eks.model.CertificateAuthority;
|
||||
import io.floci.aws.services.eks.model.Cluster;
|
||||
import io.floci.aws.services.eks.model.ClusterIdentity;
|
||||
import io.floci.aws.services.eks.model.ClusterOidcKey;
|
||||
import io.floci.aws.services.eks.model.CreateClusterRequest;
|
||||
import io.floci.aws.services.eks.model.LogSetup;
|
||||
import io.floci.aws.services.eks.model.Logging;
|
||||
import io.floci.aws.services.eks.model.Nodegroup;
|
||||
@@ -5019,10 +5018,11 @@ class EksClusterManagerTest {
|
||||
eksService,
|
||||
Mockito.mock(EksAccessEntryService.class),
|
||||
Mockito.mock(EksPodIdentityAssociationService.class),
|
||||
Mockito.mock(EksAddonService.class));
|
||||
Mockito.mock(EksAddonService.class),
|
||||
mapper);
|
||||
|
||||
for (Response response : List.of(
|
||||
controller.createCluster(new CreateClusterRequest()),
|
||||
controller.createCluster(mapper.createObjectNode()),
|
||||
controller.describeCluster("wire-cluster"),
|
||||
controller.deleteCluster("wire-cluster"))) {
|
||||
@SuppressWarnings("unchecked")
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package io.floci.aws.services.eks;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class EksRequestShapesTest {
|
||||
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"1", "0", "1.5"})
|
||||
void aNumberForABooleanMemberIsRefused(String value) {
|
||||
AwsException error = assertThrows(AwsException.class, () -> EksRequestShapes.check("CreateCluster",
|
||||
mapper.readTree("{\"resourcesVpcConfig\":{\"endpointPublicAccess\":" + value + "}}")));
|
||||
assertEquals("SerializationException", error.getErrorCode());
|
||||
assertEquals(400, error.getHttpStatus());
|
||||
assertTrue(error.getMessage().contains("Boolean"), error.getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"true", "false", "\"TRUE\"", "\"false\"", "null"})
|
||||
void aBooleanItsTextOrNullPasses(String value) {
|
||||
assertDoesNotThrow(() -> EksRequestShapes.check("CreateCluster",
|
||||
mapper.readTree("{\"accessConfig\":{\"bootstrapClusterCreatorAdminPermissions\":" + value + "}}")));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"\"public\"", "[]"})
|
||||
void aScalarOrListForAStructureIsRefused(String value) {
|
||||
AwsException error = assertThrows(AwsException.class, () -> EksRequestShapes.check("CreateCluster",
|
||||
mapper.readTree("{\"name\":\"c\",\"resourcesVpcConfig\":" + value + "}")));
|
||||
assertEquals("SerializationException", error.getErrorCode());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user