package catalogue import ( "strings" "testing" ) // The ssh-client module, composed as a machine receives it (novox/hq to-be 29, research 027/03): a // region at the START of the operator's ~/.ssh/config that includes ~/.ssh/config.d/* — first, // because ssh takes the first value it finds for each option — and the mesh's Host blocks as the // whole of ~/.ssh/config.d/00-mesh, every other node with its account. ~/.ssh and ~/.ssh/config.d // are created 0700 and owned by the account; the operator's own config below the region is kept. func TestSSHClientOwnsTheOperatorsSSHConfig(t *testing.T) { shelf := shelf(catalogueManifest(t, "ssh-client")) got, err := Resolve(shelf, []string{"ssh-client"}, Node{Name: "homer", At: "homer.internal", Account: "jo", AccountHome: "/home/jo"}, World{}) if err != nil { t.Fatal(err) } names := map[string]string{"homer.internal": "10.10.0.1", "marge.internal": "10.10.0.2", "bart.internal": "10.10.0.3"} out, err := got.Declaration(Rendering{ Names: names, Machines: names, Accounts: map[string]string{"homer": "jo", "marge": "jo", "bart": "op"}, Suffix: "internal", }) if err != nil { t.Fatal(err) } by := map[string]map[string]any{} for _, r := range out { by[r["id"].(string)] = r } for id, path := range map[string]string{ "ssh-client.ssh-dir": "/home/jo/.ssh", "ssh-client.config-d": "/home/jo/.ssh/config.d"} { dir := by[id] if dir == nil || dir["type"] != "directory" || dir["path"] != path || dir["owner"] != "jo" || dir["mode"] != "0700" { t.Fatalf("%s is not created 0700 owned by the account: %v", path, dir) } } cfg := by["ssh-client.config"] if cfg == nil || cfg["path"] != "/home/jo/.ssh/config" || cfg["owner"] != "jo" || cfg["into"] != "block" || cfg["at"] != "start" { t.Fatalf("the mesh's region is not the first thing in the operator's ~/.ssh/config: %v", cfg) } if body := cfg["content"].(string); !strings.Contains(body, "\nInclude ~/.ssh/config.d/*\n") || strings.Contains(body, "\nHost ") { t.Fatalf("the region does not just include config.d:\n%s", body) } var hosts map[string]any for _, r := range out { if r["path"] == "/home/jo/.ssh/config.d/00-mesh" { hosts = r } } if hosts == nil || hosts["type"] != "file" || hosts["into"] != nil { t.Fatalf("the mesh's hosts are not the whole of ~/.ssh/config.d/00-mesh: %v", hosts) } body := hosts["content"].(string) for _, want := range []string{"Host marge marge.internal", "Host bart bart.internal", "User jo", "User op"} { if !strings.Contains(body, want) { t.Fatalf("00-mesh does not say %q — every other node with its account:\n%s", want, body) } } if strings.Contains(body, "Host homer ") { t.Fatalf("00-mesh names the machine itself, not only its peers:\n%s", body) } }