Skip to main content

The firewall

The Shield decides an agent's connections the way it decides its actions. An agent says its network rule in its spec, under policy.shield. The Shield enforces that rule in the agent's network namespace and writes an audit row for every connection it allows or refuses.

policy:
shield:
egress: ["*"]
ingress: ["platform"]

Built: the rules below (*, *:port, exact hosts, addresses and CIDRs, with their refusals), the layers, the record of each connection, the organization's ceiling (bounded), and agent-shield firewall check, schema, compile, apply and watch. Not built: on Datalayer, the shield-firewall sidecar of the runtime pod that runs watch is not yet deployed.

The rules​

Egress is what the agent may reach beyond the platform's own endpoints. Each entry is one of:

  • *, alone: any public address, on any TCP port;
  • *:port: any public address on that TCP port only. ["*:80", "*:443"] is the web and nothing else: a page fetched over HTTP or HTTPS, never another port, never a private address;
  • an exact host (api.tavily.com), or a host and port (registry.npmjs.org:443);
  • an IPv4 address, or an address and port;
  • a CIDR, or a CIDR and port.

Some entries are refused, and the refusal gives the reason:

  • A private or reserved address. That covers 10/8, 172.16/12, 192.168/16, 100.64/10, link-local with the metadata address, loopback and multicast. * leaves these refused as well. Those are the platform's own addresses, and they belong to the base layer.
  • A wildcard host (*.example.com). A firewall sees addresses, not names. For a name-level allow-list, use the policy document's network.fetch.

Ingress is who may reach the agent's ports. Each entry is one of:

  • platform, the default: the platform's ingress and probes, which the base layer names;
  • *, or *:port;
  • an address or CIDR, with an optional :port, the agent's own port.

agent-shield firewall schema prints the JSON Schema, which is also published as schema/firewall.schema.json. agentspecs validates every agent's policy.shield against it.

The layers​

The base layer is what the runtime always reaches: the DNS resolvers, the platform's endpoints at the addresses they resolved to, and what is always refused (the metadata address). No spec can remove any of it. On Datalayer, the Operator writes the base layer from its egress table.

The agent's rules are added on top of the base. Both compile into three chains of the filter table:

  • SHIELD-EGRESS, hooked from OUTPUT;
  • SHIELD-INGRESS, hooked from INPUT, written only when ingress is enforced;
  • SHIELD-REFUSE, which rejects: a TCP reset, or port-unreachable for other protocols.

The script is applied in one iptables-restore --noflush commit, so a new set of rules replaces the old one whole. If the kernel refuses a set, the previous rules stay in force. Hosts are resolved where the rules are applied, and resolved again on each refresh. A host that resolves to nothing is refused, and the refusal says so.

The record​

Every rule that allows or refuses a new connection logs it to NFLOG group 7, with the prefix shield allow <rule> or shield refuse <rule>. The controller reads these back from nfnetlink_log and writes one connection row per connection. A row looks like this:

{"event": "connection", "by": "shield", "decision": "deny", "rule": "default",
"protocol": "tcp", "destination": "104.16.0.1:443", "source": "10.244.1.5"}

If the same decision, destination and rule come back within 60 seconds, they are folded into the next row's repeated count. Each apply writes a firewall row with the rules, the number of chain entries and any unresolved hosts.

Both kinds of row are counted with the rest of the Shield's audit. Once the OTEL export is configured (configure_export), watch also sends each row as a span of its own, so the destination reaches the platform and not only the count:

SpanAttributes
shield.firewall.connectionshield.event, shield.decision (allow or refuse), shield.rule, shield.destination (address:port), shield.host, shield.port, shield.protocol, shield.source, shield.repeated, datalayer.runtime_uid
shield.firewall.applyshield.event, shield.reason, shield.revision, shield.egress, shield.ingress, shield.base, shield.rules, shield.unresolved, datalayer.runtime_uid

The runtime's uid comes from $AGENT_SHIELD_RUNTIME_UID, or $DATALAYER_RUNTIME_ID. --no-export turns the spans off.

The organization's ceiling​

An organization's policy document may carry firewall, the most any of its agents may be granted:

scope: organization
firewall:
egress: ["github.com", "pypi.org:443", "203.0.113.0/24"]
ingress: ["10.42.0.0/16"]

bounded(rules, ceiling) holds an agent's rules to it, and the strictest wins:

  • * in the ceiling allows anything public the rules ask;
  • otherwise an exact host holds only against the same host, an address or CIDR only as far as a ceiling CIDR covers it, and a port only against the same port. An entry with no port is narrowed to the ceiling's port;
  • the rules' * becomes the ceiling's own entries;
  • platform always stays in ingress;
  • a direction the ceiling leaves out, or no ceiling at all, leaves the rules as they are.

A host is never matched against a CIDR, because the firewall resolves names only where it applies them. Only an organization's document may carry firewall; a team's or a session's is refused. On Datalayer, the Operator reads the organization's document from IAM when it assigns a runtime, and bounds the agent's rule before the firewall applies it.

The command​

CommandWhat it does
agent-shield firewall check <spec>Validates an agent spec's policy.shield, or a rules file, and says what it allows.
agent-shield firewall schemaPrints the JSON Schema.
agent-shield firewall compile --base base.json --rules spec.yamlPrints the iptables-restore script.
agent-shield firewall apply --base … --rules …Applies the rules once.
agent-shield firewall watch --rules … --state … [--base …]Runs as a sidecar's daemon. It applies the base, from --base or $AGENT_SHIELD_FIREWALL_BASE, and writes ready. Whenever the rules file's content changes it re-applies, and echoes the file's revision in applied.json, or in refused.json if the rules are refused. It resolves hosts again, records connections, writes each audit row to stdout, and sends each connection and firewall row as a span once the export is configured.

apply and watch need NET_ADMIN in the network namespace they change, and nothing else. On Datalayer they are meant to run in a shield-firewall sidecar of the runtime pod (not yet deployed). The agent's own containers never hold NET_ADMIN, so they cannot change the rules.