Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ Which command works over which connection:

| Connection | Commands |
|---------------------|----------|
| Network address | Every pod command except the USB console ones below. Settings the pod changes only over USB or the cloud (`lan-tls` switches, `cloud ca`/`proxy` changes, `identity wipe`, `usb-network`) are refused there with their code. |
| Network address | Every pod command except the USB console ones below. Settings the pod changes only over USB or the cloud (`lan-pairing`, `lan-clients` changes, `lan-tls` switches, `cloud ca`/`proxy` changes) go over the pod's own USB link when its USB-C cable is in this computer; otherwise they are refused with their code, as are `identity wipe` and `usb-network`. |
| USB | Every pod command, as JSON over the pod's USB network adapter. With USB networking off (Wi-Fi configured), the pod's Ethernet or Wi-Fi address from its console `status` when this computer reaches it (its external LAN policy applies there), and the command says so. |
| `embeddedci:<name>` | `flash-self`, `status`, `ping`, `la voltage`, `la pull`, `la status`, `la step`, `la pins`, `la dir`, `i2c`, `generate`, `lan-policy`, `sig-policy`, `cloud ca show`, `cloud ca clear`, `cloud proxy` (show, set, clear), `set-wifi`, `show-network`, `scan-wifi`, `clear-wifi`, `deregister`. |
| USB console only | `dfu`, `bootsel`, `net-reset`, `usb-network`, `usb-bridge`, `lan-policy set` over USB; `flash-self` uses USB DFU (or, with `--connection embeddedci:<name>`, embeddedci.com). A saved address or `embeddedci:` default falls through to USB auto-detection, a device path still selects the port, and an explicit `--connection embeddedci:<name>` is refused. |
Expand Down Expand Up @@ -531,8 +531,9 @@ LAN TLS of 192.168.1.220:8080 (connected over TLS)
- `https-only`, `scpi`, `regenerate` and `set-cert` only work over USB
(`--connection usb`, the pod's USB network adapter) or through embeddedci.com
(`--connection embeddedci:<name>`): the pod refuses them from Ethernet and Wi-Fi,
TLS or not (`BP0042`). The pod's USB console has the same switches
(`lan-tls https-only on|off`, `lan-tls scpi on|off`).
TLS or not (`BP0042`). Given the pod's network address while its USB-C cable is in
this computer, `benchpod` sends them over that pod's own USB link. The pod's USB
console has the same switches (`lan-tls https-only on|off`, `lan-tls scpi on|off`).
- `benchpod lan-tls show --pem > pod.pem` saves the certificate, for curl or socat
(VISA raw sockets have no TLS; see the firmware API docs, LAN TLS).

Expand Down Expand Up @@ -2324,7 +2325,9 @@ Given the pod's network address or name (`--connection 192.168.1.164`,
pod's USB-C cable is in this computer, pairs over that pod's own USB link: no need to pick
it among several pods on USB. Otherwise it says to plug the pod in over USB-C, or to use
`--connection embeddedci:<name>`. A computer that is paired already is told so. `remove`,
`clear` and `benchpod setup` take the USB link the same way.
`clear` and `benchpod setup` take the USB link the same way, and so do `benchpod lan-pairing`
and the `lan-tls` changes. A command refused with `BP0081` (this computer is not paired) from a
pod whose USB-C cable is in this computer says to pair it now with `benchpod lan-clients add`.

| Flag | Default | Purpose |
|-------------|-----------------|----------------------------------------------------------------------|
Expand Down
2 changes: 1 addition & 1 deletion cmd/benchpod-cli/adcrange.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func runADCRange(g *globalFlags, action string, out io.Writer) error {
return err
}
defer cancel()
return adcRange(ctx, t.client, t.label, action, what, out)
return adcRange(ctx, t.client, t.where(), action, what, out)
}

