server

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2026 License: Apache-2.0 Imports: 20 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package server is the ota release server: it chooses which publisher-signed offer each client sees, streams artifacts, and records what clients report. It holds no signing key and cannot create or alter an offer.

Index

Constants

View Source
const (
	DefaultHaltThreshold   = 0.05
	DefaultHaltMinAttempts = 50
)

Defaults for a rollout that does not set its own halt rule.

View Source
const (
	ReleaseIngested = "ingested"
	ReleaseRejected = "rejected"
)

Release status values.

View Source
const (
	RolloutActive   = "active"
	RolloutHalted   = "halted"
	RolloutComplete = "complete"
)

Rollout states.

View Source
const OfferAssetPrefix = "ota-offer"

OfferAssetPrefix names the offer files in a release. A release may carry several (the original and later rollback offers), each with its signature beside it as <name>.sigstore.json or <name>.sig.

Variables

View Source
var ErrNoRelease = errors.New("server: no ingested signed offer for that version")

ErrNoRelease reports a version with no ingested signed offer.

View Source
var ErrNotFound = errors.New("server: artifact not found")

ErrNotFound reports an unknown artifact.

Functions

func Bucket

func Bucket(program, channel, clientID string) int

Bucket places a client in 0–99 for a program and channel. Stable, so a client stays in or out of a rollout across checks, and independent per channel, so the same clients are not always first.

func ServeBlob

func ServeBlob(w http.ResponseWriter, r *http.Request, blobs BlobStore, sum string, opts BlobOptions)

ServeBlob streams one content-addressed artifact.

Exported so a host route that addresses artifacts differently (an installer asking by platform, say) serves the bytes the same way, with the ETag that lets a download resume and the compression a whole-file request gets.

Types

type Asset

type Asset struct {
	Name string
	Size int64
	Open func(ctx context.Context) (io.ReadCloser, error)
}

Asset is one downloadable file of a release.

type BlobOptions

type BlobOptions struct {
	// Name, when set, is offered as the download filename. The artifact route
	// leaves it empty: an agent asked for a hash and stores it under one.
	Name string
	// Header is applied before anything is written, for a caller that wants to
	// say where the bytes came from.
	Header map[string]string
	// Log receives stream interruptions. Optional.
	Log *slog.Logger
}

BlobOptions tunes ServeBlob for a caller that is not the artifact route.

type BlobStore

type BlobStore interface {
	Open(ctx context.Context, sha256 string) (io.ReadSeekCloser, time.Time, error)
	// Put stores r under sha256, refusing bytes that do not hash to it.
	Put(ctx context.Context, sha256 string, r io.Reader) error
}

BlobStore holds artifact bytes keyed by SHA-256.

type ClientRecord

type ClientRecord struct {
	ClientID       string      `json:"client_id"`
	Program        string      `json:"program"`
	OS             string      `json:"os"`
	Arch           string      `json:"arch"`
	Channel        string      `json:"channel,omitempty"`
	RunningVersion string      `json:"running_version"`
	LastSeen       time.Time   `json:"last_seen"`
	LastReport     *ota.Report `json:"last_report,omitempty"`
}

ClientRecord is the latest the server knows about one installation.

type FSBlobStore

type FSBlobStore struct {
	Dir string
}

FSBlobStore keeps artifacts as files named by their hash.

func (FSBlobStore) Open

Open implements BlobStore.

func (FSBlobStore) Put

func (s FSBlobStore) Put(_ context.Context, sum string, r io.Reader) error

Put implements BlobStore. The file only appears under its final name once its hash has been checked, so a reader never sees a partial or wrong blob.

type FileStore

type FileStore struct {
	*MemStore
	// contains filtered or unexported fields
}

FileStore is a MemStore saved to one JSON file after every change, for the standalone server. It is not encrypted: reports and client records link an installation to what it runs, so keep the directory private, or implement Store over an encrypted database for multi-tenant use.

func OpenFileStore

func OpenFileStore(dir string) (*FileStore, error)

OpenFileStore loads (or creates) the store at <dir>/state.json.

type IngestResult

type IngestResult struct {
	Ingested int      `json:"ingested"`
	Rejected int      `json:"rejected"`
	Known    int      `json:"known"`
	Errors   []string `json:"errors,omitempty"`
}

IngestResult summarises one ingest pass.

type MemStore

type MemStore struct {
	// contains filtered or unexported fields
}

