feat: add guarded periodic resource monitoring
Add checks-only scheduling, read-only status, private authenticated persistence and guarded PID1 lifecycle. Keep strategy selection/recovery and remote fault acceptance out of scope. Co-authored-by: GPT-6 <gpt-6@openai.com>
This commit is contained in:
1 parent
cfce8170d7
commit
4efc066625
16 files changed
+2307
-103
No files matched your search
@@ -14,6 +14,7 @@ import (
|
||||
"os"
|
||||
"os/signal"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
@@ -42,6 +43,8 @@ type resourceCheckSpec struct {
|
||||
Method string `json:"method"`
|
||||
TLS string `json:"tls"`
|
||||
TimeoutMS int `json:"timeout_ms"`
|
||||
IntervalMS *int `json:"interval_ms,omitempty"`
|
||||
CooldownMS *int `json:"cooldown_ms,omitempty"`
|
||||
Expect resourceCheckExpect `json:"expect"`
|
||||
HeadersEnv map[string]string `json:"headers_env,omitempty"`
|
||||
AllowPrivate bool `json:"allow_private,omitempty"`
|
||||
@@ -139,6 +142,18 @@ func parseResourceChecks(data []byte, lookup func(string) (string, bool)) (resou
|
||||
return fail()
|
||||
}
|
||||
ids[r.ID] = true
|
||||
for _, scheduling := range []struct {
|
||||
value **int
|
||||
fallback int
|
||||
}{{&r.IntervalMS, 300000}, {&r.CooldownMS, 30000}} {
|
||||
if *scheduling.value == nil {
|
||||
v := scheduling.fallback
|
||||
*scheduling.value = &v
|
||||
}
|
||||
if **scheduling.value < 100 || **scheduling.value > 86400000 {
|
||||
return fail()
|
||||
}
|
||||
}
|
||||
if (r.URL == "") == (r.URLEnv == "") {
|
||||
return fail()
|
||||
}
|
||||
@@ -202,7 +217,13 @@ func parseResourceChecks(data []byte, lookup func(string) (string, bool)) (resou
|
||||
return fail()
|
||||
}
|
||||
headers := map[string]bool{}
|
||||
for name, env := range r.HeadersEnv {
|
||||
names := make([]string, 0, len(r.HeadersEnv))
|
||||
for name := range r.HeadersEnv {
|
||||
names = append(names, name)
|
||||
}
|
||||
sort.Strings(names)
|
||||
for _, name := range names {
|
||||
env := r.HeadersEnv[name]
|
||||
lower := strings.ToLower(name)
|
||||
if !resourceHeader.MatchString(name) || headers[lower] || !resourceEnv.MatchString(env) {
|
||||
return fail()
|
||||
@@ -234,6 +255,10 @@ func parseResourceChecks(data []byte, lookup func(string) (string, bool)) (resou
|
||||
}
|
||||
|
||||
func readResourceChecks(path string, lookup func(string) (string, bool)) (resourceCheckConfig, error) {
|
||||
return readResourceChecksMode(path, lookup, false)
|
||||
}
|
||||
|
||||
func readResourceChecksMode(path string, lookup func(string) (string, bool), owned bool) (resourceCheckConfig, error) {
|
||||
fd, err := syscall.Open(path, syscall.O_RDONLY|syscall.O_NOFOLLOW|syscall.O_NONBLOCK|syscall.O_CLOEXEC, 0)
|
||||
if err != nil {
|
||||
return resourceCheckConfig{}, errResourceConfig
|
||||
@@ -244,10 +269,22 @@ func readResourceChecks(path string, lookup func(string) (string, bool)) (resour
|
||||
if err != nil || !info.Mode().IsRegular() || info.Size() > 1<<20 {
|
||||
return resourceCheckConfig{}, errResourceConfig
|
||||
}
|
||||
if owned {
|
||||
st, ok := info.Sys().(*syscall.Stat_t)
|
||||
if !ok || (st.Uid != 0 && st.Uid != uint32(os.Geteuid())) || st.Nlink != 1 || info.Mode().Perm()&0022 != 0 {
|
||||
return resourceCheckConfig{}, errResourceConfig
|
||||
}
|
||||
}
|
||||
data, err := io.ReadAll(io.LimitReader(f, (1<<20)+1))
|
||||
if err != nil {
|
||||
return resourceCheckConfig{}, errResourceConfig
|
||||
}
|
||||
if owned {
|
||||
current, err := os.Lstat(path)
|
||||
if err != nil || !os.SameFile(info, current) {
|
||||
return resourceCheckConfig{}, errResourceConfig
|
||||
}
|
||||
}
|
||||
return parseResourceChecks(data, lookup)
|
||||
}
|
||||
|
||||
@@ -329,6 +366,52 @@ type resourceCheckResult struct {
|
||||
Status int `json:"status"`
|
||||
}
|
||||
|
||||
// A synchronous runner: subprocess cancellation joins before this returns.
|
||||
// The guard is also called after DNS, immediately before the pinned transfer.
|
||||
func runResourceProtocol(ctx context.Context, service string, r resourceCheckSpec, protocol, server, proxy string, d resourceCheckDeps) (resourceCheckResult, error) {
|
||||
result := resourceCheckResult{Service: service, Resource: r.ID, Group: r.Group, TLS: protocol, Outcome: "dns_failed"}
|
||||
if err := d.guard(); err != nil {
|
||||
return result, err
|
||||
}
|
||||
probeCtx, cancel := context.WithTimeout(ctx, time.Duration(r.TimeoutMS)*time.Millisecond)
|
||||
defer cancel()
|
||||
ips, err := d.resolve(probeCtx, server, r.host)
|
||||
if err == nil && len(ips) > 0 && len(ips) <= 64 {
|
||||
approved := true
|
||||
for _, ip := range ips {
|
||||
if !approvedResourceIP(r, ip) {
|
||||
approved = false
|
||||
}
|
||||
}
|
||||
result.Outcome = "destination_denied"
|
||||
if approved {
|
||||
if err := d.guard(); err != nil {
|
||||
return result, err
|
||||
}
|
||||
pin := r.host + ":443:" + net.JoinHostPort(ips[0].String(), "443")
|
||||
options := resourceTransferOptions{generic: true, method: r.Method, limit: r.Expect.MaxBytes, timeout: time.Duration(r.TimeoutMS) * time.Millisecond, config: r.privateConfig + "connect-to = " + privateCurlValue(pin) + "\n"}
|
||||
transfer, _, err := collectResourceTransferOptions(probeCtx, proxy, r.URL, r.host, protocol, false, d.runner, options)
|
||||
result.Outcome = "transfer_failed"
|
||||
if err == nil {
|
||||
result.Bytes, result.TimeMS, result.Status = len(transfer.Body), transfer.LatencyMS, transfer.Status
|
||||
result.Outcome = "expectation_failed"
|
||||
for _, status := range r.Expect.Statuses {
|
||||
if status == transfer.Status && len(transfer.Body) >= r.Expect.MinBytes && (r.Expect.ContentType == "" || transfer.ContentType == r.Expect.ContentType) {
|
||||
result.Outcome = "pass"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if probeCtx.Err() != nil {
|
||||
result.Outcome = "timeout"
|
||||
if ctx.Err() != nil {
|
||||
result.Outcome = "cancelled"
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func resourceCheckCLI(ctx context.Context, path string, stdout, stderr io.Writer, d resourceCheckDeps) int {
|
||||
if d.lookup == nil {
|
||||
fmt.Fprintln(stderr, "configerror: invalid resource check configuration")
|
||||
@@ -357,57 +440,11 @@ func resourceCheckCLI(ctx context.Context, path string, stdout, stderr io.Writer
|
||||
if ctx.Err() != nil {
|
||||
return 1
|
||||
}
|
||||
if d.guard() != nil {
|
||||
result, err := runResourceProtocol(ctx, s.ID, r, protocol, server, proxy, d)
|
||||
if err != nil {
|
||||
fmt.Fprintln(stderr, "blocked: resource check ownership unavailable")
|
||||
return 2
|
||||
}
|
||||
result := resourceCheckResult{Service: s.ID, Resource: r.ID, Group: r.Group, TLS: protocol, Outcome: "dns_failed"}
|
||||
probeCtx, cancel := context.WithTimeout(ctx, time.Duration(r.TimeoutMS)*time.Millisecond)
|
||||
ips, err := d.resolve(probeCtx, server, r.host)
|
||||
if err == nil && len(ips) > 0 && len(ips) <= 64 {
|
||||
approved := true
|
||||
for _, ip := range ips {
|
||||
if !approvedResourceIP(r, ip) {
|
||||
approved = false
|
||||
}
|
||||
}
|
||||
result.Outcome = "destination_denied"
|
||||
if approved {
|
||||
if d.guard() != nil {
|
||||
cancel()
|
||||
fmt.Fprintln(stderr, "blocked: resource check ownership unavailable")
|
||||
return 2
|
||||
}
|
||||
// --connect-to changes the SOCKS CONNECT destination even for socks5h;
|
||||
// URL authority, HTTP Host, SNI and certificate verification stay original.
|
||||
pin := r.host + ":443:" + net.JoinHostPort(ips[0].String(), "443")
|
||||
options := resourceTransferOptions{generic: true, method: r.Method, limit: r.Expect.MaxBytes, timeout: time.Duration(r.TimeoutMS) * time.Millisecond, config: r.privateConfig + "connect-to = " + privateCurlValue(pin) + "\n"}
|
||||
transfer, _, err := collectResourceTransferOptions(probeCtx, proxy, r.URL, r.host, protocol, false, d.runner, options)
|
||||
result.Outcome = "transfer_failed"
|
||||
if err == nil {
|
||||
result.Bytes, result.TimeMS, result.Status = len(transfer.Body), transfer.LatencyMS, transfer.Status
|
||||
result.Outcome = "expectation_failed"
|
||||
statusOK := false
|
||||
for _, status := range r.Expect.Statuses {
|
||||
if status == transfer.Status {
|
||||
statusOK = true
|
||||
}
|
||||
}
|
||||
if statusOK {
|
||||
if len(transfer.Body) >= r.Expect.MinBytes && (r.Expect.ContentType == "" || transfer.ContentType == r.Expect.ContentType) {
|
||||
result.Outcome = "pass"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if probeCtx.Err() != nil {
|
||||
result.Outcome = "timeout"
|
||||
if ctx.Err() != nil {
|
||||
result.Outcome = "cancelled"
|
||||
}
|
||||
}
|
||||
cancel()
|
||||
if result.Outcome != "pass" {
|
||||
code = 1
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user