Skip to content

feat(organizations): implement the service control policy lifecycle (#578) - #612

Merged
scttfrdmn merged 2 commits into
mainfrom
feat/organizations-policy-lifecycle-578
Aug 9, 2026
Merged

scttfrdmn merged 2 commits into
mainfrom
feat/organizations-policy-lifecycle-578

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Part of #578. This is the service control policy lane of the v0.97.0
Organizations work; the OU, account and tagging clusters land separately, so
Closes is deliberately not used.

What this covers

Points 2 (SCP CRUD), 3 (attach/detach with the quotas), 5
(ListPoliciesForTarget / ListTargetsForPolicy) and 6
(EnablePolicyType / DisablePolicyType, and the unenforced state between
them) of #578, plus the parts of point 9 that belong to policies —
PolicyInUseException, IMMUTABLE_POLICY, the min/max attachment limits and
the content limit.

Eleven operations: CreatePolicy, UpdatePolicy, DeletePolicy,
DescribePolicy, ListPolicies, AttachPolicy, DetachPolicy,
ListPoliciesForTarget, ListTargetsForPolicy, EnablePolicyType,
DisablePolicyType.

Why the refusals are the point

A governance script's real behaviour is what it does when a call is refused,
and each of these distinguishes a case the caller must handle differently:

Refusal Code Why a caller needs it
Attach while the type is disabled PolicyTypeNotEnabledException #578 point 6. CreatePolicy still succeeds, so a caller treating create-then-attach as atomic would record a guardrail as applied while nothing evaluates it.
Detach the last SCP ConstraintViolationException / MIN_POLICY_TYPE_ATTACHMENT_LIMIT_EXCEEDED An entity with the type enabled and no SCP denies everything, so an emptying detach produces the reverse of the intent, silently.
Eleventh attachment ConstraintViolationException / MAX_POLICY_TYPE_ATTACHMENT_LIMIT_EXCEEDED The SCP quota is 10 per target, not the 5 that belongs to RCPs.
Write to p-FullAWSAccess InvalidInputException / IMMUTABLE_POLICY A teardown deleting every listed policy must not remove the one the min-attachment rule depends on, and must not get a not-found it reads as "already gone".
Delete an attached policy PolicyInUseException Refused rather than cascaded: a cascade would strip a guardrail from every target, and a teardown in the wrong order would look like it worked.
Duplicate name (create, or rename onto one) DuplicatePolicyException A re-run needs to tell "already made this" from "made it now", and the name is its only handle before it knows the ID.
Re-attach DuplicatePolicyAttachmentException Attaching is not idempotent; a silent success would hide the double-apply.
Detach something not attached PolicyNotAttachedException Distinct from the min-attachment floor: the caller's own record is stale, not a quota.
Unknown policy / target PolicyNotFoundException / TargetNotFoundException Both are 400 — the model declares no 404 for any Organizations exception.
Content over 10,240 characters ConstraintViolationException / POLICY_CONTENT_LIMIT_EXCEEDED Split the policy. Measured in runes, since the quota is stated in characters.
Unparseable content MalformedPolicyDocumentException Correct the document. Separate from the size limit because the fixes differ.
Enable an already-enabled type PolicyTypeAlreadyEnabledException An all-features organization starts enabled, so a caller's first enable is already its second.
Disable a type that is off, never available, or unmodeled PolicyTypeNotEnabledException The only refusal DisablePolicyType's error list declares; all three mean "nothing here to turn off".
Wrong root RootNotFoundException Checked before the feature set, so a caller that named the wrong root is not sent to fix the feature set.
Feature set has no SCPs PolicyTypeNotAvailableForOrganizationException Only on CreatePolicy and EnablePolicyType — see below.
Malformed identifiers InvalidInputException / INVALID_SYNTAX_POLICY_ID, INVALID_PATTERN_TARGET_ID, INVALID_ENUM_POLICY_TYPE A typo'd ID is a syntax error, not a not-found: a caller told "not found" would go on to recreate a policy that exists.
Quota on policies per organization ConstraintViolationException / POLICY_NUMBER_LIMIT_EXCEEDED Boundary pinned through the exported comparison rather than by creating 10,000 policies.

Two model-vs-brief discrepancies, resolved toward the model

  1. PolicyTypeNotAvailableForOrganizationException is narrower than the
    lane brief said.
    The brief called for it on any policy operation under
    CONSOLIDATED_BILLING. The API model declares it on CreatePolicy and
    EnablePolicyType only
    . Availability is therefore modeled as
    visibility: while SCPs are unavailable no policy is visible, so each
    other operation answers with the not-found code its own error list carries
    (PolicyNotFoundException, TargetNotFoundException) and the two listings
    answer an empty page, which they declare no code to refuse with. Emitting
    an undeclared exception would hand a caller something its SDK cannot catch
    by type — worse than a truthful "no such policy".

  2. Generated policy IDs use a ten-character suffix, not eight. The model
    gives two patterns for one identity: PolicyId admits p-[0-9a-zA-Z_]{8,128},
    while PolicyArn requires p-[0-9a-z]{10,32} in the org-owned ARN form.
    Eight would emit an ARN invalid against the model's own pattern, so ten
    satisfies both.

Also wired PolicyNotAttachedException on DetachPolicy, which the model
declares and the brief's refusal list omitted.

The disable/enable round trip

DisablePolicyType detaches every SCP from every entity in the root — the
root, every OU and every account — and does not remember them.
EnablePolicyType restores p-FullAWSAccess and nothing else. That is what
the User Guide documents, and helpfully restoring the prior attachments would
hide a destructive round trip from a caller that disabled the type to make a
change. Clearing only the root would leave an account carrying an SCP the root
no longer evaluates — a state no sequence of API calls can reach, so no caller
has handling for it.

Deliberate omissions

  • No ListPolicies / ListPoliciesForTarget filters beyond the required
    Filter
    ; the model has no others.
  • Only SERVICE_CONTROL_POLICY is modeled. Other enum members
    (TAG_POLICY, BACKUP_POLICY, …) are refused on create/enable with
    PolicyTypeNotAvailableForOrganizationException and return an empty page
    from the listings, which declare no refusal for it. A value outside the enum
    is INVALID_ENUM_POLICY_TYPE — a typo and an unsupported feature call for
    different action from the caller.
  • No PENDING_ENABLE / PENDING_DISABLE policy-type status. The model
    declares them, but a transition whose end no caller can observe would make a
    poll loop wait for a state change that never arrives. Clock-driven
    transitions are EC2: every instance state transition is instantaneous, so pending, stopping and shutting-down are never observable #514.
  • SCP content is not evaluated, only checked to be a JSON object. Judging
    statement semantics is modeling the authorization engine, not the API.
  • CHANGELOG.md is untouched — PR 6 of this series owns it.

New unexported helpers, for later hoisting

All live in emulator/organizations_policy.go so the parallel lanes do not
conflict; several are candidates to move into organizations_state.go once
the lanes have merged: policyTypeAvailable, loadVisiblePolicy,
scpEnabledOnStoredRoot, rootSubtree, orgRootWithPolicyTypes,
orgPolicyNotFound, orgTargetNotFound, orgPolicyArn, orgPolicyTypes
and the orgCheck* validators.

Verification

gofmt -l emulator/, go build ./..., go vet ./...,
golangci-lint run ./emulator/ (0 issues), and make test (race detector)
are all clean. Per-function statement coverage of
emulator/organizations_policy.go averages 97%.

…578)

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.
@codecov

codecov Bot commented Aug 9, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.60504% with 44 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/organizations_policy.go 92.60% 23 Missing and 21 partials ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit c9a5f47 into main Aug 9, 2026
13 checks passed
@scttfrdmn
scttfrdmn deleted the feat/organizations-policy-lifecycle-578 branch August 9, 2026 17:07
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.

1 participant