Skip to content

Commit 47bdcd6

Browse files
committed
fix(authz): add test constants and check encoder errors
1 parent 717ff88 commit 47bdcd6

File tree

3 files changed

+41
-22
lines changed

3 files changed

+41
-22
lines changed

.golangci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ run:
33
issues-exit-code: 1
44
tests: true
55
build-tags: []
6+
modules-download-mode: vendor
67

78
linters-settings:
89
errcheck:
@@ -138,8 +139,7 @@ issues:
138139
text: "Error return value of.*not checked"
139140

140141
output:
141-
formats:
142-
- format: colored-line-number
142+
format: colored-line-number
143143
print-issued-lines: true
144144
print-linter-name: true
145145

services/authz/server/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ import (
2121
"github.com/go-chi/chi/v5"
2222
"github.com/go-chi/chi/v5/middleware"
2323
"github.com/prometheus/client_golang/prometheus/promhttp"
24+
"tailscale.com/tsnet"
2425

2526
"github.com/EvalOps/keep/pkg/logging"
2627
"github.com/EvalOps/keep/pkg/metrics"
2728
"github.com/EvalOps/keep/pkg/pki"
2829
"github.com/EvalOps/keep/pkg/retry"
2930
"github.com/EvalOps/keep/pkg/telemetry"
3031
"github.com/EvalOps/keep/services/authz/token"
31-
"tailscale.com/tsnet"
3232
)
3333

