Zudoku
Plugins

Plugins

InterfacesAPICLISDKs
Introduced inRelease 11: Metis
1 note
  • Plugins are part of enterprise plans. Contact support to enable them for your account. support@unikraft.com.

Plugins let you attach small helper programs to an instance and reach each one over a direct, authenticated HTTP endpoint. A plugin runs inside the instance next to your main app, loads from its own image, and answers requests that the Unikraft Cloud API forwards to it.

Unikraft Cloud built plugins to provide a native sandbox experience. In the sandbox case, a plugin runs a small HTTP server that accepts commands to run inside the instance, exposes filesystem services, and more. The same mechanism fits any helper you want to reach over a per-instance, authenticated endpoint. A plugin can also publish services on a domain of its own.

Official plugins

Unikraft Cloud provides these plugins:

PluginImageDescription
Sandboxplugins/sandbox:latestRuns commands, and reads and writes files, inside the instance, for example for unikraft instance exec and unikraft instance shell.
noVNCplugins/novnc:latestAdds a virtual desktop that you use in a browser, and that a program drives through the plugin endpoint.
Network shieldplugins/netshield:latestServes the API that sets the policies, secrets, and limits of a network shield.

Each official plugin has an API reference: sandbox, noVNC, and network shield. To build a plugin of your own, use the Plugin SDK.

How it works

Each plugin loads from its own ROM image. When the instance boots, the platform mounts every plugin at /uk/plugins/<plugin_name> and starts its init program. The platform hands each plugin a socket to accept connections on, so every plugin has its own private channel.

You reach a plugin through the instance's API endpoint:

Code
https://api.<metro>.unikraft.cloud/v1/instances/<uuid>/plugins/<plugin_name>/<path>

The platform forwards everything after <plugin_name>/ to the plugin as the request path. A request to .../plugins/my-plugin/files/list reaches the plugin with the path /files/list.

This design has a few notable properties:

  • Authorized before it reaches the plugin. By default the platform checks the request against your account and confirms your access to the target instance. A plugin can carry a token of its own instead, which lets callers without a platform account in. Either method settles the request before the plugin sees it, which sets plugins apart from running an HTTP server as a regular service.
  • A direct line to one instance. You talk to a single instance, so no load balancing or autoscale sits in the path, and the instance needs no service group.
  • Works with scale-to-zero. When scale-to-zero has put the instance to sleep, the platform wakes it to serve the request and keeps it up for the duration, the same way a normal request does.
  • Works while draining. A plugin request also reaches an instance in the draining state, and doesn't stop the drain.

The plugin image

A plugin ships as a standard ROM image with an executable named init in its root. The platform loads the image, mounts it at /uk/plugins/<plugin_name>, and runs init when the plugin starts.

init receives these from the platform:

  • Configuration on standard input. Whatever you pass in the plugin's config field arrives on init's STDIN as JSON. A plain value such as "config": "my-string" counts as valid JSON, the same as "config": 232 or "config": { ... }.
  • A socket file descriptor. The platform passes init an --api_fd <n> argument that holds the file descriptor the plugin accepts connections on. The plugin serves API traffic on that socket. The socket uses vsock if the plugin image declares vsock support and the instance supports it. Otherwise, it uses TCP.
  • Service ports. If the plugin publishes services, the platform also passes --services <protocol>:<port>,..., for example --services tcp:8080. The plugin opens a listener on each port.

Assigning ports

The platform assigns each plugin's socket a port inside the instance, by default from the range 20000 to 29999. It gives each plugin the next free port, and skips any port that the guest already uses.

Adding plugins when you create an instance

Pass --plugin to unikraft instance create or unikraft instance run, once for each plugin:

TerminalCode
unikraft instance create \ --name my-instance \ --metro fra \ --image my-app:latest \ --plugin 'name=my-plugin,image=user/myplugin:latest,config={"workdir":"/tmp"}'

The flag takes name, image, and an optional config, which must be valid JSON. It also takes the pull-policy and headers.<key> options of the image. The CLI sends these in the plugins field of a POST /instances request.

In the API, each entry in the plugins array accepts these fields. The --plugin column shows which of them the CLI flag can set:

