-
Notifications
You must be signed in to change notification settings - Fork 12
yugal's normalising path changes #775
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+126
−10
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| package utils | ||
|
|
||
| import ( | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestNormalizePath(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| input string | ||
| expected string | ||
| }{ | ||
| { | ||
| name: "empty path", | ||
| input: "", | ||
| expected: "", | ||
| }, | ||
| { | ||
| name: "dot path", | ||
| input: ".", | ||
| expected: "/", | ||
| }, | ||
| { | ||
| name: "absolute path", | ||
| input: "/etc/passwd", | ||
| expected: "/etc/passwd", | ||
| }, | ||
| { | ||
| name: "headless proc path (task)", | ||
| input: "/46/task/46/fd", | ||
| expected: "/proc/46/task/46/fd", | ||
| }, | ||
| { | ||
| name: "headless proc path (fd)", | ||
| input: "/46/fd/3", | ||
| expected: "/proc/46/fd/3", | ||
| }, | ||
| { | ||
| name: "already absolute proc path", | ||
| input: "/proc/46/fd/3", | ||
| expected: "/proc/46/fd/3", | ||
| }, | ||
| { | ||
| name: "terminal headless proc fd path", | ||
| input: "/46/fd", | ||
| expected: "/proc/46/fd", | ||
| }, | ||
| { | ||
| name: "terminal headless proc task path", | ||
| input: "/46/task", | ||
| expected: "/proc/46/task", | ||
| }, | ||
| { | ||
| name: "relative path (not dot)", | ||
| input: "usr/bin/ls", | ||
| expected: "/usr/bin/ls", | ||
| }, | ||
| { | ||
| name: "relative path with ./", | ||
| input: "./config", | ||
| expected: "/config", | ||
| }, | ||
| { | ||
| name: "path with redundant slashes", | ||
| input: "/etc//passwd", | ||
| expected: "/etc/passwd", | ||
| }, | ||
| { | ||
| name: "path with dot components", | ||
| input: "/usr/./bin/../lib", | ||
| expected: "/usr/lib", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| if got := NormalizePath(tt.input); got != tt.expected { | ||
| t.Errorf("NormalizePath(%q) = %q, want %q", tt.input, got, tt.expected) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package utils | ||
|
|
||
| import ( | ||
| "path" | ||
| "regexp" | ||
| "strings" | ||
| ) | ||
|
|
||
| var headlessProcRegex = regexp.MustCompile(`^/\d+/(task|fd)(/|$)`) | ||
|
|
||
| // NormalizePath normalizes a path by: | ||
| // 1. Prepending "/proc" to "headless" proc paths (e.g. /46/task/46/fd -> /proc/46/task/46/fd) | ||
| // 2. Ensuring it starts with "/" if it's not empty | ||
| // 3. Converting "." to "/" | ||
| // 4. Cleaning the path (removing redundant slashes, dot-dots, etc.) | ||
| func NormalizePath(p string) string { | ||
| if p == "" { | ||
| return "" | ||
| } | ||
|
|
||
| if p == "." { | ||
| return "/" | ||
| } | ||
|
|
||
| if headlessProcRegex.MatchString(p) { | ||
| p = "/proc" + p | ||
| } | ||
|
|
||
| if !strings.HasPrefix(p, "/") { | ||
| p = "/" + p | ||
| } | ||
|
|
||
| return path.Clean(p) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: kubescape/node-agent
Length of output: 8890
🏁 Script executed:
Repository: kubescape/node-agent
Length of output: 2143
Implementation looks correct; verify downstream hash compatibility.
The normalization logic is well-structured and correctly handles the documented cases. However, the path normalization is applied in the
GetOldPath()andGetNewPath()methods (seepkg/utils/datasource_event.golines 493, 503 andpkg/utils/struct_event.golines 282, 290), which are used directly in hash calculations for symlinks and hardlinks inpkg/containerprofilemanager/v1/event_reporting.go(lines 103, 116). Since these getters now return normalized paths, existing stored profile hashes will no longer match hashes computed from new events.This is a breaking change: paths that were previously relative (now get
/prefix),.(now become/), or headless proc patterns (now get/procprefix) will all produce different hash values.Consider whether a data migration or versioning strategy is needed for existing profiles.
🤖 Prompt for AI Agents