From 162401b94207a9b1d4d1fdd7eef888e0d95abfda Mon Sep 17 00:00:00 2001 From: soreavis <263610811+soreavis@users.noreply.github.com> Date: Thu, 12 Nov 2026 03:41:41 +0100 Subject: [PATCH] fix(organizations): attach the tags passed to InviteAccountToOrganization (#8048) --- .../OrganizationsJsonHandler.java | 3 ++- .../organizations/OrganizationsService.java | 7 ++++- .../organizations/model/Handshake.java | 13 +++++++++ .../inspector2/Inspector2IntegrationTest.java | 2 +- ...anizationsCrossAccountIntegrationTest.java | 12 ++++++++- .../OrganizationsServiceTest.java | 27 ++++++++++++++++--- .../SecurityHubMembersIntegrationTest.java | 2 +- ...ecurityHubOrganizationIntegrationTest.java | 2 +- 8 files changed, 59 insertions(+), 9 deletions(-) diff --git a/src/main/java/io/floci/aws/services/organizations/OrganizationsJsonHandler.java b/src/main/java/io/floci/aws/services/organizations/OrganizationsJsonHandler.java index e1e46abaf0..d55c7710d6 100644 --- a/src/main/java/io/floci/aws/services/organizations/OrganizationsJsonHandler.java +++ b/src/main/java/io/floci/aws/services/organizations/OrganizationsJsonHandler.java @@ -588,7 +588,8 @@ public class OrganizationsJsonHandler { private Response inviteAccountToOrganization(JsonNode request, String caller) { JsonNode target = request.path("Target"); Handshake handshake = service.inviteAccountToOrganization(caller, - text(target, "Id"), text(target, "Type"), text(request, "Notes")); + text(target, "Id"), text(target, "Type"), text(request, "Notes"), + parseTags(request.path("Tags"))); return handshakeResponse(handshake); } diff --git a/src/main/java/io/floci/aws/services/organizations/OrganizationsService.java b/src/main/java/io/floci/aws/services/organizations/OrganizationsService.java index 1b02a7abc4..5fddc86ab9 100644 --- a/src/main/java/io/floci/aws/services/organizations/OrganizationsService.java +++ b/src/main/java/io/floci/aws/services/organizations/OrganizationsService.java @@ -1289,7 +1289,7 @@ public class OrganizationsService implements ScpProvider, RootAccessOrganization // ──────────────────────────── Handshakes ──────────────────────────── public Handshake inviteAccountToOrganization(String callerAccountId, String targetId, String targetType, - String notes) { + String notes, Map tags) { Organization organization = requireManagementAccount(callerAccountId); if (targetId == null || targetId.isEmpty()) { throw invalidInput("Target.Id is required."); @@ -1298,6 +1298,7 @@ public class OrganizationsService implements ScpProvider, RootAccessOrganization if (!"ACCOUNT".equals(resolvedType) && !"EMAIL".equals(resolvedType)) { throw invalidInput("Target.Type must be ACCOUNT or EMAIL."); } + validateTags(tags); String invitedAccountId = null; String invitedEmail = null; @@ -1334,6 +1335,9 @@ public class OrganizationsService implements ScpProvider, RootAccessOrganization Handshake handshake = newHandshake(organization, HANDSHAKE_INVITE); handshake.setTargetAccountId(invitedAccountId); handshake.setTargetEmail(invitedEmail); + if (tags != null) { + handshake.getTags().putAll(tags); + } handshake.getParties().add(new HandshakeParty(organization.getId(), "ORGANIZATION")); handshake.getParties().add(new HandshakeParty(targetId, resolvedType)); @@ -1378,6 +1382,7 @@ public class OrganizationsService implements ScpProvider, RootAccessOrganization : "account-" + callerAccountId + "@example.com"; OrganizationAccount account = newMemberAccount( organization, callerAccountId, email, "invited-" + callerAccountId, "INVITED"); + account.getTags().putAll(handshake.getTags()); accounts.putForAccount(organization.getMasterAccountId(), callerAccountId, account); attachFullAwsAccess(organization, callerAccountId); handshake.setTargetAccountId(callerAccountId); diff --git a/src/main/java/io/floci/aws/services/organizations/model/Handshake.java b/src/main/java/io/floci/aws/services/organizations/model/Handshake.java index 4ad286f36c..7356aaa446 100644 --- a/src/main/java/io/floci/aws/services/organizations/model/Handshake.java +++ b/src/main/java/io/floci/aws/services/organizations/model/Handshake.java @@ -4,7 +4,9 @@ import io.quarkus.runtime.annotations.RegisterForReflection; import java.time.Instant; import java.util.ArrayList; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; /** * Domain model for a handshake — the two-party exchange behind @@ -13,6 +15,8 @@ import java.util.List; *

{@code targetAccountId} and {@code targetEmail} are Floci bookkeeping, not part of the * AWS wire shape: they record which account (or email) the invitation was addressed to so * {@code ListHandshakesForAccount} can answer for a caller that has not joined yet. + * {@code tags} is bookkeeping too: the invitation's tags, attached to the account when it + * accepts. */ @RegisterForReflection public class Handshake { @@ -28,6 +32,7 @@ public class Handshake { private String organizationId; private String targetAccountId; private String targetEmail; + private Map tags = new LinkedHashMap<>(); public String getId() { return id; @@ -116,4 +121,12 @@ public class Handshake { public void setTargetEmail(String targetEmail) { this.targetEmail = targetEmail; } + + public Map getTags() { + return tags; + } + + public void setTags(Map tags) { + this.tags = tags; + } } diff --git a/src/test/java/io/floci/aws/services/inspector2/Inspector2IntegrationTest.java b/src/test/java/io/floci/aws/services/inspector2/Inspector2IntegrationTest.java index 3228d9fae6..b83c15aff7 100644 --- a/src/test/java/io/floci/aws/services/inspector2/Inspector2IntegrationTest.java +++ b/src/test/java/io/floci/aws/services/inspector2/Inspector2IntegrationTest.java @@ -192,7 +192,7 @@ class Inspector2IntegrationTest { organizationsService.createOrganization(managementAccountId, "ALL"); for (String member : members) { Handshake handshake = organizationsService.inviteAccountToOrganization( - managementAccountId, member, "ACCOUNT", null); + managementAccountId, member, "ACCOUNT", null, null); organizationsService.acceptHandshake(member, handshake.getId()); } } diff --git a/src/test/java/io/floci/aws/services/organizations/OrganizationsCrossAccountIntegrationTest.java b/src/test/java/io/floci/aws/services/organizations/OrganizationsCrossAccountIntegrationTest.java index 8a98c3c770..4ea1496283 100644 --- a/src/test/java/io/floci/aws/services/organizations/OrganizationsCrossAccountIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/organizations/OrganizationsCrossAccountIntegrationTest.java @@ -92,7 +92,8 @@ class OrganizationsCrossAccountIntegrationTest { @Order(3) void inviteAccountToOrganization() { inviteHandshakeId = organizations(MANAGEMENT_ACCOUNT, "InviteAccountToOrganization", - "{\"Target\":{\"Id\":\"" + INVITED_ACCOUNT + "\",\"Type\":\"ACCOUNT\"},\"Notes\":\"join us\"}") + "{\"Target\":{\"Id\":\"" + INVITED_ACCOUNT + "\",\"Type\":\"ACCOUNT\"},\"Notes\":\"join us\"," + + "\"Tags\":[{\"Key\":\"team\",\"Value\":\"red\"}]}") .when() .post("/") .then() @@ -179,6 +180,15 @@ class OrganizationsCrossAccountIntegrationTest { .body("Accounts.Id", hasItem(INVITED_ACCOUNT)) .body("Accounts.find { it.Id == '" + INVITED_ACCOUNT + "' }.JoinedMethod", equalTo("INVITED")) .body("Accounts.find { it.Id == '" + INVITED_ACCOUNT + "' }.Status", equalTo("ACTIVE")); + + organizations(MANAGEMENT_ACCOUNT, "ListTagsForResource", "{\"ResourceId\":\"" + INVITED_ACCOUNT + "\"}") + .when() + .post("/") + .then() + .statusCode(200) + .body("Tags", hasSize(1)) + .body("Tags[0].Key", equalTo("team")) + .body("Tags[0].Value", equalTo("red")); } @Test diff --git a/src/test/java/io/floci/aws/services/organizations/OrganizationsServiceTest.java b/src/test/java/io/floci/aws/services/organizations/OrganizationsServiceTest.java index 885f53628d..8d668e10c4 100644 --- a/src/test/java/io/floci/aws/services/organizations/OrganizationsServiceTest.java +++ b/src/test/java/io/floci/aws/services/organizations/OrganizationsServiceTest.java @@ -328,7 +328,7 @@ class OrganizationsServiceTest { void anExpiredHandshakeReportsExpiredAndCannotBeAccepted() { service.createOrganization(MANAGEMENT_ACCOUNT, "ALL"); Handshake handshake = service.inviteAccountToOrganization( - MANAGEMENT_ACCOUNT, OUTSIDER_ACCOUNT, "ACCOUNT", null); + MANAGEMENT_ACCOUNT, OUTSIDER_ACCOUNT, "ACCOUNT", null, null); handshake.setExpirationTimestamp(Instant.now().minus(1, ChronoUnit.DAYS)); handshakes.putForAccount(MANAGEMENT_ACCOUNT, handshake.getId(), handshake); @@ -344,7 +344,7 @@ class OrganizationsServiceTest { void acceptingAnInvitationJoinsTheOrganizationAsAnInvitedMember() { Organization organization = service.createOrganization(MANAGEMENT_ACCOUNT, "ALL"); Handshake handshake = service.inviteAccountToOrganization( - MANAGEMENT_ACCOUNT, OUTSIDER_ACCOUNT, "ACCOUNT", null); + MANAGEMENT_ACCOUNT, OUTSIDER_ACCOUNT, "ACCOUNT", null, null); Handshake accepted = service.acceptHandshake(OUTSIDER_ACCOUNT, handshake.getId()); assertEquals("ACCEPTED", accepted.getState()); @@ -355,11 +355,32 @@ class OrganizationsServiceTest { assertEquals(organization.getId(), service.describeOrganization(OUTSIDER_ACCOUNT).getId()); } + @Test + void acceptingAnInvitationAttachesItsTagsToTheAccount() { + service.createOrganization(MANAGEMENT_ACCOUNT, "ALL"); + Handshake handshake = service.inviteAccountToOrganization( + MANAGEMENT_ACCOUNT, OUTSIDER_ACCOUNT, "ACCOUNT", null, Map.of("team", "red")); + + service.acceptHandshake(OUTSIDER_ACCOUNT, handshake.getId()); + + assertEquals(Map.of("team", "red"), service.listTagsForResource(MANAGEMENT_ACCOUNT, OUTSIDER_ACCOUNT)); + } + + @Test + void anInvalidTagFailsTheWholeInvitation() { + service.createOrganization(MANAGEMENT_ACCOUNT, "ALL"); + + AwsException error = assertThrows(AwsException.class, () -> service.inviteAccountToOrganization( + MANAGEMENT_ACCOUNT, OUTSIDER_ACCOUNT, "ACCOUNT", null, Map.of("", "red"))); + assertEquals("InvalidInputException", error.getErrorCode()); + assertTrue(service.listHandshakesForOrganization(MANAGEMENT_ACCOUNT, null, null).isEmpty()); + } + @Test void memberAccountsCannotPerformManagementActions() { Organization organization = service.createOrganization(MANAGEMENT_ACCOUNT, "ALL"); Handshake handshake = service.inviteAccountToOrganization( - MANAGEMENT_ACCOUNT, OUTSIDER_ACCOUNT, "ACCOUNT", null); + MANAGEMENT_ACCOUNT, OUTSIDER_ACCOUNT, "ACCOUNT", null, null); service.acceptHandshake(OUTSIDER_ACCOUNT, handshake.getId()); AwsException error = assertThrows(AwsException.class, () -> service.createOrganizationalUnit( diff --git a/src/test/java/io/floci/aws/services/securityhub/SecurityHubMembersIntegrationTest.java b/src/test/java/io/floci/aws/services/securityhub/SecurityHubMembersIntegrationTest.java index 60349b127f..f67ce1edce 100644 --- a/src/test/java/io/floci/aws/services/securityhub/SecurityHubMembersIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/securityhub/SecurityHubMembersIntegrationTest.java @@ -343,7 +343,7 @@ class SecurityHubMembersIntegrationTest { private void joinOrganization(String managementAccountId, String member) { Handshake handshake = organizationsService.inviteAccountToOrganization( - managementAccountId, member, "ACCOUNT", null); + managementAccountId, member, "ACCOUNT", null, null); organizationsService.acceptHandshake(member, handshake.getId()); } diff --git a/src/test/java/io/floci/aws/services/securityhub/SecurityHubOrganizationIntegrationTest.java b/src/test/java/io/floci/aws/services/securityhub/SecurityHubOrganizationIntegrationTest.java index c69bd56fbc..c23e17f4ac 100644 --- a/src/test/java/io/floci/aws/services/securityhub/SecurityHubOrganizationIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/securityhub/SecurityHubOrganizationIntegrationTest.java @@ -148,7 +148,7 @@ class SecurityHubOrganizationIntegrationTest { organizationsService.createOrganization(managementAccountId, "ALL"); for (String member : members) { Handshake handshake = organizationsService.inviteAccountToOrganization( - managementAccountId, member, "ACCOUNT", null); + managementAccountId, member, "ACCOUNT", null, null); organizationsService.acceptHandshake(member, handshake.getId()); } }