Files
ARTEX/ghidra/compose_test.go
BaaRai b9f90d9216 test(ghidra): set the runner token in the compose fixture
The default compose now requires ARTEX_RUNNER_TOKEN for the panel, so the ghidra overlay config test must provide it like the sandbox compose test already does.
2026-11-30 19:48:11 +01:00

127 lines
4.3 KiB
Go

package ghidra
import (
"encoding/json"
"os"
"os/exec"
"path/filepath"
"slices"
"strings"
"testing"
)
func TestComposeOverlayConfigHasInternalNetworkAndHardening(t *testing.T) {
root := filepath.Join("..")
baseFile := filepath.Join(root, "docker-compose.yml")
overlayFile := filepath.Join(root, "docker-compose.ghidra.yml")
raw, err := os.ReadFile(overlayFile)
if err != nil {
t.Fatalf("read overlay: %v", err)
}
text := string(raw)
for _, needle := range []string{
"internal: true",
"ghidra-internal:",
"read_only: true",
"cap_drop:",
"- ALL",
"no-new-privileges:true",
"pids_limit:",
"mem_limit:",
"cpus:",
"tmpfs:",
"/home/ghidra:",
"uid=10001",
} {
if !strings.Contains(text, needle) {
t.Errorf("docker-compose.ghidra.yml missing %q", needle)
}
}
for _, line := range strings.Split(text, "\n") {
trimmed := strings.TrimSpace(line)
if trimmed == "- /home" || strings.HasPrefix(trimmed, "- /home:") {
t.Errorf("docker-compose.ghidra.yml must not mount /home tmpfs (masks /home/ghidra): %q", trimmed)
}
}
if _, err := exec.LookPath("docker"); err != nil {
t.Skip("docker CLI not available; static overlay assertions passed")
}
cmd := exec.Command("docker", "compose", "-f", baseFile, "-f", overlayFile, "config", "--format", "json")
cmd.Env = append(os.Environ(), "POSTGRES_PASSWORD=test", "ARTEX_RUNNER_TOKEN=testtoken")
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("docker compose config failed: %v\n%s", err, out)
}
var cfg struct {
Networks map[string]struct {
Internal bool `json:"internal"`
} `json:"networks"`
Services map[string]struct {
Networks map[string]any `json:"networks"`
Ports []any `json:"ports"`
ReadOnly bool `json:"read_only"`
CapDrop []string `json:"cap_drop"`
SecurityOpt []string `json:"security_opt"`
PidsLimit int64 `json:"pids_limit"`
MemLimit json.RawMessage `json:"mem_limit"`
CPUs json.RawMessage `json:"cpus"`
Tmpfs []string `json:"tmpfs"`
} `json:"services"`
}
if err := json.Unmarshal(out, &cfg); err != nil {
t.Fatalf("parse compose json: %v\n%s", err, out)
}
net, ok := cfg.Networks["ghidra-internal"]
if !ok || !net.Internal {
t.Fatalf("expected networks.ghidra-internal.internal=true, got %+v", cfg.Networks)
}
ghidraSvc, ok := cfg.Services["ghidra"]
if !ok {
t.Fatalf("missing ghidra service in rendered compose config")
}
if _, hasGhidraNet := ghidraSvc.Networks["ghidra-internal"]; len(ghidraSvc.Networks) != 1 || !hasGhidraNet {
t.Fatalf("ghidra service must attach ONLY to ghidra-internal (no egress), got %+v", ghidraSvc.Networks)
}
if len(ghidraSvc.Ports) != 0 {
t.Fatalf("ghidra service must expose no host ports, got %+v", ghidraSvc.Ports)
}
if !ghidraSvc.ReadOnly {
t.Fatalf("ghidra service must set read_only: true")
}
if !slices.Contains(ghidraSvc.CapDrop, "ALL") {
t.Fatalf("ghidra service must drop ALL capabilities, got %v", ghidraSvc.CapDrop)
}
if !slices.Contains(ghidraSvc.SecurityOpt, "no-new-privileges:true") {
t.Fatalf("ghidra service must set no-new-privileges:true, got %v", ghidraSvc.SecurityOpt)
}
if ghidraSvc.PidsLimit <= 0 || len(ghidraSvc.MemLimit) == 0 || len(ghidraSvc.CPUs) == 0 {
t.Fatalf("ghidra service missing resource limits: pids=%d mem=%s cpus=%s", ghidraSvc.PidsLimit, ghidraSvc.MemLimit, ghidraSvc.CPUs)
}
if len(ghidraSvc.Tmpfs) == 0 {
t.Fatalf("ghidra service missing tmpfs mounts for read_only rootfs")
}
hasHomeGhidra := false
for _, entry := range ghidraSvc.Tmpfs {
if entry == "/home" || strings.HasPrefix(entry, "/home:") {
t.Fatalf("ghidra tmpfs must not mount /home (masks /home/ghidra): %q", entry)
}
if strings.HasPrefix(entry, "/home/ghidra:") && strings.Contains(entry, "uid=10001") {
hasHomeGhidra = true
}
}
if !hasHomeGhidra {
t.Fatalf("ghidra service must mount /home/ghidra tmpfs with uid=10001, got %v", ghidraSvc.Tmpfs)
}
artexSvc, ok := cfg.Services["artex"]
if !ok {
t.Fatalf("missing artex service in rendered compose config")
}
_, hasDefault := artexSvc.Networks["default"]
_, hasInternal := artexSvc.Networks["ghidra-internal"]
if !hasDefault || !hasInternal {
t.Fatalf("artex service must attach to both default and ghidra-internal, got %+v", artexSvc.Networks)
}
}