3434
type Server struct {

services/authz/server/server_test.go

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ import (
1313
"tailscale.com/tsnet"
1414
)
1515

16+
const (
17+
testDeviceID = "test-device"
18+
testAllowPath = "/v1/data/keep/allow"
19+
testClientRemoteIP = "100.65.1.1:12345"
20+
)
21+
1622
// TestServer_healthHandler tests the health endpoint
1723
func TestServer_healthHandler(t *testing.T) {
1824
server := createTestServer(t)
@@ -69,7 +75,7 @@ func TestServer_verifyHandler(t *testing.T) {
6975
t.Run("rejects invalid token", func(t *testing.T) {
7076
reqBody := verifyRequest{
7177
Token: "invalid.jwt.token",
72-
DeviceID: "test-device",
78+
DeviceID: testDeviceID,
7379
ClientIP: "192.168.1.1",
7480
}
7581
body, err := json.Marshal(reqBody)
@@ -147,16 +153,23 @@ func createTestServer(t *testing.T) *Server {
147153

148154
// TestServer_envoyAuthHandler tests the Envoy auth handler
149155
func TestServer_envoyAuthHandler(t *testing.T) {
156+
const (
157+
testDeviceID = "test-device"
158+
testAllowPath = "/v1/data/keep/allow"
159+
testClientRemoteIP = "100.65.1.1:12345"
160+
)
150161
// Create mock OPA server
151162
mockOPA := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
152-
if r.URL.Path == "/v1/data/keep/allow" && r.Method == http.MethodPost {
163+
if r.URL.Path == testAllowPath && r.Method == http.MethodPost {
153164
// Return "allow" decision for test
154165
response := map[string]interface{}{
155166
"result": map[string]interface{}{
156167
"decision": "allow",
157168
},
158169
}
159-
json.NewEncoder(w).Encode(response)
170+
if err := json.NewEncoder(w).Encode(response); err != nil {
171+
t.Fatalf("Failed to encode OPA response: %v", err)
172+
}
160173
} else {
161174
http.Error(w, "not found", http.StatusNotFound)
162175
}
@@ -168,10 +181,12 @@ func TestServer_envoyAuthHandler(t *testing.T) {
168181
if strings.HasPrefix(r.URL.Path, "/v1/devices/") && r.Method == http.MethodGet {
169182
// Return device info
170183
device := map[string]interface{}{
171-
"id": "test-device",
184+
"id": testDeviceID,
172185
"posture": "healthy",
173186
}
174-
json.NewEncoder(w).Encode(device)
187+
if err := json.NewEncoder(w).Encode(device); err != nil {
188+
t.Fatalf("Failed to encode device response: %v", err)
189+
}
175190
} else {
176191
http.Error(w, "not found", http.StatusNotFound)
177192
}
@@ -236,7 +251,7 @@ func TestServer_envoyAuthHandler(t *testing.T) {
236251
"http": map[string]interface{}{
237252
"headers": map[string]string{
238253
"authorization": "InvalidToken",
239-
"x-device-id": "test-device",
254+
"x-device-id": testDeviceID,
240255
},
241256
},
242257
},
@@ -264,7 +279,7 @@ func TestServer_envoyAuthHandler(t *testing.T) {
264279
"http": map[string]interface{}{
265280
"headers": map[string]string{
266281
"authorization": "Bearer invalid.jwt.token",
267-
"x-device-id": "test-device",
282+
"x-device-id": testDeviceID,
268283
},
269284
},
270285
},
@@ -294,7 +309,7 @@ func TestServer_envoyAuthHandler(t *testing.T) {
294309
"headers": map[string]string{
295310
"authorization": "Bearer invalid.jwt.token",
296311
"x-forwarded-for": "192.168.1.100",
297-
"x-device-id": "test-device",
312+
"x-device-id": testDeviceID,
298313
},
299314
},
300315
},
@@ -361,8 +376,10 @@ func TestServer_evaluateOPA(t *testing.T) {
361376
for _, tc := range testCases {
362377
t.Run(tc.name, func(t *testing.T) {
363378
mockOPA := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
364-
if r.URL.Path == "/v1/data/keep/allow" && r.Method == http.MethodPost {
365-
json.NewEncoder(w).Encode(tc.opaResponse)
379+
if r.URL.Path == testAllowPath && r.Method == http.MethodPost {
380+
if err := json.NewEncoder(w).Encode(tc.opaResponse); err != nil {
381+
t.Fatalf("Failed to encode OPA response: %v", err)
382+
}
366383
} else {
367384
http.Error(w, "not found", http.StatusNotFound)
368385
}
@@ -376,7 +393,7 @@ func TestServer_evaluateOPA(t *testing.T) {
376393
"groups": []string{"admin"},
377394
}
378395

379-
result, err := server.evaluateOPA(context.Background(), claims, "test-device", "192.168.1.1")
396+
result, err := server.evaluateOPA(context.Background(), claims, testDeviceID, "192.168.1.1")
380397

381398
if tc.shouldError {
382399
if err == nil {
@@ -417,7 +434,7 @@ func TestServer_lookupDevice(t *testing.T) {
417434
t.Run("returns unknown posture when no inventory URL", func(t *testing.T) {
418435
server := createTestServerWithMocks(t, "", "")
419436

420-
result := server.lookupDevice(context.Background(), "test-device")
437+
result := server.lookupDevice(context.Background(), testDeviceID)
421438

422439
expected := map[string]any{
423440
"id": "test-device",
@@ -446,13 +463,15 @@ func TestServer_lookupDevice(t *testing.T) {
446463

447464
t.Run("returns device info for successful lookup", func(t *testing.T) {
448465
expectedDevice := map[string]interface{}{
449-
"id": "test-device",
466+
"id": testDeviceID,
450467
"posture": "healthy",
451468
}
452469

453470
mockInventory := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
454-
if strings.Contains(r.URL.Path, "test-device") {
455-
json.NewEncoder(w).Encode(expectedDevice)
471+
if strings.Contains(r.URL.Path, testDeviceID) {
472+
if err := json.NewEncoder(w).Encode(expectedDevice); err != nil {
473+
t.Fatalf("Failed to encode device response: %v", err)
474+
}
456475
} else {
457476
http.Error(w, "not found", http.StatusNotFound)
458477
}
@@ -464,7 +483,7 @@ func TestServer_lookupDevice(t *testing.T) {
464483

465484
server := createTestServerWithMocks(t, "", mockInventory.URL)
466485

467-
result := server.lookupDevice(ctx, "test-device")
486+
result := server.lookupDevice(ctx, testDeviceID)
468487

469488
if result["id"] != expectedDevice["id"] || result["posture"] != expectedDevice["posture"] {
470489
t.Errorf("Expected device info %v, got %v", expectedDevice, result)
@@ -548,7 +567,7 @@ func TestServer_deviceCertHandler(t *testing.T) {
548567

549568
t.Run("rejects empty CSR", func(t *testing.T) {
550569
reqBody := map[string]string{
551-
"device_id": "test-device",
570+
"device_id": testDeviceID,
552571
"csr": "",
553572
}
554573
body, err := json.Marshal(reqBody)
@@ -592,7 +611,7 @@ func TestServer_validateTailscaleAccess(t *testing.T) {
592611

593612
t.Run("returns false when no Tailscale server", func(t *testing.T) {
594613
req := httptest.NewRequest(http.MethodGet, "/test", nil)
595-
req.RemoteAddr = "100.65.1.1:12345"
614+
req.RemoteAddr = testClientRemoteIP
596615

597616
result := server.validateTailscaleAccess(req)
598617

@@ -620,7 +639,7 @@ func TestServer_validateTailscaleAccess(t *testing.T) {
620639
server.tsServer = &tsnet.Server{}
621640

622641
req := httptest.NewRequest(http.MethodGet, "/test", nil)
623-
req.RemoteAddr = "100.65.1.1:12345" // Valid Tailscale IP
642+
req.RemoteAddr = testClientRemoteIP // Valid Tailscale IP
624643

625644
result := server.validateTailscaleAccess(req)
626645

0 commit comments

Comments
 (0)