-
Notifications
You must be signed in to change notification settings - Fork 23
Add bosh-monit-access helper for nftables firewall #97
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
73c29ef
Add bosh-monit-access helper for nftables firewall
rkoster 9448344
bosh-monit-access: vendor dependencies
abg 4d0a95f
galera-agent: emit bosh-monit-access output to stderr logs
abg a4adf76
Make bosh-monit-access cgroup rule management idempotent
rkoster File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ templates: | |
|
|
||
| packages: | ||
| - galera-agent | ||
| - bosh-monit-access | ||
|
|
||
| provides: | ||
| - name: galera-agent | ||
|
|
||
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,8 @@ | ||
| #!/bin/bash | ||
| set -e -x | ||
|
|
||
| source /var/vcap/packages/golang-1-linux/bosh/compile.env | ||
|
|
||
| pushd bosh-monit-access | ||
| go build -o "${BOSH_INSTALL_TARGET}/bin/bosh-monit-access" . | ||
| popd |
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,8 @@ | ||
| --- | ||
| name: bosh-monit-access | ||
|
|
||
| dependencies: | ||
| - golang-1-linux | ||
|
|
||
| files: | ||
| - bosh-monit-access/**/* |
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,81 @@ | ||
| //go:build linux | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "syscall" | ||
| ) | ||
|
|
||
| // getCurrentCgroupPath reads /proc/self/cgroup and extracts the cgroupv2 path. | ||
| // Returns path WITHOUT leading slash (e.g., "system.slice/runc-bpm-galera-agent.scope") | ||
| // to match the format used by the nft CLI. | ||
| func getCurrentCgroupPath() (string, error) { | ||
| data, err := os.ReadFile("/proc/self/cgroup") | ||
| if err != nil { | ||
| return "", fmt.Errorf("reading /proc/self/cgroup: %w", err) | ||
| } | ||
|
|
||
| // Find line starting with "0::" (cgroupv2) | ||
| // Format: "0::/system.slice/runc-bpm-galera-agent.scope" | ||
| for _, line := range strings.Split(string(data), "\n") { | ||
| line = strings.TrimSpace(line) | ||
| if strings.HasPrefix(line, "0::") { | ||
| path := strings.TrimPrefix(line, "0::") | ||
| // Strip leading slash to match Noble script format | ||
| path = strings.TrimPrefix(path, "/") | ||
| return path, nil | ||
| } | ||
| } | ||
|
|
||
| return "", fmt.Errorf("cgroupv2 path not found in /proc/self/cgroup") | ||
| } | ||
|
|
||
| // isCgroupAccessible checks if the cgroup path is accessible and functional | ||
| // for nftables socket cgroupv2 matching. | ||
| // | ||
| // This returns false in these cases: | ||
| // - Cgroup path doesn't exist in /sys/fs/cgroup | ||
| // - Hybrid cgroup system (cgroupv2 mounted but no controllers delegated) | ||
| // - Nested containers where cgroup path is different from host view | ||
| func isCgroupAccessible(cgroupPath string) bool { | ||
| // Check if cgroup path exists | ||
| fullPath := filepath.Join("/sys/fs/cgroup", cgroupPath) | ||
| if _, err := os.Stat(fullPath); err != nil { | ||
| fmt.Printf("bosh-monit-access: Cgroup path doesn't exist: %s\n", fullPath) | ||
| return false | ||
| } | ||
|
|
||
| // Check if this is a hybrid cgroup system (cgroupv2 mounted but no controllers) | ||
| // On hybrid systems, /sys/fs/cgroup/cgroup.controllers exists but is empty | ||
| controllers, err := os.ReadFile("/sys/fs/cgroup/cgroup.controllers") | ||
| if err != nil { | ||
| fmt.Printf("bosh-monit-access: Cannot read cgroup.controllers: %v\n", err) | ||
| return false | ||
| } | ||
|
|
||
| if len(strings.TrimSpace(string(controllers))) == 0 { | ||
| fmt.Println("bosh-monit-access: Hybrid cgroup system detected (no controllers in cgroupv2)") | ||
| return false | ||
| } | ||
|
|
||
| return true | ||
| } | ||
|
|
||
| // getCgroupInodeID returns the inode ID for the cgroup path. | ||
| // The nftables kernel expects an 8-byte cgroup inode ID for 'socket cgroupv2' | ||
| // matching, NOT the path string. The nft CLI translates paths to inode IDs | ||
| // automatically, but the Go library requires manual lookup. | ||
| func getCgroupInodeID(cgroupPath string) (uint64, error) { | ||
| fullPath := filepath.Join("/sys/fs/cgroup", cgroupPath) | ||
|
|
||
| var stat syscall.Stat_t | ||
| if err := syscall.Stat(fullPath, &stat); err != nil { | ||
| return 0, fmt.Errorf("stat %s: %w", fullPath, err) | ||
| } | ||
|
|
||
| return stat.Ino, nil | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.