MemStore keeps everything in memory. FileStore persists it.

func NewMemStore

func NewMemStore() *MemStore

NewMemStore returns an empty MemStore.

func (*MemStore) AddReports

func (m *MemStore) AddReports(_ context.Context, rs []ota.Report) error

AddReports implements Store.

func (*MemStore) Clients

func (m *MemStore) Clients(_ context.Context, program string) ([]ClientRecord, error)

Clients implements Store.

func (*MemStore) DeletePin

func (m *MemStore) DeletePin(_ context.Context, program, clientID string) error

DeletePin implements Store.

func (*MemStore) GetPin

func (m *MemStore) GetPin(_ context.Context, program, clientID string) (*Pin, error)

GetPin implements Store.

func (*MemStore) Outcomes

func (m *MemStore) Outcomes(_ context.Context, program, version string) (Outcomes, error)

Outcomes implements Store.

func (*MemStore) PutOffer

func (m *MemStore) PutOffer(so ota.SignedOffer) error

PutOffer ingests a signed offer directly, without verifying it. It is for tests and for hosts that verify elsewhere; servers ingest through Server.

func (*MemStore) PutPin

func (m *MemStore) PutPin(_ context.Context, p Pin) error

PutPin implements Store.

func (*MemStore) PutRelease

func (m *MemStore) PutRelease(_ context.Context, r Release) error

PutRelease implements Store, merging offers into an existing release.

func (*MemStore) PutRollout

func (m *MemStore) PutRollout(_ context.Context, r Rollout) error

PutRollout implements Store; one rollout per program and channel.

func (*MemStore) Releases

func (m *MemStore) Releases(_ context.Context, program string) ([]Release, error)

Releases implements Store.

func (*MemStore) Rollouts

func (m *MemStore) Rollouts(_ context.Context, program string) ([]Rollout, error)

Rollouts implements Store.

func (*MemStore) TouchClient

func (m *MemStore) TouchClient(_ context.Context, c ClientRecord) error

TouchClient implements Store.

type Options

type Options struct {
	Store Store
	Blobs BlobStore
	// Verifier checks every offer before it is stored. Required for
	// ingest and admin uploads; a server that only serves pre-verified offers
	// from its Store may leave it nil.
	Verifier verify.Verifier
	// Source is where releases are ingested from; optional.
	Source Source
	// Targeter, if set, decides per client in place of pins and rollouts.
	Targeter Targeter
	// Authorize admits a client request and may return the caller's client
	// id, which then replaces whatever id the request body claims, so a client
	// cannot report or check as another. Nil admits everything, which is only
	// appropriate behind a host that already authenticates.
	Authorize func(*http.Request) (clientID string, err error)
	// AuthorizeAdmin admits admin requests. Nil disables the admin API.
	AuthorizeAdmin func(*http.Request) error
	// HTTPClient fetches artifacts during ingest. Defaults to a client with
	// no overall timeout (artifacts are large); ingest uses context deadlines.
	HTTPClient *http.Client
	Logger     *slog.Logger
	Now        func() time.Time
}

Options configures a Server.

type Outcomes

type Outcomes struct {
	Attempted int
	Failed    int
}

Outcomes counts distinct clients per outcome for one version, which is what auto-halt decides on.

type Pin

type Pin struct {
	Program  string `json:"program"`
	ClientID string `json:"client_id"`
	Version  string `json:"version"`
}

Pin fixes one client to one version.

type Release

type Release struct {
	Program    string        `json:"program"`
	Version    string        `json:"version"`
	Tag        string        `json:"tag,omitempty"`
	Source     string        `json:"source,omitempty"`
	Status     string        `json:"status"`
	Reason     string        `json:"reason,omitempty"`
	IngestedAt time.Time     `json:"ingested_at"`
	Offers     []StoredOffer `json:"offers"`
}

Release is a published version as the server knows it.

type ReleaseView

type ReleaseView struct {
	Program, Version, Tag, Source, Status, Reason string
	Offers                                        int
}

ReleaseView is one release as GET /v1/admin/releases lists it. Offer bytes are for clients; the admin view says what was decided and why. Untagged, so its keys are the field names.

type Rollout

type Rollout struct {
	Program         string    `json:"program"`
	Channel         string    `json:"channel"`
	Version         string    `json:"version"`
	Percent         int       `json:"percent"`
	HaltThreshold   float64   `json:"halt_threshold"`
	HaltMinAttempts int       `json:"halt_min_attempts"`
	State           string    `json:"state"`
	Reason          string    `json:"reason,omitempty"`
	UpdatedAt       time.Time `json:"updated_at"`
}

