Skip to content

Commit

Permalink
PMM-13400 Improve cgroup write check and encryption setup
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
BupycHuk committed Mar 4, 2025
1 parent 8d7bb26 commit afb175b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
7 changes: 4 additions & 3 deletions agent/utils/cgroups/CgroupWritableCheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 11 additions & 2 deletions managed/utils/encryption/encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"encoding/base64"
"fmt"
"os"
"runtime"
"slices"
"strings"
"sync"
Expand Down Expand Up @@ -72,14 +71,24 @@ 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{}
customKeyPath := os.Getenv("PMM_ENCRYPTION_KEY_PATH")
if customKeyPath != "" {
e.Path = customKeyPath
} else {
if runtime.GOOS == "darwin" { // for development on macOS
if isTest() {
e.Path = "./encryption.key"
} else {
e.Path = DefaultEncryptionKeyPath
Expand Down

0 comments on commit afb175b

Please sign in to comment.