> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tinfoil.sh/llms.txt
> Use this file to discover all available pages before exploring further.

# Custom Bunker images

> Deploy a container from your own image with the Admin API Go SDK, then verify its attestation and the shape of its config before sending it anything.

> [!WARNING]
>
> This tutorial uses [V3 attestation](https://docs.tinfoil.sh/resources/feature-status.md), which is in beta. Inline configs also require config deployments to be enabled for your organization; contact [support@tinfoil.sh](mailto:support@tinfoil.sh) to turn them on.

A [Bunker](https://docs.tinfoil.sh/bunkers/quickstart.md) runs an image that Tinfoil builds and publishes. To run your own image in an enclave instead, deploy it as a container from a config you write, then verify the result yourself: that the hardware attests to your image, and that the config it runs has the shape you expect.

This tutorial does both from one Go program. It creates the container with the [Admin API](https://docs.tinfoil.sh/admin/admin-api.md) Go SDK and verifies it with the [Tinfoil Go SDK](https://docs.tinfoil.sh/sdk/go-sdk.md).

## Prerequisites

* An [admin API key](https://docs.tinfoil.sh/admin/get-admin-key.md) with `containers.create` and `containers.read`, exported as `TINFOIL_ADMIN_KEY`.
* Your image pinned by `@sha256:` digest. For a private image, set [registry credentials](https://docs.tinfoil.sh/containers/private-images.md) first.
* Go 1.27.4 or later.

## Set up a module

```bash theme={"dark"}
mkdir my-agent && cd my-agent
go mod init my-agent
go get github.com/tinfoilsh/admin-sdk/go github.com/tinfoilsh/tinfoil-config@v0.3.0 github.com/tinfoilsh/tinfoil-go@v0.17.0-rc.14
```

## Write the config

Save this as `tinfoil-config.yml` next to your program, with your image in place of the demo image:

```yaml tinfoil-config.yml theme={"dark"}
cvm-version: 0.15.5-rc.3@sha256:2aec25a363b4eedc87d0d813d4cdf63885d2efbe13cbef87499afe2ab4d09454
cpus: 2
memory: 8192

security:
  include-config-in-attestation: true

containers:
  - name: "agent"
    image: "ghcr.io/tinfoilsh/tinfoil-containers-hello-world@sha256:c943b1cab16409e2e686ad71deab30364c27421852cdd6b3080df1c7a38331ed"
    env:
      - PUBLIC_KEY: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIF3o0LZd6zMES3hfL82ogkdrxFKUVmW/9habQ86C8yD"

shim:
  upstream-port: 8080
  paths:
    - /*
```

An inline config pins `cvm-version` to the SHA-256 of the runtime manifest, which you can compute yourself:

```bash theme={"dark"}
curl -fsSL https://images.tinfoil.sh/cvm/tinfoil-inference-v0.15.5-rc.3-manifest.json | sha256sum
```

`PUBLIC_KEY` parameterizes the image with your public key; replace the demo key with your own. Anyone can run the same image digest with a different key, so the key in the config is what makes the container yours.

`include-config-in-attestation` puts the config itself into the attestation so the client can read it. Without it, the config stays out of the attestation and a client can only check it against bytes it already holds. See the [configuration reference](https://docs.tinfoil.sh/containers/configuration.md) for the other fields.

## Deploy and verify

```go main.go theme={"dark"}
package main

import (
	"context"
	_ "embed"
	"encoding/base64"
	"errors"
	"fmt"
	"log"
	"os"
	"slices"
	"time"

	"github.com/tinfoilsh/admin-sdk/go/apis"
	"github.com/tinfoilsh/admin-sdk/go/models"
	"github.com/tinfoilsh/admin-sdk/go/runtime"
	tinfoilconfig "github.com/tinfoilsh/tinfoil-config"
	"github.com/tinfoilsh/tinfoil-config/yamlconfig"
	"github.com/tinfoilsh/tinfoil-go"
	"github.com/tinfoilsh/tinfoil-go/verify/document/collateral"
)

const (
	apiURL        = "https://api.tinfoil.sh"
	containerName = "my-agent"
	image         = "ghcr.io/tinfoilsh/tinfoil-containers-hello-world@sha256:c943b1cab16409e2e686ad71deab30364c27421852cdd6b3080df1c7a38331ed"
	publicKeyEnv  = "PUBLIC_KEY"
	publicKey     = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIF3o0LZd6zMES3hfL82ogkdrxFKUVmW/9habQ86C8yD"
	pollInterval  = 10 * time.Second
)

//go:embed tinfoil-config.yml
var config []byte

func main() {
	ctx := context.Background()
	client := runtime.NewClient(apiURL, runtime.WithAuth(runtime.BearerAuth{Token: os.Getenv("TINFOIL_ADMIN_KEY")}))

	domain, err := deploy(ctx, apis.NewContainersAPI(client))
	if err != nil {
		log.Fatal(err)
	}

	enclave, err := tinfoil.NewEnclaveHTTPClient(ctx, domain, tinfoil.AuthorizedBy{Authorize: requireShape}, nil)
	if err != nil {
		log.Fatal(err)
	}
	attested, _ := enclave.CurrentVerification().VerifiedFacts.Config()
	fmt.Printf("%s runs %s with config sha256:%s\n", domain, image, attested.Digest)
}

func deploy(ctx context.Context, containers *apis.ContainersAPI) (string, error) {
	source := "local"
	encoded := base64.StdEncoding.EncodeToString(config)
	created, err := containers.CreateContainer(ctx, &models.ContainerCreateRequest{Name: containerName, Source: &source, Config: &encoded})
	if err != nil {
		return "", err
	}
	for {
		current, err := containers.GetContainer(ctx, *created.Status201.Id)
		if err != nil {
			return "", err
		}
		switch current.Status200.Status {
		case "running":
			return *current.Status200.Domain, nil
		case "failed":
			return "", errors.New("deployment failed")
		}
		time.Sleep(pollInterval)
	}
}

func requireShape(attested collateral.ConfigEndorsement) error {
	config, err := yamlconfig.Decode(attested.Config, tinfoilconfig.Options{})
	if err != nil {
		return err
	}
	if len(config.Containers) != 1 || config.Containers[0].Image != image {
		return fmt.Errorf("config must run only %s", image)
	}
	if config.Containers[0].CVMAdmin || len(config.CVMNetwork.InboundPorts) != 0 {
		return errors.New("config must not grant administrator access")
	}
	var entry map[string]any
	if env := config.Containers[0].Env; len(env) == 1 {
		entry, _ = env[0].(map[string]any)
	}
	if entry[publicKeyEnv] != publicKey || slices.Contains(config.Containers[0].Secrets, publicKeyEnv) {
		return fmt.Errorf("config must set only %s to your public key", publicKeyEnv)
	}
	return nil
}
```

Run it:

```bash theme={"dark"}
go run .
```

The program does three things:

1. **Deploy.** `deploy` sends the config inline (`source: local`) and polls the container until it is `running`. Tinfoil stores the config in its canonical JSON form, which is the form the enclave measures.
2. **Verify the attestation.** `tinfoil.NewEnclaveHTTPClient` fetches a fresh attestation from the container's domain and checks the hardware report, the runtime that `cvm-version` pins, and that the config in the attestation is the one that was measured. `AuthorizedBy` hands that config to your function instead of requiring a registry endorsement.
3. **Check the shape.** `requireShape` rejects the enclave unless its config runs only your image, by digest, with your public key as its only environment variable and no administrator access through `cvm_admin` or [direct admin SSH](https://docs.tinfoil.sh/containers/config-networking.md#direct-admin-ssh). The key must be a literal in `env`, not a name filled in at deploy time or a secret, since neither is in the attestation. Add checks for anything else you rely on, such as networks.

If any check fails, `NewEnclaveHTTPClient` returns an error and the program sends nothing. On success, `enclave` is an `*http.Client` whose requests reach only the verified enclave, and it verifies again when its verification expires.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.