Rollout offers a version to a share of a channel.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server serves the /v1 API.

func New

func New(opts Options) (*Server, error)

New returns a Server.

func (*Server) AddOffer

func (s *Server) AddOffer(ctx context.Context, so ota.SignedOffer, source, tag string) (Release, error)

AddOffer verifies and stores one signed offer, fetching its artifacts from the URLs it lists. It is the admin upload path and the rollback path: a publisher-signed rollback offer can be posted without touching releases.

func (*Server) Handler

func (s *Server) Handler() http.Handler

Handler returns the API. Mount it at the root of a host's router or under a prefix with http.StripPrefix.

func (*Server) Ingest

func (s *Server) Ingest(ctx context.Context) (IngestResult, error)

Ingest pulls every release from the Source and stores each offer that verifies, with its artifacts copied into Blobs after their hash is checked. An offer that fails is recorded as rejected, with the reason, and never served.

func (*Server) SetPin

func (s *Server) SetPin(ctx context.Context, p Pin) error

SetPin validates and stores a pin against a signed version.

func (*Server) SetRollout

func (s *Server) SetRollout(ctx context.Context, ro Rollout) error

SetRollout validates and stores a rollout. The version must have a signed offer admitting the channel, so a rollout can never point at nothing.

type Source

type Source interface {
	Name() string
	Releases(ctx context.Context) ([]SourceRelease, error)
}

Source lists published releases, for example a project's GitHub Releases. Implementations live in server/source/*.

type SourceRelease

type SourceRelease struct {
	Tag    string
	Assets []Asset
}

SourceRelease is one tagged release and its downloadable files.

type Store

type Store interface {
	PutRelease(ctx context.Context, r Release) error
	Releases(ctx context.Context, program string) ([]Release, error)

	PutRollout(ctx context.Context, r Rollout) error
	Rollouts(ctx context.Context, program string) ([]Rollout, error)

	PutPin(ctx context.Context, p Pin) error
	DeletePin(ctx context.Context, program, clientID string) error
	GetPin(ctx context.Context, program, clientID string) (*Pin, error)

	TouchClient(ctx context.Context, c ClientRecord) error
	Clients(ctx context.Context, program string) ([]ClientRecord, error)

	// AddReports stores reports, ignoring exact repeats (same client,
	// version, offer and stage), and updates each client's record.
	AddReports(ctx context.Context, rs []ota.Report) error
	Outcomes(ctx context.Context, program, version string) (Outcomes, error)
}

Store persists what the server decides from. The server holds no key and stores offers only as the bytes the publisher signed. Hosts implement it over their own database; MemStore and FileStore are included.

Reports and client records link an installation to what it runs; a host whose installations belong to organisations should encrypt them at rest.

type StoredOffer

type StoredOffer struct {
	Signed   ota.SignedOffer `json:"signed"`
	IssuedAt int64           `json:"issued_at"`
	// ExpiresAt is copied from the offer so an expired one is never served;
	// clients would refuse it anyway, on every check.
	ExpiresAt int64    `json:"expires_at,omitempty"`
	Channels  []string `json:"channels,omitempty"`
	// Digest identifies the offer bytes, so re-ingesting is a no-op.
	Digest string `json:"digest"`
}

StoredOffer is one publisher-signed offer, kept byte for byte.

type Target

type Target struct {
	// Deny offers nothing.
	Deny bool
	// Version, if set, is offered regardless of rollouts (a host-side pin).
	Version string
	// Channel, if set, replaces the client's claimed channel for rollouts.
	Channel string
}

Target is a host's decision for one client. Returned with ok=false from a Targeter, the built-in rules decide instead.

type Targeter

type Targeter interface {
	Target(ctx context.Context, req ota.CheckRequest) (Target, bool, error)
}

Targeter lets a host decide who gets what (for example per organisation), in place of built-in pins and rollouts. It can only choose among signed offers; it cannot create one.

Directories

Path Synopsis
source
httpindex
Package httpindex reads releases from a static JSON index, which any host can publish: GitLab or Gitea release pages, an S3 bucket, a plain file server.
Package httpindex reads releases from a static JSON index, which any host can publish: GitLab or Gitea release pages, an S3 bucket, a plain file server.
github module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL