Documentation
¶
Overview ¶
Package fleet lets one box be the master of the owner's other boxes: each member keeps an outbound tunnel to the master, and the master relays an allow-listed set of each box's own API routes so one dashboard sees and acts on all of them.
Index ¶
- Constants
- Variables
- func Allowed(method, path string) (slow, ok bool)
- func CertFingerprint(certFile string) (string, error)
- func Fingerprint(cert *x509.Certificate) string
- func KeyHash(key string) string
- func NewKey() (key, keyHash string, err error)
- func Timeout(slow bool) time.Duration
- func ValidMasterURL(raw string) error
- type Box
- type BoxStatus
- type Code
- type Fleet
- func (f *Fleet) AddBox(name, link string, self bool) (Box, Code, error)
- func (f *Fleet) DisableMaster() error
- func (f *Fleet) EnableMaster(address string) error
- func (f *Fleet) HandleTunnel(w http.ResponseWriter, r *http.Request)
- func (f *Fleet) Join(code string) error
- func (f *Fleet) Leave() error
- func (f *Fleet) MasterStatus() (MasterStatus, bool)
- func (f *Fleet) MemberStatus() (MemberStatus, bool)
- func (f *Fleet) Relay(ctx context.Context, boxID, method, path, query, contentType string, ...) (Response, error)
- func (f *Fleet) RemoveBox(id string) error
- func (f *Fleet) RenameBox(id string, name, link *string) error
- func (f *Fleet) RotateCode(id string) (Code, error)
- func (f *Fleet) Run(ctx context.Context)
- func (f *Fleet) Status() Status
- type GatewayInfo
- type MasterState
- type MasterStatus
- type MemberState
- type MemberStatus
- type Response
- type Status
Constants ¶
const MaxBody = 4 << 20
MaxBody bounds a relayed request or response body, in each direction.
const StateDir = "/var/lib/casaos/fleet"
StateDir holds master.json and member.json, under Root.
Variables ¶
var ( ErrOffline = errors.New("box offline") ErrBusy = errors.New("too many requests in flight for this box") ErrTimeout = errors.New("the box did not answer in time") ErrNotAllowed = errors.New("this route is not relayed") ErrNoBox = errors.New("no such box") ErrMasterOff = errors.New("the master role is off") )
var Default = New("/")
Default is this box's fleet, wired by main.
Functions ¶
func Allowed ¶
Allowed says whether method+path (the path alone, no query) is in the table, and whether it is one of the slow routes. The path is compared as it is: no decoding, no trimming, no case folding, so the table cannot be reached around. A path carrying a character a URL reads as a delimiter or an escape (? # % \ or a control byte) is refused outright: an {id} that held one would turn a row of the table into another route once a request is built from it.
func CertFingerprint ¶
CertFingerprint is the fingerprint of the first certificate in a PEM file.
func Fingerprint ¶
func Fingerprint(cert *x509.Certificate) string
Fingerprint is the sha256 of a certificate as sent, hex: what a code pins.
func ValidMasterURL ¶
ValidMasterURL accepts https://host[:port], or plain http on this box's loopback (tests and the install check); never user info, a query or a fragment.
Types ¶
type Box ¶
type Box struct {
ID string `json:"id"`
Name string `json:"name"`
URL string `json:"url"`
KeyHash string `json:"key_sha256"`
Self bool `json:"self"`
CreatedAt time.Time `json:"created_at,omitzero"`
LastSeen time.Time `json:"last_seen,omitzero"`
Hostname string `json:"hostname"`
Version string `json:"version"`
}
Box is one enrolled box as the master keeps it: its key only hashed.
type BoxStatus ¶
type BoxStatus struct {
ID string `json:"id"`
Name string `json:"name"`
URL string `json:"url"`
Self bool `json:"self"`
Online bool `json:"online"`
LastSeen time.Time `json:"last_seen,omitzero"`
Hostname string `json:"hostname"`
Version string `json:"version"`
}
BoxStatus is one box as the dashboard sees it: never its key.
type Code ¶
type Code struct {
MasterURL string `json:"u"`
BoxID string `json:"b"`
Key string `json:"k"`
CertSHA256 string `json:"f"`
}
Code is what the owner copies from the master into a box's settings: where the master is, which box this is, the box's key, and the fingerprint of the master's certificate for when its chain does not verify. It is shown once.
type Fleet ¶
type Fleet struct {
Root string // "/" on a box; a temp dir in tests
RuntimePath func() string // where internal.secret lives
Now func() time.Time // the clock of what is recorded
Log func(msg string, err error) // one line when a file cannot be read
Gateway func() (GatewayInfo, error) // where this box's gateway listens
Hostname func() (string, error) // this box's name, said in the hello
Version func() string // this box's ReCasaOS version, said in the hello
Offline func(boxID, name string, offline bool) // the alerts hub's hook: a box gone, or back
// contains filtered or unexported fields
}
Fleet holds both roles of one box. Its func fields are what main wires and tests replace.
func (*Fleet) AddBox ¶
AddBox enrols a box and returns its code, the one time the key is handed over. self enrols this box itself: its code points at its own gateway, and its member.json is written. There is at most one self box.
func (*Fleet) DisableMaster ¶
DisableMaster closes every tunnel and forgets the boxes. A self-enrolled master's own member.json pointed at this box's own gateway, which goes with the master, so it is cleared too; a member.json of another fleet is left.
func (*Fleet) EnableMaster ¶
EnableMaster turns the role on at address, keeping the boxes already enrolled. A box that is a member of another fleet cannot become a master: the two roles are exclusive (a self-enrolled master's own member.json names itself, which is not another fleet).
func (*Fleet) HandleTunnel ¶
func (f *Fleet) HandleTunnel(w http.ResponseWriter, r *http.Request)
HandleTunnel is GET /v1/fleet/tunnel: a box's tunnel. 401 unless the Authorization header is "Fleet <id>:<key>" of an enrolled box; then the hello, then res frames until the connection goes. A member's req frame is ignored: only the master asks.
func (*Fleet) Join ¶
Join makes this box a member: the code is kept in member.json and the loop dials at once. A master does not join another fleet.
func (*Fleet) MasterStatus ¶
func (f *Fleet) MasterStatus() (MasterStatus, bool)
MasterStatus is the master's view: ok is false when the role is off.
func (*Fleet) MemberStatus ¶
func (f *Fleet) MemberStatus() (MemberStatus, bool)
MemberStatus is the member's view: ok is false when the role is off.
func (*Fleet) Relay ¶
func (f *Fleet) Relay(ctx context.Context, boxID, method, path, query, contentType string, body []byte) (Response, error)
Relay sends one request down the box's tunnel and returns what the box answered. The table is checked here, before anything reaches the tunnel.
func (*Fleet) RemoveBox ¶
RemoveBox revokes a box: its tunnel is closed and its key is worth nothing.
func (*Fleet) RenameBox ¶
RenameBox changes the name and/or the dashboard link of a box; nil leaves one as it is.
func (*Fleet) RotateCode ¶
RotateCode gives a box a new key: the old one is worth nothing and its tunnel is closed; the new code is returned once.
type GatewayInfo ¶
type GatewayInfo struct {
Public *url.URL // http(s)://127.0.0.1:<port>: where a member executes relayed requests
HTTPS *url.URL // https://127.0.0.1:<httpsport or 443>; nil when a configured certificate makes Public itself https, or HTTPS is off (httpsport=0)
CertFile string // the certificate the gateway serves: tlscert, or the default self-signed one
Unreachable string // non-empty when the gateway is bound off loopback: the member cannot relay, and says so, but the master can still read the certificate to enrol boxes
}
GatewayInfo is where this box's gateway listens, as seen from the box itself.
func ReadGatewayINI ¶
func ReadGatewayINI(path, dataPath string) (GatewayInfo, error)
ReadGatewayINI reads [gateway] port, address, tlscert, tlskey and httpsport from path; dataPath is where the default certificate lives (/var/lib/casaos). It errors only on config a box cannot use at all (no port, a bad httpsport). A gateway bound off loopback, and HTTPS turned off with httpsport=0, are normal states, not errors: the member's relay reports the first, and the certificate is still read so the master can enrol boxes.
type MasterState ¶
type MasterState struct {
Enabled bool `json:"enabled"`
Address string `json:"address"`
Boxes []Box `json:"boxes"`
}
MasterState is master.json.
type MasterStatus ¶
MasterStatus is the master half of GET /v1/fleet.
type MemberState ¶
type MemberState struct {
MasterURL string `json:"master_url"`
BoxID string `json:"box_id"`
Key string `json:"key"`
MasterCertSHA256 string `json:"master_cert_sha256"`
}
MemberState is member.json: the code the owner pasted, kept.
type MemberStatus ¶
type MemberStatus struct {
MasterURL string `json:"master_url"`
Connected bool `json:"connected"`
Since time.Time `json:"since,omitzero"`
LastError string `json:"last_error,omitempty"`
LastTry time.Time `json:"last_try,omitzero"`
}
MemberStatus is the member half of GET /v1/fleet.
type Status ¶
type Status struct {
Role string `json:"role"` // none | master | member
Master *MasterStatus `json:"master,omitempty"`
Member *MemberStatus `json:"member,omitempty"`
}
Status is GET /v1/fleet: the role, and the half that is on.