Skip to content

Commit e2e734a

Browse files
committed
fix: resolve golangci lint diagnostics
1 parent 087d0df commit e2e734a

File tree

9 files changed

+118
-61
lines changed

9 files changed

+118
-61
lines changed

.golangci.yml

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,16 @@ run:
33
issues-exit-code: 1
44
tests: true
55
build-tags: []
6-
skip-dirs:
7-
- vendor
8-
skip-files:
9-
- ".*\\.pb\\.go$"
10-
- ".*_generated\\.go$"
6+
modules-download-mode: vendor
117

128
linters-settings:
139
errcheck:
1410
check-type-assertions: true
1511
check-blank: true
16-
1712
gocyclo:
1813
min-complexity: 15
19-
2014
goimports:
2115
local-prefixes: github.com/EvalOps/keep
22-
2316
govet:
2417
enable-all: true
2518

@@ -70,11 +63,13 @@ linters-settings:
7063
- G204 # Command injection - intentional for system commands
7164

7265
staticcheck:
73-
go: "1.22"
7466
checks: ["all"]
7567

7668
gci:
77-
local-prefixes: github.com/EvalOps/keep
69+
sections:
70+
- standard
71+
- default
72+
- prefix(github.com/EvalOps/keep)
7873

7974
goconst:
8075
min-len: 2
@@ -108,16 +103,13 @@ linters:
108103
- goprintffuncname
109104
- nolintlint
110105

111-
disable:
112-
- deadcode
113-
- structcheck
114-
- varcheck
115-
- golint
116-
- maligned
117-
- interfacer
118-
- scopelint
119106

120107
issues:
108+
exclude-dirs:
109+
- vendor
110+
exclude-files:
111+
- ".*\\.pb\\.go$"
112+
- ".*_generated\\.go$"
121113
exclude-rules:
122114
# Allow long lines in tests for readability
123115
- path: "_test\\.go"

