mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(organizations): add CloudFormation resource types (#2495)
* feat(organizations): add CloudFormation resource types Adds OrganizationsCfnProvisioner covering AWS::Organizations::Organization, OrganizationalUnit, Account, Policy and ResourcePolicy. Attribute names are taken from the registry schemas' readOnlyProperties so Fn::GetAtt resolves the keys AWS publishes. CloudFormation's delete path hands over a physical id and a region but no caller identity, and every Organizations mutation is management-account scoped. Rather than injecting a second collaborator into the provisioner, OrganizationsService gains findManagementAccountForResource, which recovers the owning account from the resource id — so the provisioner keeps to the one-injected-service convention the other provisioners follow. provision serves create and update: an OU is renamed in place rather than recreated (ParentId is create-only in the schema), a Policy's TargetIds are reconciled in both directions, and an Account is moved to its ParentIds entry after creation. The integration test asserts through stack Outputs backed by Fn::GetAtt: an unmapped attribute does not fail provisioning, it silently resolves to the literal "LogicalId.Attr", so asserting CREATE_COMPLETE would pass with every attribute unwired. * fix(organizations): converge CFN provisioning on partial failure and tag drops Set the physical id as soon as the create call succeeds in provisionAccount and provisionPolicy, before moveAccount and reconcileTargets respectively. Both can throw once the resource already exists — a ParentIds entry pointing at a missing OU raises DestinationParentNotFoundException, and attachPolicy can fail on a bad target — and without an id CloudFormation's rollback has nothing to call delete() with, leaving the account or policy a real member of the organization but unmanaged by the stack. Make putResourcePolicy replace its tag map rather than merge into it, so a key dropped from a template is removed on update instead of lingering. A resource policy is not a TagResource target, so resolveTagOwner rejects rp- ids and the provisioner cannot reach it through replaceTags the way the other types do; Put is the only call that can converge those tags. Supplying Tags states the full set, while omitting them leaves the existing ones untouched, matching how an absent Tags property behaves for every other Organizations type. * fix(organizations): mark created accounts and policies as rollback-owned CloudFormationService.rollbackCreatedResources deletes a CREATE_FAILED resource only when it carries both a physical id and the CfnRollback.ROLLBACK_OWNED_ATTR marker. The provisioner set the id but not the marker, so rollback walked straight past an account whose MoveAccount failed after CreateAccount had already made it a member of the organization. The account survived, the organization could not be deleted while it still contained one, and the stack reported ROLLBACK_FAILED instead of unwinding. Set the marker as soon as the create call returns, the way IamRoleCfnProvisioner does, so the create and the ownership record cannot drift apart. OrganizationsCfnRollbackIntegrationTest drives a real stack whose ParentIds names an OU that does not exist and asserts the account id reaches the stack events, that rollback emits DELETE_COMPLETE for it, and that neither the account nor the organization is left behind. * feat(organizations): map OU Path and Account State/Paths The published registry schemas declare three readOnly properties the provisioner did not map: OrganizationalUnit Path, and Account State and Paths. An unmapped key does not fail provisioning — CloudFormationTemplateEngine resolves it to the literal string "LogicalId.Attr" — so a template asking for Fn::GetAtt Dev.State silently got back "Dev.State". Path and Paths are the organization path, o-<org>/r-<root>[/ou-<ou>]*[/<id>]/, which needs a walk from the resource up to the root. OrganizationsService owns that hierarchy and already computes the chain for effective-policy merging, so it exposes it as organizationPath rather than the provisioner rebuilding it. The account path is read after the move: accounts are always created under the root, so reading it earlier would publish a path the account no longer lives at. State is the parameter AWS is retiring Status in favour of on 2026-09-09. Floci only ever puts an account in ACTIVE or PENDING_CLOSURE, both of which are also AccountState values, so the two agree; PENDING_ACTIVATION and CLOSED are phases the emulator does not model. Paths is list-typed in the schema, but an account has exactly one parent and so exactly one path, and the engine's Fn::Select splits a comma-delimited scalar back into a list, so the string-valued attribute map carries it without loss. Adding the Path lookup to provisionOrganizationalUnit opens the same window that stranded accounts before: a call that can throw once the OU is already a real member of the organization. Mark the OU stack-owned as soon as the create returns, matching the account and policy paths, so rollback can still delete it. * fix(organizations): Ref on Organization returns the management account id AWS documents Organization as the one Organizations type where bare Ref returns the management account id rather than the resource's own id; Fn::GetAtt Org.Id already returned the organization id correctly. Adds an OrgRef stack output/assertion covering the case, since the existing integration test only ever exercised Fn::GetAtt on Org.
This commit is contained in:
1 parent
45240998ac
commit
054508df87
7 files changed
+1487
No files matched your search
+328
@@ -0,0 +1,328 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation.provisioners;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
|
||||
import io.github.hectorvent.floci.services.organizations.OrganizationsService;
|
||||
import io.github.hectorvent.floci.services.organizations.model.CreateAccountStatus;
|
||||
import io.github.hectorvent.floci.services.organizations.model.Organization;
|
||||
import io.github.hectorvent.floci.services.organizations.model.OrganizationAccount;
|
||||
import io.github.hectorvent.floci.services.organizations.model.OrganizationPolicy;
|
||||
import io.github.hectorvent.floci.services.organizations.model.OrganizationalUnit;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* CloudFormation provisioning for AWS Organizations: {@code AWS::Organizations::Organization},
|
||||
* {@code OrganizationalUnit}, {@code Account}, {@code Policy} and {@code ResourcePolicy}.
|
||||
*
|
||||
* <p>Attribute names come from the registry schemas in
|
||||
* {@code aws-cloudformation-resource-providers-organizations} ({@code readOnlyProperties}), so
|
||||
* {@code Fn::GetAtt} resolves the same keys AWS publishes.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class OrganizationsCfnProvisioner implements CfnResourceProvisioner {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(OrganizationsCfnProvisioner.class);
|
||||
|
||||
private static final String ORGANIZATION = "AWS::Organizations::Organization";
|
||||
private static final String ORGANIZATIONAL_UNIT = "AWS::Organizations::OrganizationalUnit";
|
||||
private static final String ACCOUNT = "AWS::Organizations::Account";
|
||||
private static final String POLICY = "AWS::Organizations::Policy";
|
||||
private static final String RESOURCE_POLICY = "AWS::Organizations::ResourcePolicy";
|
||||
|
||||
private final OrganizationsService organizationsService;
|
||||
|
||||
@Inject
|
||||
public OrganizationsCfnProvisioner(OrganizationsService organizationsService) {
|
||||
this.organizationsService = organizationsService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> resourceTypes() {
|
||||
return Set.of(ORGANIZATION, ORGANIZATIONAL_UNIT, ACCOUNT, POLICY, RESOURCE_POLICY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void provision(StackResource r, JsonNode props, ProvisionContext ctx) {
|
||||
switch (r.getResourceType()) {
|
||||
case ORGANIZATION -> provisionOrganization(r, props, ctx);
|
||||
case ORGANIZATIONAL_UNIT -> provisionOrganizationalUnit(r, props, ctx);
|
||||
case ACCOUNT -> provisionAccount(r, props, ctx);
|
||||
case POLICY -> provisionPolicy(r, props, ctx);
|
||||
case RESOURCE_POLICY -> provisionResourcePolicy(r, props, ctx);
|
||||
default -> throw new IllegalStateException(
|
||||
"OrganizationsCfnProvisioner cannot handle " + r.getResourceType());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String resourceType, String physicalId, String region) {
|
||||
// The delete path carries no caller identity, so recover the owning management account
|
||||
// from the resource id. Nothing matching means the resource is already gone.
|
||||
String caller = organizationsService.findManagementAccountForResource(physicalId).orElse(null);
|
||||
if (caller == null) {
|
||||
LOG.debugv("No organization owns {0}; treating {1} as already deleted",
|
||||
physicalId, resourceType);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
switch (resourceType) {
|
||||
case ORGANIZATION -> organizationsService.deleteOrganization(caller);
|
||||
case ORGANIZATIONAL_UNIT -> organizationsService.deleteOrganizationalUnit(caller, physicalId);
|
||||
case ACCOUNT -> organizationsService.removeAccountFromOrganization(caller, physicalId);
|
||||
case POLICY -> deletePolicy(caller, physicalId);
|
||||
case RESOURCE_POLICY -> organizationsService.deleteResourcePolicy(caller);
|
||||
default -> LOG.debugv("No Organizations delete for {0}", resourceType);
|
||||
}
|
||||
} catch (AwsException e) {
|
||||
// Deleting something already gone is tolerated; anything else is a real failure.
|
||||
if (!isNotFound(e)) {
|
||||
throw e;
|
||||
}
|
||||
LOG.debugv(e, "{0} {1} was already deleted", resourceType, physicalId);
|
||||
}
|
||||
}
|
||||
|
||||
// ──────────────────────────── Organization ────────────────────────────
|
||||
|
||||
private void provisionOrganization(StackResource r, JsonNode props, ProvisionContext ctx) {
|
||||
String featureSet = ctx.resolveOptional(props, "FeatureSet");
|
||||
Organization organization;
|
||||
if (r.getPhysicalId() != null) {
|
||||
// UpdateStack re-invokes provision with the previous physical id. FeatureSet is the
|
||||
// only settable property, and the only legal transition is the one-way upgrade to ALL.
|
||||
organization = organizationsService.describeOrganization(ctx.accountId());
|
||||
if ("ALL".equals(featureSet) && !"ALL".equals(organization.getFeatureSet())) {
|
||||
organizationsService.enableAllFeatures(ctx.accountId());
|
||||
organization = organizationsService.describeOrganization(ctx.accountId());
|
||||
}
|
||||
} else {
|
||||
organization = organizationsService.createOrganization(ctx.accountId(), featureSet);
|
||||
}
|
||||
|
||||
// AWS documents Organization as a special case: Ref returns the management account id,
|
||||
// not the organization id. Fn::GetAtt Org.Id below still returns the organization id.
|
||||
r.setPhysicalId(organization.getMasterAccountId());
|
||||
r.getAttributes().put("Id", organization.getId());
|
||||
r.getAttributes().put("Arn", organization.getArn());
|
||||
r.getAttributes().put("ManagementAccountArn", organization.getMasterAccountArn());
|
||||
r.getAttributes().put("ManagementAccountId", organization.getMasterAccountId());
|
||||
r.getAttributes().put("ManagementAccountEmail", organization.getMasterAccountEmail());
|
||||
r.getAttributes().put("RootId", organization.getRoot().getId());
|
||||
}
|
||||
|
||||
// ──────────────────────────── Organizational unit ────────────────────────────
|
||||
|
||||
private void provisionOrganizationalUnit(StackResource r, JsonNode props, ProvisionContext ctx) {
|
||||
String name = ctx.resolveOptional(props, "Name");
|
||||
String parentId = ctx.resolveOptional(props, "ParentId");
|
||||
Map<String, String> tags = resolveTags(props, ctx);
|
||||
|
||||
OrganizationalUnit unit;
|
||||
if (r.getPhysicalId() != null) {
|
||||
// ParentId is createOnly in the registry schema, so an update only renames.
|
||||
unit = organizationsService.updateOrganizationalUnit(ctx.accountId(), r.getPhysicalId(), name);
|
||||
replaceTags(ctx.accountId(), unit.getId(), tags);
|
||||
} else {
|
||||
unit = organizationsService.createOrganizationalUnit(ctx.accountId(), parentId, name, tags);
|
||||
// The OU is real once createOrganizationalUnit returns, so mark it stack-owned before
|
||||
// the Path lookup below gets a chance to throw. CloudFormationService only deletes a
|
||||
// CREATE_FAILED resource that is both marked owned and carries a physical id.
|
||||
r.getAttributes().put(CfnRollback.ROLLBACK_OWNED_ATTR, "true");
|
||||
}
|
||||
|
||||
r.setPhysicalId(unit.getId());
|
||||
r.getAttributes().put("Id", unit.getId());
|
||||
r.getAttributes().put("Arn", unit.getArn());
|
||||
r.getAttributes().put("Path", organizationsService.organizationPath(ctx.accountId(), unit.getId()));
|
||||
}
|
||||
|
||||
// ──────────────────────────── Account ────────────────────────────
|
||||
|
||||
private void provisionAccount(StackResource r, JsonNode props, ProvisionContext ctx) {
|
||||
String accountName = ctx.resolveOptional(props, "AccountName");
|
||||
String email = ctx.resolveOptional(props, "Email");
|
||||
Map<String, String> tags = resolveTags(props, ctx);
|
||||
// ParentIds is a list in the schema but AWS accepts at most one entry.
|
||||
List<String> parentIds = ctx.resolveStringList(props, "ParentIds");
|
||||
String desiredParent = parentIds.isEmpty() ? null : parentIds.get(0);
|
||||
|
||||
OrganizationAccount account;
|
||||
if (r.getPhysicalId() != null) {
|
||||
account = organizationsService.describeAccount(ctx.accountId(), r.getPhysicalId());
|
||||
replaceTags(ctx.accountId(), account.getId(), tags);
|
||||
} else {
|
||||
CreateAccountStatus status =
|
||||
organizationsService.createAccount(ctx.accountId(), email, accountName, tags, false);
|
||||
if (!"SUCCEEDED".equals(status.getState())) {
|
||||
throw new AwsException("ConstraintViolationException",
|
||||
"CreateAccount for " + accountName + " failed: " + status.getFailureReason(), 400);
|
||||
}
|
||||
// CreateAccount has already made this a real member of the organization, so claim it
|
||||
// for the stack before anything else can throw. CloudFormationService only deletes a
|
||||
// CREATE_FAILED resource that is both marked owned and carries a physical id, and the
|
||||
// move below raises DestinationParentNotFoundException whenever ParentIds names an OU
|
||||
// that isn't there — a broken Ref or a stale literal. Recording either marker later
|
||||
// would leave the account behind, unmanaged, after the stack reported its rollback.
|
||||
r.getAttributes().put(CfnRollback.ROLLBACK_OWNED_ATTR, "true");
|
||||
r.setPhysicalId(status.getAccountId());
|
||||
account = organizationsService.describeAccount(ctx.accountId(), status.getAccountId());
|
||||
}
|
||||
|
||||
if (desiredParent != null && !desiredParent.equals(account.getParentId())) {
|
||||
organizationsService.moveAccount(
|
||||
ctx.accountId(), account.getId(), account.getParentId(), desiredParent);
|
||||
account = organizationsService.describeAccount(ctx.accountId(), account.getId());
|
||||
}
|
||||
|
||||
r.getAttributes().put("AccountId", account.getId());
|
||||
r.getAttributes().put("Arn", account.getArn());
|
||||
r.getAttributes().put("Status", account.getStatus());
|
||||
// State supersedes Status in the Organizations API, which retires Status on 2026-09-09.
|
||||
// Floci only ever puts an account in ACTIVE or PENDING_CLOSURE, and both are AccountState
|
||||
// values too, so the two agree; the phases State adds — PENDING_ACTIVATION and CLOSED —
|
||||
// are ones the emulator does not model.
|
||||
r.getAttributes().put("State", account.getStatus());
|
||||
r.getAttributes().put("JoinedMethod", account.getJoinedMethod());
|
||||
if (account.getJoinedTimestamp() != null) {
|
||||
r.getAttributes().put("JoinedTimestamp", account.getJoinedTimestamp().toString());
|
||||
}
|
||||
// Paths is a list in the schema, but an account has exactly one parent and so exactly one
|
||||
// path. Attributes are string-valued, and the engine's Fn::Select splits a comma-delimited
|
||||
// scalar back into a list, so a one-element list round-trips as itself. Read this after the
|
||||
// move above: the path has to name the parent the account actually ended up in.
|
||||
r.getAttributes().put("Paths",
|
||||
organizationsService.organizationPath(ctx.accountId(), account.getId()));
|
||||
}
|
||||
|
||||
// ──────────────────────────── Policy ────────────────────────────
|
||||
|
||||
private void provisionPolicy(StackResource r, JsonNode props, ProvisionContext ctx) {
|
||||
String name = ctx.resolveOptional(props, "Name");
|
||||
String type = ctx.resolveOptional(props, "Type");
|
||||
String description = ctx.resolveOptional(props, "Description");
|
||||
String content = resolveContent(props, ctx);
|
||||
Map<String, String> tags = resolveTags(props, ctx);
|
||||
Set<String> desiredTargets = new LinkedHashSet<>(ctx.resolveStringList(props, "TargetIds"));
|
||||
|
||||
OrganizationPolicy policy;
|
||||
if (r.getPhysicalId() != null) {
|
||||
policy = organizationsService.updatePolicy(
|
||||
ctx.accountId(), r.getPhysicalId(), name, description, content);
|
||||
replaceTags(ctx.accountId(), policy.getId(), tags);
|
||||
} else {
|
||||
policy = organizationsService.createPolicy(ctx.accountId(), content, description, name, type, tags);
|
||||
// As above: the policy is real once createPolicy returns, so mark it stack-owned before
|
||||
// reconcileTargets gets a chance to throw.
|
||||
r.getAttributes().put(CfnRollback.ROLLBACK_OWNED_ATTR, "true");
|
||||
}
|
||||
// Same ordering as provisionAccount: the policy already exists once createPolicy returns,
|
||||
// so claim the id before reconcileTargets, whose attach/detach calls can throw.
|
||||
r.setPhysicalId(policy.getId());
|
||||
|
||||
reconcileTargets(ctx.accountId(), policy, desiredTargets);
|
||||
|
||||
r.getAttributes().put("Id", policy.getId());
|
||||
r.getAttributes().put("Arn", policy.getArn());
|
||||
r.getAttributes().put("AwsManaged", String.valueOf(policy.isAwsManaged()));
|
||||
}
|
||||
|
||||
/** Attaches targets the template added and detaches the ones it dropped. */
|
||||
private void reconcileTargets(String caller, OrganizationPolicy policy, Set<String> desired) {
|
||||
Set<String> current = new LinkedHashSet<>(policy.getTargets());
|
||||
for (String target : desired) {
|
||||
if (!current.contains(target)) {
|
||||
organizationsService.attachPolicy(caller, policy.getId(), target);
|
||||
}
|
||||
}
|
||||
for (String target : current) {
|
||||
if (!desired.contains(target)) {
|
||||
organizationsService.detachPolicy(caller, policy.getId(), target);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void deletePolicy(String caller, String policyId) {
|
||||
// DeletePolicy refuses while the policy is still attached, so drop the attachments first.
|
||||
organizationsService.listTargetsForPolicy(caller, policyId)
|
||||
.forEach(target -> organizationsService.detachPolicy(caller, policyId, target.targetId()));
|
||||
organizationsService.deletePolicy(caller, policyId);
|
||||
}
|
||||
|
||||
// ──────────────────────────── Resource policy ────────────────────────────
|
||||
|
||||
private void provisionResourcePolicy(StackResource r, JsonNode props, ProvisionContext ctx) {
|
||||
// PutResourcePolicy is already create-or-update, so the same call serves both paths.
|
||||
OrganizationsService.ResourcePolicyView policy = organizationsService.putResourcePolicy(
|
||||
ctx.accountId(), resolveContent(props, ctx), resolveTags(props, ctx));
|
||||
|
||||
r.setPhysicalId(policy.id());
|
||||
r.getAttributes().put("Id", policy.id());
|
||||
r.getAttributes().put("Arn", policy.arn());
|
||||
}
|
||||
|
||||
// ──────────────────────────── Helpers ────────────────────────────
|
||||
|
||||
/**
|
||||
* {@code Content} is a policy document that templates write either as an inline JSON object or
|
||||
* as a string built with {@code Fn::Sub}/{@code Fn::Join}. Both have to reach the service as
|
||||
* the serialized string it stores.
|
||||
*/
|
||||
private String resolveContent(JsonNode props, ProvisionContext ctx) {
|
||||
if (props == null || !props.has("Content") || props.get("Content").isNull()) {
|
||||
return null;
|
||||
}
|
||||
return ctx.engine().resolveJsonAttribute(props.get("Content"));
|
||||
}
|
||||
|
||||
/** CloudFormation tags are a list of {@code {Key, Value}} objects. */
|
||||
private Map<String, String> resolveTags(JsonNode props, ProvisionContext ctx) {
|
||||
if (props == null || !props.has("Tags") || !props.get("Tags").isArray()) {
|
||||
return null;
|
||||
}
|
||||
Map<String, String> tags = new LinkedHashMap<>();
|
||||
for (JsonNode tag : ctx.engine().resolveNode(props.get("Tags"))) {
|
||||
String key = tag.path("Key").asText(null);
|
||||
if (key != null && !key.isBlank()) {
|
||||
tags.put(key, tag.path("Value").asText(""));
|
||||
}
|
||||
}
|
||||
return tags;
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes the stored tags match the template on update: tag keys the template dropped are
|
||||
* removed rather than left behind from the previous revision.
|
||||
*/
|
||||
private void replaceTags(String caller, String resourceId, Map<String, String> tags) {
|
||||
if (tags == null) {
|
||||
return;
|
||||
}
|
||||
List<String> stale = organizationsService.listTagsForResource(caller, resourceId).keySet().stream()
|
||||
.filter(key -> !tags.containsKey(key))
|
||||
.toList();
|
||||
if (!stale.isEmpty()) {
|
||||
organizationsService.untagResource(caller, resourceId, stale);
|
||||
}
|
||||
if (!tags.isEmpty()) {
|
||||
organizationsService.tagResource(caller, resourceId, tags);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isNotFound(AwsException e) {
|
||||
return switch (e.getErrorCode()) {
|
||||
case "AWSOrganizationsNotInUseException", "AccountNotFoundException",
|
||||
"OrganizationalUnitNotFoundException", "PolicyNotFoundException",
|
||||
"ResourcePolicyNotFoundException" -> true;
|
||||
default -> false;
|
||||
};
|
||||
}
|
||||
}
|
||||
+61
@@ -397,6 +397,17 @@ public class OrganizationsService {
|
||||
return List.of(new ParentRef(parentId, parentType(organization, parentId)));
|
||||
}
|
||||
|
||||
/**
|
||||
* The organization path of a root, OU or account:
|
||||
* {@code o-<org>/r-<root>[/ou-<ou>]*[/<accountId>]/}, trailing slash included. This is the
|
||||
* value the Organizations API reports as {@code OrganizationalUnit.Path} and as the single
|
||||
* entry of {@code Account.Paths}, and what {@code Fn::GetAtt} returns for the same keys.
|
||||
*/
|
||||
public String organizationPath(String callerAccountId, String resourceId) {
|
||||
Organization organization = requireOrganizationForCaller(callerAccountId);
|
||||
return organization.getId() + "/" + String.join("/", ancestryOf(organization, resourceId)) + "/";
|
||||
}
|
||||
|
||||
public List<ChildRef> listChildren(String callerAccountId, String parentId, String childType) {
|
||||
Organization organization = requireOrganizationForCaller(callerAccountId);
|
||||
requireParent(organization, parentId);
|
||||
@@ -948,6 +959,12 @@ public class OrganizationsService {
|
||||
}
|
||||
organization.setResourcePolicyContent(content);
|
||||
if (tags != null) {
|
||||
// Put replaces rather than merges: a caller that supplies Tags is stating the full set,
|
||||
// so a key it no longer lists is dropped. The resource policy is not addressable by
|
||||
// TagResource/UntagResource the way accounts, OUs, roots and policies are, so this call
|
||||
// is the only way CloudFormation can converge Tags on an update. Omitting Tags
|
||||
// entirely (null) leaves the existing ones untouched.
|
||||
organization.getResourcePolicyTags().clear();
|
||||
organization.getResourcePolicyTags().putAll(tags);
|
||||
}
|
||||
organizations.putForAccount(organization.getMasterAccountId(), organization.getId(), organization);
|
||||
@@ -1161,6 +1178,50 @@ public class OrganizationsService {
|
||||
return organization;
|
||||
}
|
||||
|
||||
/**
|
||||
* The management account that owns the given organization, root, OU, account, policy or
|
||||
* resource-policy id, or empty when nothing matches.
|
||||
*
|
||||
* <p>CloudFormation's delete path carries no caller identity — {@code CfnResourceProvisioner}
|
||||
* hands over a physical id and a region only — so the owner has to be recovered from the
|
||||
* resource itself before a management-account-scoped operation can run against it.
|
||||
*/
|
||||
public Optional<String> findManagementAccountForResource(String resourceId) {
|
||||
if (resourceId == null || resourceId.isEmpty()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
for (Organization organization : organizations.scanAllAccounts()) {
|
||||
boolean owns = resourceId.equals(organization.getId())
|
||||
|| resourceId.equals(organization.getRoot().getId())
|
||||
|| resourceId.equals(organization.getResourcePolicyId());
|
||||
if (owns) {
|
||||
return Optional.of(organization.getMasterAccountId());
|
||||
}
|
||||
}
|
||||
if (ACCOUNT_ID_PATTERN.matcher(resourceId).matches()) {
|
||||
return accounts.scanAllAccounts().stream()
|
||||
.filter(account -> resourceId.equals(account.getId()))
|
||||
.findFirst()
|
||||
.flatMap(account -> organizationById(account.getOrganizationId()))
|
||||
.map(Organization::getMasterAccountId);
|
||||
}
|
||||
if (OU_ID_PATTERN.matcher(resourceId).matches()) {
|
||||
return organizationalUnits.scanAllAccounts().stream()
|
||||
.filter(unit -> resourceId.equals(unit.getId()))
|
||||
.findFirst()
|
||||
.flatMap(unit -> organizationById(unit.getOrganizationId()))
|
||||
.map(Organization::getMasterAccountId);
|
||||
}
|
||||
if (POLICY_ID_PATTERN.matcher(resourceId).matches()) {
|
||||
return policies.scanAllAccounts().stream()
|
||||
.filter(policy -> resourceId.equals(policy.getId()))
|
||||
.findFirst()
|
||||
.flatMap(policy -> organizationById(policy.getOrganizationId()))
|
||||
.map(Organization::getMasterAccountId);
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
private Optional<Organization> findOrganizationForAccount(String accountId) {
|
||||
for (Organization organization : organizations.scanAllAccounts()) {
|
||||
if (accountId.equals(organization.getMasterAccountId())) {
|
||||
|
||||
Reference in new issue
Block a user