func adcRange(ctx context.Context, c podCommander, via, action, what string, out io.Writer) error {
Expand Down
33 changes: 26 additions & 7 deletions cmd/benchpod-cli/cloudcfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,19 @@ func cloudCfgTarget(g *globalFlags, what string) (spec ConnSpec, err error) {
return parseTarget(raw)
}

// cloudCfgOverUSBLink is where a cloud ca / cloud proxy command for the LAN address addr goes: a
// change (or a computer that is not paired) over the pod's USB link when it is plugged into this
// computer, since the pod refuses those from the LAN.
func cloudCfgOverUSBLink(g *globalFlags, addr, action string) (dial, label string, err error) {
ctx, cancel := context.WithTimeout(context.Background(), g.effectiveTimeout(15*time.Second))
defer cancel()
need := notPairedHere
if action != "show" {
need = nil
}
return overUSBLink(ctx, addr, need)
}

// cloudCfgPod runs one cloud_ca / cloud_proxy JSON command through embeddedci.com and decodes the
// reply into out (nil to skip).
func cloudCfgPod(g *globalFlags, spec ConnSpec, def time.Duration, req map[string]any, out any) (string, error) {
Expand All @@ -216,14 +229,14 @@ func cloudCfgPod(g *globalFlags, spec ConnSpec, def time.Duration, req map[strin
}
data, err := pod.client.Command(ctx, req)
if err != nil {
return pod.label, err
return pod.where(), err
}
if out != nil {
if err := json.Unmarshal(data, out); err != nil {
return pod.label, fmt.Errorf("unexpected reply to %v: %s", req["cmd"], strings.TrimSpace(string(data)))
return pod.where(), fmt.Errorf("unexpected reply to %v: %s", req["cmd"], strings.TrimSpace(string(data)))
}
}
return pod.label, nil
return pod.where(), nil
}

// ── CA certificate checks ───────────────────────────────────────────────────
Expand Down Expand Up @@ -352,8 +365,11 @@ func runCloudCA(g *globalFlags, action, file string, out, warn io.Writer) error
label = nt.Addr
certs, err = cloudCALAN(g, nt.Addr, action, upload, warn)
default:
label = spec.Addr
certs, err = cloudCALAN(g, spec.Addr, action, upload, warn)
var dial string
if dial, label, err = cloudCfgOverUSBLink(g, spec.Addr, action); err != nil {
return fmt.Errorf("%s: %w", what, err)
}
certs, err = cloudCALAN(g, dial, action, upload, warn)
}
if err != nil {
return cloudCfgError(what, err)
Expand Down Expand Up @@ -625,8 +641,11 @@ func runCloudProxy(g *globalFlags, action, addr, user, password string, out, war
label = nt.Addr
p, err = cloudProxyLAN(g, nt.Addr, action, addr, user, password)
default:
label = spec.Addr
p, err = cloudProxyLAN(g, spec.Addr, action, addr, user, password)
var dial string
if dial, label, err = cloudCfgOverUSBLink(g, spec.Addr, action); err != nil {
return fmt.Errorf("%s: %w", what, err)
}
p, err = cloudProxyLAN(g, dial, action, addr, user, password)
}
if err != nil {
err = cloudCfgError(what, err)
Expand Down
26 changes: 22 additions & 4 deletions cmd/benchpod-cli/cloudcfg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,8 @@ func TestCloudCALAN(t *testing.T) {
t.Fatalf("set = %q", out)
}
nChunks := (len(pemData) + caLANChunk - 1) / caLANChunk
want := "cloud_ca,ota_begin" + strings.Repeat(",ota_data", nChunks) + ",ota_end,ota_commit,cloud_ca"
// status first: whether the pod's USB link answers here (the change would go over it).
want := "status,cloud_ca,ota_begin" + strings.Repeat(",ota_data", nChunks) + ",ota_end,ota_commit,cloud_ca"
if got := pod.cmds(); got != want {
t.Fatalf("requests = %s\nwant %s", got, want)
}
Expand Down Expand Up @@ -486,23 +487,40 @@ func TestCloudProxyLAN(t *testing.T) {
warn != "The pod reconnects to the cloud through proxy.corp:3128.\n" {
t.Fatalf("set = %q, %q, %v", out, warn, err)
}
set := pod.reqs[1]
var set map[string]any // each command reads status first (is the pod's USB link here?)
for _, r := range pod.reqs {
if _, ok := r["set"]; ok {
set = r
break
}
}
if set["set"] != "proxy.corp:3128" || set["user"] != "alice" || set["password"] != "pa ss" {
t.Fatalf("set request = %v", set)
}
out, _, err = runProxyCapture(g, "set", "10.1.2.3:8080", "", "")
if err != nil || !strings.HasSuffix(out, "is now 10.1.2.3:8080 (no authentication)\n") {
t.Fatalf("set noauth = %q, %v", out, err)
}
if _, ok := pod.reqs[3]["password"]; ok {
t.Fatalf("a password went out without --user: %v", pod.reqs[3])
if last := lastSet(pod.reqs); last["password"] != nil {
t.Fatalf("a password went out without --user: %v", last)
}
out, _, err = runProxyCapture(g, "clear", "", "", "")
if err != nil || !strings.HasSuffix(out, "is now none (the pod connects to the cloud directly)\n") {
t.Fatalf("clear = %q, %v", out, err)
}
}

// lastSet is the last cloud_proxy set request.
func lastSet(reqs []map[string]any) map[string]any {
var last map[string]any
for _, r := range reqs {
if _, ok := r["set"]; ok {
last = r
}
}
return last
}

func TestCloudCfgNeedsATarget(t *testing.T) {
g := &globalFlags{configFile: filepath.Join(t.TempDir(), "none.yaml")}
if _, _, err := runProxyCapture(g, "show", "", "", ""); err == nil || !strings.Contains(err.Error(), "--connection usb") {
Expand Down
4 changes: 2 additions & 2 deletions cmd/benchpod-cli/commands_discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ func printReport(in reportInput) {
w := tabwriter.NewWriter(out, 0, 2, 2, ' ', 0)
fmt.Fprintln(w, " INSTANCE\tADDRESS\tHOSTNAME\tID")
for _, p := range in.netPods {
fmt.Fprintf(w, " %s\t%s\t%s\t%s\n", p.instance, p.addr, p.hostname, shortID(p.id))
fmt.Fprintf(w, " %s\t%s\t%s\t%s\n", p.instance, tcpclient.ShownAddr(p.addr), p.hostname, shortID(p.id))
}
_ = w.Flush()
for _, p := range in.netPods {
Expand Down Expand Up @@ -540,7 +540,7 @@ func printVerdict(out *os.File, in reportInput) {
if !p.reachable {
fmt.Fprintf(out, "\n%s advertises itself but does not answer its API on %s.\n"+
"The record may be stale (a pod that has since rebooted or moved), or a firewall\n"+
"may be blocking port 8080.\n", p.instance, p.addr)
"may be blocking ports 8443 (TLS) and 8080.\n", p.instance, tcpclient.ShownAddr(p.addr))
}
}

Expand Down
31 changes: 31 additions & 0 deletions cmd/benchpod-cli/errors.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package main

import (
"context"
"errors"
"fmt"
"strings"
"time"

"github.com/embeddedci-com/benchpod-cli/internal/cloudpod"
"github.com/embeddedci-com/benchpod-cli/internal/errcatalog"
Expand Down Expand Up @@ -209,8 +211,37 @@ func codedRefusal(pe *tcpclient.PodError) (string, bool) {
if pe == nil || pe.Code == "" {
return "", false
}
if fix := pairHereFix(pe); fix != "" {
e, _ := errcatalog.Lookup(pe.Code)
code := strings.ToUpper(strings.TrimSpace(pe.Code))
return fmt.Sprintf("%s: %s; %s (%s)", code, errcatalog.Fill(e.Message, pe.Args), fix, errcatalog.DocsURL(code)), true
}
if text, ok := errcatalog.Render(pe.Code, pe.Args, "cli"); ok {
return text, true
}
return fmt.Sprintf("%s: %s (%s)", strings.ToUpper(pe.Code), strings.TrimSpace(pe.Reason), errcatalog.DocsURL(pe.Code)), true
}

// pairHereFix is the fix for a computer that is not paired (BP0081) with a pod it reached on the
// LAN when that pod's USB-C cable is in this very computer (its status shows the USB link, and
// the link answers from here): pair now, no plugging in. "" otherwise, and the catalog's fix
// stands.
func pairHereFix(pe *tcpclient.PodError) string {
if !strings.EqualFold(strings.TrimSpace(pe.Code), "BP0081") || pe.Addr == "" || isUSBLinkAddr(pe.Addr) {
return ""
}
fix, ok := pairHereFixes[pe.Addr]
if !ok {
ctx, cancel := context.WithTimeout(context.Background(), 8*time.Second)
defer cancel()
if usb, err := reachUSBLink(ctx, pe.Addr, readLanPodState(ctx, pe.Addr).usbIP); err == nil && usb != "" {
fix = "the pod is plugged into this computer over USB, so pair this computer now with " +
"`benchpod lan-clients add --connection " + tcpclient.ShownAddr(pe.Addr) + "`, then run this again"
}
pairHereFixes[pe.Addr] = fix
}
return fix
}

// pairHereFixes keeps pairHereFix's answer per address for the run (a refusal is worded more than once).
var pairHereFixes = map[string]string{}
20 changes: 15 additions & 5 deletions cmd/benchpod-cli/lanclients.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,15 +231,15 @@ func lanClientsNetwork(g *globalFlags, o lanClientsOp, key string) (lanClientsVi
if err != nil {
return lanClientsView{}, "", fmt.Errorf("lan-clients: %w", err)
}
return v, t.label, errAlreadyPaired
return v, t.where(), errAlreadyPaired
}
if err != nil {
return lanClientsView{}, "", err
}
if usb != "" {
lanAddr = t.spec.Addr
t = &podTarget{spec: ConnSpec{Kind: connNetwork, Addr: usb}, client: &tcpclient.Client{Addr: usb},
label: t.spec.Addr + " (over its USB link " + hostOf(usb) + ")"}
label: usbLinkLabel(t.spec.Addr, usb)}
}
}
req := map[string]any{"cmd": "lan_clients"}
Expand All @@ -261,7 +261,7 @@ func lanClientsNetwork(g *globalFlags, o lanClientsOp, key string) (lanClientsVi
if lanAddr != "" && o.op == "add" {
pinLanCertOverUSBLink(ctx, t.spec.Addr, lanAddr)
}
return v, t.label, nil
return v, t.where(), nil
}

// lanClientsAll runs req and reads every page of the list (the pod sends 12 a reply).
Expand Down Expand Up @@ -384,7 +384,8 @@ func newLanPairingCmd(g *globalFlags) *cobra.Command {
Long: "Show or set LAN pairing (firmware 4.0.1+). on (the default): a computer on Ethernet or\n" +
"Wi-Fi drives the pod only once it is paired (`benchpod setup`, `benchpod lan-clients`).\n" +
"off: every computer on the network may, as before 4.0.1; then keep the pod on a trusted\n" +
"lab network. Changed over USB (the console or --connection usb) or embeddedci.com.",
"lab network. Changed over USB (the console or --connection usb) or embeddedci.com; given\n" +
"the pod's network address, over that pod's own USB link when it is plugged in here.",
Args: cobra.MaximumNArgs(1),
ValidArgs: []string{"on", "off"},
RunE: func(_ *cobra.Command, args []string) error {
Expand Down Expand Up @@ -429,6 +430,15 @@ func runLanPairing(g *globalFlags, set string, out io.Writer) error {
return err
}
defer cancel()
// A LAN address: a change, and a read this computer may not make while it is not paired,
// go over the pod's USB link when it is plugged into this computer.
var need func(lanPodState) bool
if set == "" {
need = notPairedHere
}
if t, _, err = targetOverUSBLink(ctx, t, need); err != nil {
return fmt.Errorf("lan-pairing: %w", err)
}
req := map[string]any{"cmd": "lan_policy"}
if set != "" {
req["pairing"] = set == "on"
Expand All @@ -443,7 +453,7 @@ func runLanPairing(g *globalFlags, set string, out io.Writer) error {
if json.Unmarshal(data, &rep) != nil || rep.Pairing == nil {
return errors.New(lanClientsMissing)
}
on, label = *rep.Pairing, t.label
on, label = *rep.Pairing, t.where()
}
state := map[bool]string{true: "on: computers on Ethernet and Wi-Fi must be paired", false: "off: every computer on the network may use the pod"}[on]
if set != "" {
Expand Down
27 changes: 21 additions & 6 deletions cmd/benchpod-cli/lantls.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,17 @@ func runLanTLS(g *globalFlags, change map[string]any, out, warn io.Writer) error
return err
}
defer cancel()
// A change over a LAN address goes over the pod's USB link when it is plugged into this
// computer (the pod takes changes over USB or the cloud only); so does showing it to a
// computer that is not paired.
need := notPairedHere
if len(change) > 0 {
need = nil
}
t, lanAddr, err := targetOverUSBLink(ctx, t, need)
if err != nil {
return fmt.Errorf("lan-tls: %w", err)
}
req := map[string]any{"cmd": "lan_tls"}
for k, v := range change {
req[k] = v
Expand All @@ -189,8 +200,8 @@ func runLanTLS(g *globalFlags, change map[string]any, out, warn io.Writer) error
if err := json.Unmarshal(raw, &rep); err != nil {
return fmt.Errorf("lan-tls: unexpected reply: %s", strings.TrimSpace(string(raw)))
}
addr := ""
if !t.spec.IsCloud() {
addr := lanAddr
if addr == "" && !t.spec.IsCloud() {
addr = t.spec.Addr
}
// This computer made the change: pin the new certificate for the address it used.
Expand All @@ -199,7 +210,7 @@ func runLanTLS(g *globalFlags, change map[string]any, out, warn io.Writer) error
fmt.Fprintf(warn, "Could not update the pin: %v\n", err)
}
}
printLanTLS(out, t.label, addr, rep, lastTLS(t))
printLanTLS(out, t.where(), addr, rep, lastTLS(t))
if hs, ok := change["https_only"].(bool); ok && !hs {
fmt.Fprintln(warn, "Plaintext port 8080 now answers on Ethernet and Wi-Fi too: anyone on the LAN can read and drive the pod without TLS.")
}
Expand Down Expand Up @@ -374,15 +385,19 @@ func runLanTLSSetCert(g *globalFlags, certFile, keyFile string, out, warn io.Wri
defer cancel()
defer installSignalHandler(ctx, cancel)()

lan, err := openLAN(ctx, addr)
dial, label, err := overUSBLink(ctx, addr, nil)
if err != nil {
return fmt.Errorf("lan-tls set-cert: %w", err)
}
lan, err := openLAN(ctx, dial)
if err != nil {
return fmt.Errorf("lan-tls set-cert: %w", err)
}
defer lan.s.Close()
if err := lan.cmd(ctx, map[string]any{"cmd": "lan_tls"}, &lanTLSReply{}); err != nil {
return lanTLSError("lan-tls set-cert", err)
}
fmt.Fprintf(warn, "Uploading %d bytes to %s ...\n", len(bundle), addr)
fmt.Fprintf(warn, "Uploading %d bytes to %s ...\n", len(bundle), label)
if err := uploadOTALAN(ctx, lan, bundle, "tls"); err != nil {
if !lan.s.Broken() {
_ = lan.cmd(ctx, map[string]any{"cmd": "ota_abort"}, nil)
Expand All @@ -401,6 +416,6 @@ func runLanTLSSetCert(g *globalFlags, certFile, keyFile string, out, warn io.Wri
fmt.Fprintf(warn, "Could not update the pin: %v\n", err)
}
}
printLanTLS(out, addr, addr, rep, map[bool]string{true: "TLS", false: "plaintext"}[lan.s.TLS()])
printLanTLS(out, label, addr, rep, map[bool]string{true: "TLS", false: "plaintext"}[lan.s.TLS()])
return nil
}
4 changes: 4 additions & 0 deletions cmd/benchpod-cli/lantls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func TestLanTLSSwitches(t *testing.T) {
}
var req map[string]any
_ = json.Unmarshal([]byte(<-got), &req)
if req["cmd"] == "status" { // whether the pod's USB link answers here
req = nil
_ = json.Unmarshal([]byte(<-got), &req)
}
if len(req) != len(c.want) {
t.Fatalf("%v: sent %v", c.args, req)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/benchpod-cli/lavoltage.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func laVoltageDo(g *globalFlags, mv int) (string, laVoltageReply, error) {
if rep, err = laVoltageLAN(ctx, pod.client, mv); err != nil {
return "", rep, laVoltageError(what, err)
}
return pod.label, rep, nil
return pod.where(), rep, nil
}

// laVoltageLAN runs la_voltage as a JSON command, on the pod's network port or through
Expand Down
4 changes: 2 additions & 2 deletions cmd/benchpod-cli/loglevel.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ func runLogLevel(g *globalFlags, set string, out io.Writer) error {
return fmt.Errorf("log-level: %w", err)
}
if set == "" {
fmt.Fprintf(out, "Console log level of the pod at %s: %s\n", t.label, level)
fmt.Fprintf(out, "Console log level of the pod at %s: %s\n", t.where(), level)
} else {
fmt.Fprintf(out, "Console log level of the pod at %s is now %s (stored on the pod).\n", t.label, level)
fmt.Fprintf(out, "Console log level of the pod at %s is now %s (stored on the pod).\n", t.where(), level)
}
return nil
}
Expand Down
Loading