// Package isolatedjob is the provider-neutral face of hosted runner VMs. The // controller launches, watches, suspends and stops VMs through Runner and // never sees a provider type. The AWS Lambda MicroVMs implementation belongs // in awsjob. // // A provider state this package does not know maps to StateUnknown, which is // never live, never terminal and never proof that a VM has stopped. package isolatedjob import ( "context" "errors" "fmt" "net/netip" "slices" "strings" "time" ) // Runner launches and manages isolated job VMs. type Runner interface { Launch(ctx context.Context, s LaunchSpec) (Ref, error) // idempotent on AttemptID Get(ctx context.Context, ref Ref) (Job, error) Suspend(ctx context.Context, ref Ref) error Resume(ctx context.Context, ref Ref) error Stop(ctx context.Context, ref Ref, reason string) error // succeeds if already gone List(ctx context.Context) ([]Job, error) // all VMs the provider holds Limits() Limits } // Ref is the provider's identifier for one VM. type Ref string // MaxRefLen is the longest Ref Valid accepts. const MaxRefLen = 128 // Valid reports whether r is 1 to MaxRefLen letters, digits and '-' (ruling // R36). A provider's ID is checked with it before it is used, stored or // logged, so a malformed reply can never carry other text into the // controller's records. func (r Ref) Valid() bool { if len(r) == 0 || len(r) > MaxRefLen { return false } for i := 0; i < len(r); i++ { switch c := r[i]; { case c >= 'a' && c <= 'z', c >= 'A' && c <= 'Z', c >= '0' && c <= '9', c == '-': default: return false } } return true } // ImageDigest names a certified base image: "sha256:" and 64 lower-case hex // digits. type ImageDigest string // Size is the memory class of a VM. One image version has one size. type Size string const ( SizeSmall Size = "small" // 2 GB SizeLarge Size = "large" // 4 GB ) // MemoryMiB returns the VM memory for s in MiB, or 0 when s is not a known // size. func (s Size) MemoryMiB() int { switch s { case SizeSmall: return 2048 case SizeLarge: return 4096 } return 0 } // State is a VM's lifecycle state in this package's vocabulary. type State string const ( StateStarting State = "starting" StateRunning State = "running" StateSuspending State = "suspending" StateSuspended State = "suspended" StateStopping State = "stopping" StateStopped State = "stopped" StateFailed State = "failed" StateUnknown State = "unknown" ) // Live reports whether the VM may still exist and run or resume work. // Unknown and unrecognised states are not live. func (s State) Live() bool { switch s { case StateStarting, StateRunning, StateSuspending, StateSuspended, StateStopping: return true } return false } // Terminal reports whether the state proves the VM has ended. Unknown and // unrecognised states are not terminal. func (s State) Terminal() bool { switch s { case StateStopped, StateFailed: return true } return false } // HardMaxLifetime is the longest a VM may live, running and suspended time // together, whatever a provider allows. const HardMaxLifetime = 28800 * time.Second // LaunchSpec is one launch request. Launching the same AttemptID twice gives // the same VM. type LaunchSpec struct { AttemptID string Image ImageDigest // must be certified Size Size // SizeSmall or SizeLarge MaxLifetime time.Duration // includes suspended time Bootstrap []byte // the one-time enrolment code only } // Job is one VM as the provider reports it. type Job struct { Ref Ref State State StartedAt time.Time Reason string // ProviderAge is the provider's server time minus the provider's start // time for the VM, both taken from one response. It is never worked out // from this host's clock. AgeKnown is false when the response lacked // either time. ProviderAge time.Duration AgeKnown bool } // Limits are what a provider allows. type Limits struct { MaxLifetime time.Duration MaxBootstrapBytes int SuspendUsesLifetime bool // suspended time counts towards MaxLifetime ConnectionsSurvive bool // open connections survive suspend and resume } var ( // ErrNotFound: the provider holds no VM with this ref. ErrNotFound = errors.New("isolatedjob: not found") // ErrInvalid: the request is malformed or outside the provider's limits. ErrInvalid = errors.New("isolatedjob: invalid request") // ErrConflict: the VM's current state does not allow the operation, or // an attempt ID was reused with a different request. ErrConflict = errors.New("isolatedjob: conflict") // ErrThrottled: the provider asked us to slow down; retry later. ErrThrottled = errors.New("isolatedjob: throttled") // ErrIncomplete: the provider's answer cannot be trusted as whole, for // example a list that could not read every page. ErrIncomplete = errors.New("isolatedjob: incomplete answer") // ErrBootstrapTooLarge: the bootstrap is longer than the provider takes. // Validate returns it instead of ErrInvalid for an oversized bootstrap. ErrBootstrapTooLarge = errors.New("isolatedjob: bootstrap too large") ) // NetworkAuditor reads the provider's network settings for hosted VMs as they // are now, not as they were configured. type NetworkAuditor interface { AuditNetwork(ctx context.Context) (NetworkAudit, error) } // NetworkAudit is one reading of the network hosted VMs launch into. type NetworkAudit struct { ResolverDisabled bool // the provider's managed DNS is off IngressClosed bool // VMs take no inbound connections RouteOutAbsent bool // the VMs' subnet has no internet route EgressOnly []netip.AddrPort // every destination VMs are allowed to reach } // OK returns nil only when the resolver is off, ingress is closed, no route // out exists and EgressOnly holds exactly the destinations in want, in any // order. Otherwise its error names every property that failed. func (a NetworkAudit) OK(want []netip.AddrPort) error { var failed []string if !a.ResolverDisabled { failed = append(failed, "resolver enabled") } if !a.IngressClosed { failed = append(failed, "ingress open") } if !a.RouteOutAbsent { failed = append(failed, "route out present") } extra := missingFrom(a.EgressOnly, want) missing := missingFrom(want, a.EgressOnly) if len(extra) > 0 || len(missing) > 0 { failed = append(failed, fmt.Sprintf("egress differs (extra %v, missing %v)", extra, missing)) } if len(failed) == 0 { return nil } return errors.New("isolatedjob: network audit failed: " + strings.Join(failed, "; ")) } // missingFrom returns the members of have that are not in set. func missingFrom(have, set []netip.AddrPort) []netip.AddrPort { var out []netip.AddrPort for _, p := range have { if !slices.Contains(set, p) && !slices.Contains(out, p) { out = append(out, p) } } return out }