diff --git a/docs/services/emr.md b/docs/services/emr.md index 53a766948f..37924b612f 100644 --- a/docs/services/emr.md +++ b/docs/services/emr.md @@ -265,7 +265,8 @@ nothing answers. * **UpdateStudio** changes what it is given. Its `SubnetIds` "can include new subnet IDs, but must also include all of the subnet IDs previously associated with the Studio", at most 5, in the Studio's VPC; anything else is refused. * **DeleteStudio** removes the Studio and its session mappings. **ListStudios** lists the Region's Studios in the order - they were made, 50 to a page, after a `Marker`; no source gives AWS's order or page size. + they were made, 50 to a page, after a `Marker`, which resumes only the listing it came from: the same account and + Region. No source gives AWS's order or page size. * **Session mappings** are for an `SSO` Studio, as "Use CreateStudioSessionMapping to assign users to a Studio when you use IAM Identity Center authentication"; one on an `IAM` Studio is refused. A call names its user or group by "Either IdentityName or IdentityId ... but not both", and the identity must be in the identity store of the @@ -273,7 +274,8 @@ nothing answers. both. A user or group mapped twice, or not mapped, is refused, each with `InvalidRequestException` in Floci's own wording. Getting, updating and removing a mapping finds it by the id or the name it was made with, so one whose user or group has since been deleted can still be removed. The `SessionPolicyArn` is kept as given. `ListStudioSessionMappings` lists a Studio's mappings, or every - Studio's when it names none, filtered by `IdentityType`, in the order they were made, 50 to a page. + Studio's when it names none, filtered by `IdentityType`, in the order they were made, 50 to a page, after a `Marker` + that resumes only the listing it came from: the same account, Region, Studio and `IdentityType`. ## Notebook executions diff --git a/src/main/java/io/floci/aws/services/emr/EmrNotebookExecutionService.java b/src/main/java/io/floci/aws/services/emr/EmrNotebookExecutionService.java index 049bc5a5dd..01ac13b8cc 100644 --- a/src/main/java/io/floci/aws/services/emr/EmrNotebookExecutionService.java +++ b/src/main/java/io/floci/aws/services/emr/EmrNotebookExecutionService.java @@ -1,6 +1,5 @@ package io.floci.aws.services.emr; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import io.floci.aws.config.EmulatorConfig; import io.floci.aws.core.common.AwsArnUtils; @@ -21,7 +20,6 @@ import java.time.Duration; import java.time.Instant; import java.time.temporal.ChronoUnit; import java.util.ArrayList; -import java.util.Arrays; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -274,19 +272,11 @@ public class EmrNotebookExecutionService { return execution; } - /** - * What a marker resumes only: the account, the Region and each filter, an absent one told apart from any value, as - * a JSON list. - */ + // What a marker resumes only: the account, the Region and each filter, EmrService.listingNamespace. private String listingNamespace(String editorId, String status, Instant from, Instant to, String engineId) { - List parts = Arrays.asList("ListNotebookExecutions", regionResolver.getAccountId(), + return EmrService.listingNamespace("ListNotebookExecutions", regionResolver.getAccountId(), regionResolver.getRegion(), editorId, status, from != null ? from.toString() : null, to != null ? to.toString() : null, engineId); - try { - return EmrService.SNAPSHOT_MAPPER.writeValueAsString(parts); - } catch (JsonProcessingException e) { - throw new IllegalStateException("Cannot write a listing's namespace", e); - } } // The execution's cluster as it now stands, or null once it is gone; any other failure is the caller's. diff --git a/src/main/java/io/floci/aws/services/emr/EmrService.java b/src/main/java/io/floci/aws/services/emr/EmrService.java index 36f647f3e5..5700d51897 100644 --- a/src/main/java/io/floci/aws/services/emr/EmrService.java +++ b/src/main/java/io/floci/aws/services/emr/EmrService.java @@ -1610,6 +1610,21 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear .findFirst(); } + /** + * What a listing's marker resumes only, as a JSON list: the operation, the account, the Region and each filter, an + * absent filter told apart from any value. A marker from another account, Region or filter is then refused, rather + * than resuming this listing at its cursor. + */ + static String listingNamespace(String operation, String accountId, String region, String... filters) { + List parts = new ArrayList<>(Arrays.asList(operation, accountId, region)); + parts.addAll(Arrays.asList(filters)); + try { + return SNAPSHOT_MAPPER.writeValueAsString(parts); + } catch (JsonProcessingException e) { + throw new IllegalStateException("Cannot write a listing's namespace", e); + } + } + static AwsException invalid(String message) { return new AwsException("InvalidRequestException", message, 400); } diff --git a/src/main/java/io/floci/aws/services/emr/EmrStudioService.java b/src/main/java/io/floci/aws/services/emr/EmrStudioService.java index a2515677e8..80eac7971b 100644 --- a/src/main/java/io/floci/aws/services/emr/EmrStudioService.java +++ b/src/main/java/io/floci/aws/services/emr/EmrStudioService.java @@ -162,8 +162,10 @@ public class EmrStudioService { List studios = store.scan(key -> true).stream() .filter(studio -> regionResolver.getRegion().equals(studio.getRegion())) .toList(); + // A marker resumes only the listing of its account and Region. PaginatedResult page = Pagination.paginate(studios, - studio -> String.format("%020d", studio.getSequence()), PAGE_SIZE, marker, "ListStudios", + studio -> String.format("%020d", studio.getSequence()), PAGE_SIZE, marker, + EmrService.listingNamespace("ListStudios", regionResolver.getAccountId(), regionResolver.getRegion()), token -> EmrService.invalid("Invalid Marker.")); return new PaginatedResult<>(page.items().stream().map(EmrStudioService::copy).toList(), page.nextToken()); } @@ -341,8 +343,10 @@ public class EmrStudioService { } } } - String namespace = String.join("|", "ListStudioSessionMappings", String.valueOf(studioId), - String.valueOf(identityType)); + // A marker resumes only the listing of its account, Region, Studio and identity type, an absent filter told + // apart from the text "null". + String namespace = EmrService.listingNamespace("ListStudioSessionMappings", regionResolver.getAccountId(), + regionResolver.getRegion(), studioId, identityType); PaginatedResult page = Pagination.paginate(mappings, StudioMapping::cursor, PAGE_SIZE, marker, namespace, token -> EmrService.invalid("Invalid Marker.")); return new PaginatedResult<>(page.items().stream() diff --git a/src/test/java/io/floci/aws/services/emr/EmrStudioServiceTest.java b/src/test/java/io/floci/aws/services/emr/EmrStudioServiceTest.java index 328131d104..5a13fbf44c 100644 --- a/src/test/java/io/floci/aws/services/emr/EmrStudioServiceTest.java +++ b/src/test/java/io/floci/aws/services/emr/EmrStudioServiceTest.java @@ -1,5 +1,6 @@ package io.floci.aws.services.emr; +import io.floci.aws.core.common.AwsException; import io.floci.aws.core.common.RegionResolver; import io.floci.aws.core.storage.AccountAwareStorageBackend; import io.floci.aws.services.ec2.Ec2Service; @@ -7,15 +8,19 @@ import io.floci.aws.services.ec2.model.SecurityGroup; import io.floci.aws.services.ec2.model.Subnet; import io.floci.aws.services.ec2.model.Vpc; import io.floci.aws.services.emr.model.EmrStudio; +import io.floci.aws.services.emr.model.EmrStudioSessionMapping; import io.floci.aws.services.identitystore.IdentityStoreService; import io.floci.aws.services.ssoadmin.SsoAdminService; import io.floci.aws.testing.MutableClock; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; import java.util.List; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyList; @@ -23,18 +28,26 @@ import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -/** A Studio's place in its listing, which no deleted Studio's can take, through {@link EmrStudioService}. */ +/** + * A Studio's place in its listing, which no deleted Studio's can take, and the listings' markers, which resume only + * their own account's and Region's, through {@link EmrStudioService}. + */ class EmrStudioServiceTest { private static final String REGION = "us-east-1"; private static final String ACCOUNT = "000000000000"; + private static final String OTHER_REGION = "eu-west-1"; + private static final String OTHER_ACCOUNT = "111111111111"; private final MutableClock clock = new MutableClock(); + // One store under every service here, as the services of one Floci share theirs. + private final AccountAwareStorageBackend store = AccountAwareStorageBackend.inMemory(ACCOUNT); + private Ec2Service ec2; private EmrStudioService studios; @BeforeEach void setUp() { - Ec2Service ec2 = mock(Ec2Service.class); + ec2 = mock(Ec2Service.class); Vpc vpc = new Vpc(); vpc.setVpcId("vpc-1"); Subnet subnet = new Subnet(); @@ -46,11 +59,58 @@ class EmrStudioServiceTest { when(ec2.describeVpcs(eq(REGION), anyList(), any())).thenReturn(List.of(vpc)); when(ec2.describeSubnets(eq(REGION), anyList(), any())).thenReturn(List.of(subnet)); when(ec2.describeSecurityGroups(eq(REGION), anyList(), anyList(), any())).thenReturn(List.of(group)); - studios = new EmrStudioService(AccountAwareStorageBackend.inMemory(ACCOUNT), - new RegionResolver(REGION, ACCOUNT), ec2, mock(SsoAdminService.class), + studios = service(REGION, ACCOUNT); + } + + // The service as a request in region of account reaches it. + private EmrStudioService service(String region, String account) { + return new EmrStudioService(store, new RegionResolver(region, account), ec2, mock(SsoAdminService.class), mock(IdentityStoreService.class), clock, "http://localhost:4566"); } + @Test + void aStudiosListMarkerResumesOnlyTheListingOfItsAccountAndRegion() { + for (int made = 0; made < 51; made++) { + studios.createStudio(studio("studio-" + made)); + } + String marker = studios.listStudios(null).nextToken(); + + assertNotNull(marker); + assertEquals(1, studios.listStudios(marker).items().size()); + assertRefused(() -> service(OTHER_REGION, ACCOUNT).listStudios(marker)); + assertRefused(() -> service(REGION, OTHER_ACCOUNT).listStudios(marker)); + } + + @Test + void aSessionMappingsListMarkerResumesOnlyTheListingOfItsAccountRegionAndFilters() { + EmrStudio studio = new EmrStudio(); + studio.setStudioId("es-MAPPINGS"); + studio.setRegion(REGION); + studio.setAuthMode("SSO"); + studio.setSequence(1); + for (int made = 0; made < 51; made++) { + EmrStudioSessionMapping mapping = new EmrStudioSessionMapping(); + mapping.setIdentityId("user-" + made); + mapping.setIdentityType("USER"); + mapping.setSequence(made + 1); + studio.getSessionMappings().add(mapping); + } + store.put(studio.getStudioId(), studio); + String marker = studios.listSessionMappings(null, null, null).nextToken(); + + assertNotNull(marker); + assertEquals(1, studios.listSessionMappings(null, null, marker).items().size()); + assertRefused(() -> service(OTHER_REGION, ACCOUNT).listSessionMappings(null, null, marker)); + assertRefused(() -> service(REGION, OTHER_ACCOUNT).listSessionMappings(null, null, marker)); + assertRefused(() -> studios.listSessionMappings(null, "USER", marker)); + } + + private static void assertRefused(Executable listing) { + AwsException refused = assertThrows(AwsException.class, listing); + assertEquals("InvalidRequestException", refused.getErrorCode()); + assertEquals("Invalid Marker.", refused.getMessage()); + } + @Test void aStudioMadeAfterTheNewestIsDeletedListsAfterEveryEarlierOne() { String first = studios.createStudio(studio("first")).getStudioId();