Select bounded canonical strategy allowlists at explicit PID1 startup within the same immutable authenticated registry, matrix and source. Reconstruct stored policies independently for historical replay, reconciliation and rollback; preserve freshness for new policies even when rendered bytes match. Retain legacy refusal and one-shot startup guards. Local fixtures and independent local review passed; live native rotation acceptance remains pending. No uninterrupted rotation, new qualification or proof-store overwrite. Co-authored-by: GPT-6 <gpt-6@openai.com>
340 lines
11 KiB
Go
340 lines
11 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
var errResourcePlan = errors.New("invalid private qualification data")
|
|
|
|
// Input groups are metadata. Only the HTTPS collector's selectors are supported.
|
|
type strategySelectors struct {
|
|
Transport string `json:"transport"`
|
|
Ports string `json:"ports"`
|
|
L7 string `json:"l7"`
|
|
TLS string `json:"tls"`
|
|
}
|
|
type strategyDefinition struct {
|
|
ID string `json:"id"`
|
|
Args string `json:"args"`
|
|
Selectors strategySelectors `json:"selectors"`
|
|
}
|
|
type strategyRegistry struct {
|
|
Version int `json:"version"`
|
|
Strategies []strategyDefinition `json:"strategies"`
|
|
}
|
|
|
|
func strictPlanJSON(b []byte, dst interface{}) error {
|
|
if len(b) > 65536 {
|
|
return errResourcePlan
|
|
}
|
|
d := json.NewDecoder(bytes.NewReader(b))
|
|
if uniqueResourceJSON(d, 0) != nil {
|
|
return errResourcePlan
|
|
}
|
|
if _, err := d.Token(); err != io.EOF {
|
|
return errResourcePlan
|
|
}
|
|
d = json.NewDecoder(bytes.NewReader(b))
|
|
d.DisallowUnknownFields()
|
|
if d.Decode(dst) != nil {
|
|
return errResourcePlan
|
|
}
|
|
return nil
|
|
}
|
|
func planDigest(v interface{}) string {
|
|
b, _ := json.Marshal(v)
|
|
h := sha256.Sum256(b)
|
|
return hex.EncodeToString(h[:])
|
|
}
|
|
func canonicalStrategy(d strategyDefinition) (strategyDefinition, error) {
|
|
d.Args = strings.Join(strings.Fields(d.Args), " ")
|
|
if !resourceID.MatchString(d.ID) || d.Args == "" || len(d.Args) > 4096 || (&candidate{Args: d.Args}).validateArgs() != nil {
|
|
return d, errResourcePlan
|
|
}
|
|
if d.Selectors.Transport != "tcp" || d.Selectors.Ports != "443" || d.Selectors.L7 != "tls" || (d.Selectors.TLS != "both" && d.Selectors.TLS != "tls12" && d.Selectors.TLS != "tls13") {
|
|
return d, errResourcePlan
|
|
}
|
|
for _, arg := range strings.Fields(d.Args) {
|
|
option, value, _ := strings.Cut(arg, "=")
|
|
if option == "--lua-init" {
|
|
return d, errResourcePlan
|
|
}
|
|
if option != "--lua-desync" {
|
|
continue
|
|
}
|
|
parts := strings.Split(value, ":")
|
|
switch parts[0] {
|
|
case "fake", "multisplit", "multidisorder", "fakeddisorder", "split", "disorder":
|
|
default:
|
|
return d, errResourcePlan
|
|
}
|
|
for _, part := range parts[1:] {
|
|
key, v, ok := strings.Cut(part, "=")
|
|
if !ok || v == "" || strings.ContainsAny(v, "/@%") || strings.Contains(v, "..") {
|
|
return d, errResourcePlan
|
|
}
|
|
switch key {
|
|
case "pos", "repeats", "tcp_ts", "tcp_flags_set", "seqovl":
|
|
case "blob":
|
|
if v != "fake_default_tls" {
|
|
return d, errResourcePlan
|
|
}
|
|
default:
|
|
return d, errResourcePlan
|
|
}
|
|
}
|
|
}
|
|
return d, nil
|
|
}
|
|
func parseStrategyRegistry(b []byte) (strategyRegistry, error) {
|
|
var r strategyRegistry
|
|
if strictPlanJSON(b, &r) != nil || r.Version != 1 || len(r.Strategies) == 0 || len(r.Strategies) > 32 {
|
|
return r, errResourcePlan
|
|
}
|
|
seen := map[string]bool{}
|
|
for i, d := range r.Strategies {
|
|
v, err := canonicalStrategy(d)
|
|
if err != nil || seen[v.ID] {
|
|
return r, errResourcePlan
|
|
}
|
|
seen[v.ID] = true
|
|
r.Strategies[i] = v
|
|
}
|
|
sort.Slice(r.Strategies, func(i, j int) bool { return r.Strategies[i].ID < r.Strategies[j].ID })
|
|
return r, nil
|
|
}
|
|
|
|
type qualificationCheck struct {
|
|
Service string `json:"service"`
|
|
Resource string `json:"resource"`
|
|
Fingerprint string `json:"fingerprint"`
|
|
TLS string `json:"tls"`
|
|
Outcome string `json:"outcome"`
|
|
Attempts int `json:"attempts"`
|
|
}
|
|
type qualificationRun struct {
|
|
ID string `json:"id"`
|
|
Strategy string `json:"strategy"`
|
|
Definition string `json:"definition"`
|
|
Config string `json:"config"`
|
|
Started int64 `json:"started_ms"`
|
|
Completed int64 `json:"completed_ms"`
|
|
Attestation qualificationAttestation `json:"attestation"`
|
|
Checks []qualificationCheck `json:"checks"`
|
|
}
|
|
type qualificationAttestation struct {
|
|
Collector string `json:"collector"`
|
|
Runtime string `json:"runtime"`
|
|
Definition string `json:"definition"`
|
|
LuaHash string `json:"lua_hash"`
|
|
RulesHash string `json:"rules_hash"`
|
|
WorkerArgsHash string `json:"worker_args_hash"`
|
|
Queue int `json:"queue"`
|
|
PID int `json:"pid"`
|
|
StartTime int64 `json:"starttime"`
|
|
Restored bool `json:"restored"`
|
|
Joined bool `json:"joined"`
|
|
}
|
|
type qualificationMatrix struct {
|
|
Version int `json:"version"`
|
|
Registry string `json:"registry"`
|
|
Runs []qualificationRun `json:"runs"`
|
|
}
|
|
|
|
// Only the owned producer (or internal test fixtures) constructs this capability.
|
|
// JSON imports cannot authorize writes, and post-seal DTO mutation is rejected.
|
|
type completedQualification struct {
|
|
matrix qualificationMatrix
|
|
seal string
|
|
}
|
|
|
|
func hashOK(s string) bool {
|
|
b, e := hex.DecodeString(s)
|
|
return e == nil && len(b) == 32 && strings.ToLower(s) == s
|
|
}
|
|
func validateMatrix(m qualificationMatrix, registry strategyRegistry) error {
|
|
if m.Version != 1 || m.Registry != planDigest(registry) || len(m.Runs) > 32 {
|
|
return errResourcePlan
|
|
}
|
|
defs := map[string]strategyDefinition{}
|
|
for _, d := range registry.Strategies {
|
|
defs[d.ID] = d
|
|
}
|
|
ids, strategies := map[string]bool{}, map[string]bool{}
|
|
for _, r := range m.Runs {
|
|
d, ok := defs[r.Strategy]
|
|
a := r.Attestation
|
|
if !ok || !resourceID.MatchString(r.ID) || ids[r.ID] || strategies[r.Strategy] || r.Definition != planDigest(d) || !hashOK(r.Config) || r.Started <= 0 || r.Completed < r.Started || r.Completed-r.Started > 86400000 || len(r.Checks) == 0 || len(r.Checks) > 128 {
|
|
return errResourcePlan
|
|
}
|
|
ids[r.ID], strategies[r.Strategy] = true, true
|
|
if a.Collector != "owned-resource-nfq-v1" || a.Runtime != "nfqws2-1.0.2" || a.Definition != r.Definition || !hashOK(a.LuaHash) || !hashOK(a.RulesHash) || !hashOK(a.WorkerArgsHash) || a.Queue != managedAssetQueue || a.PID <= 0 || a.StartTime <= 0 || !a.Restored || !a.Joined {
|
|
return errResourcePlan
|
|
}
|
|
seen := map[string]bool{}
|
|
for _, c := range r.Checks {
|
|
id := c.Service + "/" + c.Resource + "/" + c.TLS
|
|
if !resourceID.MatchString(c.Service) || !resourceID.MatchString(c.Resource) || !hashOK(c.Fingerprint) || seen[id] || c.Attempts != 3 || (c.TLS != "tls12" && c.TLS != "tls13") {
|
|
return errResourcePlan
|
|
}
|
|
seen[id] = true
|
|
switch c.Outcome {
|
|
case "pass", "dns_failed", "destination_denied", "transfer_failed", "expectation_failed", "timeout":
|
|
default:
|
|
return errResourcePlan
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type domainCompatibility struct {
|
|
Host string `json:"host"`
|
|
Validated []string `json:"validated"`
|
|
Negative []string `json:"negative"`
|
|
Unknown []string `json:"unknown"`
|
|
}
|
|
type runtimeAssignment struct {
|
|
Strategy string `json:"strategy"`
|
|
Args string `json:"args"`
|
|
Selectors strategySelectors `json:"selectors"`
|
|
Hosts []string `json:"hosts"`
|
|
}
|
|
type resourcePlan struct {
|
|
ProposalOnly bool `json:"proposal_only"`
|
|
Policy string `json:"strategy_ids,omitempty"`
|
|
Evidence string `json:"evidence"`
|
|
Domains []domainCompatibility `json:"domains"`
|
|
Assignments []runtimeAssignment `json:"assignments"`
|
|
Unassigned []string `json:"unassigned"`
|
|
}
|
|
|
|
func deriveResourcePlan(source resourceMonitorSource, key []byte, registry strategyRegistry, matrix qualificationMatrix, now time.Time, policy ...string) resourcePlan {
|
|
p := resourcePlan{ProposalOnly: true, Evidence: "unknown", Domains: []domainCompatibility{}, Assignments: []runtimeAssignment{}, Unassigned: []string{}}
|
|
if len(policy) != 0 {
|
|
p.Policy = policy[0]
|
|
}
|
|
hosts := map[string][]qualificationCheck{}
|
|
for _, s := range source.config.Services {
|
|
for _, r := range s.Resources {
|
|
protocols := []string{r.TLS}
|
|
if r.TLS == "both" {
|
|
protocols = []string{"tls12", "tls13"}
|
|
}
|
|
for _, tls := range protocols {
|
|
host := strings.ToLower(r.host)
|
|
hosts[host] = append(hosts[host], qualificationCheck{Service: s.ID, Resource: r.ID, Fingerprint: resourceRowFingerprint(key, source, r), TLS: tls})
|
|
}
|
|
}
|
|
}
|
|
runs := map[string]qualificationRun{}
|
|
if validateMatrix(matrix, registry) == nil {
|
|
p.Evidence = "authenticated"
|
|
for _, r := range matrix.Runs {
|
|
if r.Config == source.fingerprint && r.Completed <= now.UnixMilli() && now.UnixMilli()-r.Completed <= 86400000 {
|
|
runs[r.Strategy] = r
|
|
}
|
|
}
|
|
}
|
|
names := []string{}
|
|
for host := range hosts {
|
|
names = append(names, host)
|
|
}
|
|
sort.Strings(names)
|
|
for _, host := range names {
|
|
row := domainCompatibility{Host: host, Validated: []string{}, Negative: []string{}, Unknown: []string{}}
|
|
for _, d := range registry.Strategies {
|
|
r, exists := runs[d.ID]
|
|
checks := map[string]qualificationCheck{}
|
|
for _, c := range r.Checks {
|
|
checks[c.Service+"/"+c.Resource+"/"+c.TLS] = c
|
|
}
|
|
complete, failed := exists, false
|
|
for _, expected := range hosts[host] {
|
|
c, ok := checks[expected.Service+"/"+expected.Resource+"/"+expected.TLS]
|
|
if !ok || c.Fingerprint != expected.Fingerprint || (d.Selectors.TLS != "both" && d.Selectors.TLS != expected.TLS) {
|
|
complete = false
|
|
continue
|
|
}
|
|
if c.Outcome != "pass" {
|
|
failed = true
|
|
}
|
|
}
|
|
if !complete {
|
|
row.Unknown = append(row.Unknown, d.ID)
|
|
} else if failed {
|
|
row.Negative = append(row.Negative, d.ID)
|
|
} else {
|
|
row.Validated = append(row.Validated, d.ID)
|
|
}
|
|
}
|
|
p.Domains = append(p.Domains, row)
|
|
}
|
|
// Merge only equal canonical args/selectors, retaining per-candidate alternatives.
|
|
// Greatest uncovered coverage is deterministic greedy, not globally optimal.
|
|
profiles := map[string]runtimeAssignment{}
|
|
coverage := map[string]map[string]bool{}
|
|
for _, d := range registry.Strategies {
|
|
if len(policy) != 0 && policy[0] != "" && !strings.Contains(","+policy[0]+",", ","+d.ID+",") {
|
|
continue
|
|
}
|
|
profile := planDigest(struct {
|
|
Args string
|
|
Selectors strategySelectors
|
|
}{d.Args, d.Selectors})
|
|
a, exists := profiles[profile]
|
|
if !exists || d.ID < a.Strategy {
|
|
profiles[profile] = runtimeAssignment{Strategy: d.ID, Args: d.Args, Selectors: d.Selectors}
|
|
}
|
|
if coverage[profile] == nil {
|
|
coverage[profile] = map[string]bool{}
|
|
}
|
|
for _, row := range p.Domains {
|
|
for _, id := range row.Validated {
|
|
if id == d.ID {
|
|
coverage[profile][row.Host] = true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
assigned := map[string]bool{}
|
|
for {
|
|
best := ""
|
|
var chosen []string
|
|
for profile, a := range profiles {
|
|
uncovered := []string{}
|
|
for _, host := range names {
|
|
if coverage[profile][host] && !assigned[host] {
|
|
uncovered = append(uncovered, host)
|
|
}
|
|
}
|
|
if len(uncovered) > len(chosen) || len(uncovered) > 0 && len(uncovered) == len(chosen) && (best == "" || a.Strategy < profiles[best].Strategy) {
|
|
best, chosen = profile, uncovered
|
|
}
|
|
}
|
|
if len(chosen) == 0 {
|
|
break
|
|
}
|
|
a := profiles[best]
|
|
a.Hosts = chosen
|
|
p.Assignments = append(p.Assignments, a)
|
|
for _, host := range chosen {
|
|
assigned[host] = true
|
|
}
|
|
}
|
|
for _, host := range names {
|
|
if !assigned[host] {
|
|
p.Unassigned = append(p.Unassigned, host)
|
|
}
|
|
}
|
|
return p
|
|
}
|