// Command mesh-cli is the operator's command on every node (novox/hq ADR 0272): `mesh-cli ` asks the // controller to run one of its command lines, and prints what it said. In interactive zsh it is also `nox`. // // It holds no credential and reads no file of the mesh. It asks the node-engine on this machine over a local // socket; the engine reads which account is asking from the kernel and asks the controller. The controller decides: // on the control-node, from the operator's own account, the line runs as the controller's terminal; elsewhere it is // an ordinary call, and a change made at the terminal alone is refused with the reason; from any other account // nothing runs. package main import ( "context" "errors" "fmt" "io" "os" "os/signal" "syscall" "git.novox.be/novox/mesh-sdk/go/cli" ) // socket is where the node-engine answers; a variable so a test can name its own. var socket = cli.Socket // standardInput is mesh-cli's own standard input, and whether it is a terminal (or a device such as /dev/null), // which is never read; a variable so a test can say. var standardInput = func() (io.Reader, bool) { info, err := os.Stdin.Stat() if err != nil { return os.Stdin, true } return os.Stdin, info.Mode()&os.ModeCharDevice != 0 } // carried is what mesh-cli carries of its standard input for the line: what a pipe or a file held, at most // cli.MaxStdin, for the controller's terminal alone (a secret given with `secret accept … --from -`). A terminal is // never read: the line is asked without. More than the bound is refused, and nothing is asked. func carried() ([]byte, error) { in, terminal := standardInput() if terminal { return nil, nil } body, err := io.ReadAll(io.LimitReader(in, cli.MaxStdin+1)) if err != nil { return nil, fmt.Errorf("standard input could not be read (%v); nothing was asked", err) } if len(body) > cli.MaxStdin { return nil, fmt.Errorf("standard input is more than %d bytes, which mesh-cli does not carry; nothing was asked", cli.MaxStdin) } return body, nil } const usage = `mesh-cli — the controller's command lines, from this node (novox/hq ADR 0272) mesh-cli e.g. mesh-cli status, mesh-cli settings show zsh --node nox the same, in interactive zsh Run it as yourself, never under sudo: the node-engine tells the controller which account asks. What a pipe or a file gives its standard input (at most 64 KiB) goes with the line to the controller's terminal alone, e.g. a secret: mesh-cli secret accept --from - < file On the control-node, from its operator's account, the line runs as the controller's terminal. Anywhere else it is an ordinary call, and a terminal-only change is refused with the reason. 'mesh-cli help' asks the controller for its own list of command lines. ` func main() { ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) code := run(ctx, os.Args[1:], os.Stdout, os.Stderr) stop() os.Exit(code) } func run(ctx context.Context, args []string, stdout, stderr io.Writer) int { if len(args) == 0 || args[0] == "-h" || args[0] == "--help" { fmt.Fprint(stderr, usage) return 2 } // A verb's own process carries the verb it runs for; a verb is never a person, so it is never the terminal. if verb := os.Getenv("MESH_VERB"); verb != "" { fmt.Fprintf(stderr, "mesh-cli: this process runs for the verb %q, and a verb is never the operator. "+ "Nothing was asked\n", verb) return 1 } in, err := carried() if err != nil { fmt.Fprintf(stderr, "mesh-cli: %v\n", err) return 1 } a, err := cli.AskWith(ctx, socket, cli.Request{Line: args, Stdin: in}) if err != nil { if errors.Is(ctx.Err(), context.Canceled) { fmt.Fprintln(stderr, "mesh-cli: stopped waiting; the line may still be running on the controller") return 130 } fmt.Fprintf(stderr, "mesh-cli: %v\n", err) return 1 } _, _ = stdout.Write(a.Stdout) _, _ = stderr.Write(a.Stderr) if a.Cut { fmt.Fprintln(stderr, "mesh-cli: the output was longer than one bus message, and was cut there") } if !a.Terminal && a.Why != "" { fmt.Fprintln(stderr, "mesh-cli: "+a.Why) } if a.Unknown != "" { // Asked, and what came of it not known: never said as nothing having run. fmt.Fprintln(stderr, "mesh-cli: "+a.Unknown) if a.Exit == 0 { return 1 } } if a.Refused != "" { fmt.Fprintln(stderr, "mesh-cli: "+a.Refused) if a.Exit == 0 { return 1 } } return a.Exit }