# Plugins

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](#plugin-services) on a domain of its own.

## Official plugins

Unikraft Cloud provides these plugins:

| Plugin | Image | Description |
|--------|-------|-------------|
| [Sandbox](/plugins/sandbox) | `plugins/sandbox:latest` | Runs commands, and reads and writes files, inside the instance, for example for `unikraft instance exec` and `unikraft instance shell`. |
| [noVNC](/plugins/novnc) | `plugins/novnc:latest` | Adds a virtual desktop that you use in a browser, and that a program drives through the plugin endpoint. |
| [Network shield](/plugins/netshield) | `plugins/netshield:latest` | Serves the API that sets the policies, secrets, and limits of a [network shield](/features/network-shield). |

Each official plugin has an API reference: [sandbox](/api/plugins/sandbox), [noVNC](/api/plugins/novnc), and [network shield](/api/plugins/netshield).
To build a plugin of your own, use the [Plugin SDK](/sdks/plugin).

## How it works

Each plugin loads from its own [ROM image](/features/roms).
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:

```
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](#authorization) 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](/platform/services).
- **A direct line to one instance.** You talk to a single instance, so no [load balancing](/features/load-balancing) or [autoscale](/features/autoscale) sits in the path, and the instance needs no [service group](/platform/services).
- **Works with scale-to-zero.** When [scale-to-zero](/features/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](#plugin-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`](/cli/unikraft/instances/create) or [`unikraft instance run`](/cli/unikraft/instances/run), once for each plugin:

```bash
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>`](/platform/images#pull-policy-and-fetch-headers) options of the image.
The CLI sends these in the `plugins` field of a [`POST /instances`](/api/platform/v1/instances#create-instance) 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:

| Field | Required | `--plugin` | Description |
|-------|----------|------------|-------------|
| `name` | Yes | Yes | The plugin name. It becomes the `<plugin_name>` segment in the plugin endpoint. See [Plugin names](#plugin-names) for the allowed format. |
| `image` | Yes | Yes | The plugin's ROM image, given as an image reference string such as `user/myplugin:latest`, or as an [image object](/platform/images#pull-policy-and-fetch-headers) with a `url` and optional `headers` and `pull_policy`; the [deprecated `rom` field](#the-deprecated-rom-field) still works in its place. |
| `config` | No | Yes | Arbitrary JSON that the platform passes to the plugin's `init` on `STDIN`. Any JSON value works, including a string, a number, or an object. |
| `authorization` | No | No | How the platform authorizes requests to this plugin, as described under [authorization](#authorization). |
| `services` | No | No | The services that the plugin publishes, as described under [plugin services](#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:

```json title="Response"
{
  "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.

| Field | Required | Description |
|-------|----------|-------------|
| `type` | Yes | Either `userdb` or `bearer`. |
| `token` | For `bearer` | The 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`](/cli/unikraft/api), because the `--plugin` flag can't set `authorization`:

```bash
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:

```bash
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](#changing-plugins-on-an-existing-instance) 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:

```
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`](/cli/unikraft/api), because the `--plugin` flag can't set `services`:

```bash
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:

| Field | Required | Description |
|-------|----------|-------------|
| `port` | Yes | The public port on the plugin's domain. |
| `destination_port` | Yes | The port inside the instance that the plugin listens on. |
| `handlers` | Yes | The [handlers](/platform/services#handlers) of the port, with the same port rules as other services but without `redirect`. |

:::caution
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](/features/roms#rom-names-and-mount-points) of the same instance.

The name of a plugin that publishes [services](#plugin-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`](/cli/unikraft/instances/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:

```bash
# 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`](/api/platform/v1/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.

:::caution
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`](/cli/unikraft/api).
:::

## Reloading across the instance lifecycle

The platform treats plugins like [ROMs](/features/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](/features/branching), restore a [checkpoint](/features/checkpoints), [fork](/features/forking) an instance, or start from an [on-demand template](/features/on-demand-templates).

## Use cases

The first use case for plugins is a native sandbox.
The [sandbox plugin](/plugins/sandbox) 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](/platform/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](#plugin-services).

## 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](/platform/services).

## Learn more

* [Plugin SDK](/sdks/plugin): build a plugin in Go without hand-rolling the platform contract.
* [Sandboxes](/use-cases/sandboxes): run untrusted code in a hardware-isolated microVM.
* [ROMs](/features/roms): the image format that plugins build on.
* [Instances](/platform/instances): create and manage the instances that host plugins.
* [Scale-to-zero](/features/scale-to-zero): how an idle instance wakes to serve a plugin request.
* [Services](/platform/services): the regular way to expose an HTTP server, for contrast with the authenticated plugin endpoint.
* Unikraft Cloud's [REST API reference](/api/platform/v1), in particular the [create instance endpoint](/api/platform/v1/instances#create-instance).
