-
Notifications
You must be signed in to change notification settings - Fork 0
Lowering perms to CAP_NET_ADMIN #65
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
9 commits
Select commit
Hold shift + click to select a range
e92bf2d
feat: lowering perms to CAP_NET_ADMIN
evgeniy-scherbina da85697
refactor: improve cleanup
evgeniy-scherbina 29d30b8
test: improve testing
evgeniy-scherbina 44b2590
refactor: remove dead code
evgeniy-scherbina 30cdf17
refactor: cleanup code
evgeniy-scherbina 973e338
refactor: cleanup code
evgeniy-scherbina 0ea4db6
fix: linter
evgeniy-scherbina f6a8030
fix: MacOS
evgeniy-scherbina 5d270b0
fix: MacOS
evgeniy-scherbina 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
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
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,93 @@ | ||
package e2e_tests | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const ( | ||
filterTable = "filter" | ||
natTable = "nat" | ||
) | ||
|
||
func getIptablesRules(tableName string) (string, error) { | ||
cmd := exec.Command("sudo", "iptables", "-L", "-n", "-t", tableName) | ||
output, err := cmd.Output() | ||
if err != nil { | ||
return "", fmt.Errorf("failed to get iptables rules: %v", err) | ||
} | ||
rules := string(output) | ||
|
||
return rules, nil | ||
} | ||
|
||
func TestIPTablesCleanup(t *testing.T) { | ||
// Step 1: Capture initial iptables rules | ||
initialFilterRules, err := getIptablesRules(filterTable) | ||
require.NoError(t, err) | ||
initialNatRules, err := getIptablesRules(natTable) | ||
require.NoError(t, err) | ||
|
||
// Step 2: Run Boundary | ||
// Find project root by looking for go.mod file | ||
projectRoot := findProjectRoot(t) | ||
|
||
// Build the boundary binary | ||
buildCmd := exec.Command("go", "build", "-o", "/tmp/boundary-test", "./cmd/...") | ||
buildCmd.Dir = projectRoot | ||
err = buildCmd.Run() | ||
require.NoError(t, err, "Failed to build boundary binary") | ||
|
||
// Create context for boundary process | ||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) | ||
defer cancel() | ||
|
||
// Start boundary process with sudo | ||
boundaryCmd := exec.CommandContext(ctx, "/tmp/boundary-test", | ||
"--allow", "dev.coder.com", | ||
"--allow", "jsonplaceholder.typicode.com", | ||
"--log-level", "debug", | ||
"--", "/bin/bash", "-c", "/usr/bin/sleep 10 && /usr/bin/echo 'Test completed'") | ||
|
||
boundaryCmd.Stdin = os.Stdin | ||
boundaryCmd.Stdout = os.Stdout | ||
boundaryCmd.Stderr = os.Stderr | ||
|
||
// Start the process | ||
err = boundaryCmd.Start() | ||
require.NoError(t, err, "Failed to start boundary process") | ||
|
||
// Give boundary time to start | ||
time.Sleep(2 * time.Second) | ||
|
||
// Gracefully close process, call cleanup methods | ||
err = boundaryCmd.Process.Signal(os.Interrupt) | ||
require.NoError(t, err, "Failed to interrupt boundary process") | ||
time.Sleep(time.Second * 1) | ||
|
||
// Step 3: Clean up | ||
cancel() // This will terminate the boundary process | ||
err = boundaryCmd.Wait() // Wait for process to finish | ||
if err != nil { | ||
t.Logf("Boundary process finished with error: %v", err) | ||
} | ||
|
||
// Clean up binary | ||
err = os.Remove("/tmp/boundary-test") | ||
require.NoError(t, err, "Failed to remove /tmp/boundary-test") | ||
|
||
// Step 4: Capture iptables rules after boundary has executed | ||
filterRules, err := getIptablesRules(filterTable) | ||
require.NoError(t, err) | ||
natRules, err := getIptablesRules(natTable) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, initialFilterRules, filterRules) | ||
require.Equal(t, initialNatRules, natRules) | ||
} |
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.
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.
double sudo?
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.
yeah, it's a bug, I'll fix in a follow-up PR