Skip to content

Commit 6b632b6

Browse files
committed
chore: try fix crashing on recuring scrape
1 parent 586d603 commit 6b632b6

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

internal/crowdsec/authenticator.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/hex"
77
"encoding/json"
88
"fmt"
9+
"io"
910
"log/slog"
1011
"net/http"
1112
"os"
@@ -46,15 +47,22 @@ func CheckAuth() {
4647
AuthToken.mu.Lock()
4748
defer AuthToken.mu.Unlock()
4849

50+
slog.Debug("CheckAuth called", "isRegistered", AuthToken.isRegistered, "hasRegistrationToken", AuthToken.Config.CrowdSec.RegistrationToken != "", "tokenExpired", AuthToken.Expire.Before(time.Now()))
51+
4952
// If using auto-registration and not yet registered, register first
5053
if !AuthToken.isRegistered && AuthToken.Config.CrowdSec.RegistrationToken != "" {
54+
slog.Debug("Attempting to register machine")
5155
if err := registerMachine(); err != nil {
5256
slog.Error("Failed to register machine", "error", err)
5357
os.Exit(1)
5458
}
59+
// After successful registration, we need to authenticate to get a token
60+
// Reset the expiry to force authentication
61+
AuthToken.Expire = time.Now()
5562
}
5663

5764
if AuthToken.Expire.Before(time.Now()) {
65+
slog.Debug("Token expired, authenticating", "machineId", AuthToken.machineLogin)
5866
authenticate()
5967
}
6068
}
@@ -72,6 +80,8 @@ func authenticate() {
7280
credentials.Machine_id = AuthToken.machineLogin
7381
credentials.Password = AuthToken.machinePasswd
7482

83+
slog.Debug("Authenticating with credentials", "machineId", credentials.Machine_id)
84+
7585
credentials_json, err := json.Marshal(credentials)
7686
if err != nil {
7787
slog.Error("Failed to marshal credentials", "error", err)
@@ -94,7 +104,7 @@ func authenticate() {
94104
defer res.Body.Close()
95105

96106
if res.StatusCode != http.StatusOK {
97-
slog.Error("Authentication failed", "status", res.StatusCode)
107+
slog.Error("Authentication failed", "status", res.StatusCode, "machineId", credentials.Machine_id)
98108
os.Exit(1)
99109
}
100110

@@ -145,6 +155,8 @@ func registerMachine() error {
145155
RegistrationToken: AuthToken.Config.CrowdSec.RegistrationToken,
146156
}
147157

158+
slog.Debug("Registering machine", "machineId", registerData.MachineId, "url", AuthToken.Config.CrowdSec.URL+"/v1/watchers")
159+
148160
registerJSON, err := json.Marshal(registerData)
149161
if err != nil {
150162
return fmt.Errorf("failed to marshal registration data: %w", err)
@@ -163,7 +175,15 @@ func registerMachine() error {
163175
}
164176
defer res.Body.Close()
165177

178+
slog.Debug("Registration response", "status", res.StatusCode, "machineId", registerData.MachineId)
179+
166180
if res.StatusCode != http.StatusCreated && res.StatusCode != http.StatusAccepted {
181+
// Log response body for debugging
182+
bodyBytes := make([]byte, 0)
183+
if res.Body != nil {
184+
bodyBytes, _ = io.ReadAll(res.Body)
185+
}
186+
slog.Error("Registration failed", "status", res.StatusCode, "machineId", registerData.MachineId, "responseBody", string(bodyBytes))
167187
return fmt.Errorf("registration failed with status: %d", res.StatusCode)
168188
}
169189

0 commit comments

Comments
 (0)