From afb175bdebcfcce18b331e4c8b42d590939ba046 Mon Sep 17 00:00:00 2001 From: Nurlan Moldomurov Date: Tue, 4 Mar 2025 11:13:23 +0300 Subject: [PATCH] PMM-13400 Improve cgroup write check and encryption setup Suppress lint warnings in cgroup write test and simplify error handling. Add `isTest` function to detect test environment and update encryption key path logic accordingly. --- agent/utils/cgroups/CgroupWritableCheck.go | 7 ++++--- managed/utils/encryption/encryption.go | 13 +++++++++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/agent/utils/cgroups/CgroupWritableCheck.go b/agent/utils/cgroups/CgroupWritableCheck.go index 504ae01229..886605d15a 100644 --- a/agent/utils/cgroups/CgroupWritableCheck.go +++ b/agent/utils/cgroups/CgroupWritableCheck.go @@ -26,13 +26,14 @@ func IsCgroupsWritable() bool { testFile := filepath.Join(cgroupDir, "test_write") // Attempt to create and remove a test file to check for write access - file, err := os.Create(testFile) + file, err := os.Create(testFile) //nolint:gosec if err != nil { return false // Not writable } defer func() { - file.Close() - os.Remove(testFile) // Clean up the test file + // Clean up the test file + file.Close() //nolint:errcheck + os.Remove(testFile) //nolint:errcheck }() return true // Writable diff --git a/managed/utils/encryption/encryption.go b/managed/utils/encryption/encryption.go index 99498fb4df..2173a5178f 100644 --- a/managed/utils/encryption/encryption.go +++ b/managed/utils/encryption/encryption.go @@ -21,7 +21,6 @@ import ( "encoding/base64" "fmt" "os" - "runtime" "slices" "strings" "sync" @@ -72,6 +71,16 @@ type QueryValues struct { WhereValues [][]any } +func isTest() bool { + // Check if tests are running by inspecting os.Args + for _, arg := range os.Args { + if strings.HasPrefix(arg, "-test.") { + return true + } + } + return false +} + // New creates an encryption; if key on path doesn't exist, it will be generated. func New() *Encryption { e := &Encryption{} @@ -79,7 +88,7 @@ func New() *Encryption { if customKeyPath != "" { e.Path = customKeyPath } else { - if runtime.GOOS == "darwin" { // for development on macOS + if isTest() { e.Path = "./encryption.key" } else { e.Path = DefaultEncryptionKeyPath