diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RamResourceShareAssociationsTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RamResourceShareAssociationsTest.java new file mode 100644 index 0000000000..3e76b56a47 --- /dev/null +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RamResourceShareAssociationsTest.java @@ -0,0 +1,59 @@ +package com.floci.test; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import software.amazon.awssdk.services.ram.RamClient; +import software.amazon.awssdk.services.ram.model.GetResourceShareAssociationsRequest; +import software.amazon.awssdk.services.ram.model.ResourceShareAssociation; +import software.amazon.awssdk.services.ram.model.ResourceShareAssociationType; + +import java.util.List; +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assumptions.assumeFalse; + +@DisplayName("RAM resource share associations") +class RamResourceShareAssociationsTest { + private static final String ACCOUNT = "777777777779"; + + @Test + @DisplayName("lists resource and principal associations through the AWS SDK") + void listsAssociationsWithSdkPagination() { + assumeFalse(TestFixtures.isRealAws(), "Uses synthetic resource ARNs"); + + String suffix = UUID.randomUUID().toString(); + String resourceOne = "arn:aws:ec2:us-east-1:" + ACCOUNT + ":transit-gateway/tgw-" + suffix; + String resourceTwo = "arn:aws:ec2:us-east-1:" + ACCOUNT + ":transit-gateway/tgw-second-" + suffix; + String principal = "888888888888"; + try (RamClient ram = TestFixtures.ramClient(ACCOUNT)) { + String shareArn = ram.createResourceShare(request -> request.name("sdk-associations-" + suffix) + .resourceArns(resourceOne, resourceTwo).principals(principal)) + .resourceShare().resourceShareArn(); + try { + List resources = ram.getResourceShareAssociationsPaginator( + GetResourceShareAssociationsRequest.builder() + .associationType(ResourceShareAssociationType.RESOURCE) + .resourceShareArns(shareArn).maxResults(1).build()) + .stream().flatMap(page -> page.resourceShareAssociations().stream()).toList(); + + assertThat(resources).extracting(ResourceShareAssociation::associatedEntity) + .containsExactlyInAnyOrder(resourceOne, resourceTwo); + assertThat(resources).allSatisfy(association -> { + assertThat(association.resourceShareArn()).isEqualTo(shareArn); + assertThat(association.statusAsString()).isEqualTo("ASSOCIATED"); + assertThat(association.creationTime()).isNotNull(); + }); + + assertThat(ram.getResourceShareAssociations(request -> request + .associationType(ResourceShareAssociationType.PRINCIPAL) + .resourceShareArns(shareArn).principal(principal)) + .resourceShareAssociations()) + .extracting(ResourceShareAssociation::associatedEntity) + .containsExactly(principal); + } finally { + ram.deleteResourceShare(request -> request.resourceShareArn(shareArn)); + } + } + } +} diff --git a/docs/services/index.md b/docs/services/index.md index 62011ff94e..f980be76f0 100644 --- a/docs/services/index.md +++ b/docs/services/index.md @@ -116,7 +116,7 @@ Operation counts are exact. For dispatch-table services (Query and JSON 1.1) eac | [AWS Service Catalog](service-catalog.md) | `POST /` + `X-Amz-Target: AWS242ServiceCatalogService.*` | JSON 1.1 | 89 | | [Service Quotas](servicequotas.md) | `POST /` + `X-Amz-Target: ServiceQuotasV20190624.*` | JSON 1.1 | 7 | | [AWS Budgets](budgets.md) | `POST /` + `X-Amz-Target: AWSBudgetServiceGateway.*` | JSON 1.1 | 26 | -| [AWS RAM](ram.md) | `POST /{operationname}` (lowercase), `DELETE /deleteresourceshare` | REST JSON | 15 | +| [AWS RAM](ram.md) | `POST /{operationname}` (lowercase), `DELETE /deleteresourceshare` | REST JSON | 16 | | [Control Catalog](controlcatalog.md) | `/get-control`, `/list-controls` | REST JSON | 2 | | [AWS Marketplace](marketplace.md) | Marketplace Catalog API | REST JSON | 56 | | [Control Tower](controltower.md) | `/list-landingzones`, `/get-landingzone`, `/create-landingzone`, `/*-baseline*` | REST JSON | 15 | diff --git a/docs/services/ram.md b/docs/services/ram.md index 21a6c33c6d..4fb48c70b5 100644 --- a/docs/services/ram.md +++ b/docs/services/ram.md @@ -23,6 +23,7 @@ below for what's simplified. | `UpdateResourceShare` | Renames a share and/or toggles `allowExternalPrincipals` (`POST /updateresourceshare`) | | `AssociateResourceShare` | Adds resource ARNs and/or principals to an existing share (`POST /associateresourceshare`) | | `DisassociateResourceShare` | Removes resource ARNs and/or principals from a share (`POST /disassociateresourceshare`) | +| `GetResourceShareAssociations` | Lists active resource or principal associations for shares owned by the caller, with entity, share ARN, status, and pagination filters (`POST /getresourceshareassociations`) | | `ListPrincipals` | Lists principals associated with visible shares (`POST /listprincipals`) | | `ListResourceSharePermissions` | Lists the permissions associated with a share: one default AWS managed permission (`AWSRAMDefaultPermission`, `AWS_MANAGED`) per distinct resource type in the share; unknown share ARN is `UnknownResourceException` (`POST /listresourcesharepermissions`) | | `TagResource` | Adds/overwrites tags on a resource share (`POST /tagresource`) | @@ -92,8 +93,10 @@ aws --endpoint-url http://localhost:4566 ram accept-resource-share-invitation \ unimplemented operation falls through to S3's `/{bucket}` route and comes back as an S3 `NoSuchBucket` XML body where the SDK expects JSON, not a modeled `UnknownOperationException`. If a client reports an XML parse error against a RAM call, that is the cause, not credentials. - `GetResourceShareAssociations` is the one most likely to be hit: Terraform's - `aws_ram_resource_association` and `aws_ram_principal_association` read it on every refresh. +- `GetResourceShareAssociations` reports current `ASSOCIATED` resource and principal entries + for caller-owned shares. There are no persisted per-association transitions or source entries, + so `SOURCE` and other status filters return an empty page. Association creation and update + times use the containing share's times; `external` is always false. - `AssociateResourceShare`/`DisassociateResourceShare` responses synthesize one `resourceShareAssociation` row per requested ARN/principal rather than tracking real per-association status transitions (e.g. no `ASSOCIATING`/`DISASSOCIATING` intermediate diff --git a/src/main/java/io/github/hectorvent/floci/services/ram/RamController.java b/src/main/java/io/github/hectorvent/floci/services/ram/RamController.java index a0b151a06a..4b4404ef01 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ram/RamController.java +++ b/src/main/java/io/github/hectorvent/floci/services/ram/RamController.java @@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.ObjectReader; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.PaginatedResult; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.common.ServicePrincipals; import io.github.hectorvent.floci.services.iam.IamService; @@ -14,6 +15,7 @@ import io.github.hectorvent.floci.services.iam.ServiceLinkedRoles; import io.github.hectorvent.floci.services.organizations.OrganizationsService; import io.github.hectorvent.floci.services.ram.model.PrincipalAssociation; import io.github.hectorvent.floci.services.ram.model.ResourceShare; +import io.github.hectorvent.floci.services.ram.model.ResourceShareAssociation; import io.github.hectorvent.floci.services.ram.model.ResourceShareInvitation; import io.github.hectorvent.floci.services.ram.model.SharePermission; import io.github.hectorvent.floci.services.ram.model.SharedResource; @@ -197,6 +199,49 @@ public class RamController { return Response.ok(response).build(); } + @POST + @Path("/getresourceshareassociations") + @Consumes(MediaType.WILDCARD) + public Response getResourceShareAssociations(String body) { + JsonNode request = readTree(body); + JsonNode maxResultsNode = request.get("maxResults"); + Integer maxResults = null; + if (maxResultsNode != null && !maxResultsNode.isNull()) { + if (!maxResultsNode.isIntegralNumber() || !maxResultsNode.canConvertToInt()) { + throw new AwsException("InvalidParameterException", "maxResults must be an integer.", 400); + } + maxResults = maxResultsNode.intValue(); + } + PaginatedResult page = service.getResourceShareAssociations( + regionResolver.getAccountId(), regionResolver.getRegion(), + request.path("associationType").asText(), + request.hasNonNull("associationStatus") ? request.path("associationStatus").asText() : null, + stringList(request.path("resourceShareArns")), + request.hasNonNull("resourceArn") ? request.path("resourceArn").asText() : null, + request.hasNonNull("principal") ? request.path("principal").asText() : null, + maxResults, request.hasNonNull("nextToken") ? request.path("nextToken").asText() : null); + + ArrayNode associations = objectMapper.createArrayNode(); + for (ResourceShareAssociation association : page.items()) { + ObjectNode node = objectMapper.createObjectNode(); + node.put("resourceShareArn", association.resourceShareArn()); + node.put("resourceShareName", association.resourceShareName()); + node.put("associatedEntity", association.associatedEntity()); + node.put("associationType", association.associationType()); + node.put("status", "ASSOCIATED"); + node.put("creationTime", association.creationTime().toEpochMilli() / 1000.0); + node.put("lastUpdatedTime", association.lastUpdatedTime().toEpochMilli() / 1000.0); + node.put("external", false); + associations.add(node); + } + ObjectNode response = objectMapper.createObjectNode(); + response.set("resourceShareAssociations", associations); + if (page.nextToken() != null) { + response.put("nextToken", page.nextToken()); + } + return Response.ok(response).build(); + } + @POST @Path("/listprincipals") @Consumes(MediaType.WILDCARD) diff --git a/src/main/java/io/github/hectorvent/floci/services/ram/RamService.java b/src/main/java/io/github/hectorvent/floci/services/ram/RamService.java index f97639ee06..0541501daa 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ram/RamService.java +++ b/src/main/java/io/github/hectorvent/floci/services/ram/RamService.java @@ -4,6 +4,8 @@ import com.fasterxml.jackson.core.type.TypeReference; import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.AwsPartitions; +import io.github.hectorvent.floci.core.common.PaginatedResult; +import io.github.hectorvent.floci.core.common.Pagination; import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; import io.github.hectorvent.floci.core.storage.StorageBackend; import io.github.hectorvent.floci.core.storage.StorageFactory; @@ -11,6 +13,7 @@ import io.github.hectorvent.floci.services.organizations.OrganizationsService; import io.github.hectorvent.floci.services.organizations.model.Organization; import io.github.hectorvent.floci.services.ram.model.PrincipalAssociation; import io.github.hectorvent.floci.services.ram.model.ResourceShare; +import io.github.hectorvent.floci.services.ram.model.ResourceShareAssociation; import io.github.hectorvent.floci.services.ram.model.ResourceShareInvitation; import io.github.hectorvent.floci.services.ram.model.SharePermission; import io.github.hectorvent.floci.services.ram.model.SharedResource; @@ -54,6 +57,9 @@ public class RamService { /** The modeled ResourceShareStatus enum. */ private static final List SHARE_STATUSES = List.of("PENDING", "ACTIVE", "FAILED", "DELETING", "DELETED"); + private static final List ASSOCIATION_TYPES = List.of("PRINCIPAL", "RESOURCE", "SOURCE"); + private static final List ASSOCIATION_STATUSES = List.of("ASSOCIATING", "ASSOCIATED", "FAILED", + "DISASSOCIATING", "DISASSOCIATED", "SUSPENDED", "SUSPENDING", "RESTORING"); /** The owner segment of an AWS managed permission ARN. */ private static final String MANAGED_PERMISSION_OWNER = "aws"; /** @@ -325,15 +331,7 @@ public class RamService { requireResourceOwner(resourceOwner); List result = new ArrayList<>(); for (ResourceShare share : sharesIn(region)) { - if (!isVisible(share, callerAccountId, resourceOwner)) { - continue; - } - // A deleted share stays readable via GetResourceShares (status DELETED) - // but its contents stop being consumable. - if ("DELETED".equals(share.getStatus())) { - continue; - } - if (!resourceShareArns.isEmpty() && !resourceShareArns.contains(share.getResourceShareArn())) { + if (!isConsumableShare(share, callerAccountId, resourceOwner, resourceShareArns)) { continue; } for (String resourceArn : share.getResourceArns()) { @@ -400,15 +398,7 @@ public class RamService { requireResourceOwner(resourceOwner); List result = new ArrayList<>(); for (ResourceShare share : sharesIn(region)) { - if (!isVisible(share, callerAccountId, resourceOwner)) { - continue; - } - // A deleted share stays readable via GetResourceShares (status DELETED) - // but its contents stop being consumable. - if ("DELETED".equals(share.getStatus())) { - continue; - } - if (!resourceShareArns.isEmpty() && !resourceShareArns.contains(share.getResourceShareArn())) { + if (!isConsumableShare(share, callerAccountId, resourceOwner, resourceShareArns)) { continue; } for (String principal : share.getPrincipals()) { @@ -419,6 +409,70 @@ public class RamService { return result; } + public PaginatedResult getResourceShareAssociations( + String callerAccountId, String region, String associationType, String associationStatus, + List resourceShareArns, String resourceArn, String principal, + Integer maxResults, String nextToken) { + if (associationType == null || !ASSOCIATION_TYPES.contains(associationType)) { + throw new AwsException("InvalidParameterException", "associationType must be one of " + + ASSOCIATION_TYPES + ".", 400); + } + if (associationStatus != null && !ASSOCIATION_STATUSES.contains(associationStatus)) { + throw new AwsException("InvalidParameterException", "associationStatus must be one of " + + ASSOCIATION_STATUSES + ".", 400); + } + if (("RESOURCE".equals(associationType) && principal != null) + || ("PRINCIPAL".equals(associationType) && resourceArn != null)) { + throw new AwsException("InvalidParameterException", + "The entity filter is not valid for this associationType.", 400); + } + requireValidArns(resourceShareArns); + if (resourceArn != null && !isValidArn(resourceArn)) { + throw new AwsException("MalformedArnException", "Invalid resourceArn: " + resourceArn, 400); + } + if (maxResults != null && (maxResults < 1 || maxResults > 500)) { + throw new AwsException("InvalidParameterException", "maxResults must be between 1 and 500.", 400); + } + if (nextToken != null && nextToken.isEmpty()) { + throw new AwsException("InvalidNextTokenException", "Invalid nextToken.", 400); + } + + List associations = new ArrayList<>(); + if (associationStatus == null || "ASSOCIATED".equals(associationStatus)) { + for (ResourceShare share : sharesIn(region)) { + if (!isConsumableShare(share, callerAccountId, "SELF", resourceShareArns)) { + continue; + } + if ("RESOURCE".equals(associationType)) { + for (String entity : new LinkedHashSet<>(share.getResourceArns())) { + if (resourceArn == null || resourceArn.equals(entity)) { + associations.add(association(share, entity, "RESOURCE")); + } + } + } else if ("PRINCIPAL".equals(associationType)) { + for (String entity : new LinkedHashSet<>(share.getPrincipals())) { + if (principal == null || principal.equals(entity)) { + associations.add(association(share, entity, "PRINCIPAL")); + } + } + } + } + } + + String namespace = "ram-associations|" + callerAccountId + "|" + region + "|" + associationType + + "|" + associationStatus + "|" + resourceShareArns.stream().sorted().toList() + + "|" + resourceArn + "|" + principal; + return Pagination.paginate(associations, + association -> association.resourceShareArn() + "\u0000" + association.associatedEntity(), + maxResults != null ? maxResults : 100, nextToken, namespace, + ignored -> new AwsException("InvalidNextTokenException", "Invalid nextToken.", 400)); + } + + private static ResourceShareAssociation association(ResourceShare share, String entity, String type) { + return new ResourceShareAssociation(share.getResourceShareArn(), share.getName(), entity, type, + share.getCreationTime(), share.getLastUpdatedTime()); + } + /** * Floci has no explicit permission model: every resource type in a share is governed by RAM's * default AWS managed permission for that type, so one summary is returned per distinct type. @@ -593,6 +647,14 @@ public class RamService { .anyMatch(principal -> isVisibleToPrincipal(share, principal, callerAccountId)); } + private boolean isConsumableShare(ResourceShare share, String callerAccountId, + String resourceOwner, List resourceShareArns) { + // Deleted shares remain readable through GetResourceShares, but not these contents. + return isVisible(share, callerAccountId, resourceOwner) + && !"DELETED".equals(share.getStatus()) + && (resourceShareArns.isEmpty() || resourceShareArns.contains(share.getResourceShareArn())); + } + private boolean isVisibleToPrincipal(ResourceShare share, String principal, String callerAccountId) { if (ACCOUNT_ID_PRINCIPAL.matcher(principal).matches()) { if (!principal.equals(callerAccountId)) { diff --git a/src/main/java/io/github/hectorvent/floci/services/ram/model/ResourceShareAssociation.java b/src/main/java/io/github/hectorvent/floci/services/ram/model/ResourceShareAssociation.java new file mode 100644 index 0000000000..e8ae4d5c4a --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/ram/model/ResourceShareAssociation.java @@ -0,0 +1,9 @@ +package io.github.hectorvent.floci.services.ram.model; + +import java.time.Instant; + +/** An active resource or principal association in an owned RAM resource share. */ +public record ResourceShareAssociation(String resourceShareArn, String resourceShareName, + String associatedEntity, String associationType, + Instant creationTime, Instant lastUpdatedTime) { +} diff --git a/src/test/java/io/github/hectorvent/floci/services/ram/RamGetResourceShareAssociationsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ram/RamGetResourceShareAssociationsIntegrationTest.java new file mode 100644 index 0000000000..4d9d3714d1 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/ram/RamGetResourceShareAssociationsIntegrationTest.java @@ -0,0 +1,82 @@ +package io.github.hectorvent.floci.services.ram; + +import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.notNullValue; + +@QuarkusTest +class RamGetResourceShareAssociationsIntegrationTest { + + private static final String AUTH_HEADER = + "AWS4-HMAC-SHA256 Credential=777777777777/20260101/us-east-1/ram/aws4_request"; + private static final String EMPTY_ACCOUNT_AUTH_HEADER = + "AWS4-HMAC-SHA256 Credential=777777777778/20260101/us-east-1/ram/aws4_request"; + + @BeforeAll + static void configureRestAssured() { + RestAssuredJsonUtils.configureAwsContentTypes(); + } + + @Test + void getResourceShareAssociationsReturnsAnEmptyJsonArrayBeforeAnySharesExist() { + given() + .contentType("application/json") + .header("Authorization", EMPTY_ACCOUNT_AUTH_HEADER) + .body("{\"associationType\":\"RESOURCE\"}") + .when() + .post("/getresourceshareassociations") + .then() + .statusCode(200) + .contentType("application/json") + .body("resourceShareAssociations.size()", equalTo(0)); + } + + @Test + void getResourceShareAssociationsReturnsTheRequestedResourceAndPrincipal() { + String resourceArn = "arn:aws:ec2:us-east-1:777777777777:transit-gateway/tgw-association"; + String shareArn = given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body("{\"name\":\"association-test\",\"principals\":[\"888888888888\"]," + + "\"resourceArns\":[\"" + resourceArn + "\"]}") + .when() + .post("/createresourceshare") + .then() + .statusCode(200) + .extract().path("resourceShare.resourceShareArn"); + + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body("{\"associationType\":\"RESOURCE\",\"resourceShareArns\":[\"" + shareArn + + "\"],\"resourceArn\":\"" + resourceArn + "\"}") + .when() + .post("/getresourceshareassociations") + .then() + .statusCode(200) + .body("resourceShareAssociations.size()", equalTo(1)) + .body("resourceShareAssociations[0].resourceShareArn", equalTo(shareArn)) + .body("resourceShareAssociations[0].associatedEntity", equalTo(resourceArn)) + .body("resourceShareAssociations[0].associationType", equalTo("RESOURCE")) + .body("resourceShareAssociations[0].status", equalTo("ASSOCIATED")) + .body("resourceShareAssociations[0].creationTime", notNullValue()); + + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body("{\"associationType\":\"PRINCIPAL\",\"resourceShareArns\":[\"" + shareArn + + "\"],\"principal\":\"888888888888\"}") + .when() + .post("/getresourceshareassociations") + .then() + .statusCode(200) + .body("resourceShareAssociations.size()", equalTo(1)) + .body("resourceShareAssociations[0].associatedEntity", equalTo("888888888888")) + .body("resourceShareAssociations[0].associationType", equalTo("PRINCIPAL")); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/ram/RamServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/ram/RamServiceTest.java index 4b26589551..bc97ba9006 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ram/RamServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ram/RamServiceTest.java @@ -2,12 +2,14 @@ package io.github.hectorvent.floci.services.ram; import com.fasterxml.jackson.core.type.TypeReference; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.PaginatedResult; import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; import io.github.hectorvent.floci.core.storage.StorageBackend; import io.github.hectorvent.floci.core.storage.StorageFactory; import io.github.hectorvent.floci.services.organizations.OrganizationsService; import io.github.hectorvent.floci.services.organizations.model.Organization; import io.github.hectorvent.floci.services.ram.model.ResourceShare; +import io.github.hectorvent.floci.services.ram.model.ResourceShareAssociation; import io.github.hectorvent.floci.services.ram.model.ResourceShareInvitation; import io.github.hectorvent.floci.services.ram.model.SharedResource; import org.junit.jupiter.api.BeforeEach; @@ -70,6 +72,8 @@ class RamServiceTest { assertTrue(service.listResources(ACCEPTER, "OTHER-ACCOUNTS", List.of(), "us-east-1").isEmpty()); assertTrue(service.listPrincipals(ACCEPTER, "OTHER-ACCOUNTS", List.of(), "us-east-1").isEmpty()); assertTrue(service.listResources(OWNER, "SELF", List.of(), "us-east-1").isEmpty()); + assertTrue(associations(OWNER, "us-east-1", "RESOURCE", null, + List.of(share.getResourceShareArn()), null, null, null, null).items().isEmpty()); // The share itself remains readable with DELETED status. List own = service.getResourceShares(OWNER, "SELF", "us-east-1"); @@ -77,6 +81,89 @@ class RamServiceTest { assertEquals("DELETED", own.get(0).getStatus()); } + @Test + void getResourceShareAssociationsFiltersOwnedResourcesAndPrincipals() { + ResourceShare share = service.createResourceShare("associations", List.of(OU_ARN), + List.of(TGW_ARN), false, "us-east-1", OWNER); + service.createResourceShare("other-region", List.of(OU_ARN), List.of(TGW_ARN), + false, "eu-west-1", OWNER); + service.createResourceShare("other-owner", List.of(OWNER), List.of(TGW_ARN), + false, "us-east-1", ACCEPTER); + + PaginatedResult resources = associations( + OWNER, "us-east-1", "RESOURCE", null, List.of(), TGW_ARN, null, null, null); + assertEquals(1, resources.items().size()); + assertEquals(share.getResourceShareArn(), resources.items().getFirst().resourceShareArn()); + assertEquals(TGW_ARN, resources.items().getFirst().associatedEntity()); + assertEquals(share.getCreationTime(), resources.items().getFirst().creationTime()); + assertNull(resources.nextToken()); + + assertEquals(1, associations(OWNER, "us-east-1", "PRINCIPAL", null, + List.of(share.getResourceShareArn()), null, OU_ARN, null, null).items().size()); + assertTrue(associations(ACCEPTER, "us-east-1", "RESOURCE", null, + List.of(share.getResourceShareArn()), TGW_ARN, null, null, null).items().isEmpty()); + assertTrue(associations(OWNER, "eu-west-1", "RESOURCE", null, + List.of(share.getResourceShareArn()), TGW_ARN, null, null, null).items().isEmpty()); + assertTrue(associations(OWNER, "us-east-1", "RESOURCE", "DISASSOCIATED", + List.of(), null, null, null, null).items().isEmpty()); + assertTrue(associations(OWNER, "us-east-1", "SOURCE", null, + List.of(), null, null, null, null).items().isEmpty()); + + service.disassociateResourceShare(share.getResourceShareArn(), List.of(TGW_ARN), List.of(), + OWNER, "us-east-1"); + assertTrue(associations(OWNER, "us-east-1", "RESOURCE", null, + List.of(share.getResourceShareArn()), null, null, null, null).items().isEmpty()); + } + + @Test + void getResourceShareAssociationsPagesWithoutLeakingFiltersAcrossTokens() { + ResourceShare share = service.createResourceShare("many-associations", List.of(OU_ARN), + List.of(TGW_ARN, TGW_ARN + "2"), false, "us-east-1", OWNER); + PaginatedResult first = associations(OWNER, "us-east-1", "RESOURCE", + null, List.of(share.getResourceShareArn()), null, null, 1, null); + assertEquals(1, first.items().size()); + assertNotNull(first.nextToken()); + PaginatedResult second = associations(OWNER, "us-east-1", "RESOURCE", + null, List.of(share.getResourceShareArn()), null, null, 1, first.nextToken()); + assertEquals(1, second.items().size()); + assertNotEquals(first.items().getFirst().associatedEntity(), second.items().getFirst().associatedEntity()); + assertNull(second.nextToken()); + + AwsException mismatched = assertThrows(AwsException.class, () -> associations( + OWNER, "us-east-1", "PRINCIPAL", null, List.of(share.getResourceShareArn()), + null, null, 1, first.nextToken())); + assertEquals("InvalidNextTokenException", mismatched.getErrorCode()); + AwsException malformed = assertThrows(AwsException.class, () -> associations( + OWNER, "us-east-1", "RESOURCE", null, List.of(share.getResourceShareArn()), + null, null, 1, "not-a-token")); + assertEquals("InvalidNextTokenException", malformed.getErrorCode()); + } + + @Test + void getResourceShareAssociationsRejectsInvalidInput() { + assertInvalidParameter(() -> associations(OWNER, "us-east-1", null, null, + List.of(), null, null, null, null)); + assertInvalidParameter(() -> associations(OWNER, "us-east-1", "RESOURCE", null, + List.of(), null, OWNER, null, null)); + assertInvalidParameter(() -> associations(OWNER, "us-east-1", "PRINCIPAL", null, + List.of(), TGW_ARN, null, null, null)); + assertInvalidParameter(() -> associations(OWNER, "us-east-1", "RESOURCE", null, + List.of(), null, null, 0, null)); + assertInvalidParameter(() -> associations(OWNER, "us-east-1", "RESOURCE", null, + List.of(), null, null, 501, null)); + AwsException malformedArn = assertThrows(AwsException.class, () -> associations( + OWNER, "us-east-1", "RESOURCE", null, List.of("not-an-arn"), + null, null, null, null)); + assertEquals("MalformedArnException", malformedArn.getErrorCode()); + } + + private PaginatedResult associations(String caller, String region, String type, + String status, List shareArns, String resourceArn, String principal, + Integer maxResults, String nextToken) { + return service.getResourceShareAssociations(caller, region, type, status, shareArns, + resourceArn, principal, maxResults, nextToken); + } + @Test void createResourceShareIsVisibleToOwnerAsSelf() { ResourceShare share = service.createResourceShare(