|
| 1 | +package e2e_tests |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "path/filepath" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/coder/boundary/util" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | +) |
| 17 | + |
| 18 | +// findProjectRoot finds the project root by looking for go.mod file |
| 19 | +func findProjectRoot(t *testing.T) string { |
| 20 | + cwd, err := os.Getwd() |
| 21 | + require.NoError(t, err, "Failed to get current working directory") |
| 22 | + |
| 23 | + // Start from current directory and walk up until we find go.mod |
| 24 | + dir := cwd |
| 25 | + for { |
| 26 | + goModPath := filepath.Join(dir, "go.mod") |
| 27 | + if _, err := os.Stat(goModPath); err == nil { |
| 28 | + return dir |
| 29 | + } |
| 30 | + |
| 31 | + parent := filepath.Dir(dir) |
| 32 | + if parent == dir { |
| 33 | + // Reached filesystem root |
| 34 | + t.Fatalf("Could not find go.mod file starting from %s", cwd) |
| 35 | + } |
| 36 | + dir = parent |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +// getNamespaceName gets the single network namespace name |
| 41 | +// Fails if there are 0 or multiple namespaces |
| 42 | +func getNamespaceName(t *testing.T) string { |
| 43 | + cmd := exec.Command("ip", "netns", "list") |
| 44 | + output, err := cmd.Output() |
| 45 | + require.NoError(t, err, "Failed to list network namespaces") |
| 46 | + |
| 47 | + lines := strings.Split(string(output), "\n") |
| 48 | + var namespaces []string |
| 49 | + |
| 50 | + for _, line := range lines { |
| 51 | + line = strings.TrimSpace(line) |
| 52 | + if line != "" { |
| 53 | + // Extract namespace name (first field) |
| 54 | + parts := strings.Fields(line) |
| 55 | + if len(parts) > 0 { |
| 56 | + namespaces = append(namespaces, parts[0]) |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + require.Len(t, namespaces, 1, "Expected exactly one network namespace, found %d: %v", len(namespaces), namespaces) |
| 62 | + return namespaces[0] |
| 63 | +} |
| 64 | + |
| 65 | +func TestBoundaryIntegration(t *testing.T) { |
| 66 | + // Find project root by looking for go.mod file |
| 67 | + projectRoot := findProjectRoot(t) |
| 68 | + |
| 69 | + // Build the boundary binary |
| 70 | + buildCmd := exec.Command("go", "build", "-o", "/tmp/boundary-test", "./cmd/...") |
| 71 | + buildCmd.Dir = projectRoot |
| 72 | + err := buildCmd.Run() |
| 73 | + require.NoError(t, err, "Failed to build boundary binary") |
| 74 | + |
| 75 | + // Create context for boundary process |
| 76 | + ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) |
| 77 | + defer cancel() |
| 78 | + |
| 79 | + // Start boundary process with sudo |
| 80 | + boundaryCmd := exec.CommandContext(ctx, "/tmp/boundary-test", |
| 81 | + "--allow", "dev.coder.com", |
| 82 | + "--allow", "jsonplaceholder.typicode.com", |
| 83 | + "--log-level", "debug", |
| 84 | + "--", "bash", "-c", "sleep 10 && echo 'Test completed'") |
| 85 | + |
| 86 | + // Suppress output to prevent terminal corruption |
| 87 | + boundaryCmd.Stdout = os.Stdout // Let it go to /dev/null |
| 88 | + boundaryCmd.Stderr = os.Stderr |
| 89 | + |
| 90 | + // Start the process |
| 91 | + err = boundaryCmd.Start() |
| 92 | + require.NoError(t, err, "Failed to start boundary process") |
| 93 | + |
| 94 | + // Give boundary time to start |
| 95 | + time.Sleep(2 * time.Second) |
| 96 | + |
| 97 | + // Get the namespace name that boundary created |
| 98 | + namespaceName := getNamespaceName(t) |
| 99 | + |
| 100 | + // Test HTTP request through boundary (from inside the jail) |
| 101 | + t.Run("HTTPRequestThroughBoundary", func(t *testing.T) { |
| 102 | + // Run curl directly in the namespace using ip netns exec |
| 103 | + curlCmd := exec.Command("sudo", "ip", "netns", "exec", namespaceName, |
| 104 | + "curl", "http://jsonplaceholder.typicode.com/todos/1") |
| 105 | + |
| 106 | + // Capture stderr separately |
| 107 | + var stderr bytes.Buffer |
| 108 | + curlCmd.Stderr = &stderr |
| 109 | + output, err := curlCmd.Output() |
| 110 | + |
| 111 | + if err != nil { |
| 112 | + t.Fatalf("curl command failed: %v, stderr: %s, output: %s", err, stderr.String(), string(output)) |
| 113 | + } |
| 114 | + |
| 115 | + // Verify response contains expected content |
| 116 | + expectedResponse := `{ |
| 117 | + "userId": 1, |
| 118 | + "id": 1, |
| 119 | + "title": "delectus aut autem", |
| 120 | + "completed": false |
| 121 | +}` |
| 122 | + require.Equal(t, expectedResponse, string(output)) |
| 123 | + }) |
| 124 | + |
| 125 | + // Test HTTPS request through boundary (from inside the jail) |
| 126 | + t.Run("HTTPSRequestThroughBoundary", func(t *testing.T) { |
| 127 | + _, _, _, _, configDir := util.GetUserInfo() |
| 128 | + certPath := fmt.Sprintf("%v/ca-cert.pem", configDir) |
| 129 | + |
| 130 | + // Run curl directly in the namespace using ip netns exec |
| 131 | + curlCmd := exec.Command("sudo", "ip", "netns", "exec", namespaceName, |
| 132 | + "env", fmt.Sprintf("SSL_CERT_FILE=%v", certPath), "curl", "-s", "https://dev.coder.com/api/v2") |
| 133 | + |
| 134 | + // Capture stderr separately |
| 135 | + var stderr bytes.Buffer |
| 136 | + curlCmd.Stderr = &stderr |
| 137 | + output, err := curlCmd.Output() |
| 138 | + |
| 139 | + if err != nil { |
| 140 | + t.Fatalf("curl command failed: %v, stderr: %s, output: %s", err, stderr.String(), string(output)) |
| 141 | + } |
| 142 | + |
| 143 | + // Verify response contains expected content |
| 144 | + expectedResponse := `{"message":"👋"} |
| 145 | +` |
| 146 | + require.Equal(t, expectedResponse, string(output)) |
| 147 | + }) |
| 148 | + |
| 149 | + // Test blocked domain (from inside the jail) |
| 150 | + t.Run("BlockedDomainTest", func(t *testing.T) { |
| 151 | + // Run curl directly in the namespace using ip netns exec |
| 152 | + curlCmd := exec.Command("sudo", "ip", "netns", "exec", namespaceName, |
| 153 | + "curl", "-s", "http://example.com") |
| 154 | + |
| 155 | + // Capture stderr separately |
| 156 | + var stderr bytes.Buffer |
| 157 | + curlCmd.Stderr = &stderr |
| 158 | + output, err := curlCmd.Output() |
| 159 | + |
| 160 | + if err != nil { |
| 161 | + t.Fatalf("curl command failed: %v, stderr: %s, output: %s", err, stderr.String(), string(output)) |
| 162 | + } |
| 163 | + require.Contains(t, string(output), "Request Blocked by Boundary") |
| 164 | + }) |
| 165 | + |
| 166 | + // Clean up |
| 167 | + cancel() // This will terminate the boundary process |
| 168 | + err = boundaryCmd.Wait() // Wait for process to finish |
| 169 | + if err != nil { |
| 170 | + t.Logf("Boundary process finished with error: %v", err) |
| 171 | + } |
| 172 | + |
| 173 | + // Clean up binary |
| 174 | + err = os.Remove("/tmp/boundary-test") |
| 175 | + require.NoError(t, err, "Failed to remove /tmp/boundary-test") |
| 176 | +} |
0 commit comments