0.38.6_agent_hotfix

This commit is contained in:
Mateusz Gruszczyński committed 2026-11-19 08:53:34 +01:00
1 parent 1239ff524d
commit d4a604970e
14 files changed
+478 -162

No files matched your search

+35 -5
View File
@@ -16,12 +16,36 @@ func tokenOK(got, expected string) bool {
return subtle.ConstantTimeCompare([]byte(got), []byte(expected)) == 1
}
func requireToken(w http.ResponseWriter, r *http.Request, expected string) bool {
auth := strings.TrimSpace(r.Header.Get("Authorization"))
const prefix = "Bearer "
if strings.HasPrefix(auth, prefix) && tokenOK(strings.TrimSpace(strings.TrimPrefix(auth, prefix)), expected) {
return true
}
w.Header().Set("WWW-Authenticate", "Bearer")
http.Error(w, "unauthorized", http.StatusUnauthorized)
return false
}
func handler(cfg config) http.Handler {
mux := http.NewServeMux()
detector := &ipDetector{}
collector := newSystemCollector()
stats := newContainerCollector()
inventorySlots := make(chan struct{}, 2)
mux.HandleFunc("/v1/enroll", enrollHandler(cfg))
mux.HandleFunc("/v1/diagnostics", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
if !requireToken(w, r, cfg.Token) {
return
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "no-store")
_ = json.NewEncoder(w).Encode(agentDiagnostics())
})
mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
@@ -39,11 +63,17 @@ func handler(cfg config) http.Handler {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
auth := strings.TrimSpace(r.Header.Get("Authorization"))
const prefix = "Bearer "
if !strings.HasPrefix(auth, prefix) || !tokenOK(strings.TrimSpace(strings.TrimPrefix(auth, prefix)), cfg.Token) {
w.Header().Set("WWW-Authenticate", "Bearer")
http.Error(w, "unauthorized", http.StatusUnauthorized)
if !requireToken(w, r, cfg.Token) {
return
}
// No waiter queue: excess authenticated polls fail fast, so a
// stalled Docker socket cannot accumulate unbounded request work.
select {
case inventorySlots <- struct{}{}:
defer func() { <-inventorySlots }()
default:
w.Header().Set("Retry-After", "1")
http.Error(w, "inventory busy", http.StatusServiceUnavailable)
return
}
containers, err := dockerContainers(r.Context(), cfg.DockerSock)