FieldRequired--pluginDescription
nameYesYesThe plugin name. It becomes the <plugin_name> segment in the plugin endpoint. See Plugin names for the allowed format.
imageYesYesThe plugin's ROM image, given as an image reference string such as user/myplugin:latest, or as an image object with a url and optional headers and pull_policy; the deprecated rom field still works in its place.
configNoYesArbitrary JSON that the platform passes to the plugin's init on STDIN. Any JSON value works, including a string, a number, or an object.
authorizationNoNoHow the platform authorizes requests to this plugin, as described under authorization.
servicesNoNoThe services that the plugin publishes, as described under plugin services.

You can attach up to 8 plugins to an instance.

The deprecated rom field

The API used to call the image field rom. It still accepts rom, but a response to a request that uses it carries a deprecation warning:

JSONResponse
{ "status": "success", ... "warnings": [ { "type": "deprecated_migration", "message": "The plugin `rom` field is deprecated. Use the `image` field instead" } ] }

A plugin entry takes only one of the two fields, and a request that sets both fails with Only one of 'rom' and 'image' may be set. The --plugin flag still accepts rom= as an alias for image=.

Authorization

Every request to a plugin endpoint carries a token in an Authorization: Bearer <token> header. The platform authorizes the request before it forwards anything, and strips that header on the way, so the plugin never sees the token.

In the API, a plugin entry picks how the platform checks the token with an optional authorization object. The --plugin flag can't set this object, so a plugin that you load with the CLI uses the userdb method.

FieldRequiredDescription
typeYesEither userdb or bearer.
tokenFor bearerThe plugin's own token, at most 256 characters, which only a bearer plugin may carry.

Platform users

A plugin without an authorization object uses the userdb method. The token has to belong to a platform user with access to the instance, so the token you use for the rest of the API reaches the plugin as well.

A token of the plugin's own

With "type": "bearer" the platform compares the supplied token against the plugin's own token and consults no user account. Send the request with unikraft api, because the --plugin flag can't set authorization:

TerminalCode
unikraft api /v1/instances --metro fra \ '{ "name": "my-instance", "image": "official/alpine:latest", "plugins": [ { "name": "my-plugin", "image": "user/myplugin:latest", "authorization": { "type": "bearer", "token": "s3cr3t" } } ] }'

A caller then reaches the plugin with that token alone:

TerminalCode
curl -H "Authorization: Bearer s3cr3t" \ https://api.fra.unikraft.cloud/v1/instances/<uuid>/plugins/my-plugin/files/list

This suits callers with no platform account and no business holding one: a sidecar, your own control plane, or a webhook from a third-party service. The token opens that one plugin on that one instance, and nothing else of the API.

The instance status in the API echoes a non-default type back in the plugin's authorization object, and never echoes the token. A plugin on the default method reports no authorization object at all. unikraft instance get doesn't show authorization, so read it from the API with unikraft api /v1/instances/<uuid> --metro fra.

Two consequences follow from bearer consulting no user account:

  • Your platform token stops working on that plugin, so the plugin's token becomes the only way in.
  • Changing the token takes the same PATCH path as other plugin changes, so it lands the next time the instance stops. Send it with unikraft api, because the CLI flags can't set authorization.

Plugin services

A plugin can also publish up to 4 services. Clients connect to these services on a subdomain of the metro's API domain:

Code
https://<plugin_name>-<uuid>.api.<metro>.unikraft.cloud

The platform sends each connection to the destination_port of the service inside the instance. Set the services in the services array of the plugin entry. Send the request with unikraft api, because the --plugin flag can't set services:

TerminalCode
unikraft api /v1/instances --metro fra \ '{ "name": "my-instance", "image": "official/alpine:latest", "plugins": [ { "name": "my-plugin", "image": "user/myplugin:latest", "services": [ { "port": 443, "destination_port": 8080, "handlers": ["tls", "http"] } ] } ] }'

A client then connects to the service at https://my-plugin-<uuid>.api.fra.unikraft.cloud.

Each service takes these fields:

FieldRequiredDescription
portYesThe public port on the plugin's domain.
destination_portYesThe port inside the instance that the plugin listens on.
handlersYesThe handlers of the port, with the same port rules as other services but without redirect.

