server

package
v0.0.0-...-5eca219 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2024 License: BSD-3-Clause Imports: 42 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultMaxModules        = 64
	DefaultMaxProcs          = 4
	DefaultTotalStorageSize  = 256 * 1024 * 1024
	DefaultTotalResidentSize = 64 * 1024 * 1024
	DefaultMaxModuleSize     = 32 * 1024 * 1024
	DefaultMaxTextSize       = 16 * 1024 * 1024
	DefaultMaxMemorySize     = 32 * 1024 * 1024
	DefaultStackSize         = wa.PageSize
	DefaultTimeResolution    = time.Second / 100
)

Variables

View Source
var ErrServerClosed = errors.New("server closed")

Functions

func ErrorEventLogger

func ErrorEventLogger(errorLog, eventLog Logger) func(*event.Event, error)

ErrorEventLogger creates an event monitor which prints log messages. Internal errors are printed to errorLog and other events to eventLog.

func ErrorLogger

func ErrorLogger(errorLog Logger) func(*event.Event, error)

ErrorLogger creates an event monitor which prints log messages. Internal errors are printed to errorLog and other events are ignored.

func MultiMonitor

func MultiMonitor(monitors ...func(*event.Event, error)) func(*event.Event, error)

MultiMonitor combines multiple event monitors.

func PermissionDenied

func PermissionDenied(internalDetails string) error

PermissionDenied error. The details are not exposed to the client.

func RetryAfter

func RetryAfter(t time.Time) error

RetryAfter creates a TooManyRequests error with the earliest time when the request should be retried.

func Unauthenticated

func Unauthenticated(publicReason string) error

Unauthenticated error. The reason will be shown to the client.

func Unavailable

func Unavailable(internal error) error

Unavailable service error. The details are not exposed to the client.

Types

type AccessConfig

type AccessConfig struct {
	ResourcePolicy
	ProgramPolicy
	InstancePolicy
}

AccessConfig utility for Authorizer implementations. InstancePolicy.Services must be set explicitly, other fields have defaults.

func (*AccessConfig) ConfigureInstance

func (config *AccessConfig) ConfigureInstance(p *InstancePolicy)

func (*AccessConfig) ConfigureProgram

func (config *AccessConfig) ConfigureProgram(p *ProgramPolicy)

func (*AccessConfig) ConfigureResource

func (config *AccessConfig) ConfigureResource(p *ResourcePolicy)

type Authorizer

type Authorizer interface {
	Authorize(context.Context) (context.Context, error)
	AuthorizeProgram(context.Context, *ResourcePolicy, *ProgramPolicy) (context.Context, error)
	AuthorizeProgramSource(context.Context, *ResourcePolicy, *ProgramPolicy, string) (context.Context, error)
	AuthorizeInstance(context.Context, *ResourcePolicy, *InstancePolicy) (context.Context, error)
	AuthorizeProgramInstance(context.Context, *ResourcePolicy, *ProgramPolicy, *InstancePolicy) (context.Context, error)
	AuthorizeProgramInstanceSource(context.Context, *ResourcePolicy, *ProgramPolicy, *InstancePolicy, string) (context.Context, error)
	// contains filtered or unexported methods
}

Authorizer and moderator of server access.

The methods should return Unauthenticated, PermissionDenied or Unavailable errors to signal successful prevention of access. Other types of errors are interpreted as failures of the authorization mechanism. Returning a nil error grants access.

An implementation should adjust the ResourcePolicy, ProgramPolicy and InstancePolicy objects' fields. The limits are enforced automatically by the server, which may also lead to denial of access.

Principal id can be obtained using the principal.ContextID(context.Context) function. If it is nil, the request didn't contain credentials, and the access should be denied unless the policy allows anonymous access. If the principal id is non-nil, it should be checked unless the policy allows access to everyone.

An implementation may choose to discriminate based on server operation type. It can be obtained using the ContextOp(context.Context) function.

Authorizer may be expanded with new methods (prefixed with the Authorize namespace) also between major releases. Implementations must inherit methods from a concrete access authorization type, and must not add unrelated methods with the Authorize prefix to avoid breakage. The conservative choice is to inherit from NoAccess. That way, new functionality will be effectively disabled.

type Config

type Config struct {
	ImageStorage   image.Storage
	Inventory      Inventory
	ProcessFactory runtime.ProcessFactory
	AccessPolicy   Authorizer
	ModuleSources  map[string]Source
	Monitor        func(*event.Event, error)
	OpenDebugLog   func(string) io.WriteCloser
}

func (*Config) Configured

func (c *Config) Configured() bool

type Instance

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

func (*Instance) Connect

func (inst *Instance) Connect(ctx context.Context, r io.Reader, w io.WriteCloser) error

Connect to a running instance. Disconnection happens when context is canceled, the instance stops running, or the program closes the connection.

func (*Instance) ID

func (inst *Instance) ID() string

func (*Instance) Kill

