|
| 1 | +package actions |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os/exec" |
| 6 | + "runtime" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/urfave/cli" |
| 10 | +) |
| 11 | + |
| 12 | +type DoctorAction struct{} |
| 13 | + |
| 14 | +func NewDoctorAction() *DoctorAction { |
| 15 | + return &DoctorAction{} |
| 16 | +} |
| 17 | + |
| 18 | +func (a *DoctorAction) Execute(c *cli.Context) error { |
| 19 | + fmt.Printf("aigate doctor — runtime checks (%s/%s)\n\n", runtime.GOOS, runtime.GOARCH) |
| 20 | + |
| 21 | + switch runtime.GOOS { |
| 22 | + case "linux": |
| 23 | + runLinuxChecks() |
| 24 | + case "darwin": |
| 25 | + runDarwinChecks() |
| 26 | + default: |
| 27 | + fmt.Println(" No platform-specific checks for this OS.") |
| 28 | + } |
| 29 | + |
| 30 | + return nil |
| 31 | +} |
| 32 | + |
| 33 | +// ── Linux ──────────────────────────────────────────────────────────────────── |
| 34 | + |
| 35 | +func runLinuxChecks() { |
| 36 | + bwrapOK := printCheck("bwrap", |
| 37 | + "sandbox isolation (mount / pid / user namespaces)", |
| 38 | + "sudo dnf install bubblewrap OR sudo apt install bubblewrap") |
| 39 | + |
| 40 | + slirpOK := printCheck("slirp4netns", |
| 41 | + "network filtering — required for allow_net rules", |
| 42 | + "sudo dnf install slirp4netns OR sudo apt install slirp4netns") |
| 43 | + |
| 44 | + printCheck("setfacl", |
| 45 | + "persistent ACLs — deny_read enforced on disk between sessions", |
| 46 | + "sudo dnf install acl OR sudo apt install acl") |
| 47 | + |
| 48 | + unshareOK := checkUserNamespaces() |
| 49 | + |
| 50 | + fmt.Println() |
| 51 | + printLinuxIsolationMode(bwrapOK, slirpOK, unshareOK) |
| 52 | +} |
| 53 | + |
| 54 | +// printCheck looks up tool by name, prints a status line, and returns true if found. |
| 55 | +// installHint is printed on a second line when the tool is missing. |
| 56 | +func printCheck(name, desc, installHint string) bool { |
| 57 | + path, err := exec.LookPath(name) |
| 58 | + if err != nil { |
| 59 | + fmt.Printf(" WARN %-16s not found\n", name) |
| 60 | + fmt.Printf(" %s\n", desc) |
| 61 | + if installHint != "" { |
| 62 | + fmt.Printf(" Install: %s\n", installHint) |
| 63 | + } |
| 64 | + return false |
| 65 | + } |
| 66 | + |
| 67 | + ver := toolVersion(name) |
| 68 | + if ver != "" { |
| 69 | + fmt.Printf(" ok %-16s %s (%s)\n", name, ver, path) |
| 70 | + } else { |
| 71 | + fmt.Printf(" ok %-16s %s\n", name, path) |
| 72 | + } |
| 73 | + fmt.Printf(" %s\n", desc) |
| 74 | + return true |
| 75 | +} |
| 76 | + |
| 77 | +// toolVersion runs `name --version` and returns the first meaningful token. |
| 78 | +func toolVersion(name string) string { |
| 79 | + out, err := exec.Command(name, "--version").CombinedOutput() //nolint:gosec |
| 80 | + if err != nil { |
| 81 | + return "" |
| 82 | + } |
| 83 | + line := strings.SplitN(strings.TrimSpace(string(out)), "\n", 2)[0] |
| 84 | + // Keep only the version token (e.g. "bubblewrap 0.10.0" → "v0.10.0") |
| 85 | + parts := strings.Fields(line) |
| 86 | + for _, p := range parts { |
| 87 | + if len(p) > 0 && (p[0] >= '0' && p[0] <= '9') { |
| 88 | + return "v" + p |
| 89 | + } |
| 90 | + } |
| 91 | + return "" |
| 92 | +} |
| 93 | + |
| 94 | +// checkUserNamespaces verifies that unprivileged user namespaces are enabled. |
| 95 | +func checkUserNamespaces() bool { |
| 96 | + // Attempt a trivial unshare; if it fails the kernel has them disabled. |
| 97 | + err := exec.Command("unshare", "--user", "--", "true").Run() |
| 98 | + if err != nil { |
| 99 | + fmt.Printf(" WARN %-16s disabled\n", "user namespaces") |
| 100 | + fmt.Printf(" Required for all sandbox modes.\n") |
| 101 | + fmt.Printf(" Enable: echo 1 | sudo tee /proc/sys/kernel/unprivileged_userns_clone\n") |
| 102 | + return false |
| 103 | + } |
| 104 | + fmt.Printf(" ok %-16s enabled\n", "user namespaces") |
| 105 | + fmt.Printf(" Required for all sandbox modes.\n") |
| 106 | + return true |
| 107 | +} |
| 108 | + |
| 109 | +// printLinuxIsolationMode describes which sandbox path will be taken based on |
| 110 | +// available tools, mirroring the dispatch logic in RunSandboxed. |
| 111 | +func printLinuxIsolationMode(bwrap, slirp, unshare bool) { |
| 112 | + fmt.Println("Isolation mode:") |
| 113 | + |
| 114 | + switch { |
| 115 | + case bwrap && slirp: |
| 116 | + fmt.Println(" bwrap + slirp4netns (full isolation)") |
| 117 | + fmt.Println() |
| 118 | + fmt.Println(" deny_read bwrap bind mounts kernel-enforced, per-run") |
| 119 | + fmt.Println(" deny_exec bwrap bind mounts kernel-enforced, per-run") |
| 120 | + fmt.Println(" allow_net bwrap --unshare-net network namespace via bwrap") |
| 121 | + fmt.Println(" slirp4netns + iptables egress filtered to allowed hosts") |
| 122 | + fmt.Println(" config dir bwrap tmpfs overlay ~/.aigate hidden from agent") |
| 123 | + case bwrap && !slirp: |
| 124 | + fmt.Println(" bwrap (no network filtering — slirp4netns missing)") |
| 125 | + fmt.Println() |
| 126 | + fmt.Println(" deny_read bwrap bind mounts kernel-enforced, per-run") |
| 127 | + fmt.Println(" deny_exec bwrap bind mounts kernel-enforced, per-run") |
| 128 | + fmt.Println(" allow_net INACTIVE install slirp4netns to enable") |
| 129 | + fmt.Println(" config dir bwrap tmpfs overlay ~/.aigate hidden from agent") |
| 130 | + case !bwrap && slirp && unshare: |
| 131 | + fmt.Println(" unshare + slirp4netns (fallback — install bwrap for stronger isolation)") |
| 132 | + fmt.Println() |
| 133 | + fmt.Println(" deny_read mount namespace overrides shell-script-based") |
| 134 | + fmt.Println(" deny_exec mount namespace overrides shell-script-based") |
| 135 | + fmt.Println(" allow_net unshare --net + slirp4netns egress filtered to allowed hosts") |
| 136 | + fmt.Println(" config dir tmpfs mount ~/.aigate hidden from agent") |
| 137 | + case !bwrap && !slirp && unshare: |
| 138 | + fmt.Println(" unshare (fallback — install bwrap for stronger isolation)") |
| 139 | + fmt.Println() |
| 140 | + fmt.Println(" deny_read mount namespace overrides shell-script-based") |
| 141 | + fmt.Println(" deny_exec mount namespace overrides shell-script-based") |
| 142 | + fmt.Println(" allow_net INACTIVE install slirp4netns to enable") |
| 143 | + fmt.Println(" config dir tmpfs mount ~/.aigate hidden from agent") |
| 144 | + default: |
| 145 | + fmt.Println(" NONE — user namespaces are disabled; sandbox cannot run") |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +// ── macOS ──────────────────────────────────────────────────────────────────── |
| 150 | + |
| 151 | +func runDarwinChecks() { |
| 152 | + printCheck("sandbox-exec", |
| 153 | + "macOS Seatbelt sandbox (deny_read, deny_exec, allow_net)", |
| 154 | + "Built into macOS — should always be present") |
| 155 | + |
| 156 | + fmt.Println() |
| 157 | + fmt.Println("Isolation mode: sandbox-exec (Seatbelt)") |
| 158 | + fmt.Println() |
| 159 | + fmt.Println(" deny_read Seatbelt file-read* deny rules kernel-enforced") |
| 160 | + fmt.Println(" deny_exec Seatbelt process-exec deny kernel-enforced") |
| 161 | + fmt.Println(" allow_net Seatbelt network-outbound rules kernel-enforced") |
| 162 | +} |
0 commit comments