Each server runs an ultralight probe (scratch image, ~7 MiB RSS) that tails the logs, ships raw lines to the hub over HTTPS with an on-disk spool, and applies enforce decisions locally. The hub keeps one node per probe (primary node = the 1.x data dir, unchanged), does all analysis and serves the UI. Peer federation is removed. 'urutau import' brings 1.x data dirs in with hash and count verification. UI: probes page, host qualified incidents, IPs never wrap, egress heatmap labels, live and replay globes no longer auto-rotate, replay repeat, live traffic metrics. BREAKING CHANGE: URUTAU_PEERS, URUTAU_PEER_* and /api/v1/peer/* are gone; log path variables move from the hub to the probe. See docs/migration-2.0.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
215 lines
5.1 KiB
Go
215 lines
5.1 KiB
Go
// Package hub is the server side of the probe protocol: the registry of
|
|
// probes (probes.json), the per-node command queue and the ingest handlers.
|
|
package hub
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"crypto/sha256"
|
|
"crypto/subtle"
|
|
"encoding/base64"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/wire"
|
|
)
|
|
|
|
// TokenPrefix starts every probe token.
|
|
const TokenPrefix = "urp_"
|
|
|
|
// Egress is the per-probe configuration of the egress analysis.
|
|
type Egress struct {
|
|
Workloads string `json:"workloads,omitempty"`
|
|
Resolvers string `json:"resolvers,omitempty"`
|
|
Allow string `json:"allow,omitempty"`
|
|
}
|
|
|
|
// Probe is one entry of probes.json. The token is never stored, only its hash.
|
|
type Probe struct {
|
|
Name string `json:"name"`
|
|
TokenSHA256 string `json:"token_sha256"`
|
|
Created int64 `json:"created"`
|
|
Revoked bool `json:"revoked"`
|
|
Location *wire.Location `json:"location,omitempty"`
|
|
Egress Egress `json:"egress"`
|
|
}
|
|
|
|
// Errors of the registry.
|
|
var (
|
|
ErrExists = errors.New("probe already exists")
|
|
ErrNotFound = errors.New("probe not found")
|
|
ErrName = errors.New("invalid probe name (a-z, 0-9 and -, at most 32 characters)")
|
|
)
|
|
|
|
// Registry is probes.json.
|
|
type Registry struct {
|
|
mu sync.Mutex
|
|
path string
|
|
probes map[string]*Probe
|
|
}
|
|
|
|
type regFile struct {
|
|
Probes []*Probe `json:"probes"`
|
|
}
|
|
|
|
// OpenRegistry loads path (a missing file is an empty registry).
|
|
func OpenRegistry(path string) (*Registry, error) {
|
|
r := &Registry{path: path, probes: map[string]*Probe{}}
|
|
b, err := os.ReadFile(path)
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
return r, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var f regFile
|
|
if err := json.Unmarshal(b, &f); err != nil {
|
|
return nil, fmt.Errorf("%s: %w", path, err)
|
|
}
|
|
for _, p := range f.Probes {
|
|
if p != nil && wire.ValidName(p.Name) {
|
|
r.probes[p.Name] = p
|
|
}
|
|
}
|
|
return r, nil
|
|
}
|
|
|
|
// save writes the file atomically with mode 0600; the caller holds mu.
|
|
func (r *Registry) save() error {
|
|
list := make([]*Probe, 0, len(r.probes))
|
|
for _, p := range r.probes {
|
|
list = append(list, p)
|
|
}
|
|
sort.Slice(list, func(i, j int) bool { return list[i].Name < list[j].Name })
|
|
b, err := json.MarshalIndent(regFile{Probes: list}, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := os.MkdirAll(filepath.Dir(r.path), 0o700); err != nil {
|
|
return err
|
|
}
|
|
tmp := r.path + ".tmp"
|
|
if err := os.WriteFile(tmp, b, 0o600); err != nil {
|
|
return err
|
|
}
|
|
if err := os.Chmod(tmp, 0o600); err != nil {
|
|
return err
|
|
}
|
|
return os.Rename(tmp, r.path)
|
|
}
|
|
|
|
// HashToken is the hex sha256 of a token.
|
|
func HashToken(tok string) string {
|
|
h := sha256.Sum256([]byte(tok))
|
|
return hex.EncodeToString(h[:])
|
|
}
|
|
|
|
// NewToken returns a fresh token: "urp_" + 43 base64url characters.
|
|
func NewToken() (string, error) {
|
|
var b [32]byte
|
|
if _, err := rand.Read(b[:]); err != nil {
|
|
return "", err
|
|
}
|
|
return TokenPrefix + base64.RawURLEncoding.EncodeToString(b[:]), nil
|
|
}
|
|
|
|
// Create registers a probe and returns its token, shown only once. Creating a
|
|
// probe that was revoked replaces its token and makes it active again.
|
|
func (r *Registry) Create(name string) (string, error) {
|
|
if !wire.ValidName(name) || name == "all" {
|
|
return "", ErrName
|
|
}
|
|
tok, err := NewToken()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
if p, ok := r.probes[name]; ok && !p.Revoked {
|
|
return "", ErrExists
|
|
}
|
|
p := r.probes[name]
|
|
if p == nil {
|
|
p = &Probe{Name: name, Created: time.Now().Unix()}
|
|
r.probes[name] = p
|
|
}
|
|
p.TokenSHA256, p.Revoked = HashToken(tok), false
|
|
if err := r.save(); err != nil {
|
|
delete(r.probes, name)
|
|
return "", err
|
|
}
|
|
return tok, nil
|
|
}
|
|
|
|
// Revoke disables the token of a probe; its data stays.
|
|
func (r *Registry) Revoke(name string) error {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
p, ok := r.probes[name]
|
|
if !ok {
|
|
return ErrNotFound
|
|
}
|
|
p.Revoked = true
|
|
return r.save()
|
|
}
|
|
|
|
// Update replaces the location and the egress configuration of a probe.
|
|
func (r *Registry) Update(name string, loc *wire.Location, eg Egress) error {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
p, ok := r.probes[name]
|
|
if !ok {
|
|
return ErrNotFound
|
|
}
|
|
p.Location, p.Egress = loc, eg
|
|
return r.save()
|
|
}
|
|
|
|
// Verify checks a token in constant time; revoked and unknown probes fail.
|
|
func (r *Registry) Verify(name, token string) bool {
|
|
r.mu.Lock()
|
|
p := r.probes[name]
|
|
var want string
|
|
revoked := true
|
|
if p != nil {
|
|
want, revoked = p.TokenSHA256, p.Revoked
|
|
}
|
|
r.mu.Unlock()
|
|
if want == "" {
|
|
want = strings.Repeat("0", 64)
|
|
}
|
|
got := HashToken(token)
|
|
ok := subtle.ConstantTimeCompare([]byte(got), []byte(want)) == 1
|
|
return ok && !revoked
|
|
}
|
|
|
|
// Get returns a copy of the entry.
|
|
func (r *Registry) Get(name string) (Probe, bool) {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
p, ok := r.probes[name]
|
|
if !ok {
|
|
return Probe{}, false
|
|
}
|
|
return *p, true
|
|
}
|
|
|
|
// List returns every entry sorted by name.
|
|
func (r *Registry) List() []Probe {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
out := make([]Probe, 0, len(r.probes))
|
|
for _, p := range r.probes {
|
|
out = append(out, *p)
|
|
}
|
|
sort.Slice(out, func(i, j int) bool { return out[i].Name < out[j].Name })
|
|
return out
|
|
}
|