func (inst *Instance) Kill(ctx context.Context) error

func (*Instance) Status

func (inst *Instance) Status(ctx context.Context) *api.Status

func (*Instance) Suspend

func (inst *Instance) Suspend(ctx context.Context) error

Suspend the instance and make it non-transient.

func (*Instance) Wait

func (inst *Instance) Wait(ctx context.Context) (status *api.Status)

type InstanceConnector

type InstanceConnector interface {
	// Connect allocates a new I/O stream.  The returned function is to be used
	// to transfer data between a network connection and the instance.  If it's
	// non-nil, a connection was established.
	Connect(context.Context) func(context.Context, io.Reader, io.WriteCloser) error

	// Close causes currently blocked and future Connect calls to return nil.
	// Established connections will not be closed.
	Close() error
}

type InstancePolicy

type InstancePolicy struct {
	MaxMemorySize  int           // Linear memory growth limit.
	StackSize      int           // Including system/runtime overhead.
	TimeResolution time.Duration // Granularity of time functions.

	// Services function defines which services are discoverable by the
	// instance.
	Services func(context.Context) InstanceServices
}

type InstanceServices

type InstanceServices interface {
	InstanceConnector
	runtime.ServiceRegistry
}

type Inventory

type Inventory interface {
	GetSourceModule(ctx context.Context, source string) (module string, err error)
	AddModuleSource(ctx context.Context, module, source string) error
}

type Logger

type Logger interface {
	Printf(string, ...any)
}

type NoAccess

type NoAccess struct{}

NoAccess permitted to any resource.

func (NoAccess) Authorize

func (NoAccess) Authorize(ctx context.Context) (context.Context, error)

func (NoAccess) AuthorizeInstance

func (NoAccess) AuthorizeInstance(ctx context.Context, _ *ResourcePolicy, _ *InstancePolicy) (context.Context, error)

func (NoAccess) AuthorizeProgram

func (NoAccess) AuthorizeProgram(ctx context.Context, _ *ResourcePolicy, _ *ProgramPolicy) (context.Context, error)

func (NoAccess) AuthorizeProgramInstance

func (NoAccess) AuthorizeProgramInstance(ctx context.Context, _ *ResourcePolicy, _ *ProgramPolicy, _ *InstancePolicy) (context.Context, error)

func (NoAccess) AuthorizeProgramInstanceSource

func (NoAccess) AuthorizeProgramInstanceSource(ctx context.Context, _ *ResourcePolicy, _ *ProgramPolicy, _ *InstancePolicy, _ string) (context.Context, error)

func (NoAccess) AuthorizeProgramSource

func (NoAccess) AuthorizeProgramSource(ctx context.Context, _ *ResourcePolicy, _ *ProgramPolicy, _ string) (context.Context, error)

type ProgramPolicy

type ProgramPolicy struct {
	MaxModuleSize int // WebAssembly module size.
	MaxTextSize   int // Native program code size.
	MaxStackSize  int // Suspended stack size.
}

type PublicAccess

type PublicAccess struct {
	AccessConfig
}

PublicAccess authorization for everyone, including anonymous requests. Configurable resource limits.

func NewPublicAccess

func NewPublicAccess(services func(context.Context) InstanceServices) *PublicAccess

func (*PublicAccess) Authorize

func (*PublicAccess) Authorize(ctx context.Context) (context.Context, error)

func (*PublicAccess) AuthorizeInstance

func (a *PublicAccess) AuthorizeInstance(ctx context.Context, res *ResourcePolicy, inst *InstancePolicy) (context.Context, error)

func (*PublicAccess) AuthorizeProgram

func (a *PublicAccess) AuthorizeProgram(ctx context.Context, res *ResourcePolicy, prog *ProgramPolicy) (context.Context, error)

func (*PublicAccess) AuthorizeProgramInstance

func (a *PublicAccess) AuthorizeProgramInstance(ctx context.Context, res *ResourcePolicy, prog *ProgramPolicy, inst *InstancePolicy) (context.Context, error)

func (*PublicAccess) AuthorizeProgramInstanceSource

func (a *PublicAccess) AuthorizeProgramInstanceSource(ctx context.Context, res *ResourcePolicy, prog *ProgramPolicy, inst *InstancePolicy, _ string) (context.Context, error)

func (*PublicAccess) AuthorizeProgramSource

func (a *PublicAccess) AuthorizeProgramSource(ctx context.Context, res *ResourcePolicy, prog *ProgramPolicy, _ string) (context.Context, error)

type ResourcePolicy

type ResourcePolicy struct {
	MaxModules        int // Pinned module limit.
	MaxProcs          int // Active instance limit.
	TotalStorageSize  int // Sum of pinned module and metadata sizes.
	TotalResidentSize int // Sum of all memory mapping and buffer sizes.
}

TODO: ResourcePolicy is not yet enforced by server

type Server

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

func New

func New(ctx context.Context, config *Config) (_ *Server, err error)

