Skip to content

feat(telemetry): sandbox egress port scoping, collector lane & in-container trace context #702

Closed

Description

@dan-petty

Problem

devops sandbox network-policy (src/devops_cli/commands/sandbox.py:999-1053) and the MCP tool sandbox_network_policy (src/devops_cli/ai/mcp/server.py:1419-1446) print a Kubernetes NetworkPolicy for a sandbox pod. Isolated mode is deny-all (src/devops_cli/sandbox/models.py:339-340). The namespace and whitelist modes add a kube-dns rule on port 53 (models.py:138-151). The whitelist modes then add one rule whose peers are the resolved ipBlocks (models.py:230-264). That rule has no ports, so it opens every port and protocol to each listed address. Sandboxes still default to no network (models.py:269, :428).

  1. Ports are dropped. _extract_host_or_ip keeps the host and drops the port (models.py:67-68, :80-81). Both builders emit {"to": [...]} with no ports (models.py:237-244, :254-264). Measured on 9c879ae: the entries https://1.1.1.1:8443 and 1.1.1.1:8443 both yield {"to": [{"ipBlock": {"cidr": "1.1.1.1/32"}}]}. The MCP tool refuses / in whitelist items (server.py:111), so it cannot pass a URL. It does accept host:port, and that loses its port the same way. An agent that asks for one port gets all of them.

  2. Public CIDR entries are not checked as ranges. _resolve_public_host admits a network when is_non_public_ip is false (models.py:174-184), and that function is not addr.is_global (src/devops_cli/core/validation.py:107-117). For a network, Python's is_global only asks whether the whole network sits inside one special-purpose block. A wide range that contains private blocks therefore counts as global. Measured on 9c879ae: devops sandbox network-policy --network-mode public_whitelist --public-whitelist 0.0.0.0/0 exits 0 and emits ipBlock: {cidr: 0.0.0.0/0}. ::/0, 128.0.0.0/1 (which contains 172.16.0.0/12 and 192.168.0.0/16) and 8.0.0.0/5 (which contains 10.0.0.0/8) are also admitted. Such a policy reaches the pod network, the API server and 169.254.169.254. The plan to "keep the resolved-/32 allowlist" holds only for host entries.

  3. No lane reaches the OTel collector. The cluster collector is the otel-collector release of open-telemetry/opentelemetry-collector in namespace otel (src/devops_cli/commands/k8s/stack_lifecycle.py:103-108, src/devops_cli/config/constants.py:826-827). That namespace already admits OTLP on 4317 and 4318 from every namespace (k8s/otel/networkpolicy.yaml:36-45), so only the sandbox's egress rule is missing. Jaeger runs in the same namespace and also listens on 4317 and 4318 (k8s/otel/jaeger.yaml:54,57). A selector on the namespace alone would let a sandbox bypass the collector.

  4. Containers get no trace context. When tracing is on, run_subprocess and run_subprocess_async write TRACEPARENT into the child's environment (src/devops_cli/core/process.py:264,404) through inject_trace_env (src/devops_cli/telemetry/tracer.py:815-824), which mutates the dict it is given. The two container builders pass only config.env: WorkloadSandboxEngine._build_create_kwargs (src/devops_cli/sandbox/engine.py:230, :247-253) and WorkloadSandboxRunner._network_create_kwargs (src/devops_cli/docker/sandbox.py:237-255). DockerEngineService.exec_in_container takes no environment (src/devops_cli/docker/engine.py:401-412), and WorkloadSandboxEngine.exec opens no span (sandbox/engine.py:423-453). grep -rn 'TRACEPARENT\|inject_env\|inject_trace_env' src/devops_cli/sandbox src/devops_cli/docker | wc -l prints 0.

What changed since this issue was written:

Enforcement:

  • A NetworkPolicy only works if the CNI enforces it. devops k8s bootstrap starts minikube with no --cni (src/devops_cli/commands/k8s/cluster_runtime.py:95,100), but its help says Calico (src/devops_cli/lang/en/help.py:266). The previous body called this a separate item, but no such issue exists.
  • The homelab cluster is k3s, whose built-in policy controller enforces NetworkPolicy unless it is disabled. Nothing in the repository records how the cluster was installed.

Docker lanes: the whitelist modes refuse to start without an egress proxy (sandbox/engine.py:242-245, docker/sandbox.py:230-233). Since #734, no CLI option sets egress_proxy (models.py:272-275). Port scoping therefore changes only the emitted NetworkPolicy.

