Job and node tokens travel in the per-MAC pxelinux file, readable by any host on the provisioning network over TFTP; with a job token one could fetch the deploy seed (user-data, tag cloud-config) or report the job, with a node token the stateless overlay (e.g. munge.key). The region knows the address it gives each machine (static DHCP leases from the network model): /jobs/self, job progress, phone-home and the node overlay are now refused from anywhere else (403, an event on the machine). Through a remote rack controller the address is the rack's own X-Forwarded-For entry, believed only from an authenticated rack. A machine without a fixed address (proxy DHCP) binds a job to the first address that uses it. agent_source_check: false turns this off. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
102 lines
3.1 KiB
Go
102 lines
3.1 KiB
Go
package main
|
|
|
|
// Where machine-side tokens may be used from. Job tokens (rollcall.job=) and
|
|
// node tokens (rollcall.node=) travel in the per-MAC pxelinux file, which any
|
|
// host on the provisioning network can fetch over TFTP; without a hardware
|
|
// root of trust the machine holds no secret of its own. What the region does
|
|
// know is the address it gives the machine (a static DHCP lease from the
|
|
// network model), so a token is only accepted from there. A machine without
|
|
// a fixed address (proxy DHCP, a site's DHCP server) binds a job to the
|
|
// first address that uses it. Refusals are events on the machine.
|
|
//
|
|
// agent_source_check: false in config.json turns this off (NAT between the
|
|
// machines and Rollcall).
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"net"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
// clientIP is the machine's address: a remote rack controller proxies the
|
|
// node API and appends it to X-Forwarded-For (the last entry is the rack's).
|
|
func (a *App) clientIP(r *http.Request) string {
|
|
host, _, err := net.SplitHostPort(r.RemoteAddr)
|
|
if err != nil {
|
|
host = r.RemoteAddr
|
|
}
|
|
if rack := a.fromRack(r); rack != "" && rack != a.cfg.RackName {
|
|
if xff := r.Header.Get("X-Forwarded-For"); xff != "" {
|
|
parts := strings.Split(xff, ",")
|
|
host = strings.TrimSpace(parts[len(parts)-1])
|
|
}
|
|
}
|
|
if ad, ok := parseAddr(host); ok {
|
|
return ad.String()
|
|
}
|
|
return host
|
|
}
|
|
|
|
// expectedIPs are the addresses the region hands this machine itself.
|
|
func (a *App) expectedIPs(ctx context.Context, m *Machine) []string {
|
|
var out []string
|
|
add := func(ip string) {
|
|
if ad, ok := parseAddr(ip); ok && !hasTag(out, ad.String()) {
|
|
out = append(out, ad.String())
|
|
}
|
|
}
|
|
subnets := a.subnetsSnapshot()
|
|
if ips, err := a.store.IPs(ctx, "", m.ID); err == nil {
|
|
for _, x := range ips {
|
|
if sn := subnetFor(subnets, x.IP); sn == nil || sn.DHCPMode != "proxy" {
|
|
add(x.IP)
|
|
}
|
|
}
|
|
}
|
|
if mg := deref(m.MgmtIP); mg != "" {
|
|
if sn := subnetFor(subnets, mg); sn == nil || sn.DHCPMode != "proxy" {
|
|
add(mg)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func (s *Store) bindJobIP(ctx context.Context, id, ip string) (string, error) {
|
|
var bound string
|
|
err := s.pool.QueryRow(ctx, `UPDATE jobs SET bound_ip=coalesce(bound_ip, $2) WHERE id=$1 RETURNING bound_ip`, id, ip).Scan(&bound)
|
|
return bound, err
|
|
}
|
|
|
|
// tokenSource checks where a machine token is used from. job is nil for node
|
|
// tokens and phone-home, which are checked against fixed addresses only.
|
|
func (a *App) tokenSource(ctx context.Context, r *http.Request, m *Machine, j *Job, what string) error {
|
|
if a.cfg.AgentSourceCheck != nil && !*a.cfg.AgentSourceCheck {
|
|
return nil
|
|
}
|
|
ip := a.clientIP(r)
|
|
want := a.expectedIPs(ctx, m)
|
|
var err error
|
|
switch {
|
|
case len(want) > 0:
|
|
if !hasTag(want, ip) {
|
|
err = fmt.Errorf("%s token used from %s; this machine is %s", what, ip, strings.Join(want, ", "))
|
|
}
|
|
case j != nil:
|
|
bound, berr := a.store.bindJobIP(ctx, j.ID, ip)
|
|
if berr != nil {
|
|
return berr
|
|
}
|
|
if bound != ip {
|
|
err = fmt.Errorf("%s token used from %s; job %s is bound to %s, where it was first used", what, ip, j.ID, bound)
|
|
}
|
|
}
|
|
if err != nil {
|
|
_ = a.store.AddEvent(ctx, m.ID, "token refused", err.Error())
|
|
log.Printf("%s: %v", m.ID, err)
|
|
}
|
|
return err
|
|
}
|