func (*Server) DebugInstance

func (s *Server) DebugInstance(ctx context.Context, instance string, req *api.DebugRequest) (_ *api.DebugResponse, err error)

func (*Server) DeleteInstance

func (s *Server) DeleteInstance(ctx context.Context, instance string) (err error)

func (*Server) Features

func (s *Server) Features() *api.Features

func (*Server) InstanceConnection

func (s *Server) InstanceConnection(ctx context.Context, instance string) (
	_ api.Instance,
	_ func(context.Context, io.Reader, io.WriteCloser) error,
	err error,
)

func (*Server) InstanceInfo

func (s *Server) InstanceInfo(ctx context.Context, instance string) (_ *api.InstanceInfo, err error)

func (*Server) Instances

func (s *Server) Instances(ctx context.Context) (_ *api.Instances, err error)

func (*Server) KillInstance

func (s *Server) KillInstance(ctx context.Context, instance string) (_ api.Instance, err error)

func (*Server) ModuleContent

func (s *Server) ModuleContent(ctx context.Context, module string) (stream io.ReadCloser, length int64, err error)

func (*Server) ModuleInfo

func (s *Server) ModuleInfo(ctx context.Context, module string) (_ *api.ModuleInfo, err error)

func (*Server) Modules

func (s *Server) Modules(ctx context.Context) (_ *api.Modules, err error)

func (*Server) NewInstance

func (s *Server) NewInstance(ctx context.Context, module string, launch *api.LaunchOptions) (_ api.Instance, err error)

func (*Server) PinModule

func (s *Server) PinModule(ctx context.Context, module string, know *api.ModuleOptions) (err error)

func (*Server) ResumeInstance

func (s *Server) ResumeInstance(ctx context.Context, instance string, resume *api.ResumeOptions) (_ api.Instance, err error)

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

func (*Server) Snapshot

func (s *Server) Snapshot(ctx context.Context, instance string, know *api.ModuleOptions) (module string, err error)

func (*Server) SourceModule

func (s *Server) SourceModule(ctx context.Context, uri string, know *api.ModuleOptions) (module string, err error)

func (*Server) SourceModuleInstance

func (s *Server) SourceModuleInstance(ctx context.Context, uri string, know *api.ModuleOptions, launch *api.LaunchOptions) (module string, _ api.Instance, err error)

func (*Server) SuspendInstance

func (s *Server) SuspendInstance(ctx context.Context, instance string) (_ api.Instance, err error)

func (*Server) UnpinModule

func (s *Server) UnpinModule(ctx context.Context, module string) (err error)

func (*Server) UpdateInstance

func (s *Server) UpdateInstance(ctx context.Context, instance string, update *api.InstanceUpdate) (_ *api.InstanceInfo, err error)

func (*Server) UploadModule

func (s *Server) UploadModule(ctx context.Context, upload *api.ModuleUpload, know *api.ModuleOptions) (module string, err error)

func (*Server) UploadModuleInstance

func (s *Server) UploadModuleInstance(ctx context.Context, upload *api.ModuleUpload, know *api.ModuleOptions, launch *api.LaunchOptions) (_ string, _ api.Instance, err error)

func (*Server) WaitInstance

func (s *Server) WaitInstance(ctx context.Context, instID string) (_ *api.Status, err error)

type Source

type Source interface {
	// CanonicalURI converts a source URI to its canonical form.  The result
	// should be byte-wise identical to all other canonicalized URIs which
	// refer to the same location.
	//
	// CanonicalURI is called with an absolute URI which doesn't contain
	// successive slashes.  It starts with the source name (e.g. "/foo/...").
	//
	// If the URI is know to be invalid, an error should be returned.
	CanonicalURI(uri string) (string, error)

	// OpenURI for reading an object.  The argument is a URI returned by
	// CanonicalizeURI.
	//
	// If the object's size exceeds maxSize, the object is not to be opened.
	// The reader is not necessarily drained, but it will be closed.  The
	// reader must produce exactly contentLength's worth of bytes when read in
	// full.
	//
	// Not-found condition can be signaled by returning nil content with zero
	// length.  Content-too-long condition can be signaled by returning nil
	// content with nonzero length (doesn't have to be actual content length).
	OpenURI(
		ctx context.Context,
		uri string,
		maxSize int,
	) (
		content io.ReadCloser,
		contentLength int64,
		err error,
	)
}

Source of immutable data.

Directories

Path Synopsis
api
pb
sql
Package sql implements Inventory and NonceChecker backed by SQL database.
Package sql implements Inventory and NonceChecker backed by SQL database.
pb
web
api
Package api contains definitions useful for accessing the HTTP and websocket APIs.
Package api contains definitions useful for accessing the HTTP and websocket APIs.
api/debug
Package debug contains functionality useful for accessing the instance debugging API via HTTP.
Package debug contains functionality useful for accessing the instance debugging API via HTTP.

Jump to

Keyboard shortcuts

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