agent/internal/posture/posture_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ func TestFileExists(t *testing.T) {
268268

269269
t.Run("returns true for existing file", func(t *testing.T) {
270270
testFile := filepath.Join(tmpDir, "exists.txt")
271-
if err := os.WriteFile(testFile, []byte("test"), 0644); err != nil {
271+
if err := os.WriteFile(testFile, []byte("test"), 0o600); err != nil {
272272
t.Fatalf("failed to write test file: %v", err)
273273
}
274274

agent/internal/service/service.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,10 +347,10 @@ func (s *Service) obtainCertificate() error {
347347
if err != nil {
348348
return err
349349
}
350-
if err := os.MkdirAll(filepath.Dir(s.config.CAPath), 0o755); err != nil {
350+
if err := os.MkdirAll(filepath.Dir(s.config.CAPath), 0o700); err != nil {
351351
return err
352352
}
353-
if err := os.WriteFile(s.config.CAPath, rawCA, 0o644); err != nil {
353+
if err := os.WriteFile(s.config.CAPath, rawCA, 0o600); err != nil {
354354
return err
355355
}
356356
}
@@ -407,7 +407,7 @@ func (s *Service) get(endpoint string) (*http.Response, error) {
407407
// writePIDFile writes the process ID to a file
408408
func (s *Service) writePIDFile() error {
409409
pid := os.Getpid()
410-
return os.WriteFile(s.config.PIDFile, []byte(fmt.Sprintf("%d\n", pid)), 0o644)
410+
return os.WriteFile(s.config.PIDFile, []byte(fmt.Sprintf("%d\n", pid)), 0o600)
411411
}
412412

413413
// removePIDFile removes the PID file

agent/internal/service/service_test.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
"github.com/EvalOps/keep/agent/internal/posture"
1717
)
1818

19+
const testDeviceID = "test-device"
20+
1921
// mockPostureCollector implements the posture.Collector interface for testing
2022
type mockPostureCollector struct {
2123
postureData *posture.DevicePosture
@@ -41,7 +43,7 @@ func (e *mockError) Error() string {
4143
func TestService_New(t *testing.T) {
4244
t.Run("creates service with valid config", func(t *testing.T) {
4345
config := &Config{
44-
DeviceID: "test-device",
46+
DeviceID: testDeviceID,
4547
InventoryURL: "http://localhost:8081",
4648
AttestURL: "http://localhost:8443",
4749
KeyPath: "/tmp/test.key",
@@ -84,7 +86,7 @@ func TestService_initialRegistration(t *testing.T) {
8486
return
8587
}
8688

87-
if req.ID != "test-device" {
89+
if req.ID != testDeviceID {
8890
t.Errorf("Expected device ID 'test-device', got %s", req.ID)
8991
}
9092

@@ -97,7 +99,9 @@ func TestService_initialRegistration(t *testing.T) {
9799
}
98100

99101
w.WriteHeader(http.StatusOK)
100-
json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
102+
if err := json.NewEncoder(w).Encode(map[string]string{"status": "ok"}); err != nil {
103+
t.Fatalf("Failed to encode response: %v", err)
104+
}
101105
} else {
102106
http.Error(w, "not found", http.StatusNotFound)
103107
}
@@ -106,7 +110,7 @@ func TestService_initialRegistration(t *testing.T) {
106110

107111
// Create test config
108112
config := &Config{
109-
DeviceID: "test-device",
113+
DeviceID: testDeviceID,
110114
InventoryURL: mockInventory.URL,
111115
AttestURL: "http://localhost:8443",
112116
KeyPath: filepath.Join(tmpDir, "test.key"),
@@ -142,7 +146,7 @@ func TestService_initialRegistration(t *testing.T) {
142146

143147
t.Run("handles posture collection failure", func(t *testing.T) {
144148
config := &Config{
145-
DeviceID: "test-device",
149+
DeviceID: testDeviceID,
146150
InventoryURL: "http://localhost:8081",
147151
KeyPath: filepath.Join(tmpDir, "test.key"),
148152
}
@@ -180,7 +184,7 @@ func TestService_initialRegistration(t *testing.T) {
180184
defer mockInventory.Close()
181185

182186
config := &Config{
183-
DeviceID: "test-device",
187+
DeviceID: testDeviceID,
184188
InventoryURL: mockInventory.URL,
185189
KeyPath: filepath.Join(tmpDir, "test.key"),
186190
}
@@ -234,15 +238,17 @@ func TestService_updatePosture(t *testing.T) {
234238
}
235239

236240
w.WriteHeader(http.StatusOK)
237-
json.NewEncoder(w).Encode(map[string]string{"status": "updated"})
241+
if err := json.NewEncoder(w).Encode(map[string]string{"status": "updated"}); err != nil {
242+
t.Fatalf("Failed to encode response: %v", err)
243+
}
238244
} else {
239245
http.Error(w, "not found", http.StatusNotFound)
240246
}
241247
}))
242248
defer mockInventory.Close()
243249

244250
config := &Config{
245-
DeviceID: "test-device",
251+
DeviceID: testDeviceID,
246252
InventoryURL: mockInventory.URL,
247253
}
248254

@@ -269,7 +275,7 @@ func TestService_updatePosture(t *testing.T) {
269275

270276
t.Run("handles posture collection failure", func(t *testing.T) {
271277
config := &Config{
272-
DeviceID: "test-device",
278+
DeviceID: testDeviceID,
273279
InventoryURL: "http://localhost:8081",
274280
}
275281

@@ -316,11 +322,15 @@ func TestService_obtainCertificate(t *testing.T) {
316322
response := certResponse{
317323
Certificate: "-----BEGIN CERTIFICATE-----\nMOCK_CERT_DATA\n-----END CERTIFICATE-----",
318324
}
319-
json.NewEncoder(w).Encode(response)
325+
if err := json.NewEncoder(w).Encode(response); err != nil {
326+
t.Fatalf("Failed to encode certificate response: %v", err)
327+
}
320328
} else if r.URL.Path == "/v1/certs/ca" && r.Method == http.MethodGet {
321329
// Return mock CA certificate
322330
w.Header().Set("Content-Type", "application/x-pem-file")
323-
w.Write([]byte("-----BEGIN CERTIFICATE-----\nMOCK_CA_DATA\n-----END CERTIFICATE-----"))
331+
if _, err := w.Write([]byte("-----BEGIN CERTIFICATE-----\nMOCK_CA_DATA\n-----END CERTIFICATE-----")); err != nil {
332+
t.Fatalf("Failed to write CA certificate: %v", err)
333+
}
324334
} else {
325335
http.Error(w, "not found", http.StatusNotFound)
326336
}
@@ -432,7 +442,7 @@ func TestService_removePIDFile(t *testing.T) {
432442
pidFile := filepath.Join(tmpDir, "test.pid")
433443

434444
// Create PID file
435-
err := os.WriteFile(pidFile, []byte("12345\n"), 0644)
445+
err := os.WriteFile(pidFile, []byte("12345\n"), 0o600)
436446
if err != nil {
437447
t.Fatalf("Failed to create test PID file: %v", err)
438448
}

cmd/inventory/main.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ import (
1111
serverpkg "github.com/EvalOps/keep/services/inventory/server"
1212
)
1313

14+
const (
15+
flagValueTrue = "true"
16+
defaultAppEnv = "development"
17+
defaultRequireMTLS = "false"
18+
)
19+
1420
func main() {
1521
// Initialize secret management
1622
secretHelper := secrets.NewHelperFromEnv()
@@ -31,15 +37,15 @@ func main() {
3137
ClientCA: secretHelper.GetOrDefault("INVENTORY_CLIENT_CA", tlsConfig["INVENTORY_CLIENT_CA"]),
3238
AuthzJWKS: envOrDefault("AUTHZ_JWKS_URL", ""),
3339
Shutdown: 5 * time.Second,
34-
RequireMTLS: envOrDefault("INVENTORY_REQUIRE_MTLS", "false") == "true",
40+
RequireMTLS: envOrDefault("INVENTORY_REQUIRE_MTLS", defaultRequireMTLS) == flagValueTrue,
3541
}
3642

3743
ctx := context.Background()
3844
if err := telemetry.Init(ctx, telemetry.Config{
3945
Endpoint: os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT"),
40-
Insecure: os.Getenv("OTEL_EXPORTER_OTLP_INSECURE") == "true",
46+
Insecure: os.Getenv("OTEL_EXPORTER_OTLP_INSECURE") == flagValueTrue,
4147
ServiceName: "inventory",
42-
Environment: envOrDefault("APP_ENV", "development"),
48+
Environment: envOrDefault("APP_ENV", defaultAppEnv),
4349
}); err != nil {
4450
log.Printf("telemetry init failed: %v", err)
4551
}

pkg/pki/ca_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ func TestLoadCA(t *testing.T) {
8484
keyPath := filepath.Join(tmpDir, "key.pem")
8585

8686
// Write invalid certificate
87-
if err := os.WriteFile(certPath, []byte("invalid pem"), 0644); err != nil {
87+
if err := os.WriteFile(certPath, []byte("invalid pem"), 0o600); err != nil {
8888
t.Fatalf("failed to write invalid certificate: %v", err)
8989
}
90-
if err := os.WriteFile(keyPath, []byte("-----BEGIN PRIVATE KEY-----\nMIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg\n-----END PRIVATE KEY-----"), 0600); err != nil {
90+
if err := os.WriteFile(keyPath, []byte("-----BEGIN PRIVATE KEY-----\nMIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg\n-----END PRIVATE KEY-----"), 0o600); err != nil {
9191
t.Fatalf("failed to write invalid key: %v", err)
9292
}
9393

@@ -112,7 +112,7 @@ func TestLoadCA(t *testing.T) {
112112
}
113113

114114
// Now corrupt the key file
115-
if err := os.WriteFile(keyPath, []byte("invalid key pem"), 0600); err != nil {
115+
if err := os.WriteFile(keyPath, []byte("invalid key pem"), 0o600); err != nil {
116116
t.Fatalf("failed to corrupt key file: %v", err)
117117
}
118118

pkg/pki/device_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@ func TestLoadSigningKey(t *testing.T) {
146146

147147
t.Run("fails with invalid PEM", func(t *testing.T) {
148148
invalidPEMPath := filepath.Join(tmpDir, "invalid.key")
149-
os.WriteFile(invalidPEMPath, []byte("not a pem file"), 0600)
149+
if err := os.WriteFile(invalidPEMPath, []byte("not a pem file"), 0o600); err != nil {
150+
t.Fatalf("Failed to write invalid PEM file: %v", err)
151+
}
150152

151153
_, err := LoadSigningKey(invalidPEMPath)
152154
if err == nil {
@@ -168,7 +170,9 @@ MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC7VJTUt9Us8cKB
168170
wHVKYdZyLkmMdVNjJqLs2Nx7e62VQqTrqTqhqY+HVhMV7HjfRqNVM6pYsf3VrGQh
169171
-----END PRIVATE KEY-----`)
170172

171-
os.WriteFile(rsaKeyPath, keyData, 0600)
173+
if err := os.WriteFile(rsaKeyPath, keyData, 0o600); err != nil {
174+
t.Fatalf("Failed to write RSA key: %v", err)
175+
}
172176

173177
_, err := LoadSigningKey(rsaKeyPath)
174178
if err == nil {
@@ -184,7 +188,9 @@ wHVKYdZyLkmMdVNjJqLs2Nx7e62VQqTrqTqhqY+HVhMV7HjfRqNVM6pYsf3VrGQh
184188
invalidbase64data!!!
185189
-----END PRIVATE KEY-----`
186190

187-
os.WriteFile(corruptPath, []byte(corruptPEM), 0600)
191+
if err := os.WriteFile(corruptPath, []byte(corruptPEM), 0o600); err != nil {
192+
t.Fatalf("Failed to write corrupt PEM: %v", err)
193+
}
188194

189195
_, err := LoadSigningKey(corruptPath)
190196
if err == nil {

0 commit comments

Comments
 (0)