fix(organizations): attach the tags passed to InviteAccountToOrganization (#8048)

This commit is contained in:
soreavis authored and GitHub committed 2026-11-11 22:41:41 -04:00
1 parent a27670c4bb
commit 162401b942
8 files changed
+59 -9

No files matched your search

@@ -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);
}
@@ -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<String, String> 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);
@@ -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;
* <p>{@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<String, String> 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<String, String> getTags() {
return tags;
}
public void setTags(Map<String, String> tags) {
this.tags = tags;
}
}
@@ -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());
}
}
@@ -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
@@ -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(
@@ -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());
}
@@ -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());
}
}