@@ -2,6 +2,8 @@ package jail
2
2
3
3
import (
4
4
"context"
5
+ "crypto/tls"
6
+ "fmt"
5
7
"log/slog"
6
8
"os"
7
9
"runtime"
@@ -10,24 +12,20 @@ import (
10
12
"time"
11
13
12
14
"github.com/coder/jail/audit"
15
+ "github.com/coder/jail/namespace"
13
16
"github.com/coder/jail/rules"
14
- "github.com/coder/jail/tls"
15
17
)
16
18
17
19
// Mock implementations for testing
18
20
type mockRuleEngine struct {
19
21
allowAll bool
20
22
}
21
23
22
- func (m * mockRuleEngine ) IsAllowed (method , url string ) bool {
23
- return m .allowAll
24
- }
25
-
26
- func (m * mockRuleEngine ) GetMatchingRule (method , url string ) string {
24
+ func (m * mockRuleEngine ) Evaluate (method , url string ) rules.Result {
27
25
if m .allowAll {
28
- return "allow *"
26
+ return rules. Result { Allowed : true , Rule : "allow *" }
29
27
}
30
- return ""
28
+ return rules. Result { Allowed : false , Rule : "" }
31
29
}
32
30
33
31
type mockAuditor struct {
@@ -42,25 +40,11 @@ type mockTLSManager struct {
42
40
returnError bool
43
41
}
44
42
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 ) {
43
+ func (m * mockTLSManager ) SetupTLSAndWriteCACert () (* tls.Config , string , string , error ) {
53
44
if m .returnError {
54
- return nil , os .ErrPermission
45
+ return nil , "" , "" , os .ErrPermission
55
46
}
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
47
+ return & tls.Config {}, "/tmp/cert.pem" , "/tmp/key.pem" , nil
64
48
}
65
49
66
50
// Helper function to check if we can create namespaces
@@ -305,12 +289,32 @@ func TestNewNamespaceCommander(t *testing.T) {
305
289
}
306
290
307
291
// 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 )))
292
+ config := namespace.Config {
293
+ Logger : slog .New (slog .NewTextHandler (os .Stdout , nil )),
294
+ HttpProxyPort : 8080 ,
295
+ HttpsProxyPort : 8443 ,
296
+ UserInfo : namespace.UserInfo {
297
+ Username : "testuser" ,
298
+ Uid : 1000 ,
299
+ Gid : 1000 ,
300
+ HomeDir : "/home/testuser" ,
301
+ ConfigDir : "/home/testuser/.config" ,
302
+ },
303
+ Env : map [string ]string {},
304
+ }
313
305
306
+ var commander namespace.Commander
307
+ var err error
308
+
309
+ switch runtime .GOOS {
310
+ case "linux" :
311
+ commander , err = namespace .NewLinux (config )
312
+ case "darwin" :
313
+ commander , err = namespace .NewMacOS (config )
314
+ default :
315
+ err = fmt .Errorf ("unsupported platform: %s" , runtime .GOOS )
316
+ }
317
+
314
318
if tt .goos == runtime .GOOS {
315
319
// Should work on current platform
316
320
if err != nil {
0 commit comments