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:
Plugin entry
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:
| Key | Default | Description |
|---|---|---|
sandbox_iface | eth0 | The interface on which the traffic of the sandboxes arrives, such as the second interface, eth1, in the relay example. |
resolv_conf | /etc/resolv.conf | The file with the DNS servers to which the shield sends the names that a policy allows. |
ipset_maxelem | 4096 | The 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:
Code
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:
Code
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
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:
| Runtime | Setting |
|---|---|
| Node.js | NODE_EXTRA_CA_CERTS, set to the path of ca.crt |
Python requests | REQUESTS_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt, the system bundle that update-ca-certificates writes |
| Java | keytool -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:
| Field | Required | Description |
|---|---|---|
id | Yes | The name of the policy: 1 to 64 letters, digits, ., _, and -. |
direction | Yes | outbound for traffic from the sandboxes, or inbound for traffic to a sandbox, as described under inbound policies. |
handler | Yes | How the shield carries the traffic: http, https, or passthrough. |
match | Yes | The traffic that the policy allows. |
priority | No | The order in which the shield checks the policies, highest first, with 100 as the default. |
request_limit | No | A limit on the requests. |
connection_limit | No | A limit on the new connections. |
The handler sets the protocol:
| Handler | Description |
|---|---|
http | Plain HTTP, which the shield proxies and inspects. |
https | TLS, which the shield intercepts with its certificate authority and inspects, and the only handler that can carry secrets. |
passthrough | Traffic 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:
| Field | Description |
|---|---|
hosts | Host names, such as api.github.com, or patterns, as described after this table. |
networks | IPv4 ranges in CIDR notation, with hosts or in their place. |
ports | Port numbers: an outbound policy needs at least one, and an inbound policy needs at least one port in ports or port_ranges. |
protocols | tcp, udp, or both, with tcp as the default. |
port_ranges | Ranges of ports, such as {"first": 20000, "last": 29999}, on an inbound passthrough policy only. |
sources | IPv4 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:
Code
| Request | Description |
|---|---|
GET /policies | Lists the policies. |
POST /policies | Adds policies. |
PUT /policies | Replaces 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:
Code
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:
| Handler | Description |
|---|---|
passthrough | The shield forwards the connections to one sandbox, or to a range of sandboxes in networks, such as 10.0.0.0/8. |
http | The 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.
| Field | Handlers | Description |
|---|---|---|
request_limit | http, https | Up 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_limit | All | Up 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 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:
Code
A secret takes these fields:
| Field | Required | Description |
|---|---|---|
id | Yes | The name of the secret, with the same characters as a policy id. |
value | To add a secret | The credential, which the API never returns, and which a replace can leave out to keep the stored value. |
inject | Yes | Where the shield writes the value: method is http_header, header_name is the header, and pattern is the text to replace. |
hostname | Yes | The exact host names to which the shield sends the value, without wildcards, of which an https policy must allow at least one. |
bound_to_policies | No | The 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:
Code
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:
Code
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
httpstraffic. - 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.
sourcestakes 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
- Network shield and relay: how traffic goes through a shield, with a full example.
- Network shield plugin API reference: every request of the plugin, with all its fields.
- Plugins: how plugins load, and how the platform authorizes requests to them.
- Sandboxes: run untrusted code in a hardware-isolated microVM.
- Kubernetes: a shield for each Pod on a Kraftlet node.