feat(core): add naming, RCON, store, config, and image-build libraries
Foundational libraries: deterministic resource naming, the RCON client, the Postgres store with embedded SQL migrations, configuration loading, and container image-build helpers.
This commit is contained in:
18 files changed
+3475
No files matched your search
@@ -0,0 +1,96 @@
|
||||
// Package naming enforces the portability and admission rules (spec §2, §22):
|
||||
// a server name matches ^[a-z0-9-]{3,32}$ and is non-reserved, and every
|
||||
// hostname must be a single label under the configured root_domain. The root
|
||||
// domain is never hardcoded — it is always supplied by config — so this package
|
||||
// stays free of any deployment-specific domain.
|
||||
package naming
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
serverNameRE = regexp.MustCompile(`^[a-z0-9-]{3,32}$`)
|
||||
dnsLabelRE = regexp.MustCompile(`^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$`)
|
||||
)
|
||||
|
||||
// reserved subdomains/server names that users may not claim: proxy/lobby and
|
||||
// the platform's own faces.
|
||||
var reserved = map[string]struct{}{
|
||||
"lobby": {},
|
||||
"admin": {},
|
||||
"panel": {},
|
||||
"api": {},
|
||||
"felis": {},
|
||||
"velocity": {},
|
||||
"registry": {},
|
||||
"internal": {},
|
||||
"www": {},
|
||||
}
|
||||
|
||||
// ValidateServerName checks the §22 name rule and reservation list.
|
||||
func ValidateServerName(name string) error {
|
||||
if !serverNameRE.MatchString(name) {
|
||||
return fmt.Errorf("naming: invalid server name %q: must match ^[a-z0-9-]{3,32}$", name)
|
||||
}
|
||||
if strings.HasPrefix(name, "-") || strings.HasSuffix(name, "-") {
|
||||
return fmt.Errorf("naming: server name %q must not start or end with '-'", name)
|
||||
}
|
||||
if _, ok := reserved[name]; ok {
|
||||
return fmt.Errorf("naming: server name %q is reserved", name)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsReserved reports whether label is on the reserved list.
|
||||
func IsReserved(label string) bool {
|
||||
_, ok := reserved[label]
|
||||
return ok
|
||||
}
|
||||
|
||||
// worldVolumeName mirrors operator.dataVolumeName: the per-server StatefulSet's
|
||||
// volumeClaimTemplate is named "world", so a single-replica server's world PVC
|
||||
// is "world-<name>-0". This is the one naming convention shared by the operator
|
||||
// (which creates the PVC), the reaper (which deletes it), and restore (which
|
||||
// mounts it), so it lives here rather than being duplicated per subsystem.
|
||||
const worldVolumeName = "world"
|
||||
|
||||
// WorldPVCName returns the world PersistentVolumeClaim name for a server,
|
||||
// matching the operator's StatefulSet volumeClaimTemplate naming
|
||||
// ("world-<name>-0" for the sole replica).
|
||||
func WorldPVCName(server string) string {
|
||||
return worldVolumeName + "-" + server + "-0"
|
||||
}
|
||||
|
||||
// Hostname composes subdomain.rootDomain after validating the subdomain.
|
||||
func Hostname(subdomain, rootDomain string) (string, error) {
|
||||
if err := ValidateServerName(subdomain); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if rootDomain == "" {
|
||||
return "", fmt.Errorf("naming: root domain is empty")
|
||||
}
|
||||
return subdomain + "." + rootDomain, nil
|
||||
}
|
||||
|
||||
// ValidateHostname enforces the §2 invariant that host is a single label
|
||||
// directly under rootDomain.
|
||||
func ValidateHostname(host, rootDomain string) error {
|
||||
if rootDomain == "" {
|
||||
return fmt.Errorf("naming: root domain is empty")
|
||||
}
|
||||
suffix := "." + rootDomain
|
||||
if !strings.HasSuffix(host, suffix) {
|
||||
return fmt.Errorf("naming: hostname %q must be under %q", host, rootDomain)
|
||||
}
|
||||
label := strings.TrimSuffix(host, suffix)
|
||||
if label == "" || strings.Contains(label, ".") {
|
||||
return fmt.Errorf("naming: hostname %q must be a single label under %q", host, rootDomain)
|
||||
}
|
||||
if !dnsLabelRE.MatchString(label) {
|
||||
return fmt.Errorf("naming: invalid hostname label %q", label)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package naming_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"felis.lolicon.best/internal/naming"
|
||||
)
|
||||
|
||||
func TestValidateServerName(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
ok bool
|
||||
}{
|
||||
{"survival", true},
|
||||
{"creative-2", true},
|
||||
{"abc", true},
|
||||
{"a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6", true}, // 32 chars
|
||||
{"ab", false}, // too short
|
||||
{"a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q", false}, // 33 chars
|
||||
{"Survival", false}, // uppercase
|
||||
{"has_underscore", false}, // illegal char
|
||||
{"has space", false}, // illegal char
|
||||
{"-leading", false}, // leading hyphen
|
||||
{"trailing-", false}, // trailing hyphen
|
||||
{"lobby", false}, // reserved
|
||||
{"admin", false}, // reserved
|
||||
{"api", false}, // reserved
|
||||
}
|
||||
for _, c := range cases {
|
||||
err := naming.ValidateServerName(c.name)
|
||||
if c.ok && err != nil {
|
||||
t.Errorf("ValidateServerName(%q) = %v, want ok", c.name, err)
|
||||
}
|
||||
if !c.ok && err == nil {
|
||||
t.Errorf("ValidateServerName(%q) = nil, want error", c.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestWorldPVCName(t *testing.T) {
|
||||
cases := map[string]string{
|
||||
"survival": "world-survival-0",
|
||||
"creative-2": "world-creative-2-0",
|
||||
}
|
||||
for server, want := range cases {
|
||||
if got := naming.WorldPVCName(server); got != want {
|
||||
t.Errorf("WorldPVCName(%q) = %q, want %q", server, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHostname(t *testing.T) {
|
||||
const root = "mc.example.net"
|
||||
got, err := naming.Hostname("survival", root)
|
||||
if err != nil {
|
||||
t.Fatalf("Hostname: %v", err)
|
||||
}
|
||||
if got != "survival.mc.example.net" {
|
||||
t.Errorf("Hostname = %q, want survival.mc.example.net", got)
|
||||
}
|
||||
if _, err := naming.Hostname("lobby", root); err == nil {
|
||||
t.Error("Hostname should reject a reserved subdomain")
|
||||
}
|
||||
if _, err := naming.Hostname("survival", ""); err == nil {
|
||||
t.Error("Hostname should reject an empty root domain")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateHostname(t *testing.T) {
|
||||
const root = "mc.example.net"
|
||||
cases := []struct {
|
||||
host string
|
||||
ok bool
|
||||
}{
|
||||
{"survival.mc.example.net", true},
|
||||
{"a.mc.example.net", true},
|
||||
{"deep.sub.mc.example.net", false}, // not a single label under root
|
||||
{"survival.evil.example.org", false},
|
||||
{"mc.example.net", false}, // bare root, no label
|
||||
{".mc.example.net", false},
|
||||
{"-bad.mc.example.net", false},
|
||||
}
|
||||
for _, c := range cases {
|
||||
err := naming.ValidateHostname(c.host, root)
|
||||
if c.ok && err != nil {
|
||||
t.Errorf("ValidateHostname(%q) = %v, want ok", c.host, err)
|
||||
}
|
||||
if !c.ok && err == nil {
|
||||
t.Errorf("ValidateHostname(%q) = nil, want error", c.host)
|
||||
}
|
||||
}
|
||||
if err := naming.ValidateHostname("x.mc.example.net", ""); err == nil {
|
||||
t.Error("ValidateHostname should reject an empty root domain")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user