Repository navigation
Conversation
…r there is a change and leave unaffected connections untouched
|
Thank you for your interest in contributing to OpenShell, @mr-miles. This project uses a vouch system for first-time contributors. Before submitting a pull request, you need to be vouched by a maintainer. To get vouched:
See CONTRIBUTING.md for details. |
|
All contributors have signed the DCO ✍️ ✅ |
Author
|
I have read the DCO document and I hereby sign the DCO. |
Author
|
/recheck |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
fix(network): keep connections open across policy reloads that do not affect them
Problem
Every network policy reload, provider attach/detach, and middleware registry change advances one sandbox-wide policy generation. Each relay pins the generation that authorized it and closes as soon as that generation moves (
PolicyGenerationGuard::is_stale/wait_until_stale). So approving an endpoint for one host drops every open proxied connection in the sandbox: raw TCP, TLS passthrough, HTTP keep-alive, WebSocket, MCP and so on, even connections to hosts whose rules did not change.This is reported in #4945 which this PR fixes.
This was deliberate (#2373, "invalidate relays on policy changes") and is documented, but it is coarser than it needs to be. RFC 0005 only says that a reload "can invalidate mappings whose endpoint contract is no longer current".
Change
When the generation moves, a pinned guard now re-checks the connection it protects and closes only if the answer changed.
ConnectionPolicyContract: the L4 action and matched policy, the orderedendpoint_configs, the matched endpoints (policy name plus full endpoint object),exact_declared_endpoint_host, and the middleware chain selected for the host (plus the registry epoch when that chain is non-empty).PolicyRevalidatorattached to the guard re-evaluates the connection's original OPA input against the live engine when the generation moves. Authorization and middleware selection run under one engine lock. If the contract is equal, the guard re-pins to the new generation and the relay continues. Otherwise the guard becomes permanently stale and the relay closes exactly as before.Arc<AtomicU64>), so every clone a relay holds sees a re-pin.proxy/relay.rs(pin_policy_generation,pin_l7_evaluator,prepare_http_relay,prepare_raw_relay), so it covers raw TCP, TLS passthrough, transparent TCP, CONNECT HTTP (passthrough and inspected), upgrades, and forward HTTP. Existing call sites go throughis_stale()and needed no change. The one exception is the raw generation comparison in the HTTP passthrough middleware path, which now lets the guard re-pin first.Why a re-pinned tunnel can keep its cloned evaluator
Per-request Rego rules (
allow_request,deny_request,request_deny_reason,allowed_token_grant_ownersand their helpers) only read endpoints of policies whose binaries admit the caller and whose endpoints match the connection's host and port. That is exactlymatched_endpoints. An unchanged contract therefore implies unchanged per-request decisions, andTunnelPolicyEnginekeeps its clone.Deliberately conservative
policy_hashis excluded from the comparison. It is the hash of the complete canonical policy, stamped into every endpoint during proto conversion, so it changes on every reload. It only binds endpoint-status observations to an inventory. The endpoint index is also excluded, so inserting an unrelated endpoint in the same policy does not close connections.last_fail_closed_generation).middleware_registry_epoch).Known limitations and follow-ups
policy_hash, so the new inventory rejects its endpoint-status observations until the client reconnects. Authorization is unaffected. A follow-up could refresh the observation binding on re-pin.opa/revalidation.rsstate this invariant.Files
crates/openshell-supervisor-network/src/opa/revalidation.rsConnectionPolicyContract,AuthorizedConnection,PolicyRevalidatorcrates/openshell-supervisor-network/src/opa.rsPolicyGenerationGuard,TunnelPolicyEngine::with_revalidator,connection_policy_contract,middleware_selection_at,authorize_egress_locked, quarantine generation and registry epoch tracking, testscrates/openshell-supervisor-network/src/proxy/relay.rsEgressDecisionand pin credentials for HTTP relays, testscrates/openshell-supervisor-network/src/proxy/egress.rsEgressDecision::policy_input()crates/openshell-supervisor-network/src/proxy.rscrates/openshell-supervisor-network/src/l7/relay.rscrates/openshell-supervisor-middleware/src/lib.rsChainEntry: PartialEqdocs/how-it-works/policies/manage-policies.mdx,network-rules.mdxTests
New engine tests in
opa.rs:policy_hashrestamping is not a contract change;New relay tests in
proxy/relay.rs:relay_tcpacross an unrelated reload, and the relay closes when its endpoint is revoked;Manual check: open a long-lived connection (for example a WebSocket or
curlto a slow endpoint) through the sandbox proxy, approve an endpoint for a different host, and confirm that the connection survives, the log shows "Policy change does not affect established connection; re-pinned", and the connection closes when its own host's rules change.