Skip to content

Commit 07a9af3

Browse files
test works
1 parent d628bfc commit 07a9af3

File tree

2 files changed

+129
-2
lines changed

2 files changed

+129
-2
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package e2e_tests
2+
3+
import (
4+
"context"
5+
"os"
6+
"os/exec"
7+
"strings"
8+
"testing"
9+
"time"
10+
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
// getNamespaceName gets the single network namespace name
15+
// Fails if there are 0 or multiple namespaces
16+
func getNamespaceName(t *testing.T) string {
17+
cmd := exec.Command("ip", "netns", "list")
18+
output, err := cmd.Output()
19+
require.NoError(t, err, "Failed to list network namespaces")
20+
21+
lines := strings.Split(string(output), "\n")
22+
var namespaces []string
23+
24+
for _, line := range lines {
25+
line = strings.TrimSpace(line)
26+
if line != "" {
27+
// Extract namespace name (first field)
28+
parts := strings.Fields(line)
29+
if len(parts) > 0 {
30+
namespaces = append(namespaces, parts[0])
31+
}
32+
}
33+
}
34+
35+
require.Len(t, namespaces, 1, "Expected exactly one network namespace, found %d: %v", len(namespaces), namespaces)
36+
return namespaces[0]
37+
}
38+
39+
func TestBoundaryIntegration(t *testing.T) {
40+
if testing.Short() {
41+
t.Skip("Skipping integration test in short mode")
42+
}
43+
44+
// Build the boundary binary
45+
buildCmd := exec.Command("go", "build", "-o", "/tmp/boundary-test", "./cmd/...")
46+
buildCmd.Dir = "/home/coder/boundary"
47+
err := buildCmd.Run()
48+
require.NoError(t, err, "Failed to build boundary binary")
49+
50+
// Create context for boundary process
51+
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
52+
defer cancel()
53+
54+
// Start boundary process with sudo
55+
boundaryCmd := exec.CommandContext(ctx, "/tmp/boundary-test",
56+
"--allow", "dev.coder.com",
57+
"--allow", "jsonplaceholder.typicode.com",
58+
"--log-level", "error",
59+
"--", "bash", "-c", "sleep 10 && echo 'Test completed'")
60+
61+
// Set up environment
62+
boundaryCmd.Env = append(os.Environ(), "SSL_CERT_FILE=/home/coder/.config/coder_boundary/ca-cert.pem")
63+
64+
// Suppress output to prevent terminal corruption
65+
boundaryCmd.Stdout = nil // Let it go to /dev/null
66+
boundaryCmd.Stderr = nil // Let it go to /dev/null
67+
68+
// Start the process
69+
err = boundaryCmd.Start()
70+
require.NoError(t, err, "Failed to start boundary process")
71+
72+
// Give boundary time to start
73+
time.Sleep(2 * time.Second)
74+
75+
// Get the namespace name that boundary created
76+
namespaceName := getNamespaceName(t)
77+
78+
// Test HTTP request through boundary (from inside the jail)
79+
t.Run("HTTPRequestThroughBoundary", func(t *testing.T) {
80+
// Run curl directly in the namespace using ip netns exec
81+
curlCmd := exec.Command("sudo", "ip", "netns", "exec", namespaceName,
82+
"curl", "-s", "http://jsonplaceholder.typicode.com/todos/1")
83+
84+
// Capture output
85+
output, err := curlCmd.Output()
86+
require.NoError(t, err, "curl command failed")
87+
88+
// Verify response contains expected content
89+
require.Contains(t, string(output), "delectus aut autem")
90+
require.Contains(t, string(output), "userId")
91+
})
92+
93+
// Test HTTPS request through boundary (from inside the jail)
94+
t.Run("HTTPSRequestThroughBoundary", func(t *testing.T) {
95+
// Run curl directly in the namespace using ip netns exec
96+
curlCmd := exec.Command("sudo", "ip", "netns", "exec", namespaceName,
97+
"curl", "-s", "-k", "https://dev.coder.com/api/v2")
98+
99+
// Capture output
100+
output, err := curlCmd.Output()
101+
require.NoError(t, err, "curl command failed")
102+
103+
// Verify response contains expected content
104+
require.Contains(t, string(output), "👋")
105+
})
106+
107+
//// Test blocked domain (from inside the jail)
108+
//t.Run("BlockedDomainTest", func(t *testing.T) {
109+
// // Run curl directly in the namespace using ip netns exec
110+
// curlCmd := exec.Command("sudo", "ip", "netns", "exec", namespaceName,
111+
// "curl", "-s", "http://example.com")
112+
//
113+
// // This should fail
114+
// _, err := curlCmd.Output()
115+
// require.Error(t, err, "Expected blocked domain to fail")
116+
//})
117+
118+
// Clean up
119+
cancel() // This will terminate the boundary process
120+
err = boundaryCmd.Wait() // Wait for process to finish
121+
if err != nil {
122+
t.Logf("Boundary process finished with error: %v", err)
123+
}
124+
125+
// Clean up binary
126+
os.Remove("/tmp/boundary-test")
127+
}

jail/linux.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ func (l *LinuxJail) Start() error {
7575
func (l *LinuxJail) Command(command []string) *exec.Cmd {
7676
l.logger.Debug("Creating command with namespace", "namespace", l.namespace)
7777

78-
cmdArgs := []string{"ip", "netns", "exec", l.namespace}
78+
cmdArgs := []string{"netns", "exec", l.namespace}
7979
cmdArgs = append(cmdArgs, command...)
8080

81-
cmd := exec.Command("sudo", cmdArgs...)
81+
cmd := exec.Command("ip", cmdArgs...)
8282
cmd.Env = l.commandEnv
8383

8484
return cmd

0 commit comments

Comments
 (0)