Zudoku
Plugins

Network shield plugin

The network shield plugin, netshield, is the control API of the official network shield. A network shield is an instance between your sandboxes and the internet: all the network traffic of the sandboxes goes through it. The plugin sets what traffic the shield lets through, and what the shield adds to it:

  • Policies allow traffic out of the sandboxes, or into them, by host, network, port, and protocol.
  • Secrets are credentials that the shield writes into the headers of outgoing HTTPS requests, so that a sandbox never holds them.
  • Limits cap the requests, the new connections, and the bandwidth of the sandboxes.

The API reference lists every request of the plugin.

Load the plugin

The plugin runs in the official shield image, official/utils/netshield:latest. The image contains the proxy, the DNS resolver, and the packet filter that do the work. The plugin only turns your policies, secrets, and limits into their configuration. Load the plugin as plugins/netshield:latest when you create the shield:

JSONPlugin entry
{ "name": "netshield", "image": "plugins/netshield:latest", "config": { "sandbox_iface": "eth1" } }

The relay example creates a shield with this plugin, and a sandbox behind the shield. The shield also needs a certificate authority, and more than the default 128 MB of memory.

The plugin takes these keys in config, all optional:

KeyDefaultDescription
sandbox_ifaceeth0The interface on which the traffic of the sandboxes arrives, such as the second interface, eth1, in the relay example.
resolv_conf/etc/resolv.confThe file with the DNS servers to which the shield sends the names that a policy allows.
ipset_maxelem4096The maximum number of addresses that the shield keeps for each policy and port that names hosts.

The plugin ignores a key that it doesn't know and a value that isn't valid, and logs a warning.

Send requests to the plugin through the plugin endpoint of the shield, for example with unikraft api:

TerminalCode
unikraft api /v1/instances/<shield-uuid>/plugins/netshield/status --metro fra

The plugin has no authentication of its own. The platform authorizes each request before the plugin gets it.

Certificate authority

A policy with the https handler intercepts TLS. The shield ends the TLS connection of the sandbox, so that it can read the request, limit requests, and add secrets. It shows the sandbox a certificate for the host, signed with a certificate authority (CA) that you supply. The http and passthrough handlers don't intercept TLS.

The shield doesn't start without the CA, even when no policy intercepts TLS. Pass the certificate in CA_CERT and the private key in CA_KEY, both in PEM format with their line breaks.

Create a certificate authority

Create a CA for the shield alone:

TerminalCode
openssl genrsa -out ca.key 2048 openssl req -new -x509 -key ca.key -out ca.crt -days 3650 \ -subj "/CN=Network shield CA" \ -addext "basicConstraints=critical,CA:TRUE" \ -addext "keyUsage=critical,keyCertSign,cRLSign" \ -addext "extendedKeyUsage=serverAuth"

Give ca.key only to the shield, and keep it out of sandbox images. Anyone with the key can create a certificate for any host, and every sandbox that trusts the CA accepts it. For the same reason, don't use a CA that other systems trust.

Trust the CA in a sandbox

Every sandbox behind the shield has to trust ca.crt. Otherwise, its connections through an https policy fail with a certificate error.

On Debian and Ubuntu, update-ca-certificates adds every file under /usr/local/share/ca-certificates/, in subdirectories too, whose name ends in .crt to the system trust store. You can put the certificate there without rebuilding the image, with an inline ROM that you add to the sandbox when you create it. The relay example puts it at /usr/local/share/ca-certificates/shield/ca.crt in the official Debian image. Then, in the sandbox, install the ca-certificates package, which runs update-ca-certificates. In an image that already has the package, run update-ca-certificates yourself.

In an image of your own, copy the certificate there when you build it:

