Skip to content

Building 70 Projects ranging from beginner to advanced so anyone can — learn from, build upon, use as a reference, or even copy directly. Owned by my cybersecurity company 👇

fix(khaos): distinguish host mounts from container identity

CarterPerez-devfff434e72f7c
diff --git a/PROJECTS/intermediate/chaos-engineering-tool/internal/dockerx/self.go b/PROJECTS/intermediate/chaos-engineering-tool/internal/dockerx/self.go
index a1c544df..b95e4919 100644
--- a/PROJECTS/intermediate/chaos-engineering-tool/internal/dockerx/self.go
+++ b/PROJECTS/intermediate/chaos-engineering-tool/internal/dockerx/self.go
@@ -6,6 +6,7 @@ package dockerx
import (
"os"
"regexp"
+ "strings"
)
const (
@@ -13,22 +14,58 @@ const (
procCgroup = "/proc/self/cgroup"
)
-var containerIDPattern = regexp.MustCompile(`[0-9a-f]{64}`)
+var (
+ containerCgroupPattern = regexp.MustCompile(
+ `(?:^|/)docker(?:/|-)([0-9a-f]{64})(?:\.scope)?(?:/|$)`,
+ )
+ containerMountPattern = regexp.MustCompile(
+ `(?:^|/)containers/([0-9a-f]{64})/(?:hostname|hosts|resolv\.conf)$`,
+ )
+)
func SelfContainerID() string {
- for _, path := range []string{procMountInfo, procCgroup} {
- if id := scanForContainerID(path); id != "" {
+ if raw, err := os.ReadFile(procCgroup); err == nil {
+ if id := containerIDFromCgroup(raw); id != "" {
return id
}
}
- return ""
-}
-func scanForContainerID(path string) string {
- //nolint:gosec // a procfs path from khaos's own fixed list
- raw, err := os.ReadFile(path)
+ raw, err := os.ReadFile(procMountInfo)
if err != nil {
return ""
}
- return containerIDPattern.FindString(string(raw))
+ return containerIDFromMountInfo(raw)
+}
+
+func containerIDFromCgroup(raw []byte) string {
+ for _, line := range strings.Split(string(raw), "\n") {
+ fields := strings.SplitN(line, ":", 3)
+ if len(fields) != 3 {
+ continue
+ }
+ match := containerCgroupPattern.FindStringSubmatch(fields[2])
+ if len(match) > 1 {
+ return match[1]
+ }
+ }
+ return ""
+}
+
+func containerIDFromMountInfo(raw []byte) string {
+ for _, line := range strings.Split(string(raw), "\n") {
+ fields := strings.Fields(line)
+ if len(fields) < 6 {
+ continue
+ }
+ switch fields[4] {
+ case "/etc/hostname", "/etc/hosts", "/etc/resolv.conf":
+ default:
+ continue
+ }
+ match := containerMountPattern.FindStringSubmatch(fields[3])
+ if len(match) > 1 {
+ return match[1]
+ }
+ }
+ return ""
}
diff --git a/PROJECTS/intermediate/chaos-engineering-tool/internal/dockerx/self_test.go b/PROJECTS/intermediate/chaos-engineering-tool/internal/dockerx/self_test.go
new file mode 100644
index 00000000..175afdf5
--- /dev/null
+++ b/PROJECTS/intermediate/chaos-engineering-tool/internal/dockerx/self_test.go
@@ -0,0 +1,68 @@
+// ©AngelaMos | 2026
+// self_test.go
+
+package dockerx
+
+import (
+ "strings"
+ "testing"
+)
+
+func TestContainerIDFromCgroup(t *testing.T) {
+ id := strings.Repeat("a", 64)
+ for _, tc := range []struct {
+ name string
+ raw string
+ want string
+ }{
+ {"v1", "5:cpu,cpuacct:/docker/" + id, id},
+ {"v2", "0::/system.slice/docker-" + id + ".scope", id},
+ {"rootless", "0::/user.slice/user-1000.slice/docker-" + id + ".scope", id},
+ {"nested", "0::/docker/" + id + "/worker", id},
+ {"private namespace", "0::/", ""},
+ {"host", "0::/user.slice/user-1000.slice/session-1.scope", ""},
+ {"unrelated hash", "0::/system.slice/runner-" + id + ".scope", ""},
+ {"long hash", "0::/docker/" + id + "a", ""},
+ {"short hash", "0::/docker/" + id[:12], ""},
+ {"malformed", "/docker/" + id, ""},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ if got := containerIDFromCgroup([]byte(tc.raw)); got != tc.want {
+ t.Fatalf("container ID = %q, want %q", got, tc.want)
+ }
+ })
+ }
+}
+
+func TestContainerIDFromMountInfo(t *testing.T) {
+ id := strings.Repeat("a", 64)
+ peer := strings.Repeat("b", 64)
+ root := "/var/lib/docker/containers/" + id
+ mount := func(root, point string) string {
+ return "100 90 8:1 " + root + " " + point +
+ " rw,relatime shared:7 - ext4 /dev/root rw\n"
+ }
+ hostMount := mount("/", "/var/lib/docker/rootfs/overlayfs/"+peer)
+ for _, tc := range []struct {
+ name string
+ raw string
+ want string
+ }{
+ {"hostname", mount(root+"/hostname", "/etc/hostname"), id},
+ {"hosts", mount(root+"/hosts", "/etc/hosts"), id},
+ {"resolver", mount(root+"/resolv.conf", "/etc/resolv.conf"), id},
+ {"custom root", mount("/srv/docker/containers/"+id+"/hosts", "/etc/hosts"), id},
+ {"host overlay", hostMount, ""},
+ {"host container file", mount(root+"/hostname", root+"/hostname"), ""},
+ {"unrelated bind", mount(root+"/hosts", "/mnt/hosts"), ""},
+ {"unrelated hash", mount("/srv/"+id+"/hosts", "/etc/hosts"), ""},
+ {"overlay before identity", hostMount + mount(root+"/hosts", "/etc/hosts"), id},
+ {"malformed", root + "/hostname /etc/hostname", ""},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ if got := containerIDFromMountInfo([]byte(tc.raw)); got != tc.want {
+ t.Fatalf("container ID = %q, want %q", got, tc.want)
+ }
+ })
+ }
+}