Skip to content

feat(organizations): persist the root and add the shared storage layer (#577) - #606

Merged
scttfrdmn merged 3 commits into
mainfrom
feat/organizations-foundation-577
Aug 9, 2026
Merged

scttfrdmn merged 3 commits into
mainfrom
feat/organizations-foundation-577

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

ListRoots minted a fresh r- ID on every call and never persisted it:

rootID := "r-" + randomLowerHex(4)   // organizations_plugin.go:261, before

Two calls disagreed, the ARN built from it moved with them, and nothing could
reference the root. The failure mode is not an error — a caller attaches a policy
to the root, re-reads, sees a different ID, and concludes the attachment
vanished. That is a determinism defect in the one property substrate exists to
provide.

The root is now created once alongside the organization and the management
account, persisted under root:<account>, and returned unchanged for the life of
the state store.

The storage layer

Every control #578 adds attaches to that root, so this PR also lands the shared
storage the remaining Organizations work is built on — deliberately as one
serial change, so the operation clusters that follow add operations rather than
data structures and can be written independently:

  • the hierarchy in both directions (parent:<child>, children:<parent>), with
    new accounts and the management account placed in the root so a ListParents
    walk has somewhere to start
  • OU, policy, attachment, tag and CreateAccount-status stores with their indices
  • resolveOrgTarget, which names what an ID refers to — the policy and tagging
    clusters both branch on it
  • orgPaginate, honoring the model's MaxResults ceiling of 20
  • the quota constants, each carrying its User Guide value in a doc comment
  • error constructors carrying the Reason the model documents

The per-operation dispatch switch is replaced by a chain of per-cluster claim
functions, four of them currently empty, so each cluster owns one file rather
than contending for one statement.

p-FullAWSAccess

AWS attaches this AWS-managed SCP to the root, every OU and every account when
the SCP type is enabled, and the minimum-one-SCP rule then prevents detaching
it without a replacement. It had zero occurrences in emulator/ but is
load-bearing: without it a fresh organization reports no attached policies, which
is wrong, and the minimum-attachment rule has nothing to hold. It is synthesized
rather than stored, so it cannot be updated or deleted, and its ARN is owned by
aws rather than by the management account.

Seeds

  • POST/DELETE /v1/organizations/feature-set — sets the feature set, making a
    CONSOLIDATED_BILLING organization (one in which no SCP can exist at all)
    reachable. The seed wins over the stored value, so a test can flip an
    already-observed organization rather than having to arrange the seed before the
    first request.
  • POST/DELETE /v1/organizations/create-account-failure — seeds the
    asynchronous CreateAccount outcome, keyed by account name or "*". An
    unknown failureReason is refused with a 400, for the reason the Price List
    seed already gives: a typo'd value would seed a FAILED status carrying
    something no SDK catch branch matches, so the caller's fallback path would go
    untested while the test still passed.

Two fidelity fixes in the same code

  • AccountNotFoundException is now 400, not 404. The API model declares no
    404 for any Organizations error, so the status was one a caller could not
    reproduce against AWS. Updates the assertion in
    organizations_plugin_test.go.
  • An unparseable body is InvalidInputException, not MalformedData, which
    is not an Organizations code — so an SDK caller's catch branch never matched
    it. The InvalidInputExceptionReason is carried in the message, since the
    JSON-RPC error document has nowhere else to put it.

Compatibility

A fresh organization now reports FullAWSAccess attached to the root and to
every account, so a test asserting an empty policy list will see one entry.
AccountNotFoundException is 400 rather than 404.

Verification

gofmt -l . && go build ./... && go vet ./... && golangci-lint run ./...   # 0 issues
make test                                                                 # race detector on

All green. Sources for every code, status and quota: the botocore Organizations
model at 2016-11-28, and Quotas for AWS Organizations.
Two figures #578 states are corrected against that page — an SCP limit is 10
per target and 10,240 characters; 5/5120 are the RCP values.

Closes #577

#577)

ListRoots minted a fresh r- ID per call and never persisted it, so two calls
disagreed and nothing could reference the root — a caller that attached a policy
to it and re-read would conclude the attachment had vanished. The root is now
created once with the organization and persisted under root:<account>.

Alongside it, the storage layer the remaining Organizations operations are built
on: the hierarchy in both directions, OUs, policies, attachments, tags, and
CreateAccount statuses, plus pagination that honors the model's MaxResults
ceiling and a target resolver the policy and tagging clusters share. The
per-operation dispatch switch is replaced by a chain of per-cluster claim
functions so each cluster owns one file.

p-FullAWSAccess is synthesized rather than stored — so it is immutable — and
attached to the root and every account and OU while the SCP type is enabled, as
AWS does. Two control-plane seeds ship with it: the organization feature set,
which makes the no-SCPs-possible state reachable, and the asynchronous
CreateAccount failure, whose reason is validated against the model enum so a
typo cannot seed an outcome no SDK catch branch matches.

Two fidelity fixes fall out: every Organizations exception is HTTP 400 (the
model declares no 404, so DescribeAccount's was unreproducible against AWS),
and an unparseable body is InvalidInputException rather than MalformedData,
which is not an Organizations code.

Closes #577
@codecov

codecov Bot commented Aug 9, 2026 •

Copy link
Copy Markdown

The foundation shipped storage helpers reachable only from the four lanes
that follow, so codecov/patch reported them uncovered on #606. Each is
tested directly rather than merged past: a lane inherits tested storage
instead of assuming it.

Covers saveRoot/loadStoredRoot as the pair DisablePolicyType writes
through, the OU index a listing pages over, detachPolicyFrom clearing
both directions of the attachment index, the tag store's ordering and
per-resource scoping, the create-account-status store including the
unknown-ID answer, and the ConstraintViolationException and empty-body
shapes.
Every read helper distinguishes three answers a caller must not confuse:
the entity exists, it does not, or the store could not be read. The last
is retryable and the middle is terminal, so collapsing one into the other
sends a consumer down the wrong path — and for the root specifically, a
swallowed read failure has the caller mint a new root, which is the
unstable identity #577 was about.

Adds a prefix-scoped fault state manager and a corrupt-record one, and
covers: auto-creation refusing on a failure at any of its writes; both
halves of every save (record and index), since a save reported successful
with only one written is invisible to a listing or names an ID no
Describe resolves; both directions of an attachment write; the seed reads,
where a swallowed failure silently exercises the nominal path the seed
exists to avoid; and the wire result, a 500 InternalFailure the SDK
retries rather than a 400 it treats as terminal.

Also pins the operation-level guards the lanes will inherit: a stale
NextToken is refused rather than restarting a paging loop, an unparseable
body is refused per operation, an empty body is not (the CLI sends none
for ListRoots), a vanished record is skipped rather than listed as an
empty account, and the depth walk terminates on a cycle.
@scttfrdmn
scttfrdmn merged commit 4be0ff7 into main Aug 9, 2026
13 checks passed
scttfrdmn added a commit that referenced this pull request Aug 9, 2026
…578) (#612)

Implements the eleven SCP operations — CreatePolicy, UpdatePolicy,
DeletePolicy, DescribePolicy, ListPolicies, AttachPolicy, DetachPolicy,
ListPoliciesForTarget, ListTargetsForPolicy, EnablePolicyType and
DisablePolicyType — over the storage layer PR #606 landed. Every
request/response shape, error code and quota is verified against the
Organizations API model rather than recall.

The point of the lane is the refusals, because a governance script is built
on them:

  - PolicyTypeNotEnabledException on AttachPolicy is #578 point 6. An
    all-features organization with SCPs disabled still accepts CreatePolicy
    and refuses the attach, so a caller that treats create-then-attach as one
    atomic step finds out here rather than recording a guardrail as applied
    while nothing evaluates it.
  - MIN_POLICY_TYPE_ATTACHMENT_LIMIT_EXCEEDED on the last DetachPolicy. An
    entity with the type enabled and no SCP attached denies everything in real
    AWS, so a detach that emptied the set would produce the reverse of what
    the caller intended, silently.
  - IMMUTABLE_POLICY on writes to p-FullAWSAccess. A teardown that deletes
    every policy it can list must not be able to remove the one the
    minimum-attachment rule depends on, and must not get a not-found it would
    read as "already cleaned up".
  - PolicyInUseException rather than a cascade on DeletePolicy: a cascade
    would strip a guardrail from every entity it was attached to, and a
    teardown running in the wrong order would look like it worked.
  - DuplicatePolicyException, DuplicatePolicyAttachmentException and
    PolicyNotAttachedException, so a re-run can tell "I already made this"
    from "I made it now" and a stale local record from a quota.
  - MAX_POLICY_TYPE_ATTACHMENT_LIMIT_EXCEEDED at ten SCPs per target — the
    SCP quota, not the five that belongs to resource control policies — and
    POLICY_CONTENT_LIMIT_EXCEEDED at 10,240 characters, measured in runes
    because the quota is stated in characters.
  - MalformedPolicyDocumentException separately from the size limit: an
    oversize document has to be split and an unparseable one corrected, and
    one code cannot say which.

DisablePolicyType detaches every SCP from every entity in the root and does
not remember them; re-enabling restores only FullAWSAccess. That is what the
User Guide documents, and modeling it as a toggle would hide a destructive
round trip from a caller that disabled the type to make a change.

Availability is modeled as visibility. Under CONSOLIDATED_BILLING no SCP is
visible, so each operation answers with the not-found code its own error list
declares; only CreatePolicy and EnablePolicyType name the feature set,
because they are the only two operations whose model error list declares
PolicyTypeNotAvailableForOrganizationException. Emitting it elsewhere would
hand a caller an exception its SDK cannot catch by type.

Generated policy IDs use a ten-character suffix: the model's PolicyId pattern
admits eight, but the PolicyArn pattern requires ten to thirty-two in the ARN
it embeds, so a shorter suffix would produce an ARN invalid against the
model's own pattern.

Touches only this cluster's files, so the other v0.97.0 Organizations lanes
do not conflict.
@scttfrdmn
scttfrdmn deleted the feat/organizations-foundation-577 branch August 9, 2026 17:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Organizations: ListRoots mints a new random root ID on every call, so the root has no identity

1 participant