Code
FROM debian:bookworm-slim RUN apt-get update \ && apt-get install -y --no-install-recommends ca-certificates \ && rm -rf /var/lib/apt/lists/* COPY ca.crt /usr/local/share/ca-certificates/network-shield.crt RUN update-ca-certificates

Tools that use the system trust store, such as curl, wget, and Go programs, then trust the shield. Some runtimes keep their own list of certificate authorities and ignore the system trust store. Point them at the certificate as well:

RuntimeSetting
Node.jsNODE_EXTRA_CA_CERTS, set to the path of ca.crt
Python requestsREQUESTS_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt, the system bundle that update-ca-certificates writes
Javakeytool -importcert -cacerts -alias network-shield -file ca.crt -storepass changeit -noprompt

Policies

The shield starts closed: it drops all traffic of the sandboxes until a policy allows it. A policy has these fields:

FieldRequiredDescription
idYesThe name of the policy: 1 to 64 letters, digits, ., _, and -.
directionYesoutbound for traffic from the sandboxes, or inbound for traffic to a sandbox, as described under inbound policies.
handlerYesHow the shield carries the traffic: http, https, or passthrough.
matchYesThe traffic that the policy allows.
priorityNoThe order in which the shield checks the policies, highest first, with 100 as the default.
request_limitNoA limit on the requests.
connection_limitNoA limit on the new connections.

The handler sets the protocol:

HandlerDescription
httpPlain HTTP, which the shield proxies and inspects.
httpsTLS, which the shield intercepts with its certificate authority and inspects, and the only handler that can carry secrets.
passthroughTraffic that the shield forwards without interception, by address only, so a host that shares an address with an allowed host also gets through.

The match object takes these fields:

FieldDescription
hostsHost names, such as api.github.com, or patterns, as described after this table.
networksIPv4 ranges in CIDR notation, with hosts or in their place.
portsPort numbers: an outbound policy needs at least one, and an inbound policy needs at least one port in ports or port_ranges.
protocolstcp, udp, or both, with tcp as the default.
port_rangesRanges of ports, such as {"first": 20000, "last": 29999}, on an inbound passthrough policy only.
sourcesIPv4 ranges of the clients that an inbound http policy admits, where an empty list admits all clients.

A host name matches only itself. *.github.com matches every name under github.com, but not github.com. A site on two ports needs two policies, for example http on port 80 and https on port 443. A site that redirects from example.com to www.example.com needs both names.

Add policies with a POST request. The request takes an array, even for one policy, and adds all its policies, or none:

TerminalCode
unikraft api /v1/instances/<shield-uuid>/plugins/netshield/policies --metro fra \ '[ { "id": "allow-openai", "direction": "outbound", "handler": "https", "match": { "hosts": ["api.openai.com"], "ports": [443] }, "request_limit": { "max_requests": 100, "window_seconds": 60 } } ]'
RequestDescription
GET /policiesLists the policies.
POST /policiesAdds policies.
PUT /policiesReplaces all policies.
GET /policies/<id>Gets one policy.
PUT /policies/<id>Replaces one policy.
DELETE /policies/<id>Deletes one policy, unless a secret needs it.

The shield answers a change when the change is in force. The next request of a sandbox uses the new policies.

Inbound policies

The shield also drops traffic from the platform to a sandbox behind it. This traffic includes the requests to the plugins of the sandbox, such as the sandbox plugin, and the connections to its published services. An inbound policy opens ports on a sandbox to the platform. It names the sandbox by its private name, <name>.internal, so send it after you create the sandbox.

By default, the platform serves plugins on ports from 20000 to 29999. This policy opens these ports on my-sandbox:

JSONCode
{ "id": "sandbox-plugins", "direction": "inbound", "handler": "passthrough", "match": { "hosts": ["my-sandbox.internal"], "port_ranges": [{ "first": 20000, "last": 29999 }] } }

To open these ports on every sandbox behind the shield, name 0.0.0.0/0 in networks in place of hosts. This policy then also covers the sandboxes that you add later, and a sandbox that you replace.

An inbound policy takes these handlers:

HandlerDescription
passthroughThe shield forwards the connections to one sandbox, or to a range of sandboxes in networks, such as 10.0.0.0/8.
httpThe shield proxies the requests to one port of one sandbox, and admits only the clients in sources, if you set it.

Use passthrough for the plugin ports, and for services that aren't HTTP. Use http for a service that the platform publishes on port 443 or 80. An inbound policy can't take https, because the platform ends TLS before the traffic gets to the shield. It can't take limits.

Limits

A policy can carry two limits. The shield counts each limit for each port of the policy, and all sandboxes behind the shield share the counter.

FieldHandlersDescription
request_limithttp, httpsUp to max_requests requests in a sliding window of window_seconds seconds, after which the shield answers 429 Too Many Requests with a Retry-After header.
connection_limitAllUp to max_connections new connections in a window of window_seconds, which is 1, 60, 3600, or 86400 seconds, after which the shield refuses connections with a TCP reset.

A client can send many requests over one connection, so a connection limit is weaker than a request limit for HTTP traffic.

Secrets

A secret is a credential that the shield writes into the requests of the sandboxes. The sandbox sends a pattern, such as {{GITHUB_TOKEN}}, in a header. The shield replaces the pattern with the value of the secret on the way out, so the sandbox never holds the value:

Code
The sandbox sends: Authorization: Bearer {{GITHUB_TOKEN}} The origin receives: Authorization: Bearer ghp_realvalue

The shield replaces patterns only in HTTPS requests that an https policy allows, because it can read only these requests and send them on encrypted. Add the policy first, and then the secret:

TerminalCode
unikraft api /v1/instances/<shield-uuid>/plugins/netshield/policies --metro fra \ '[ { "id": "allow-github-api", "direction": "outbound", "handler": "https", "match": { "hosts": ["api.github.com"], "ports": [443] } } ]' unikraft api /v1/instances/<shield-uuid>/plugins/netshield/secrets --metro fra \ '[ { "id": "github-token", "value": "<your GitHub token>", "inject": { "method": "http_header", "header_name": "Authorization", "pattern": "{{GITHUB_TOKEN}}" }, "hostname": ["api.github.com"], "bound_to_policies": ["allow-github-api"] } ]'

A secret takes these fields:

FieldRequiredDescription
idYesThe name of the secret, with the same characters as a policy id.
valueTo add a secretThe credential, which the API never returns, and which a replace can leave out to keep the stored value.
injectYesWhere the shield writes the value: method is http_header, header_name is the header, and pattern is the text to replace.
hostnameYesThe exact host names to which the shield sends the value, without wildcards, of which an https policy must allow at least one.
bound_to_policiesNoThe https policies whose requests get the value, or an empty list, the default, for every https policy that allows a host in hostname.

The requests on /secrets are the same as on /policies.

Bandwidth limit

A bandwidth limit caps the traffic that the shield sends to the sandboxes, in bits per second, for all sandboxes together. The value must be divisible by 8:

TerminalCode
unikraft api -X PUT /v1/instances/<shield-uuid>/plugins/netshield/limits --metro fra \ '{ "bandwidth_limit": { "max_bits_per_second": 10000000 } }'

The limit applies to the answers that the sandboxes download, and not to what they upload. Traffic over the limit waits in a queue, and nothing tells the sandbox about it. Set bandwidth_limit to null to remove the limit.

Status and logs

GET /status shows what the shield enforces: the number of policies and secrets, the rules that it installed, and whether a change failed.

GET /logs shows the changes to the shield and the requests that failed. It doesn't show the traffic of the sandboxes. The category parameter selects api or policy entries, and order=desc lists the newest entries first:

TerminalCode
unikraft api '/v1/instances/<shield-uuid>/plugins/netshield/logs?category=policy&order=desc&count=10' --metro fra

When a change fails

The shield checks each change before it applies it. It refuses a change that isn't valid with 422, or with 409 for a name that's already in use, and keeps its policies as they were. It also refuses with 409 to delete a policy that a secret needs. A secret needs a policy when its bound_to_policies names the policy, or when that list is empty and the policy is the last https policy that allows a host in its hostname.

If the shield can't apply a valid change, it answers 503 and seals itself: it drops all traffic of all its sandboxes, and refuses all further changes. A failure of this type comes from the host, so the same request fails again. Replace the shield instance. GET /status and the other reads still work, so that you can find the cause.

Limitations

  • A host pattern can have only one wildcard, as a leading *..
  • The shield replaces secrets only in HTTP headers of https traffic.
  • The bandwidth limit applies only to the traffic to the sandboxes.
  • An outbound policy can't use port 53 or the ports 3128 to 3131, which the shield uses itself.
  • sources takes IPv4 ranges only, so a non-empty list refuses clients that connect over IPv6.
  • A shield that fails to apply a change stays closed until you replace it.

Learn more

Last modified on