The platform doesn't authenticate connections to plugin services. The plugin must authenticate its clients.

A connection to a plugin service wakes an instance in standby, the same as for other services. An instance in the draining state takes no new connections to plugin services. The instance status in the API shows the services of each plugin in its services array. unikraft instance get doesn't show them, so read them with unikraft api /v1/instances/<uuid> --metro fra.

Plugin names

A plugin name has a maximum length of 63 characters and contains only these characters:

  • Lowercase and uppercase letters (a–z, A–Z)
  • Digits (0–9)
  • Hyphen (-) and underscore (_)

A plugin can't share its name with a ROM of the same instance.

The name of a plugin that publishes services is part of a domain. It has a maximum length of 26 characters, and contains only lowercase letters, digits, and hyphens.

Changing plugins on an existing instance

Use unikraft instance edit to change the plugins of an existing instance. --plugin replaces the instance's plugin list, --add plugins=... appends to it, and --del plugins=<name> removes one plugin:

TerminalCode
# Replace the plugin list unikraft instance edit my-instance \ --plugin name=my-plugin,image=user/myplugin:latest # Append to the plugin list unikraft instance edit my-instance \ --add plugins=name=my-other-plugin,image=user/myotherplugin:latest # Remove a plugin by name unikraft instance edit my-instance \ --del plugins=my-other-plugin

Each flag sends a PATCH /instances request with prop set to plugins. --plugin sends the set op, --add sends add, and --del sends del. The platform handles these requests the same way whether they come from the CLI or the API. A plugin that a set leaves out of the new list unloads, the same as with del. The platform applies the change while the instance sits in the stopped state. On a running instance, the platform queues the change and applies it the next time the instance stops. A restart and a scale-to-zero both count as a stop. Until then, the instance keeps its current plugins, and a removed plugin still answers requests.

The --plugin flag doesn't carry authorization or services. Thus, unikraft instance edit --plugin turns every bearer plugin on the instance back into a userdb plugin, and removes all plugin services. --add leaves the existing plugins as they are. To replace the list and keep these settings, send the set request with unikraft api.

Reloading across the instance lifecycle

The platform treats plugins like ROMs across state changes. It reloads them whenever the instance goes through a scale-to-zero, suspend, or restart cycle, so a plugin returns with the instance each time it comes back up.

Inherited when you clone an instance

Plugins belong to the instance definition, so an instance that comes from another one keeps the same plugins. This applies when you use branching, restore a checkpoint, fork an instance, or start from an on-demand template.

Use cases

The first use case for plugins is a native sandbox. The sandbox plugin runs a small HTTP server that runs commands inside the instance and serves its filesystem, over the authenticated plugin endpoint.

Plugins fit other patterns too. A few ideas:

  • Admin and debug endpoints that stay separate from your app's public services and stay reachable only through the authenticated API.
  • Health and inspection probes that read state from inside the instance on demand.
  • Sidecar utilities such as a metrics collector, a log tailer, or a configuration reloader.
  • Per-instance tooling that you attach to one instance without a change to its base image.
  • Web interfaces for tools inside the instance, such as a debug console, on a plugin service.

Limitations

  • You can attach at most 8 plugins to an instance.
  • A plugin name has a maximum length of 63 characters and uses only letters, digits, -, and _.
  • A bearer token has a maximum length of 256 characters.
  • A plugin can publish at most 4 services, and only over TCP.
  • The name of a plugin that publishes services has a maximum length of 26 characters and uses only lowercase letters, digits, and -.
  • The platform tracks up to 64 ports that an instance listens on, including the ports of its plugins and plugin services. Once an instance listens on 64 ports, a plugin service doesn't reach a port that the instance opens after that, see Services.

Learn more

  • Plugin SDK: build a plugin in Go without hand-rolling the platform contract.
  • Sandboxes: run untrusted code in a hardware-isolated microVM.
  • ROMs: the image format that plugins build on.
  • Instances: create and manage the instances that host plugins.
  • Scale-to-zero: how an idle instance wakes to serve a plugin request.
  • Services: the regular way to expose an HTTP server, for contrast with the authenticated plugin endpoint.
  • Unikraft Cloud's REST API reference, in particular the create instance endpoint.
Last modified on