Out of scope:

Acceptance criteria

All four parts land in one pull request, in the order below.

Port scoping (sandbox/models.py)

  • Each whitelist entry becomes its own egress rule with its own ports. Entries never share a rule, so no address is opened on another entry's port.
  • The port comes from the entry:
    • scheme://host:port, host:port and [v6]:port use the stated port.
    • https://host and a bare host, IP or CIDR use TCP 443.
    • http://host uses TCP 80.
    • A bare IPv6 address takes no port suffix and gets 443.
  • These entries are refused with an error that names the entry, and the CLI exits 1:
    • an entry with any other scheme and no port;
    • a port outside 1-65535.
  • Tests stub DNS with the public_dns fixture or a patched socket.getaddrinfo, and check:
    • https://1.1.1.1:8443 and 1.1.1.1:8443 give 1.1.1.1/32 on TCP 8443 only;
    • http://example.com gives TCP 80, and example.com gives TCP 443;
    • [2606:4700::1]:8443 gives 2606:4700::1/128 on TCP 8443;
    • http://localhost:11434 in the local whitelist gives 127.0.0.1/32 on TCP 11434;
    • a.example:8443,b.example gives two rules, and neither rule pairs A's addresses with 443 or B's with 8443.
  • One test runs over every whitelist policy the tests generate. It asserts that every egress rule except the DNS rule has a non-empty ports.
  • The --public-whitelist and --local-whitelist help (commands/sandbox.py:1017-1026) states the port syntax and defaults. It also says to regenerate the policy when a name's addresses change.

Public CIDRs

Collector lane

  • devops sandbox network-policy --allow-collector and sandbox_network_policy(allow_collector=True) add one egress rule with a single peer:
    • that peer has both namespaceSelector: {matchLabels: {kubernetes.io/metadata.name: otel}} and podSelector: {matchLabels: {app.kubernetes.io/name: opentelemetry-collector, app.kubernetes.io/instance: otel-collector}};
    • the rule's ports are TCP 4317 and 4318, and it has no ipBlock;
    • the namespace and labels are constants next to CONST_OTEL_COLLECTOR_NAMESPACE.
  • The flag is accepted with sandbox_namespace, public_whitelist and local_whitelist. With isolated or bridge it is refused with an error that names the modes it applies to.
  • Without the flag, no rule selects otel.
  • No mode emits a rule for port 6379.

Container trace context

  • With tracing enabled, both builders call get_tracer().inject_trace_env on a copy of config.env. That puts TRACEPARENT, and TRACESTATE when one is set, into the container's environment.
    • The deploy engine does this inside the sandbox.deploy span, and the workload runner inside docker.workload_sandbox.run. The injected parent id is that span's id.
    • config.env is unchanged afterwards.
    • In proxy modes the proxy variables are still present.
  • With tracing disabled, the container's environment is exactly what it is today.
  • DockerEngineService.exec_in_container takes environment: dict[str, str] | None = None and passes it to exec_run.
  • WorkloadSandboxEngine.exec opens a sandbox.exec span and passes that span's context to the exec. Two execs in one sandbox carry different parent ids, and neither carries the context from deploy time.
  • These tests use the mocked Docker SDK and need no daemon.

General

  • No test resolves a real name or opens a non-loopback socket. uv run devops ci passes.
  • Generated docs are refreshed with devops docs generate --sync-readme.
  • The pull request carries its task file and changelog.d/702.md. It does not edit CHANGELOG.md or docs/ROADMAP.md.
  • Person-run check, on the homelab cluster:
    1. Apply a policy generated with --public-whitelist 1.1.1.1:443 to a test pod in a test namespace.
    2. Confirm that 1.1.1.1:443 connects and 1.1.1.1:80 times out.
    3. Apply a policy generated with --allow-collector and confirm that an OTLP request reaches the collector.
    4. Record the results in the task file. If port 80 connects, the cluster does not enforce NetworkPolicy, and the owner decides what follows.
  • Person-run check: kubectl -n otel get pods --show-labels shows the two collector labels the rule selects.

