Documentation
¶
Overview ¶
Package adminsock is an application's admin socket: its operator commands (Family Hub's `hub admin …`) run inside the application process and are reached over a unix socket. Packages and the application add commands to one Registry; a Server serves them while the application runs, and CLI is the client side for the application's command line.
The registry ¶
A Registry holds the commands by name. Any package adds its own with Registry.Add — package auth adds setup-link and reset-access when auth.Options.Admin is set, and an application adds its own (Family Hub: status, legacy keys, cloudflare connect, …):
reg := adminsock.NewRegistry()
a, err := auth.New(auth.Options{…, Admin: reg}) // setup-link, reset-access
err = reg.Add(adminsock.Command{Name: "status", Summary: "…", Run: status})
srv, err := adminsock.New(adminsock.Options{Path: "data/admin.sock", Logger: log, Registry: reg})
err = srv.Start(ctx); defer srv.Stop(ctx)
A name is one or more lower-case words ("setup-link", "legacy-key import"); "help" belongs to the CLI. Add refuses a bad or repeated name, or a command without Run, and then adds none of the commands it was given. Commands may be added before or after Start: the server looks them up per request.
Who may use it ¶
The socket file is 0600 and the server also checks the peer's uid (SO_PEERCRED on Linux, LOCAL_PEERCRED on macOS; other platforms refuse every peer): only the application's own user and root get in, so a mode slip alone does not open it to others. Whoever gets in is the server operator: commands need no passkey, no session and no step-up.
The wire ¶
HTTP/1.1 over the socket:
GET /v1/commands → 200 [{"name","usage","summary","stdin"}]
POST /v1/run {"command","args","stdin"}
→ 200 application/x-ndjson: one {"out":"…"} frame per write of the command, then a final
{"done":true} or {"error":"<code>","message":"…"} frame;
400 or 404 {"error":"<code>","message":"…"} when it cannot start
Output streams while the command runs, so a command can print a URL and wait for the operator (Family Hub's `cloudflare connect`). It goes to the operator's terminal only: the server never logs a command's arguments, standard input or output, only its name, its result code and how long it took.
Index ¶
Constants ¶
const ( ExitOK = 0 ExitFailed = 1 ExitUsage = 2 )
Exit codes of CLI.Run.
const (
// MaxStdin caps the standard input the CLI sends along for a command that reads it.
MaxStdin = 64 << 10
)
Variables ¶
var ErrUsage = errors.New("usage")
ErrUsage marks a command error the operator caused: wrong arguments. The CLI then exits with 2.
Functions ¶
This section is empty.
Types ¶
type CLI ¶
type CLI struct {
Socket string // the socket's path
Prog string // how the operator runs it, in usage lines and errors, e.g. "hub admin"; "" is "admin"
App string // what serves the socket, in errors, e.g. "hub"; "" is "server"
Hint string // a line printed when the socket does not answer; "" prints none
}
CLI is the operator's side of the socket, for the application's command line (Family Hub: `hub admin <args…>` is CLI{Socket: <data dir>/admin.sock, Prog: "hub admin", App: "hub", Hint: "Is the hub running? Run this inside its container."}.Run(…)).
func (CLI) Run ¶
func (cli CLI) Run(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) int
Run finds the command among the running server's (the longest name that the arguments start with), sends the remaining arguments and, for a command that reads it, the standard input; it streams the output to stdout and errors to stderr, and returns the exit code: ExitOK, ExitFailed or ExitUsage. No arguments, or "help", lists the commands.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client talks to a running application over its admin socket.
func NewClient ¶
NewClient returns a client for the socket at path. It has no timeout of its own: commands may wait for the operator; cancel the context instead.
type Command ¶
type Command struct {
// Name is one or more lower-case words, e.g. "setup-link" or "legacy-key import".
Name string
// Usage shows the arguments after the name, e.g. "--module <id> --person <id> --label <label>".
Usage string
// Summary is one line for the CLI's help.
Summary string
// Stdin makes the CLI read its standard input (at most MaxStdin bytes) and send it along.
Stdin bool
// Run does the work. What it writes to inv.Out reaches the operator's terminal as it is written. An error ends
// the command with its text as the message; errors wrapping ErrUsage, core.ErrConflict or core.ErrNotFound get
// the codes invalid, conflict and not_found. Error texts are shown, never logged: keep secrets out of them anyway.
Run func(ctx context.Context, inv Invocation) error
}
Command is one admin command.
type CommandInfo ¶
type CommandInfo struct {
Name string `json:"name"`
Usage string `json:"usage,omitempty"`
Summary string `json:"summary"`
Stdin bool `json:"stdin,omitempty"`
}
CommandInfo describes a command to the CLI.
type Invocation ¶
type Invocation struct {
Args []string // the words after the command name; never nil
Stdin []byte // the CLI's standard input when Command.Stdin is set, else nil
Out io.Writer // the operator's terminal; nothing written here is logged
}
Invocation is one run of a command.
type Options ¶
type Options struct {
// Path is the socket file, at most 103 bytes (macOS's sun_path). Its directory is made 0700 when missing.
Path string
// Logger gets the server's own lines; nil discards.
Logger *slog.Logger
// Registry holds the commands; nil serves an empty one (Server.Registry returns it).
Registry *Registry
}
Options is what New needs.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry is the set of commands a Server serves. It is safe for concurrent use.
func (*Registry) Add ¶
Add checks the commands (names of lower-case words, unique here and among the registered ones, none called "help", a Run each) and registers them, all or none.
func (*Registry) Commands ¶
func (r *Registry) Commands() []CommandInfo
Commands lists the registered commands, sorted by name; never nil.
type RemoteError ¶
type RemoteError struct {
Code string // invalid, conflict, not_found, failed or internal
Message string
}
RemoteError is a refusal or a failure the server reported for a command.
func (*RemoteError) Error ¶
func (e *RemoteError) Error() string
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server serves a registry's commands on the socket.
func (*Server) Handler ¶
Handler serves the wire protocol; Start serves it on the socket. Tests use it directly.