fix(instance): never serve a container-id hostname as the instance name
os.Hostname() inside the Maestro container is the bare container id (e.g. 6079761d6e4d) — a hex-only hostname now falls back to 'maestro' so the public profile stays meaningful out of the box. DCNTRLIZED_INSTANCE_NAME and instance.json still win.
This commit is contained in:
@@ -83,12 +83,31 @@ func Save(dataRoot string, id Identity) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// osHostname is swapped in tests.
|
||||||
|
var osHostname = os.Hostname
|
||||||
|
|
||||||
func defaultIdentity() Identity {
|
func defaultIdentity() Identity {
|
||||||
name := os.Getenv("DCNTRLIZED_INSTANCE_NAME")
|
if name := os.Getenv("DCNTRLIZED_INSTANCE_NAME"); name != "" {
|
||||||
if name == "" {
|
|
||||||
if host, err := os.Hostname(); err == nil {
|
|
||||||
name = host
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return Identity{Name: name}
|
return Identity{Name: name}
|
||||||
|
}
|
||||||
|
// Maestro normally runs containerized, where os.Hostname() is the bare
|
||||||
|
// container id — useless (and mildly leaking) as a public display name.
|
||||||
|
if host, err := osHostname(); err == nil && !looksLikeContainerID(host) {
|
||||||
|
return Identity{Name: host}
|
||||||
|
}
|
||||||
|
return Identity{Name: "maestro"}
|
||||||
|
}
|
||||||
|
|
||||||
|
// looksLikeContainerID reports whether s is a hex-only Docker-style container
|
||||||
|
// id or short id.
|
||||||
|
func looksLikeContainerID(s string) bool {
|
||||||
|
if len(s) < 6 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, r := range s {
|
||||||
|
if !(r >= '0' && r <= '9' || r >= 'a' && r <= 'f' || r >= 'A' && r <= 'F') {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,91 @@
|
|||||||
|
// Copyright (C) 2026 Dcntrlized
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Affero General Public License as published
|
||||||
|
// by the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Affero General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Affero General Public License
|
||||||
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package instance
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestLoadPrefersFileOverEnv(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
if err := Save(dir, Identity{Name: "home-node", Title: "Home"}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Setenv("DCNTRLIZED_INSTANCE_NAME", "from-env")
|
||||||
|
|
||||||
|
id := Load(dir)
|
||||||
|
if id.Name != "home-node" || id.Title != "Home" {
|
||||||
|
t.Fatalf("got %+v", id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDefaultIdentityFallbacks(t *testing.T) {
|
||||||
|
orig := osHostname
|
||||||
|
defer func() { osHostname = orig }()
|
||||||
|
|
||||||
|
t.Run("env wins", func(t *testing.T) {
|
||||||
|
t.Setenv("DCNTRLIZED_INSTANCE_NAME", "edge-1")
|
||||||
|
if id := defaultIdentity(); id.Name != "edge-1" {
|
||||||
|
t.Fatalf("got %q", id.Name)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("hostname used when human-readable", func(t *testing.T) {
|
||||||
|
os.Unsetenv("DCNTRLIZED_INSTANCE_NAME")
|
||||||
|
osHostname = func() (string, error) { return "archlinux", nil }
|
||||||
|
if id := defaultIdentity(); id.Name != "archlinux" {
|
||||||
|
t.Fatalf("got %q", id.Name)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("container-id hostname replaced", func(t *testing.T) {
|
||||||
|
os.Unsetenv("DCNTRLIZED_INSTANCE_NAME")
|
||||||
|
for _, host := range []string{"6079761d6e4d", "6079761d6e4dabcdef1234567890abcdef1234567890abcdef1234567890abcd"} {
|
||||||
|
osHostname = func() (string, error) { return host, nil }
|
||||||
|
if id := defaultIdentity(); id.Name != "maestro" {
|
||||||
|
t.Fatalf("host %q: got %q", host, id.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLooksLikeContainerID(t *testing.T) {
|
||||||
|
cases := map[string]bool{
|
||||||
|
"6079761d6e4d": true,
|
||||||
|
"ABCDEF0123456789abcdef0123456789": true,
|
||||||
|
"archlinux": false,
|
||||||
|
"maestro-1": false,
|
||||||
|
"abc": false,
|
||||||
|
"": false,
|
||||||
|
}
|
||||||
|
for s, want := range cases {
|
||||||
|
if got := looksLikeContainerID(s); got != want {
|
||||||
|
t.Errorf("looksLikeContainerID(%q) = %v, want %v", s, got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSaveRejectsEmptyName(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
if err := Save(dir, Identity{}); err == nil {
|
||||||
|
t.Fatal("expected error for empty name")
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(filepath.Join(dir, "instance.json")); !os.IsNotExist(err) {
|
||||||
|
t.Fatal("no file should be written for an invalid identity")
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user