Files
rollcall/cmd/rollcall/power.go
T
Ilker ManapandClaude Opus 5.5 9ab081aa5a
CI / agent (push) Successful in 3s
CI / site (push) Successful in 2s
CI / go (push) Successful in 42s
BMC certificates pinned on first use (Redfish, Proxmox)
The Redfish and Proxmox clients skipped TLS verification (BMCs ship
self-signed certificates), so a host posing as a BMC on the network got
the Basic-auth password. Before any credentials go to an HTTPS BMC the
region now reads its certificate with a handshake-only operation
(tls-fingerprint, run on the rack controller) and pins the SHA-256 on
the BMC record (extra tls_fingerprint); every later connection must show
the same certificate or nothing is sent. Changing the address, clearing
tls_fingerprint, or a BMC firmware update run by Rollcall (new firmware
often means a new certificate) pins anew. verify_tls=true still uses the
system CAs instead. The lab emulator keeps its certificate like a BMC.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-29 22:32:38 +01:00

348 lines
10 KiB
Go

package main
// Power drivers. A machine's BMC record names a driver; the command runs on
// the rack controller that reaches the BMC network (the embedded one runs it
// in-process). Every driver answers the same operations:
//
// status on off soft cycle reset bootdev(pxe|disk|bios|cdrom|none) probe
//
// and reports power state the way ipmitool does ("Chassis Power is on"), so
// callers do not care which one ran.
import (
"bytes"
"context"
"crypto/sha256"
"crypto/subtle"
"crypto/tls"
"crypto/x509"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/http"
"net/url"
"os/exec"
"sort"
"strconv"
"strings"
"time"
)
type powerDriver struct {
Name string `json:"name"`
Description string `json:"description"`
Family string `json:"family"` // ipmi | redfish | api | none
DefaultPort int `json:"default_port"`
}
var powerDrivers = append(append([]powerDriver{{"ipmi", "IPMI 2.0 over LAN (ipmitool lanplus); any vendor", "ipmi", 623}},
redfishDrivers()...),
powerDriver{"proxmox", "Proxmox VE virtual machine (API token: user = token id, password = secret; extra node, vmid)", "api", 8006},
powerDriver{"manual", "no power control: someone presses the button", "none", 0})
func driverByName(n string) (powerDriver, bool) {
if n == "" {
n = "ipmi"
}
for _, d := range powerDrivers {
if d.Name == n {
return d, true
}
}
return powerDriver{}, false
}
var powerOps = map[string]bool{"status": true, "on": true, "off": true, "soft": true, "cycle": true, "reset": true, "bootdev": true, "probe": true}
var errManualPower = errors.New("this machine has manual power control: do it by hand")
// powerRun executes one operation with a driver.
func powerRun(ctx context.Context, ipmitool string, c *bmcCreds, op, arg string) (string, error) {
if op == "tls-fingerprint" {
ctx, cancel := context.WithTimeout(ctx, 20*time.Second)
defer cancel()
return bmcTLSFingerprint(ctx, c)
}
if t, ok := fwTimeouts[op]; ok { // firmware: Redfish UpdateService (redfish_fw.go)
ctx, cancel := context.WithTimeout(ctx, t)
defer cancel()
return firmwareRun(ctx, c, op, arg)
}
if !powerOps[op] {
return "", fmt.Errorf("unknown power operation %q", op)
}
if op == "bootdev" && !bootDevices[arg] {
return "", fmt.Errorf("boot device must be one of pxe, disk, cdrom, bios, none")
}
ctx, cancel := context.WithTimeout(ctx, 90*time.Second)
defer cancel()
d, ok := driverByName(c.Driver)
if !ok {
return "", fmt.Errorf("unknown power driver %q", c.Driver)
}
switch d.Family {
case "ipmi":
return ipmiOp(ctx, ipmitool, c, op, arg)
case "redfish":
if op == "probe" {
return redfishProbe(ctx, c)
}
return redfishOp(ctx, c, op, arg)
case "api":
return proxmoxOp(ctx, c, op, arg)
default:
if op == "status" {
return "Chassis Power is unknown (manual power control)", nil
}
return "", errManualPower
}
}
// ---------------------------------------------------------------- IPMI
func ipmiOp(ctx context.Context, tool string, c *bmcCreds, op, arg string) (string, error) {
var args []string
switch op {
case "bootdev":
args = []string{"chassis", "bootdev", arg}
case "probe":
args = []string{"mc", "info"}
default:
args = []string{"chassis", "power", op}
}
return ipmiExec(ctx, tool, c, args...)
}
func ipmiExec(ctx context.Context, tool string, c *bmcCreds, args ...string) (string, error) {
if tool == "" {
tool = "ipmitool"
}
iface := c.Extra["interface"]
if iface == "" {
iface = "lanplus"
}
port := c.Port
if port == 0 || port == 443 {
port = 623
}
full := []string{"-I", iface, "-H", c.Address, "-p", strconv.Itoa(port), "-U", c.User, "-E"}
if v := c.Extra["cipher"]; v != "" { // 17 on newer BMCs that dropped suite 3
full = append(full, "-C", v)
}
if v := c.Extra["privilege"]; v != "" {
full = append(full, "-L", v)
}
cmd := exec.CommandContext(ctx, tool, append(full, args...)...)
cmd.Env = []string{"IPMI_PASSWORD=" + c.Password, "PATH=/usr/sbin:/usr/bin:/sbin:/bin"}
var out bytes.Buffer
cmd.Stdout, cmd.Stderr = &out, &out
err := cmd.Run()
return strings.TrimSpace(out.String()), err
}
// ---------------------------------------------------------------- BMC HTTP (Redfish, Proxmox)
// bmcHTTPClient: BMCs ship self-signed certificates, so a BMC is trusted by
// the fingerprint of the certificate it showed first (extra tls_fingerprint,
// pinned by the region before credentials are ever sent; see pinBMC).
// verify_tls=true checks it against the system's CAs instead.
func bmcHTTPClient(c *bmcCreds) *http.Client {
tc := &tls.Config{InsecureSkipVerify: c.Extra["verify_tls"] != "true"} //nolint:gosec // pinned below
if pin := strings.ToLower(c.Extra["tls_fingerprint"]); pin != "" && c.Extra["verify_tls"] != "true" {
tc.VerifyConnection = func(cs tls.ConnectionState) error {
if len(cs.PeerCertificates) == 0 {
return errors.New("the BMC sent no certificate")
}
got := certSHA256(cs.PeerCertificates[0])
if subtle.ConstantTimeCompare([]byte(got), []byte(pin)) != 1 {
return fmt.Errorf("the BMC's certificate changed (pinned %s, now %s): not sending credentials; if the BMC was replaced or got new firmware, clear tls_fingerprint to trust the new one", short(pin), short(got))
}
return nil
}
}
tr := &http.Transport{TLSClientConfig: tc, DialContext: (&net.Dialer{Timeout: 10 * time.Second}).DialContext}
return &http.Client{Timeout: 45 * time.Second, Transport: tr}
}
func certSHA256(c *x509.Certificate) string {
s := sha256.Sum256(c.Raw)
return hex.EncodeToString(s[:])
}
func short(fp string) string {
if len(fp) > 16 {
return fp[:16] + "…"
}
return fp
}
// bmcTLSFingerprint connects to the BMC's HTTPS port and returns its
// certificate's SHA-256 (op tls-fingerprint): a handshake only, no request,
// no credentials.
func bmcTLSFingerprint(ctx context.Context, c *bmcCreds) (string, error) {
port := 443
if d, _ := driverByName(c.Driver); d.Family == "api" {
port = 8006
}
u, err := url.Parse(bmcBase(c, "https", port))
if err != nil {
return "", err
}
host := u.Host
if u.Port() == "" {
host = net.JoinHostPort(u.Hostname(), "443")
}
d := &tls.Dialer{NetDialer: &net.Dialer{Timeout: 10 * time.Second}, Config: &tls.Config{InsecureSkipVerify: true}} //nolint:gosec // reading the certificate is the point
conn, err := d.DialContext(ctx, "tcp", host)
if err != nil {
return "", err
}
defer conn.Close()
cs := conn.(*tls.Conn).ConnectionState()
if len(cs.PeerCertificates) == 0 {
return "", errors.New("the BMC sent no certificate")
}
return certSHA256(cs.PeerCertificates[0]), nil
}
// bmcBase builds https://host[:port] from the record; IPMI's port is not Redfish's.
func bmcBase(c *bmcCreds, scheme string, defPort int) string {
a := strings.TrimRight(c.Address, "/")
if strings.Contains(a, "://") { // an operator's https://host:port: scheme and authority only
u, err := url.Parse(a)
if err != nil || (u.Scheme != "https" && u.Scheme != "http") || u.Host == "" || u.User != nil {
return "https://invalid.invalid"
}
return u.Scheme + "://" + u.Host
}
port := c.Port
if port == 623 || port == 0 {
port = defPort
}
host := a
if strings.Contains(host, ":") && !strings.HasPrefix(host, "[") {
host = "[" + host + "]"
}
if (scheme == "https" && port == 443) || (scheme == "http" && port == 80) {
return scheme + "://" + host
}
return fmt.Sprintf("%s://%s:%d", scheme, host, port)
}
func asObj(v any) obj {
o, _ := v.(obj)
return o
}
// ---------------------------------------------------------------- Proxmox VE
func proxmoxOp(ctx context.Context, c *bmcCreds, op, arg string) (string, error) {
node, vmid := c.Extra["node"], c.Extra["vmid"]
if node == "" || vmid == "" {
return "", errors.New("proxmox driver needs extra node and vmid")
}
base := bmcBase(c, "https", 8006) + "/api2/json/nodes/" + url.PathEscape(node) + "/qemu/" + url.PathEscape(vmid)
hc := bmcHTTPClient(c)
call := func(method, path string, form url.Values) (obj, error) {
var rd io.Reader
if form != nil {
rd = strings.NewReader(form.Encode())
}
req, err := http.NewRequestWithContext(ctx, method, base+path, rd)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "PVEAPIToken="+c.User+"="+c.Password)
if form != nil {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}
resp, err := hc.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
data, _ := io.ReadAll(resp.Body)
if resp.StatusCode >= 300 {
return nil, fmt.Errorf("proxmox %s %s: %s %s", method, path, resp.Status, oneLine(string(data)))
}
var out obj
_ = json.Unmarshal(data, &out)
return out, nil
}
cur, err := call("GET", "/status/current", nil)
if err != nil {
return "", err
}
on := str(dig(cur, "data", "status")) == "running"
act := func(what string) (string, error) {
if _, err := call("POST", "/status/"+what, url.Values{}); err != nil {
return "", err
}
return "Chassis Power Control: " + what + " (proxmox)", nil
}
switch op {
case "status", "probe":
if on {
return "Chassis Power is on", nil
}
return "Chassis Power is off", nil
case "on":
if on {
return "Chassis Power is on already", nil
}
return act("start")
case "off":
if !on {
return "Chassis Power is off already", nil
}
return act("stop")
case "soft":
return act("shutdown")
case "cycle", "reset":
if !on {
return act("start")
}
return act("reset")
case "bootdev":
cfg, err := call("GET", "/config", nil)
if err != nil {
return "", err
}
d, _ := cfg["data"].(obj)
var disks, nets, cds []string
keys := make([]string, 0, len(d))
for k := range d {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
v := str(d[k])
switch {
case strings.HasPrefix(k, "net") && len(k) > 3 && k[3] >= '0' && k[3] <= '9':
nets = append(nets, k)
case strings.Contains(v, "media=cdrom"):
cds = append(cds, k)
case hasTag([]string{"scsi0", "virtio0", "sata0", "ide0", "nvme0"}, k):
disks = append(disks, k)
}
}
var order []string
switch arg {
case "pxe":
order = append(append(append(order, nets...), disks...), cds...)
case "cdrom":
order = append(append(append(order, cds...), disks...), nets...)
default:
order = append(append(append(order, disks...), cds...), nets...)
}
if _, err := call("PUT", "/config", url.Values{"boot": {"order=" + strings.Join(order, ";")}}); err != nil {
return "", err
}
return "Set Boot Device to " + arg + " (proxmox boot order " + strings.Join(order, ";") + ")", nil
}
return "", fmt.Errorf("proxmox: %s is not supported", op)
}