Key questions

  1. Which entry forms, and which defaults? Proposed: those listed in the acceptance criteria.
    • Only TCP is supported. There is no UDP syntax, because no current use needs it.
    • Both whitelists follow the same rules. A local entry such as 192.168.1.50 opens every port today and will open only 443 unless it names a port. This is a deliberate break; pre-1.0, there is no shim.
  2. Should names still be resolved to /32 and /128 when the policy is generated? The alternative is vibes' 0.0.0.0/0 except RFC 1918. Proposed: keep resolving, because the except form admits every public host. A resolved address goes stale when DNS changes; the fix is to regenerate the policy, and the help says so.
  3. Should the public whitelist keep CIDR entries? Proposed: yes, with the range check. feat(cli): ipv6 egress cidr fix and deny-class property tests for sandbox policy #701 kept them, and tests cover them.
  4. Should the MCP tool accept URLs and bracketed IPv6? Proposed: no. Its character check (server.py:111) stays as it is, because host and host:port cover what an agent needs, and MCP clients are untrusted.
  5. Should MCP expose the collector lane? Proposed: yes. The tool only prints YAML, and a person or a pipeline applies it. The lane reaches one labelled pod on two ports.
  6. Can isolated mode have the collector lane? Proposed: no. Isolated means no egress. A sandbox that has to export telemetry uses sandbox_namespace with the lane.
  7. Should there be a lane for the cache on port 6379? Proposed: no.
    • The cache Valkey is inside the llm perimeter, which admits only its own namespace, monitoring and Traefik (k8s/llm/networkpolicy.yaml:22-38).
    • valkey-runs holds the shared run index, and a sandbox has no reason to reach it.
  8. Should blocked egress raise an alert? Proposed: no.
  9. Should the bubblewrap host sandbox get trace context as well? It runs with --clearenv and sets only its default environment plus the caller's (src/devops_cli/sandbox/host.py:379-389, :407). Proposed: no, not in this item.
    • The host sandbox runs with --unshare-all, so no process inside it can export a span.
    • Its output is already captured under the caller's span.
  10. Should the containers get an exporter endpoint? Proposed: no, this item only passes trace context.
    • A workload that exports sets OTEL_EXPORTER_OTLP_ENDPOINT through config.env.
    • Only bridge mode, or a pod with the collector lane, can reach a collector.
  11. Should this item repair the Docker whitelist path? Proposed: no. Those modes refuse to start without an egress proxy, and nothing has set one since feat(k8s): modernize monitoring stack, llm profiles, traefik routing, and remove squid proxy #734. Removing or replacing egress_proxy is separate work.
  12. Should the false Calico claim be fixed here? Proposed: no. It needs its own fix item: either pass a CNI that enforces policy, or correct the help text.

Depends on: #897 (v0.2.27). The 64:ff9b::/96 criterion uses its NAT64-aware classifier, and it lands first by release order.
Related: #701 (done in v0.2.24, full-length prefixes); #734 (done, Squid removed); #549 (v0.2.28, Alertmanager); #857 (v0.2.28, ToolAnnotations on the same MCP tool, no conflict); #698 (backlog, declared versus enforced sandbox audit).
Source: vibes patterns/zero-trust-sandboxing-and-observability.md, resources/k8s/agent-sandbox/network-policy.yaml, resources/observability/prometheus-agent-alerts.yaml.

