You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
feat(agent): hold and evaluate 2.x policies in the package broker - #2229
Temporary git dependencies, each of which must become a crates.io version before this merges:
now-policy, now-policy-api and now-policy-server-template: [patch.crates-io] in the root Cargo.toml, on Devolutions/now-libraries at 5b23b1a616ca97590d4d6b9e610cd07772d30453, for the 2.0 package broker API types.
now-policy-eval: a dependency of crates/now-package-broker, same repository at 25e75110c067239e44e42f005abbf566f32f803a, for the 2.x policy reader and evaluator.
Builds on #2224, #2225 and #2226: the base branch is master with those three merged.
The package broker holds a policy file in format 2.x and decides requests against it with the now-policy-eval evaluator.
Nothing serves the 2.0 request encoding yet: this change gives the policy store what the /v2/ endpoints need, and the endpoints themselves follow in the pull requests stacked on this one.
The store observes the policy file once and publishes two views of that observation.
The 1.x endpoints keep the snapshot they read today.
The 2.0 view is the policy management state of CONTRACT.wire.md: Active with the accepted document and its text, Missing, or Invalid with the policy errors of the 2.x reader or the reason the storage failed.
A configured path whose form the broker does not read is UnsupportedPath for the 2.0 view, and the 1.x endpoints report it as before.
A replacement of the policy goes through one store operation that answers the outcomes in the order of the contract's error list: a stale store token, a policy that cannot be written, an invalid document, a Revision that is not higher than the accepted one for the same Id, a write that failed, a written file the broker does not hold, and the replacement.
The accepted document is written exactly as sent, and every outcome that reaches the store's audit is recorded once.
A request the policy allows is still refused, as not runnable, when it would run a Pre or Post command with administrator rights, including when audit mode is what allowed it.
The 1.x policy endpoints never write over a file in format 2.x.
The file is told from the errors of the 2.x reader, and from its top-level PolicyFormatVersion members when the reader stops on a repeated key before it reads the format version.
A file the 2.x reader finds malformed or too large stays writable for them, so a broken 1.x file can still be repaired.
No conversion from a 1.x policy is defined, so the store refuses each one: with a 1.x file in place the 2.0 view is Invalid with Unconvertible, and the 1.x endpoints keep enforcing the file.
The refusal is one function that returns either a 2.x document with the differences it reports, or a refusal, so a conversion replaces it without touching the store.
policy_store::windows tests of the observation of such a path
Convert a 1.x policy
Stopped: no rule is defined, so every 1.x policy is refused and reported as Unconvertible
document_tests: format_1_file_is_unconvertible_while_the_earlier_endpoints_still_read_it, converted_format_1_policy_is_active_with_the_text_of_its_file (the slot, with a test conversion), conversion_that_is_no_valid_document_is_unconvertible
The package broker reads 2.x policies and evaluates requests with `now-policy-eval`.
The crate is not published yet, so the dependency is pinned to a git revision until it is.
Issue: DGW-499
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The 2.0 request and response types live in `now_policy_api::v2`, which no published version carries yet.
The now-policy crates are patched to one git revision until they are published, so the Agent and the package
broker keep sharing one copy of the 1.x types.
Issue: DGW-499
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
An accepted policy keeps the text it was read from next to the parsed document, because the management
state reports that text and a replacement compares identities without reading the file again.
`Metadata.Id` and `Metadata.Revision` are read from the valid document, since the evaluator does not expose them.
The evaluation time goes through the timestamp the broker writes in its responses, because the evaluator
reads its time from text only.
Issue: DGW-499
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The policy store now reads the policy file once for both request
encodings.
Beside the 1.x snapshot it holds what the 2.0 endpoints report: the
policy management state of the wire contract, and the accepted policy
when the file is a valid 2.x document.
A replacement of the 2.0 encoding writes the document text unchanged as
the whole file through the transactions the 1.x writes use, decides its
outcome in the order the wire contract gives, and succeeds only when the
file read back holds exactly the document that was sent.
A file in format 2.x is read-only for the 1.x management endpoints,
because their reader reports it as invalid and a repair would overwrite
it.
A file the 1.x reader accepts stays exactly as it was for those
endpoints, and is `Unconvertible` for the 2.0 endpoints: no conversion
is defined, so the one function a conversion would live in refuses
every policy.
Issue: DGW-499
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
A second, lenient JSON parser decided whether the policy file is in
format 2.x, and it disagreed with the reader of that format.
A 2.x document holding a number that parser refuses was not told as
2.x, so a 1.x repair could overwrite it, and a document repeating
`PolicyFormatVersion` was told as 2.x although the reader calls it
malformed.
The store now tells the format from the errors the reader reports, in
the code that exists only for the 1.x endpoints.
A replacement of the 2.0 encoding fails before it records an attempt or
touches the file when no policy management state can carry the policy
path, so nothing can fail once the outcome is decided.
Its success audit record names the 1.x policy it replaced when the file
held one, because the 1.x endpoints enforced that policy.
The storage observes the written file with the source the store reads
the path from.
A 1.x policy refused for the 2.0 endpoints is logged once for each
change of the file, so the refusal is reported beyond the policy
management state.
Tests now pin how each storage failure and read-only reason maps to the
`InvalidReason` and `ReadOnlyReason` of the wire contract, and that
recovery accepts a published replacement that is a 2.x document.
The content check of a recovered replacement is its own function, so a
test reaches it without a file secured for the broker, which only an
elevated process can create.
Issue: DGW-499
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…ersion
The 2.x reader reports a repeated key before it reads the format
version, and reports nothing else.
A file whose only `PolicyFormatVersion` is `"2.0.0"` and that repeats a
key elsewhere is a 2.x file the errors of the reader cannot tell from a
1.x file that repeats a key, so a 1.x repair could overwrite a policy
that belongs to the 2.0 endpoints.
The rule that tells a 2.x file changes for that one case.
When the reader reports a repeated key other than the format version,
the top-level `PolicyFormatVersion` members of the document decide: the
file is in format 2.x when there is at least one and each of them is the
string `"2.0.0"`.
They are read without interpreting any other value, so a number of any
magnitude does not prevent it.
A 1.x file that repeats a key stays repairable, and so does a file that
gives its format version twice, whatever the two values.
Every other case is still decided from the errors of the reader.
A malformed or too large 2.x file stays writable for the 1.x endpoints,
because nothing reads a format version from it.
Issue: DGW-499
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…d path
The policy management state of the 2.0 endpoints has one reason for a
path whose form or extension the broker does not read or write, and
keeps `InsecureStorage` and `UnsafePath` for a path or location that
fails the storage security checks.
A relative path, a path ending with a separator, a path with a `.` or
`..` component and a path that names no file were reported with the
security reasons, as the 1.x management state reports them.
The 2.0 state now gives `UnsupportedPath` for both the invalid reason
and the read-only reason of such a path, like a path with an extension
the broker does not read.
The 1.x management state is unchanged.
Issue: DGW-499
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The `Path` of the 2.0 policy management state is the configured path,
not the canonical path of the observation, which the 1.x management
state shows.
The success audit record of a replacement can name the 1.x policy the
replacement took the place of.
A 1.x policy refused for the 2.0 endpoints is logged each time the
store builds a view, which a change of the security or the write
capability of unchanged bytes also causes.
The observation a document write returns is read with the configuration
source of the store, a property no test checks.
The test of the content check of a recovered replacement gains a 1.x
policy that parses and fails validation, and compares the refusal, so
each case reaches the branch it is meant for.
The observation doubles of the storage tests move next to the test
module, out of the production items.
Issue: DGW-499
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
… version
Whether a file that repeats a key is in format 2.x depended on which key
the 2.x reader reports first.
A file giving `"2.0.0"` twice as its `PolicyFormatVersion` was not told
as a 2.x file, while the same file with a repeated key of another object
between the two was.
The rule is now the same for every repeated key.
When the reader reports a repeated key, whatever the key, the top-level
`PolicyFormatVersion` members of the document decide: the file is in
format 2.x when there is at least one and each of them is the string
`"2.0.0"`.
A file that gives that format version twice is in format 2.x, so the 1.x
management endpoints do not write it.
A file that gives two different format versions is not, and a 1.x file
that repeats a key stays repairable.
Every other case is still decided from the errors of the reader.
Issue: DGW-499
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
… decided
The wire contract decides each of these cases, and a broker that got
them wrong passed every test:
- A stale store token sent while the file is not watched is
`StaleStoreToken`, not `PolicyNotWritable`.
- A file that became read-only after its state was read is
`PolicyNotWritable`, decided from the file as it is now.
- A file someone else changed before a write that then fails for
another reason is `StaleStoreToken`, and the change is reported.
- A file that holds another valid document after the write is
`ActivationFailed`, not a successful replacement.
- A file the broker starts watching is no longer read-only for
`MonitoringUnavailable`.
The table of documents with one defect compared the code and pointer of
four of the ten errors the policy contract has a reader report.
It now has a document too large, a repeated key, a missing key,
conflicting keys, a value of the wrong type and a repeated rule id.
The table of files the 1.x endpoints do not write gains a 2.x document
that misses a required key other than its format version, which a
reader taking any missing key for a missing format version would hand
to the 1.x endpoints.
The storage double can refuse writes after it was built, and can fail a
write over a file someone else changed without telling of the change.
Issue: DGW-499
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The 2.0 view of a path of unsupported form is set right beside the code
that builds the 1.x view of it, and no test of the storage decided what
the 1.x endpoints report for such a path.
A test now pins the state, the write capability and the read-only
reason they get for each rule that refuses a form.
The path meant to name no file ended with a separator, so the rule for
a trailing separator refused it and the rule for a missing file name was
never reached.
A share root reaches that rule, which its own test now shows.
Issue: DGW-499
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…nd nesting
The members that decide the format of a file that repeats a key are read
by another parser than the 2.x reader, so the two must read a key, a
value and a nested value alike.
The table now holds a format version key and a format version value
spelled with a JSON escape, and a value nested to the deepest level the
reader accepts, each alone and beside a repeated key, and a value nested
one level deeper.
The two agree on every row, so the rule is unchanged.
The fixtures that rewrite the text of a document now fail when the text
they look for is not there exactly once.
A fixture that returns its input unchanged leaves a row that no longer
tests its case, and the row still passes where a valid document is
expected.
Issue: DGW-499
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The documentation of `observe` gave `UnsupportedFormat` as the read-only
reason of every path of unsupported form or extension.
The 1.x endpoints get `UnsafePath` for a form and `UnsupportedFormat`
for an extension, and the 2.0 endpoints get `UnsupportedPath` for both.
The read-only reason of a file the store stopped watching is marked as
a value no test decides in a flow the broker has: the state of an
unwatched file gets its reason elsewhere, and the test of the mapping
uses a storage that gives the reason for a watched file.
Issue: DGW-499
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Intent drift audit: the broker's 2.x policy side below the HTTP routes
Audited against the policies folder of Devolutions/now-libraries at 77307f5106d9764d03a420d22fd8377c99432af5: CONTRACT.wire.md (38 rows), CONTRACT.policy.md (12 rows), INTENT.md (5 rows), INTENT.wire.md (2 rows), INTENT.legacy.md (5 rows).
Revision audited: e6093c06c, as the diff from 983f00e59.
Code audited: v2/policy.rs and policy_store/** of crates/now-package-broker/src/; every path below is relative to that folder, and every line number is a line at e6093c06c.
All audited code is new in this diff, so every verdict is "introduced" unless a row says otherwise.
A statement ID is the section anchor plus the ordinal of the statement in its section; a table counts as one statement, and a letter names a clause or a table row.
ID
Statement
Verdict
Evidence
Note
INTENT.legacy.md#format-1-1
The broker converts a 1.x policy in memory when it loads it, refuses any policy it cannot convert exactly, and reports the refusal.
No conversion is defined: every 1.x policy is refused, reported as Invalid / Unconvertible and logged once per observed change, and the 1.x endpoints keep enforcing it. See Partials.
CONTRACT.wire.md#management-state-2a
"State is Active when the broker holds an accepted policy read from the policy file, Missing when no file exists at Path, and Invalid otherwise"
partial (judgement)
policy_store/windows.rs:1323-1352, policy_store/windows.rs:1391-1415 before 1524-1558, policy_store/mod.rs:396-421, test policy_store/windows.rs:5036
Missing is reported only when the storage reached the file lookup. Three states are Invalid whether or not a file exists. See Partials.
CONTRACT.wire.md#management-state-8
The InvalidReason table: ReadFailed "cannot be opened or read", InsecureStorage "or its location fails the broker's storage security checks"
An existing file under a directory that fails the security verification is ReadFailed, with the ReadOnlyReasonUnsafePath. Whether ReadFailed "applies" to a file the broker declines to open decides this; if it does, the choice is implementation-defined (#management-state-11). The other rows conform: policy_store/document.rs:48-51, policy_store/document.rs:128, policy_store/document.rs:134, policy_store/windows.rs:1348-1351, tests policy_store/document_tests.rs:492, :417, :435, :623.
CONTRACT.policy.md#evaluation-1
The broker evaluates each request once, before running it, against the one policy it holds, at its current time.
not yet realised
v2/policy.rs:75-84, policy_store/mod.rs:321-323
The store holds one accepted policy and evaluate takes the time from its caller; no production code calls either at this revision.
CONTRACT.policy.md#evaluation-3c
The broker "runs no request while it holds no accepted policy".
Each content of the policy file, and its absence, has its own store token; two states carry the same token only when the broker observed no change between them.
A file that changes after the token check and before the write makes the request StaleStoreToken, and the broker never writes over that change.
conforms
policy_store/mod.rs:756-772, policy_store/mod.rs:138-167, policy_store/windows.rs:2068-2083, policy_store/windows.rs:2277-2385, policy_store/windows.rs:3213-3222, policy_store/windows.rs:3278-3295, test policy_store/document_tests.rs:851
By reading for the real storage: no test writes a real file. UNTESTED: at policy_store/mod.rs:131-137, policy_store/windows.rs:2064-2067 and :3210-3212 state it.
CONTRACT.wire.md#replacement-5
A document with another Id may have any Revision, and so may any document while the broker holds no accepted policy.
conforms
v2/policy.rs:89-91, policy_store/mod.rs:742-743, test policy_store/document_tests.rs:780-831
CONTRACT.wire.md#replacement-6
On success the broker writes the UTF-8 encoding of Document as the whole file, unchanged, and holds it as its accepted policy before writing the response.
conforms
policy_store/mod.rs:748-753, policy_store/windows.rs:3331-3375, policy_store/mod.rs:787-805, test policy_store/document_tests.rs:780-831
The published file is compared byte for byte, and the snapshot is replaced before the outcome returns.
CONTRACT.wire.md#replacement-7
The replacement response's Management is Active, its Document equals the request's, and its StoreToken differs from ExpectedStoreToken.
conforms
policy_store/mod.rs:787-805, policy_store/mod.rs:816, test policy_store/document_tests.rs:822-829
The token is a fresh random UUID.
CONTRACT.wire.md#replacement-8
A package request evaluated after the response is evaluated against the new policy.
The identity is read a second time with another JSON reader (v2/policy.rs:156-164); a valid document has no construct the two read differently, and a disagreement would refuse the document, not accept it.
INTENT.md#ownership-1, #ownership-2
The policy specification owns the meaning of a policy and the broker implements it, through a Rust crate the broker depends on.
The 2.x reader and evaluator hold nothing for 1.x. The store, which both encodings share, holds 1.x-only code outside format_1.rs: policy_store/document.rs:36, :129-135, :198-221, policy_store/mod.rs:819-834, policy_store/windows.rs:1838-1845, :1923-1927.
INTENT.legacy.md preamble 2
Each earlier format is read in its own code path, apart from the current reader and evaluator, so that removing it deletes that code without changing them.
Removing 1.x changes the store, not v2/policy.rs or the evaluator.
INTENT.legacy.md#format-1-2
Refusing is acceptable even though a refused policy leaves the broker refusing every request until the policy is replaced.
conforms
policy_store/document.rs:134, test policy_store/document_tests.rs:801-806
The 2.0 side holds no accepted policy for a refused file, and a replacement over a 1.x file succeeds.
INTENT.legacy.md#format-1-3, #format-1-4
A 1.x policy that uses a pre-release setting is refused; a priority ordering converts only when exact analysis allows it, otherwise the policy is refused.
conforms
policy_store/format_1.rs:30-32
Trivially: every 1.x policy is refused.
Outside this change
Listed once, not audited statement by statement.
CONTRACT.wire.md: #general, #values, #json, #transport, #endpoints, #authentication, the codes, statuses and order of #errors, #health, #capabilities, #package-request, #opt-ins, #equal-requests, #decision, #evaluate, #execute, #bypass, #bypass-reading, #operations, #status, #cancel, #event-channel, #frames, #frame-order, #management (the response), the request tables of #validation and #replacement, #validation-3 (body limit), #replacement-10 (what a client does).
CONTRACT.policy.md: everything the reader and the evaluator decide by themselves, which the pinned now-policy-eval implements and this change only calls: #text-comparison, #patterns, the rest of #json, #timestamps, #document-keys-1, #format-version-1 to -3, #metadata, #validity-window-1 and -2, #trusted-sources to #opt-ins, #errors-2, #errors-4, #errors-5, #reading to #missing-version, #applicability to #decision, #properties to #risk-guidance, and #evaluation-4.
INTENT.md: #vocabulary to #bypass-token, the rest of #ownership, #rules, #trusted-sources, #decisions, the rest of #fail-closed, #broker-limits-3, #remote-operations, #warnings.
INTENT.legacy.md: #format-1-5 to -7 (editors, deprecation, authoring), #wire-1.
Known items, one line each
A 1.x policy file is not converted: partial against INTENT.legacy.md#format-1-1, as the first row has it; CONTRACT.wire.md#management-state itself conforms (Invalid / Unconvertible).
Two mismatches in the published now_policy_api::v2 types left as ignored tests: outside this change; no ignored test exists under v2/policy.rs or policy_store/** at e6093c06c.
An Update under Scoop or Windows PowerShell ends Failed without a step: outside this change (CONTRACT.wire.md#operations, CONTRACT.policy.md#managers).
The bypass endpoint: outside this change (CONTRACT.wire.md#bypass).
Partials
INTENT.legacy.md#format-1-1: no 1.x policy converts.
Required: convert a 1.x policy in memory when it converts exactly, refuse the others, report the refusal.
The code refuses every 1.x policy (policy_store/format_1.rs:30-32) and reports each refusal (policy_store/document.rs:199-205), so the refusing and reporting half is met and the converting half is not.
One consequence is not in the known report: while a 1.x file is in place the 2.0 side holds no accepted policy, so a replacement with the same Id and a lower Revision succeeds (policy_store/mod.rs:742-743, case "file in an earlier format" at policy_store/document_tests.rs:801-806). CONTRACT.wire.md#replacement-5 allows it, and INTENT.wire.md#replacement-1 reads against it if the 1.x policy counts as the active policy.
Smallest fix: define the conversion behind format_1::convert, which the store already reads as a 2.x document; the revision rule then follows, because a converted policy carries its Id and Revision.
CONTRACT.wire.md#management-state-2a: Invalid where no file exists.
Required: Missing when no file exists at Path, Invalid only otherwise.
The code reports Invalid without knowing whether a file exists in three cases:
a path whose form or extension the broker does not read is Invalid / UnsupportedPath before anything is looked up (policy_store/windows.rs:1323-1352, test policy_store/windows.rs:5036);
a failure while checking the directory is Invalid / ReadFailed or InsecureStorage before the file lookup (policy_store/windows.rs:1391-1415, lookup at :1524-1558);
a file that cannot be watched is Invalid / MonitoringUnavailable whatever the storage last saw (policy_store/mod.rs:396-421).
The InvalidReason table describes each of these by the path or the watch, not by a file, which supports the code's reading; the sentence in #management-state-2 does not.
A directory that cannot be verified at all does report Missing for an absent file (policy_store/windows.rs:1938-1958), so the code is not uniform either.
Smallest fix: one sentence in the contract saying which of Missing and an InvalidReason of the path, location or watch wins; or, in the code, a lookup of the file before each of the three returns.
Cannot verify
CONTRACT.wire.md#management-state-8: ReadFailed for a file under an unverified directory. observe_leaf_under_unverifiable_directory reports an existing file as unreadable (policy_store/windows.rs:1959-1965), which becomes ReadFailed (policy_store/document.rs:48), while its own comment gives the cause as a directory that "could not be trusted" and the same state has the ReadOnlyReasonUnsafePath. InsecureStorage is the row that names a location failing the security checks.
It conforms if ReadFailed also applies to a file the broker declines to open, and is a wrong reason otherwise; the contract does not say.
Fix if wanted: give that return the document reason InsecureStorage, as policy_store/windows.rs:1348-1351 already overrides the reason of an unsupported path.
Not yet realised
AcceptedPolicy::evaluate, validate, is_not_runnable, PolicyStore::policy_management and PolicyStore::replace_document have no production caller at e6093c06c; replace_document says so itself (policy_store/mod.rs:677-680), and accepted_policy is read only to audit an external change (policy_store/mod.rs:864).
The five rows above wait for the endpoints, and so does every wire-level statement of #management, #validation and #replacement, whose store side is what this audit covers.
Not covered by intent
Behaviour of the audited code that no governing statement decides.
A policy path that is empty or longer than 32767 UTF-16 code units: policy_management() and replace_document() return a ValueError and no state (policy_store/mod.rs:294, :329-331, :689, test policy_store/document_tests.rs:1119). Path is the configured path through Path::display(), so a path that is not valid Unicode is reported with replacement characters.
A replacement of a file the broker does not watch is decided against the published state without reading the file (policy_store/mod.rs:696-710): a client that sends the published token gets PolicyNotWritable even if the file changed on disk. Nothing is written either way.
While the watcher is starting, a state can be Active with the ReadOnlyReasonMonitoringUnavailable (policy_store/mod.rs:299, policy_store/document.rs:160-164), although the table words that reason as "cannot watch".
A file or directory the broker cannot read has the ReadOnlyReasonUnsafePath (for example policy_store/windows.rs:1391-1397); the closed set has no value for "cannot read".
An evaluation time outside the years 0001 to 9999 gives EvaluationError::TimeOutOfRange and no decision (v2/policy.rs:78-81, test v2/policy.rs:353). How the evaluation time of a leap second converts was not checked.
The 1.x endpoints never write a file the 2.x reader tells is in format 2.x, and may write one that is malformed or too large (policy_store/format_1.rs:37-75, tests policy_store/document_tests.rs:922, :1039). INTENT.legacy.md does not say what the earlier endpoints may do to a 2.x file.
Two mappings exist for storage reasons the storage never gives, each under an UNTESTED: comment that says the contract has no value for it (policy_store/document.rs:55-64, :80-84).
The audit records of a replacement (audit.rs, policy_store/mod.rs:705-814).
Other notes
UNTESTED: comments on audited statements: policy_store/mod.rs:131-137, policy_store/windows.rs:2064-2067, policy_store/windows.rs:3210-3212 (the real-storage write behind #replacement-4 and #replacement-6), policy_store/document.rs:55-58 and :80-83. Every replacement test runs over a storage double (policy_store/document_tests.rs:35-198).
Repository rule, not a contract statement: the TEMPORARY: comments at Cargo.toml:36-37 and crates/now-package-broker/Cargo.toml:31-32 say the git dependencies must go "before this change merges", which names a work stage in committed text, and the dependencies they mark are still git revisions at e6093c06c.
The conformance of now-policy-eval to CONTRACT.policy.md was not audited; the broker's rows above assume the reader and evaluator at the pinned revision do what the contract says.
The four commits after e6093c06c on this branch change tests, documentation comments and one UNTESTED: comment only, so the line numbers above are those of e6093c06c.
… a test decides
The arm that maps `ManagementDisabled` has a test, so its comment no longer marks it as untested and says instead why no client reads its value.
The fixture that adds an unknown key now fails when the text it replaces is not there exactly once, like the other fixtures that rewrite a document.
The rows that quote the policy contract cite the revision the quotes come from.
Issue: DGW-499
Changelog: ignore
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Lines changed against the base branch (approximate split): production +1230 −48, tests +1868, other +17 −6.
Checks run on d3cd6103d
cargo +nightly fmt --all -- --check: pass.
cargo clippy -p now-package-broker --all-targets -- -D warnings: pass. The workspace-wide cargo clippy --workspace --all-targets -- -D warnings passes on the top of the stack, which contains this branch.
cargo test -p now-package-broker: 874 passed, 2 ignored (both ignored tests exist on the base).
ci/check-oracle-markers.ps1: pass on 47fec3e51; the commit after it changes no marker.
Some policy-store tests return early in a process that is not elevated, so a run without elevation says nothing about them.
UNTESTED: properties added
crates/now-package-broker/src/policy_store/document.rs:55: the storage never fails with these reasons before it read the content, so no test decides what this arm reports, and the wire contract has no InvalidReason for it. A state from this arm would tell a client the file is an invalid document without having read it. When the storage starts failing with one of these reasons, give it its own arm and a contract value.
crates/now-package-broker/src/policy_store/document.rs:82: the storage never gives PathNotConfigured, because the broker always reads a path, so no test decides what this arm reports, and the wire contract has no ReadOnlyReason for a broker without a path. A state from this arm would name a reason that is not why the file cannot be written. When the storage starts giving this reason, decide its contract value before relying on this arm.
crates/now-package-broker/src/policy_store/mod.rs:131: a missing file is created without ever replacing one that appeared since, any other file is replaced through the handle its observation retained, and the returned observation is read after the write, with the configuration source of the store, which decides how the directory of the file is verified. No test runs it, because writing needs a directory secured for the broker, and the storage doubles ignore the source. When changing it, check the first three against how create and replace write a 1.x policy, and that the source reaches windows::observe unchanged.
crates/now-package-broker/src/policy_store/windows.rs:2065: this publication goes through the same transaction as atomic_replace, so a file that changed since the observation is never written over and an interruption stays recoverable. No test runs it, because it needs a directory secured for the broker. When changing it, check that it still differs from atomic_replace only in how the published file is verified.
crates/now-package-broker/src/policy_store/windows.rs:3211: this publication never overwrites an existing destination, exactly as atomic_create. No test runs it, because it needs a directory secured for the broker. When changing it, check that it still differs from atomic_create only in how the published file is verified.
Every replacement test runs over a storage double; the real-storage write is covered only by the last three comments above.
Stopped or deferred
Conversion of a 1.x policy. No rule is defined, so policy_store::format_1::convert refuses every policy. It returns either a 2.x document with the differences it reports, or a refusal, and the store reads the returned document exactly as it reads a 2.x file, so a conversion only replaces that function.
Consequence of the above. With a 1.x file in place the 2.0 side holds no accepted policy, so a 2.x replacement with the Id of the 1.x policy is accepted whatever its Revision.
ReadFailed under a directory that fails the security check. The directory verification returns one untyped error for "could not read the security information" and for "the directory is not secure", so the 2.0 state reports ReadFailed for both, where InsecureStorage reads better for the second. Separating them needs a typed error from policy_security.rs, which this change does not touch.
Missing against Invalid. For a path of unsupported form or extension, a directory check that fails, and a file that cannot be watched, the state is Invalid with its reason without looking whether a file exists. CONTRACT.wire.md §management-state says Missing "when no file exists at Path"; the InvalidReason table describes these reasons by the path or the watch. The broker follows the table.
A 2.x file the 2.x reader finds malformed or too large is not told as 2.x, so a 1.x repair write can replace it. Every valid 2.x document and every invalid one whose errors are about values is protected.
Deviations from the conventions
The description starts with the list of temporary git dependencies and carries the scope table, so it holds more than a squash commit body would.
Changelog: ignore: nothing a user can observe changes until the /v2/ endpoints exist. Drop the footer if this should appear in the changelog on its own.
The TEMPORARY: comments on the git dependencies say they must go "before this change merges", which names a stage of the work in committed text; they are there to be removed with the dependencies.
Review and pruning
Review: the last review of the store's code approved it; a separate review of the test and documentation commits after it asked for one comment to change, which d3cd6103d does. Left from it, not blocking: the case tables are for loops, as the crate's older tables are, because the crate has no rstest dev-dependency; and the documentation on ConfiguredPathError in policy_store/windows.rs, which predates this change, says the path form check is never applied to the default path, while observe applies it to every source.
Dependencies: the only additions are the git dependencies listed in the description. now-policy-eval brings no dependency of its own; the lockfile goes from 933 to 934 packages.
Tests: 47 tests in scope, none removed or merged. Mutants came from three sources: 22 written from the contract without sight of the tests, 22 written after reading them, and cargo-mutants on format_1.rs, document.rs and v2/policy.rs (52 mutants: 34 caught, 3 missed, 15 unviable). Three added tests and new rows in four tables kill every survivor but three, which are left: nothing states which failure of a 2.0 replacement is recorded as stale_conflict in the audit; one mutant is equivalent (the message of an error the code discards); one changes only the text of EvaluationError. cargo-mutants was not run on policy_store/mod.rs, audit.rs and policy_store/windows.rs, which only the hand-written mutants cover.
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
A race in policy_management() can return a stale active document and token alongside MonitoringUnavailable, violating the contract’s requirement for a consistent management state.
Extends the Windows Agent package broker’s shared policy store to hold and evaluate 2.x policies while preserving the existing 1.x endpoints.
The 2.0 endpoints themselves will follow separately.
Changes:
Adds 2.x policy parsing, evaluation, identity checks, and elevated-command restrictions.
Adds version-specific store views, ordered replacement checks, exact-text persistence, recovery, and audit outcomes.
Adds policy/store tests and temporary pinned policy-library dependencies.
File
Description
crates/now-package-broker/src/v2/wire/mod.rs
Adds a placeholder for 2.0 wire types.
crates/now-package-broker/src/v2/policy.rs
Implements accepted policies, evaluation, and request restrictions.
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
V2 policy replacements still log attempts and cancellations as v1 requests while outcomes identify v2, producing inconsistent audit trails that cannot be correlated by intent.
V2 audit attempts and cancellations use the v1 intent
crates/now-package-broker/src/audit.rs:687
Every v2 replacement currently starts with a PUT /v1/policy audit attempt because WriteAudit::begin_with_recorder hard-codes the v1 intent; the cancellation fallback does too. These outcome helpers instead use POST /v2/policy, so one request produces inconsistent audit records. Add a v2-aware initializer, retain its intent in WriteAuditState, and use it for attempts, outcomes, and cancellation.
Publish refreshed observations before revision-conflict exits
If an atomicity probe recovers, the fresh observation can become writable without changing its fingerprint or store token. A same-revision replacement then returns RevisionNotHigher with the old ReadOnlyReason, and the cached management state stays stale. Publish the observation before non-writing validation/revision exits and build the revision-conflict response from the refreshed snapshot.
Report directory security failures as InsecureStorage
When directory security verification rejects an existing, readable policy's location, observe_leaf_under_unverifiable_directory labels it Unreadable, so this reports ReadFailed. The wire contract's “Policy management state” assigns failed storage-security checks to InsecureStorage, so clients receive the wrong diagnosis. Preserve the 1.x diagnostics but report Unusable(InsecureStorage) for the 2.0 view when the leaf metadata check succeeds, keeping actual metadata/read failures as ReadFailed.
Add required contract links to policy documentation
These docs and policy_management at line 327 refer to the wire contract without linking it; the same issue occurs in v2/policy.rs:26,99 for errors and timestamps. STYLE.md:312-316 requires reference-style links and verbatim section titles, as demonstrated in server/mod.rs:58-60. Link “Policy replacement” and “Policy management state” in the pinned CONTRACT.wire.md, and “Validation errors” and “Timestamps” in the pinned CONTRACT.policy.md.
Factor duplicated transaction bodies into non-generic cores
conditional_replace now generates a separate copy of the roughly 100-line transaction body for each callback/result type; create_verified has the same pattern. STYLE.md (“Avoid monomorphization”) calls for a thin generic wrapper over a non-generic core. Keep the typed return value in the wrapper and use a dyn callback for the shared transaction body.
…est without a source
The API types at the new revision read a closed-set value from a string only, refuse a timestamp of a leap second, and check the members of a status response, a policy identity and a policy error.
The evaluator at its new revision takes a request whose source is optional, which a bypass request may leave out, and reads such a request the way the broker executes it.
The two pins differ because the two crates are changed on separate branches of the repository; the evaluator has no dependency of its own, so no type exists twice.
Issue: DGW-499
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
V2 policy replacements log attempts and cancellation fallbacks as v1 requests, producing inconsistent audit trails and breaking endpoint-based correlation of attempts with outcomes.
Keep the replacement intent consistent across audit events
crates/now-package-broker/src/audit.rs:687
WriteAudit::begin_with_recorder and its drop fallback still use PUT /v1/policy, while these document outcomes use POST /v2/policy. A document replacement therefore has mismatched attempt/outcome records, and cancellation is attributed to v1. Add a v2-aware constructor, retain its intent in WriteAuditState for all events, and assert the intent in the document audit tests.
Report insecure storage instead of unreadable files
When a custom policy directory fails a security check (for example, because it is user-writable), observe_leaf_under_unverifiable_directory reports an existing file as DiskFailureReason::Unreadable. This exposes ReadFailed even though the broker refused to read an insecure location, which CONTRACT.wire.md §management-state classifies as InsecureStorage. Preserve the security-check failure in the 2.0 observation while keeping the existing 1.x diagnostic if needed.
Test overlapping replacements with forced lock contention
This does not exercise overlapping replacements. tokio::join! polls both futures on the same task, and replace_document has no suspension point after acquiring an uncontended writer lock, so the first replacement finishes before the second is polled. The assertions would therefore still pass without request serialization. Please use separately spawned tasks on a multithreaded runtime with a gated storage double to force contention, then verify that the second request becomes stale without publishing.
…icy-7702d2
Both sides patch `now-policy`, `now-policy-api` and
`now-policy-server-template` to a now-libraries revision. The patches
stay at the revision that carries the 2.0 package broker API types,
which also carries `ErrorCode::BrokerBusy`.
Issue: DGW-499
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Merged master up the stack (merge commits only); head 89e0bd494.
master patches now-policy, now-policy-api and now-policy-server-template to the now-libraries revision that adds ErrorCode::BrokerBusy. The patches stay at 5b23b1a616ca97590d4d6b9e610cd07772d30453, which carries the 2.0 API types and that error code, so the list at the top of the description is unchanged.
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
V2 policy replacements misattribute audit attempts and cancellations to v1, and rejection paths can expose stale management state after a successful filesystem-probe retry.
let document = core::str::from_utf8(bytes).map_err(|_| unreadable())?;
let (id, revision) = read_identity(document).ok_or_else(unreadable)?;
This branch has not been deployed
No deployments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Temporary git dependencies, each of which must become a crates.io version before this merges:
now-policy,now-policy-apiandnow-policy-server-template:[patch.crates-io]in the rootCargo.toml, onDevolutions/now-librariesat5b23b1a616ca97590d4d6b9e610cd07772d30453, for the 2.0 package broker API types.now-policy-eval: a dependency ofcrates/now-package-broker, same repository at25e75110c067239e44e42f005abbf566f32f803a, for the 2.x policy reader and evaluator.Builds on #2224, #2225 and #2226: the base branch is
masterwith those three merged.The package broker holds a policy file in format 2.x and decides requests against it with the
now-policy-evalevaluator.Nothing serves the 2.0 request encoding yet: this change gives the policy store what the
/v2/endpoints need, and the endpoints themselves follow in the pull requests stacked on this one.The store observes the policy file once and publishes two views of that observation.
The 1.x endpoints keep the snapshot they read today.
The 2.0 view is the policy management state of
CONTRACT.wire.md:Activewith the accepted document and its text,Missing, orInvalidwith the policy errors of the 2.x reader or the reason the storage failed.A configured path whose form the broker does not read is
UnsupportedPathfor the 2.0 view, and the 1.x endpoints report it as before.A replacement of the policy goes through one store operation that answers the outcomes in the order of the contract's error list: a stale store token, a policy that cannot be written, an invalid document, a
Revisionthat is not higher than the accepted one for the sameId, a write that failed, a written file the broker does not hold, and the replacement.The accepted document is written exactly as sent, and every outcome that reaches the store's audit is recorded once.
A request the policy allows is still refused, as not runnable, when it would run a
PreorPostcommand with administrator rights, including when audit mode is what allowed it.The 1.x policy endpoints never write over a file in format 2.x.
The file is told from the errors of the 2.x reader, and from its top-level
PolicyFormatVersionmembers when the reader stops on a repeated key before it reads the format version.A file the 2.x reader finds malformed or too large stays writable for them, so a broken 1.x file can still be repaired.
No conversion from a 1.x policy is defined, so the store refuses each one: with a 1.x file in place the 2.0 view is
InvalidwithUnconvertible, and the 1.x endpoints keep enforcing the file.The refusal is one function that returns either a 2.x document with the differences it reports, or a refusal, so a conversion replaces it without touching the store.
policy_store::document_tests:valid_document_file_is_active_with_its_text,invalid_document_file_is_invalid_with_its_errors,missing_file_is_missing,file_changed_by_someone_else_is_the_accepted_policy_after_a_reload;v2::policy:accepted_policy_keeps_its_text_and_identity,leading_byte_order_mark_stays_in_the_document_textnow-policy-evalv2::policy:evaluation_decides_against_the_document,evaluation_uses_the_given_time,request_that_is_not_well_formed_gets_no_decisionIdunless itsRevisionis higherv2::policy::revision_is_refused_only_for_the_same_id_and_no_higher_revision;document_tests::revision_that_is_not_higher_for_the_same_id_is_refused_before_writing,invalid_document_is_refused_with_its_errors_before_its_revision_is_compareddocument_tests:stale_store_token_is_decided_before_writability,read_only_policy_is_not_writable_whatever_the_document,write_that_fails_leaves_the_policy_as_it_was,written_document_the_broker_does_not_hold_is_an_activation_failure,only_one_of_two_replacements_with_the_same_store_token_succeeds,accepted_replacement_holds_the_document_exactly_as_sentPreorPostcommands for a request that runs elevated, audit mode includedv2::policy:allowed_elevated_request_with_commands_is_not_runnable,request_allowed_by_audit_mode_is_not_runnable_either,denied_request_is_not_reported_as_not_runnabledocument_tests:earlier_endpoints_write_no_file_the_reader_tells_is_in_format_2,format_1_write_never_replaces_a_format_2_fileUnsupportedPathfor the 2.0 viewpolicy_store::windowstests of the observation of such a pathUnconvertibledocument_tests:format_1_file_is_unconvertible_while_the_earlier_endpoints_still_read_it,converted_format_1_policy_is_active_with_the_text_of_its_file(the slot, with a test conversion),conversion_that_is_no_valid_document_is_unconvertibleIssue: DGW-499
Changelog: ignore
🤖 Generated with Claude Code