mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-12-03 18:43:38 +01:00
Validate repeated root and destination members before resolving observer credentials. Preserve singleton aliases, explicit transport and provisioning choices, and saved primary authority on refusal and correction. Keep the pending hosted lifecycle candidate separate. Change-source: pulse-maintainer
334 lines
11 KiB
Go
334 lines
11 KiB
Go
package agenttarget
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strconv"
|
|
"strings"
|
|
"unicode"
|
|
|
|
"github.com/rcourtman/pulse-go-rewrite/pkg/securityutil"
|
|
)
|
|
|
|
const ConfigVersion = 1
|
|
|
|
const maxConfigBytes = 256 * 1024
|
|
const maxObservers = 16
|
|
const maxTokenBytes = 64 * 1024
|
|
|
|
// Observer is a report-only Pulse destination. The primary Pulse URL remains
|
|
// the sole authority for remote configuration, commands, and agent updates.
|
|
type Observer struct {
|
|
Name string
|
|
URL string
|
|
Token string
|
|
InsecureSkipVerify bool
|
|
AllowPlaintextHTTP bool
|
|
CACertPath string
|
|
ServerFingerprint string
|
|
ProvisionProxmox bool
|
|
ID string
|
|
}
|
|
|
|
type observerFile struct {
|
|
Version int `json:"version"`
|
|
Observers []observerFileTarget `json:"observers"`
|
|
}
|
|
|
|
type observerFileTarget struct {
|
|
Name string `json:"name"`
|
|
URL string `json:"url"`
|
|
TokenFile string `json:"tokenFile"`
|
|
InsecureSkipVerify bool `json:"insecureSkipVerify,omitempty"`
|
|
AllowPlaintextHTTP bool `json:"allowPlaintextHTTP,omitempty"`
|
|
CACertPath string `json:"caCertPath,omitempty"`
|
|
ServerFingerprint string `json:"serverFingerprint,omitempty"`
|
|
ProvisionProxmox *bool `json:"provisionProxmox,omitempty"`
|
|
}
|
|
|
|
// LoadObservers validates and resolves an owner-private observer configuration.
|
|
// Raw tokens are never accepted in the JSON document; each destination must
|
|
// reference a separately permissioned token file.
|
|
func LoadObservers(path string, primaryURL string) ([]Observer, error) {
|
|
path = strings.TrimSpace(path)
|
|
if path == "" {
|
|
return nil, nil
|
|
}
|
|
if !filepath.IsAbs(path) {
|
|
return nil, errors.New("observer config path must be absolute")
|
|
}
|
|
data, err := readPrivateRegularFile(path, "observer config", maxConfigBytes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
dec := json.NewDecoder(bytes.NewReader(data))
|
|
dec.DisallowUnknownFields()
|
|
var parsed observerFile
|
|
if err := dec.Decode(&parsed); err != nil {
|
|
return nil, fmt.Errorf("decode observer config: %w", err)
|
|
}
|
|
if err := ensureJSONEOF(dec); err != nil {
|
|
return nil, err
|
|
}
|
|
if parsed.Version != ConfigVersion {
|
|
return nil, fmt.Errorf("observer config version %d is unsupported; expected %d", parsed.Version, ConfigVersion)
|
|
}
|
|
if len(parsed.Observers) > maxObservers {
|
|
return nil, fmt.Errorf("observer config has %d destinations; maximum is %d", len(parsed.Observers), maxObservers)
|
|
}
|
|
if err := rejectDuplicateObserverFields(data, true); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
normalizedPrimary := ""
|
|
if strings.TrimSpace(primaryURL) != "" {
|
|
primary, err := NormalizePulseURL(primaryURL, true, true)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("normalize primary Pulse URL: %w", err)
|
|
}
|
|
normalizedPrimary = primary
|
|
}
|
|
|
|
result := make([]Observer, 0, len(parsed.Observers))
|
|
seenNames := make(map[string]struct{}, len(parsed.Observers))
|
|
seenURLs := make(map[string]struct{}, len(parsed.Observers))
|
|
for index, item := range parsed.Observers {
|
|
observer, err := resolveObserver(item)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("observer %d: %w", index+1, err)
|
|
}
|
|
nameKey := strings.ToLower(observer.Name)
|
|
if _, exists := seenNames[nameKey]; exists {
|
|
return nil, fmt.Errorf("observer %d: duplicate name %q", index+1, observer.Name)
|
|
}
|
|
if _, exists := seenURLs[observer.URL]; exists {
|
|
return nil, fmt.Errorf("observer %d: duplicate URL", index+1)
|
|
}
|
|
if normalizedPrimary != "" && observer.URL == normalizedPrimary {
|
|
return nil, fmt.Errorf("observer %d: URL duplicates the primary Pulse destination", index+1)
|
|
}
|
|
seenNames[nameKey] = struct{}{}
|
|
seenURLs[observer.URL] = struct{}{}
|
|
result = append(result, observer)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// The strict schema/type/EOF pass above runs first, so these objects contain
|
|
// only the fixed root or destination fields. Do not let encoding/json's
|
|
// last-value-wins rule choose competing destinations or trust/provisioning
|
|
// settings. EqualFold preserves its singleton field aliases, including Unicode
|
|
// aliases, while also detecting JSON-escaped duplicates. Check the whole file
|
|
// before resolving any token file; diagnostics must not echo keys or values.
|
|
func rejectDuplicateObserverFields(data []byte, root bool) error {
|
|
decoder := json.NewDecoder(bytes.NewReader(data))
|
|
start, err := decoder.Token()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if start != json.Delim('{') {
|
|
return nil // A null destination retains its ordinary validation error.
|
|
}
|
|
var names []string
|
|
for decoder.More() {
|
|
token, err := decoder.Token()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
name := token.(string) // The schema pass already validated object syntax.
|
|
for _, previous := range names {
|
|
if strings.EqualFold(name, previous) {
|
|
return errors.New("observer config contains repeated JSON fields; use each field once")
|
|
}
|
|
}
|
|
names = append(names, name)
|
|
var value json.RawMessage
|
|
if err := decoder.Decode(&value); err != nil {
|
|
return err
|
|
}
|
|
if root && strings.EqualFold(name, "observers") {
|
|
var destinations []json.RawMessage
|
|
if err := json.Unmarshal(value, &destinations); err != nil {
|
|
return err
|
|
}
|
|
for _, destination := range destinations {
|
|
if err := rejectDuplicateObserverFields(destination, false); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func resolveObserver(item observerFileTarget) (Observer, error) {
|
|
name := strings.TrimSpace(item.Name)
|
|
if err := validateName(name); err != nil {
|
|
return Observer{}, err
|
|
}
|
|
url, err := NormalizePulseURL(item.URL, false, item.AllowPlaintextHTTP)
|
|
if err != nil {
|
|
return Observer{}, fmt.Errorf("invalid URL: %w", err)
|
|
}
|
|
|
|
tokenPath := strings.TrimSpace(item.TokenFile)
|
|
if !filepath.IsAbs(tokenPath) {
|
|
return Observer{}, errors.New("tokenFile must be an absolute path")
|
|
}
|
|
tokenBytes, err := readPrivateRegularFile(tokenPath, "observer token", maxTokenBytes)
|
|
if err != nil {
|
|
return Observer{}, err
|
|
}
|
|
token := strings.TrimSpace(string(tokenBytes))
|
|
if token == "" {
|
|
return Observer{}, errors.New("observer token file is empty")
|
|
}
|
|
if strings.IndexFunc(token, unicode.IsSpace) >= 0 {
|
|
return Observer{}, errors.New("observer token contains whitespace")
|
|
}
|
|
|
|
provisionProxmox := true
|
|
if item.ProvisionProxmox != nil {
|
|
provisionProxmox = *item.ProvisionProxmox
|
|
}
|
|
return Observer{
|
|
Name: name,
|
|
URL: url,
|
|
Token: token,
|
|
InsecureSkipVerify: item.InsecureSkipVerify,
|
|
AllowPlaintextHTTP: item.AllowPlaintextHTTP,
|
|
CACertPath: strings.TrimSpace(item.CACertPath),
|
|
ServerFingerprint: strings.TrimSpace(item.ServerFingerprint),
|
|
ProvisionProxmox: provisionProxmox,
|
|
ID: stableID(url),
|
|
}, nil
|
|
}
|
|
|
|
// NormalizePulseURL applies the transport policy for one report destination.
|
|
// Authoritative primary destinations retain the established self-hosted
|
|
// local-network HTTP allowance. Report-only observers require an explicit
|
|
// per-destination opt-in for every non-loopback plaintext URL, even when the
|
|
// process-wide primary override is enabled.
|
|
func NormalizePulseURL(raw string, authoritative bool, allowPlaintext bool) (string, error) {
|
|
if !authoritative && !allowPlaintext {
|
|
parsed, err := url.Parse(strings.TrimSpace(raw))
|
|
if err == nil && parsed.Hostname() != "" && strings.EqualFold(parsed.Scheme, "http") &&
|
|
!securityutil.IsLoopbackHost(parsed.Hostname()) {
|
|
return "", errors.New("Pulse URL must use https unless the observer explicitly allows plaintext HTTP")
|
|
}
|
|
}
|
|
parsed, err := securityutil.NormalizePulseHTTPBaseURLWithOptions(raw, securityutil.PulseURLValidationOptions{
|
|
AllowLocalNetworkHTTP: authoritative,
|
|
AllowOperatorPlaintextHTTP: allowPlaintext,
|
|
})
|
|
if err != nil {
|
|
return "", pulseURLDiagnostic(raw, authoritative)
|
|
}
|
|
return strings.TrimRight(parsed.String(), "/"), nil
|
|
}
|
|
|
|
// The shared normalizer's refusal remains authoritative. Its errors can
|
|
// contain the entire rejected URL (including credentials and query values), so
|
|
// agent startup/configuration diagnostics must not wrap or print those errors.
|
|
// Classify a failure only to retain useful, content-free correction guidance.
|
|
func pulseURLDiagnostic(raw string, authoritative bool) error {
|
|
if strings.TrimSpace(raw) == "" {
|
|
return errors.New("Pulse URL is required")
|
|
}
|
|
parsed, err := url.Parse(strings.TrimSpace(raw))
|
|
if err != nil {
|
|
return errors.New("Pulse URL is invalid; check the host, port and URL escaping")
|
|
}
|
|
if parsed.Scheme == "" {
|
|
return errors.New("Pulse URL must include scheme (https:// or permitted http://)")
|
|
}
|
|
if parsed.Host == "" || parsed.Hostname() == "" {
|
|
return errors.New("Pulse URL must include host")
|
|
}
|
|
if parsed.User != nil {
|
|
return errors.New("Pulse URL must not include user credentials")
|
|
}
|
|
if parsed.RawQuery != "" || parsed.Fragment != "" {
|
|
return errors.New("Pulse URL must not include query or fragment")
|
|
}
|
|
if port := parsed.Port(); port != "" {
|
|
value, err := strconv.Atoi(port)
|
|
if err != nil || value < 1 || value > 65535 {
|
|
return errors.New("Pulse URL has invalid port: must be between 1 and 65535")
|
|
}
|
|
}
|
|
switch strings.ToLower(parsed.Scheme) {
|
|
case "http":
|
|
if authoritative {
|
|
return errors.New("Pulse URL must use https unless host is loopback or local/private")
|
|
}
|
|
return errors.New("Pulse URL must use https unless the observer explicitly allows plaintext HTTP")
|
|
case "https":
|
|
return errors.New("Pulse URL is invalid")
|
|
default:
|
|
return errors.New("Pulse URL has unsupported scheme; use https:// or permitted http://")
|
|
}
|
|
}
|
|
|
|
func validateName(name string) error {
|
|
if name == "" {
|
|
return errors.New("name is required")
|
|
}
|
|
if len(name) > 64 {
|
|
return errors.New("name must be 64 characters or fewer")
|
|
}
|
|
for _, r := range name {
|
|
if unicode.IsLetter(r) || unicode.IsDigit(r) || r == '-' || r == '_' || r == '.' {
|
|
continue
|
|
}
|
|
return fmt.Errorf("name %q contains unsupported characters", name)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func requirePrivateRegularFile(path string, label string, maxBytes int64) error {
|
|
info, err := os.Lstat(path)
|
|
if err != nil {
|
|
return fmt.Errorf("stat %s file: %w", label, err)
|
|
}
|
|
return validatePrivateRegularFile(info, label, maxBytes)
|
|
}
|
|
|
|
func validatePrivateRegularFile(info os.FileInfo, label string, maxBytes int64) error {
|
|
if info.Mode()&os.ModeSymlink != 0 || !info.Mode().IsRegular() {
|
|
return fmt.Errorf("%s file must be a regular file, not a symlink", label)
|
|
}
|
|
if info.Size() > maxBytes {
|
|
return fmt.Errorf("%s file exceeds the %d-byte size limit", label, maxBytes)
|
|
}
|
|
if runtime.GOOS != "windows" && info.Mode().Perm()&0o077 != 0 {
|
|
return fmt.Errorf("%s file permissions must not grant group or other access", label)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ensureJSONEOF(dec *json.Decoder) error {
|
|
var extra any
|
|
if err := dec.Decode(&extra); err == io.EOF {
|
|
return nil
|
|
} else if err != nil {
|
|
return fmt.Errorf("decode observer config trailer: %w", err)
|
|
}
|
|
return errors.New("observer config contains multiple JSON values")
|
|
}
|
|
|
|
func stableID(url string) string {
|
|
sum := sha256.Sum256([]byte(strings.TrimSpace(url)))
|
|
return hex.EncodeToString(sum[:8])
|
|
}
|