|
| 1 | +package jail |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log/slog" |
| 6 | + "os" |
| 7 | + "runtime" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/coder/jail/audit" |
| 13 | + "github.com/coder/jail/rules" |
| 14 | + "github.com/coder/jail/tls" |
| 15 | +) |
| 16 | + |
| 17 | +// Mock implementations for testing |
| 18 | +type mockRuleEngine struct { |
| 19 | + allowAll bool |
| 20 | +} |
| 21 | + |
| 22 | +func (m *mockRuleEngine) IsAllowed(method, url string) bool { |
| 23 | + return m.allowAll |
| 24 | +} |
| 25 | + |
| 26 | +func (m *mockRuleEngine) GetMatchingRule(method, url string) string { |
| 27 | + if m.allowAll { |
| 28 | + return "allow *" |
| 29 | + } |
| 30 | + return "" |
| 31 | +} |
| 32 | + |
| 33 | +type mockAuditor struct { |
| 34 | + requests []audit.Request |
| 35 | +} |
| 36 | + |
| 37 | +func (m *mockAuditor) AuditRequest(req audit.Request) { |
| 38 | + m.requests = append(m.requests, req) |
| 39 | +} |
| 40 | + |
| 41 | +type mockTLSManager struct { |
| 42 | + returnError bool |
| 43 | +} |
| 44 | + |
| 45 | +func (m *mockTLSManager) SetupTLS() error { |
| 46 | + if m.returnError { |
| 47 | + return os.ErrPermission |
| 48 | + } |
| 49 | + return nil |
| 50 | +} |
| 51 | + |
| 52 | +func (m *mockTLSManager) GetTLSConfig() (*tls.Config, error) { |
| 53 | + if m.returnError { |
| 54 | + return nil, os.ErrPermission |
| 55 | + } |
| 56 | + return &tls.Config{}, nil |
| 57 | +} |
| 58 | + |
| 59 | +func (m *mockTLSManager) GetCACertPEM() ([]byte, error) { |
| 60 | + if m.returnError { |
| 61 | + return nil, os.ErrPermission |
| 62 | + } |
| 63 | + return []byte("fake-ca-cert"), nil |
| 64 | +} |
| 65 | + |
| 66 | +// Helper function to check if we can create namespaces |
| 67 | +func canCreateNamespace() bool { |
| 68 | + // Only test on Linux where we might have permissions |
| 69 | + return runtime.GOOS == "linux" |
| 70 | +} |
| 71 | + |
| 72 | +func TestConfig_Validation(t *testing.T) { |
| 73 | + tests := []struct { |
| 74 | + name string |
| 75 | + config Config |
| 76 | + wantErr bool |
| 77 | + }{ |
| 78 | + { |
| 79 | + name: "valid config", |
| 80 | + config: Config{ |
| 81 | + RuleEngine: &mockRuleEngine{allowAll: true}, |
| 82 | + Auditor: &mockAuditor{}, |
| 83 | + CertManager: &mockTLSManager{returnError: false}, |
| 84 | + Logger: slog.New(slog.NewTextHandler(os.Stdout, nil)), |
| 85 | + }, |
| 86 | + wantErr: false, |
| 87 | + }, |
| 88 | + { |
| 89 | + name: "nil cert manager causes panic", |
| 90 | + config: Config{ |
| 91 | + RuleEngine: &mockRuleEngine{allowAll: true}, |
| 92 | + Auditor: &mockAuditor{}, |
| 93 | + CertManager: nil, // This should cause issues |
| 94 | + Logger: slog.New(slog.NewTextHandler(os.Stdout, nil)), |
| 95 | + }, |
| 96 | + wantErr: true, |
| 97 | + }, |
| 98 | + } |
| 99 | + |
| 100 | + for _, tt := range tests { |
| 101 | + t.Run(tt.name, func(t *testing.T) { |
| 102 | + ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) |
| 103 | + defer cancel() |
| 104 | + |
| 105 | + _, err := New(ctx, tt.config) |
| 106 | + if (err != nil) != tt.wantErr { |
| 107 | + t.Errorf("New() error = %v, wantErr %v", err, tt.wantErr) |
| 108 | + } |
| 109 | + }) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +func TestNew_Success(t *testing.T) { |
| 114 | + ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) |
| 115 | + defer cancel() |
| 116 | + |
| 117 | + config := Config{ |
| 118 | + RuleEngine: &mockRuleEngine{allowAll: true}, |
| 119 | + Auditor: &mockAuditor{}, |
| 120 | + CertManager: &mockTLSManager{returnError: false}, |
| 121 | + Logger: slog.New(slog.NewTextHandler(os.Stdout, nil)), |
| 122 | + } |
| 123 | + |
| 124 | + jail, err := New(ctx, config) |
| 125 | + if err != nil { |
| 126 | + // In test environments, namespace creation may fail due to permissions |
| 127 | + if strings.Contains(err.Error(), "permission denied") || |
| 128 | + strings.Contains(err.Error(), "operation not permitted") { |
| 129 | + t.Skipf("skipping due to insufficient permissions: %v", err) |
| 130 | + } |
| 131 | + t.Fatalf("New() failed: %v", err) |
| 132 | + } |
| 133 | + |
| 134 | + if jail == nil { |
| 135 | + t.Fatal("expected jail instance, got nil") |
| 136 | + } |
| 137 | + |
| 138 | + // Clean up |
| 139 | + if err := jail.Close(); err != nil { |
| 140 | + t.Logf("error closing jail: %v", err) |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +func TestNew_TLSError(t *testing.T) { |
| 145 | + ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) |
| 146 | + defer cancel() |
| 147 | + |
| 148 | + config := Config{ |
| 149 | + RuleEngine: &mockRuleEngine{allowAll: true}, |
| 150 | + Auditor: &mockAuditor{}, |
| 151 | + CertManager: &mockTLSManager{returnError: true}, // This will cause TLS setup to fail |
| 152 | + Logger: slog.New(slog.NewTextHandler(os.Stdout, nil)), |
| 153 | + } |
| 154 | + |
| 155 | + _, err := New(ctx, config) |
| 156 | + if err == nil { |
| 157 | + t.Error("expected error due to TLS setup failure, got nil") |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +func TestJail_StartStop(t *testing.T) { |
| 162 | + // This test will work on systems where namespace creation succeeds |
| 163 | + if !canCreateNamespace() { |
| 164 | + t.Skip("skipping test: cannot create namespace on this system") |
| 165 | + } |
| 166 | + |
| 167 | + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 168 | + defer cancel() |
| 169 | + |
| 170 | + config := Config{ |
| 171 | + RuleEngine: &mockRuleEngine{allowAll: true}, |
| 172 | + Auditor: &mockAuditor{}, |
| 173 | + CertManager: &mockTLSManager{returnError: false}, |
| 174 | + Logger: slog.New(slog.NewTextHandler(os.Stdout, nil)), |
| 175 | + } |
| 176 | + |
| 177 | + jail, err := New(ctx, config) |
| 178 | + if err != nil { |
| 179 | + t.Skipf("skipping test: failed to create jail: %v", err) |
| 180 | + } |
| 181 | + |
| 182 | + // Use a channel to coordinate shutdown |
| 183 | + done := make(chan struct{}) |
| 184 | + defer func() { |
| 185 | + close(done) |
| 186 | + if closeErr := jail.Close(); closeErr != nil { |
| 187 | + t.Logf("error closing jail: %v", closeErr) |
| 188 | + } |
| 189 | + }() |
| 190 | + |
| 191 | + err = jail.Start() |
| 192 | + if err != nil { |
| 193 | + // Check if it's a permission or system capability error |
| 194 | + if strings.Contains(err.Error(), "permission denied") || |
| 195 | + strings.Contains(err.Error(), "executable file not found") || |
| 196 | + strings.Contains(err.Error(), "operation not permitted") { |
| 197 | + t.Skipf("skipping test: insufficient permissions or missing tools: %v", err) |
| 198 | + } |
| 199 | + t.Fatalf("failed to start jail: %v", err) |
| 200 | + } |
| 201 | + |
| 202 | + // Give it more time to start properly |
| 203 | + time.Sleep(500 * time.Millisecond) |
| 204 | + |
| 205 | + // Test Command method |
| 206 | + cmd := jail.Command([]string{"echo", "test"}) |
| 207 | + if cmd == nil { |
| 208 | + t.Fatal("expected command, got nil") |
| 209 | + } |
| 210 | +} |
| 211 | + |
| 212 | +func TestJail_Command(t *testing.T) { |
| 213 | + tests := []struct { |
| 214 | + name string |
| 215 | + args []string |
| 216 | + want bool // whether we expect a non-nil command |
| 217 | + }{ |
| 218 | + { |
| 219 | + name: "simple echo", |
| 220 | + args: []string{"echo", "hello"}, |
| 221 | + want: true, |
| 222 | + }, |
| 223 | + { |
| 224 | + name: "empty command", |
| 225 | + args: []string{}, |
| 226 | + want: true, // Should still return a command object |
| 227 | + }, |
| 228 | + { |
| 229 | + name: "multiple args", |
| 230 | + args: []string{"ls", "-la", "/tmp"}, |
| 231 | + want: true, |
| 232 | + }, |
| 233 | + } |
| 234 | + |
| 235 | + // Create a simple jail instance for testing Command method |
| 236 | + ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) |
| 237 | + defer cancel() |
| 238 | + |
| 239 | + config := Config{ |
| 240 | + RuleEngine: &mockRuleEngine{allowAll: true}, |
| 241 | + Auditor: &mockAuditor{}, |
| 242 | + CertManager: &mockTLSManager{returnError: false}, |
| 243 | + Logger: slog.New(slog.NewTextHandler(os.Stdout, nil)), |
| 244 | + } |
| 245 | + |
| 246 | + jail, err := New(ctx, config) |
| 247 | + if err != nil { |
| 248 | + // Skip if we can't create jail due to system constraints |
| 249 | + if strings.Contains(err.Error(), "permission denied") || |
| 250 | + strings.Contains(err.Error(), "operation not permitted") { |
| 251 | + t.Skipf("skipping due to insufficient permissions: %v", err) |
| 252 | + } |
| 253 | + t.Fatalf("failed to create jail: %v", err) |
| 254 | + } |
| 255 | + defer jail.Close() |
| 256 | + |
| 257 | + for _, tt := range tests { |
| 258 | + t.Run(tt.name, func(t *testing.T) { |
| 259 | + cmd := jail.Command(tt.args) |
| 260 | + got := cmd != nil |
| 261 | + if got != tt.want { |
| 262 | + t.Errorf("Command() = %v, want %v", got, tt.want) |
| 263 | + } |
| 264 | + }) |
| 265 | + } |
| 266 | +} |
| 267 | + |
| 268 | +func TestNewNamespaceCommander(t *testing.T) { |
| 269 | + tests := []struct { |
| 270 | + name string |
| 271 | + goos string |
| 272 | + wantType string |
| 273 | + skip bool |
| 274 | + }{ |
| 275 | + { |
| 276 | + name: "linux support", |
| 277 | + goos: "linux", |
| 278 | + wantType: "*namespace.Linux", |
| 279 | + skip: runtime.GOOS != "linux", |
| 280 | + }, |
| 281 | + { |
| 282 | + name: "darwin support", |
| 283 | + goos: "darwin", |
| 284 | + wantType: "*namespace.Darwin", |
| 285 | + skip: runtime.GOOS != "darwin", |
| 286 | + }, |
| 287 | + { |
| 288 | + name: "unsupported platform", |
| 289 | + goos: "windows", |
| 290 | + wantType: "", |
| 291 | + skip: runtime.GOOS == "windows", // Would error in real scenario |
| 292 | + }, |
| 293 | + { |
| 294 | + name: "unknown platform", |
| 295 | + goos: "plan9", |
| 296 | + wantType: "", |
| 297 | + skip: true, // Always skip this fictional case |
| 298 | + }, |
| 299 | + } |
| 300 | + |
| 301 | + for _, tt := range tests { |
| 302 | + t.Run(tt.name, func(t *testing.T) { |
| 303 | + if tt.skip { |
| 304 | + t.Skip("skipping cross-platform test") |
| 305 | + } |
| 306 | + |
| 307 | + // Test the current platform's implementation |
| 308 | + commander, err := NewNamespaceCommander(UserInfo{ |
| 309 | + Username: "testuser", |
| 310 | + UID: 1000, |
| 311 | + GID: 1000, |
| 312 | + }, slog.New(slog.NewTextHandler(os.Stdout, nil))) |
| 313 | + |
| 314 | + if tt.goos == runtime.GOOS { |
| 315 | + // Should work on current platform |
| 316 | + if err != nil { |
| 317 | + // May fail due to permissions, which is okay |
| 318 | + if strings.Contains(err.Error(), "permission denied") { |
| 319 | + t.Skipf("skipping due to insufficient permissions: %v", err) |
| 320 | + } |
| 321 | + } |
| 322 | + if commander == nil && err == nil { |
| 323 | + t.Error("expected commander or error, got neither") |
| 324 | + } |
| 325 | + } |
| 326 | + }) |
| 327 | + } |
| 328 | +} |
0 commit comments