Activity

  1. added this to the v0.2.24 milestone on Sep 28, 2026
  2. added
    type/feature
    scope/telemetry
    priority/p1-high
    on Sep 28, 2026
  3. modified the milestones: v0.2.24, v0.2.25, v0.2.26 on Sep 30, 2026
  4. dan-petty commented on Sep 30, 2026

    @dan-petty
    OwnerAuthor

    Moved from v0.2.25 to v0.2.26. v0.2.25 is capped at 12 items (#740): it keeps #739-#742, their dependency #704, and the oldest P1 items. This P1 item is next in line.

  5. removed this from the v0.2.26 milestone on Oct 1, 2026
  6. dan-petty commented on Oct 1, 2026

    @dan-petty
    OwnerAuthor

    Moved from v0.2.26 to the backlog. After the v0.2.23 P1 leftovers were retargeted (they were committed earlier), v0.2.25 and v0.2.26 are full at 12 items each, and the roadmap-service items placed in v0.2.26 stay. It is next in line for a planned release.

  7. added
    scope/security
    on Oct 2, 2026
  8. added this to the v0.2.29 milestone on Oct 2, 2026
  9. dan-petty commented on Oct 2, 2026

    @dan-petty
    OwnerAuthor

    Placed in v0.2.29 on 2026-10-02. v0.2.29 holds P1 work that has to land before v0.3.0, which v0.2.25 to v0.2.28 had no room for. Backlog P1 items were ranked by Value, then Effort, with defects and security fixes first at a tie. This item ranked in the top ten, and two of the release's twelve slots stay open for P1 items that surface before then.

  10. dan-petty commented on Oct 2, 2026

    @dan-petty
    OwnerAuthor

    Refined to Ready on 2026-10-02 for v0.2.29. The body was checked claim by claim against release/v0.2.25 at 9c879ae, and a second agent then tried to refute it; every major objection was resolved in a revision. Cited lines were updated for code that moved, and the scope was corrected where merged work changed it. Acceptance criteria were tightened so each fails on the current code, and every key question now has a proposed answer.

  11. removed this from the v0.2.29 milestone on Oct 2, 2026
  12. dan-petty commented on Oct 2, 2026

    @dan-petty
    OwnerAuthor

    Moved to the backlog: #1021 (a P1 review defect, High value) took this slot in v0.2.29. This item was the lowest-ranked one the job placed there (a P1 feature, Medium/Medium). It goes first among backlog P1s when a pre-v0.3.0 slot opens.

  13. dan-petty commented on Oct 3, 2026

    @dan-petty
    OwnerAuthor

    Lines cited at dcafadf.

    The first PR of #897 (v0.2.27) changes the public-whitelist check in _resolve_public_host (sandbox/models.py:174-184). With ipaddress objects and the metadata classifier #897 settles, it refuses a CIDR that overlaps link-local space (169.254.0.0/16, fe80::/10), holds a cloud metadata address, is not is_global, or has a non-public first or last address.

    • Measured offline, that rule refuses 0.0.0.0/0, ::/0, 128.0.0.0/1 and 64:ff9b::/96 from this item's refused list, and still admits 2606:4700::/48 and 140.82.112.0/20.
    • It still admits 8.0.0.0/5: both ends are public and it overlaps no link-local space, but it holds an RFC 1918 block.

    The general rule, that no address in a public range may be non-public, stays with this item, and 8.0.0.0/5 is its case. Build it on #897's predicates in core/validation.py, as this item's "same classification" criterion already says, and keep the refused and admitted lists in this item's acceptance criteria.

  14. added this to the v0.2.30 milestone on Oct 8, 2026
  15. dan-petty commented on Oct 8, 2026

    @dan-petty
    OwnerAuthor

    Placed in v0.2.30 on 2026-10-08 at the owner's request ("Populate 10 security and ci issues into each v0.2.30, v0.2.31, and v0.2.32 milestone"). This is a person's placement. Release theme: P1 secret and sandbox-boundary fixes, the review tools lock, kube-linter finding identity and a faster gate. Position 6 of 10 in the release's planned merge order. Its board Status is set to Ready, so the release-start fill keeps it.

  16. dan-petty-bot commented on Oct 9, 2026

    @dan-petty-bot
    Collaborator

    What changed

    #1465, merged into release/v0.2.30 as b37eb54.

    Closes #702

    23 file(s) changed.

    How it was verified

    Check runs at 21c3c3f:

    • Prune PR DevContainer Images: pass
    • CodeQL: pass
    • Static Analysis: pass
    • Analyze (actions): pass
    • Analyze (python): pass
    • Tests & Coverage: pass
    • DevContainer Image: skipping
    • Service Image: skipping
    • Dependency Freshness: skipping

    Acceptance Criteria of docs/agent/tasks/task-702-sandbox-egress-port-scoping-collector-lane-in-container-trac.md:

    • Each whitelist entry is its own egress rule with its own TCP port: https://1.1.1.1:8443 and 1.1.1.1:8443 give 1.1.1.1/32 on 8443 only, http://example.com 80, example.com 443, [2606:4700::1]:8443 2606:4700::1/128 on 8443, and local http://localhost:11434 127.0.0.1/32 on 11434 (tests/test_sandbox_networking.py::test_a_whitelist_entry_opens_only_its_own_port, names through public_dns).
    • example.com:8443,example.org, each name stubbed to its own public address through a patched socket.getaddrinfo, gives two rules, and neither pairs one entry's addresses with the other's port (test_two_entries_never_share_a_rule).
    • network-policy exits 1 with the entry in its output for ftp://example.com, example.com:0, example.com:70000 and 140.82.112.0/20:8443 (test_network_policy_exits_1_naming_an_entry_it_cannot_scope_to_a_port); the parser's forms and refusals are pinned by test_a_whitelist_entry_parses_into_its_host_and_port, test_a_whitelist_entry_that_cannot_name_one_port_is_refused and the IPv6 property test in tests/test_sandbox_policy_properties.py.
    • Every egress rule but the DNS rule names its ports, over one module-level list of every whitelist configuration the networking tests build (test_every_whitelist_rule_but_the_dns_rule_names_its_ports).
    • is_non_public_ip refuses 0.0.0.0/0, ::/0, 128.0.0.0/1, 8.0.0.0/5, 64:ff9b::/96, 64:ff9b::800:0/101, ::800:0/101 and 64::/16, and admits 2606:4700::/48 and 140.82.112.0/20 (tests/test_validation.py::test_a_range_is_non_public_when_any_of_its_addresses_is). test_is_non_public_ip, which keeps 64:ff9b::808:0/112 public, and the feat(cli): ipv6 egress cidr fix and deny-class property tests for sandbox policy #701 tests pass unchanged.
    • --allow-collector adds exactly the collector rule in sandbox_namespace, public_whitelist and local_whitelist; with isolated or bridge the command exits 1 naming those three modes; without the flag no rule selects otel; no mode, with or without the flag, opens 6379; the MCP tool adds --allow-collector only when asked (test_the_collector_lane_*, test_without_the_flag_no_rule_selects_the_collector_namespace, test_no_policy_opens_the_cache_port, test_the_mcp_tool_asks_for_the_collector_lane_only_when_told_to).
    • A deployed container's TRACEPARENT names the sandbox.deploy span and a workload's the docker.workload_sandbox.run span; config.env is unchanged; proxy variables stay; with tracing disabled the environment is exactly config.env (tests/test_sandbox_lifecycle.py, tests/test_workload_sandbox.py, mocked Docker SDK, no daemon).
    • Two execs in one sandbox carry different parents, each its own sandbox.exec span and neither the deploy span; with tracing disabled an exec sends no environment; exec_in_container hands environment to exec_run (test_each_exec_is_parented_to_its_own_exec_span, test_an_exec_with_tracing_off_sends_no_environment, tests/test_docker_engine.py::test_exec_in_container_hands_its_environment_to_the_exec).
    • No test resolves a real name or opens a non-loopback socket. Generated docs are refreshed with devops docs generate --sync-readme and pass devops docs check; docs/TELEMETRY.md, written from docs/generator.py, names the sandbox containers and execs, and changelog.d/702.md carries the entry; CHANGELOG.md and docs/ROADMAP.md are not edited.
    • Pending a person: on the cluster, in a test namespace, apply the policy from devops sandbox network-policy --network-mode public_whitelist --public-whitelist 1.1.1.1:443 --name egress-check --namespace <test-namespace> | kubectl apply -f -, run a pod labelled app.kubernetes.io/name=egress-check there (kubectl -n <test-namespace> run egress-check --image=curlimages/curl --labels=app.kubernetes.io/name=egress-check --command -- sleep 3600), and from it confirm 1.1.1.1:443 connects and 1.1.1.1:80 times out (kubectl -n <test-namespace> exec egress-check -- curl -sS -m 5 -o /dev/null https://1.1.1.1 and kubectl -n <test-namespace> exec egress-check -- curl -sS -m 5 -o /dev/null http://1.1.1.1). If port 80 connects, the cluster does not enforce NetworkPolicy, and the owner decides what follows.
    • Pending a person: apply a policy generated with devops sandbox network-policy --network-mode sandbox_namespace --allow-collector --name egress-check --namespace <test-namespace> the same way, and confirm an OTLP request from the pod reaches the collector (kubectl -n <test-namespace> exec egress-check -- curl -sS -m 5 -o /dev/null -w '%{http_code}' -X POST -H 'Content-Type: application/json' -d '{}' http://otel-collector-opentelemetry-collector.otel:4318/v1/traces prints a status rather than timing out).
    • Pending a person: kubectl -n otel get pods --show-labels shows the collector pod with both app.kubernetes.io/name=opentelemetry-collector and app.kubernetes.io/instance=otel-collector.
  17. added a commit that references this issue on Oct 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority/p1-highscope/securityscope/telemetrytype/feature

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions