carlos / platform Public

namedunits: one strict gate for the declaration file

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 0f4da5331c37acaca91e2126dc9cf3e9c535a218 parent bdf68e1
3 files changed, +248 −21
  • internal/host/proofnamed.go +5 −21
  • internal/namedunits/namedunits.go +114 −0
  • internal/namedunits/namedunits_test.go +129 −0
diff --git a/internal/host/proofnamed.go b/internal/host/proofnamed.go
index e75789ca7..afe94c626 100644
--- a/internal/host/proofnamed.go
+++ b/internal/host/proofnamed.go
@@ -1,7 +1,6 @@
package host
import (
- "bytes"
"context"
"crypto/sha256"
"encoding/hex"
@@ -12,8 +11,8 @@ import (
"path/filepath"
"strings"
- "github.com/carlosframework/platform/internal/controlname"
"github.com/carlosframework/platform/internal/hostfloor"
+ "github.com/carlosframework/platform/internal/namedunits"
"github.com/carlosframework/platform/internal/provision"
"github.com/carlosframework/platform/internal/release"
"github.com/carlosframework/platform/internal/rootstate"
@@ -21,14 +20,7 @@ import (
const rootNamedUnits = "/etc/carlos/named-units.json"
-type namedUnit struct {
- Host string `json:"host"`
- Account string `json:"account,omitempty"`
- App string `json:"app"`
- Unit string `json:"unit"`
- Addr string `json:"addr"`
- Health *provision.Health `json:"health"`
-}
+type namedUnit = namedunits.Entry
type proofRecord struct {
record *provision.Record
@@ -99,19 +91,11 @@ func (s *proofBindingSource) record(ctx context.Context, host string) (proofReco
}
raw, err := add(s.namedPath)
if err == nil {
- var list struct {
- Schema int `json:"schema"`
- Units []namedUnit `json:"units"`
- }
- dec := json.NewDecoder(bytes.NewReader(raw))
- dec.DisallowUnknownFields()
- if dec.Decode(&list) != nil || dec.Decode(new(any)) != io.EOF || list.Schema != 1 {
+ units, err := namedunits.Decode(raw)
+ if err != nil {
return proofRecord{}, os.ErrPermission
}
- for _, u := range list.Units {
- if !controlname.ValidHostShape(u.Host) || !release.ValidApp(u.App) || (u.Account != "" && !release.ValidAccount(u.Account)) {
- return proofRecord{}, os.ErrPermission
- }
+ for _, u := range units {
if u.Host == host {
if selected != nil {
return proofRecord{}, os.ErrPermission
diff --git a/internal/namedunits/namedunits.go b/internal/namedunits/namedunits.go
new file mode 100644
index 000000000..6c4b2990c
--- /dev/null
+++ b/internal/namedunits/namedunits.go
@@ -0,0 +1,114 @@
+// Package namedunits is the one reading of /etc/carlos/named-units.json that
+// both of its consumers agree on: the release path (internal/host) and config
+// delivery (internal/appconfig). ValidateStrict accepts a file only if both
+// would.
+package namedunits
+
+import (
+ "bytes"
+ "encoding/json"
+ "errors"
+ "fmt"
+ "io"
+ "strings"
+
+ "github.com/carlosframework/platform/internal/controlname"
+ "github.com/carlosframework/platform/internal/provision"
+ "github.com/carlosframework/platform/internal/registry"
+ "github.com/carlosframework/platform/internal/release"
+)
+
+// MaxBytes is the largest declaration file ValidateStrict accepts.
+const MaxBytes = 4096
+
+// Entry is one named-unit declaration.
+type Entry struct {
+ Host string `json:"host"`
+ Account string `json:"account,omitempty"`
+ App string `json:"app"`
+ Unit string `json:"unit"`
+ Addr string `json:"addr"`
+ Health *provision.Health `json:"health"`
+}
+
+// Decode is the release path's reading: DisallowUnknownFields, one JSON
+// document then EOF, schema 1, every entry's host/app/account valid.
+func Decode(raw []byte) ([]Entry, error) {
+ var list struct {
+ Schema int `json:"schema"`
+ Units []Entry `json:"units"`
+ }
+ dec := json.NewDecoder(bytes.NewReader(raw))
+ dec.DisallowUnknownFields()
+ if err := dec.Decode(&list); err != nil {
+ return nil, fmt.Errorf("named units: %w", err)
+ }
+ if dec.Decode(new(any)) != io.EOF {
+ return nil, errors.New("named units: more than one JSON document")
+ }
+ if list.Schema != 1 {
+ return nil, errors.New("named units: schema must be 1")
+ }
+ for _, u := range list.Units {
+ if !controlname.ValidHostShape(u.Host) {
+ return nil, fmt.Errorf("named units: host %q is not a valid host name", u.Host)
+ }
+ if !release.ValidApp(u.App) {
+ return nil, fmt.Errorf("named units: host %s: app %q is not valid", u.Host, u.App)
+ }
+ if u.Account != "" && !release.ValidAccount(u.Account) {
+ return nil, fmt.Errorf("named units: host %s: account %q is not valid", u.Host, u.Account)
+ }
+ }
+ return list.Units, nil
+}
+
+// ValidateStrict is Decode plus, for every entry: the file is at most
+// MaxBytes, the unit is non-empty, valid and not a carlos-app@ unit, addr is
+// set, health.path is set, and no host or unit appears twice.
+func ValidateStrict(raw []byte) error {
+ if len(raw) > MaxBytes {
+ return fmt.Errorf("named units: file is %d bytes, the limit is %d", len(raw), MaxBytes)
+ }
+ entries, err := Decode(raw)
+ if err != nil {
+ return err
+ }
+ hosts := map[string]bool{}
+ units := map[string]bool{}
+ for _, u := range entries {
+ if u.Unit == "" {
+ return fmt.Errorf("named units: host %s: unit is required", u.Host)
+ }
+ if !registry.ValidUnitName(u.Unit) {
+ return fmt.Errorf("named units: host %s: unit %q is not a valid unit name", u.Host, u.Unit)
+ }
+ if strings.HasPrefix(u.Unit, "carlos-app@") {
+ return fmt.Errorf("named units: host %s: unit %q is a carlos-app@ unit, which cannot be declared", u.Host, u.Unit)
+ }
+ if u.Addr == "" {
+ return fmt.Errorf("named units: host %s: addr is required", u.Host)
+ }
+ if u.Health == nil || u.Health.Path == "" {
+ return fmt.Errorf("named units: host %s: health.path is required", u.Host)
+ }
+ if hosts[u.Host] {
+ return fmt.Errorf("named units: host %s is declared twice", u.Host)
+ }
+ hosts[u.Host] = true
+ if units[u.Unit] {
+ return fmt.Errorf("named units: unit %s is declared twice", u.Unit)
+ }
+ units[u.Unit] = true
+ }
+ return nil
+}
+
+// Hosts returns the hosts a decoded file names, in file order.
+func Hosts(entries []Entry) []string {
+ out := make([]string, 0, len(entries))
+ for _, u := range entries {
+ out = append(out, u.Host)
+ }
+ return out
+}
diff --git a/internal/namedunits/namedunits_test.go b/internal/namedunits/namedunits_test.go
new file mode 100644
index 000000000..b3312192c
--- /dev/null
+++ b/internal/namedunits/namedunits_test.go
@@ -0,0 +1,129 @@
+package namedunits
+
+import (
+ "strings"
+ "testing"
+)
+
+const entryA = `{"host":"a.example.com","app":"shop","unit":"shop.service","addr":"127.0.0.1:1","health":{"path":"/"}}`
+const entryB = `{"host":"b.example.com","app":"shop","unit":"shop-b.service","addr":"127.0.0.1:2","health":{"path":"/"}}`
+
+func file(entries ...string) string {
+ return `{"schema":1,"units":[` + strings.Join(entries, ",") + `]}`
+}
+
+func TestDecodeRefusesUnknownField(t *testing.T) {
+ raw := file(strings.Replace(entryA, `"health":{"path":"/"}`, `"health":{"path":"/"},"extra":1`, 1))
+ if _, err := Decode([]byte(raw)); err == nil {
+ t.Fatal("unknown field accepted")
+ }
+ raw = file(strings.Replace(entryA, `{"path":"/"}`, `{"path":"/","bogus":1}`, 1))
+ if _, err := Decode([]byte(raw)); err == nil {
+ t.Fatal("unknown health field accepted")
+ }
+}
+
+func TestDecodeRefusesTrailingDocument(t *testing.T) {
+ if _, err := Decode([]byte(file(entryA) + `{}`)); err == nil {
+ t.Fatal("trailing document accepted")
+ }
+}
+
+func TestDecodeRefusesSchema2(t *testing.T) {
+ raw := strings.Replace(file(entryA), `"schema":1`, `"schema":2`, 1)
+ if _, err := Decode([]byte(raw)); err == nil {
+ t.Fatal("schema 2 accepted")
+ }
+}
+
+func TestDecodeRefusesBadHostAppAccount(t *testing.T) {
+ for _, bad := range []string{
+ strings.Replace(entryA, "a.example.com", "A_B", 1),
+ strings.Replace(entryA, `"app":"shop"`, `"app":"../x"`, 1),
+ strings.Replace(entryA, `"app":"shop"`, `"app":"shop","account":"../x"`, 1),
+ } {
+ if _, err := Decode([]byte(file(bad))); err == nil {
+ t.Fatalf("accepted %s", bad)
+ }
+ }
+}
+
+func TestValidateStrictRequiresAddrAndHealthOnEveryEntry(t *testing.T) {
+ noAddr := strings.Replace(entryB, `"addr":"127.0.0.1:2",`, "", 1)
+ err := ValidateStrict([]byte(file(entryA, noAddr)))
+ if err == nil || !strings.Contains(err.Error(), "b.example.com") {
+ t.Fatalf("want error naming b.example.com, got %v", err)
+ }
+ noPath := strings.Replace(entryB, `"health":{"path":"/"}`, `"health":{}`, 1)
+ err = ValidateStrict([]byte(file(entryA, noPath)))
+ if err == nil || !strings.Contains(err.Error(), "b.example.com") {
+ t.Fatalf("want error naming b.example.com, got %v", err)
+ }
+ noHealth := strings.Replace(entryB, `,"health":{"path":"/"}`, "", 1)
+ if err := ValidateStrict([]byte(file(entryA, noHealth))); err == nil {
+ t.Fatal("missing health accepted")
+ }
+}
+
+func TestValidateStrictRefusesEmptyUnit(t *testing.T) {
+ if err := ValidateStrict([]byte(file(strings.Replace(entryA, "shop.service", "", 1)))); err == nil {
+ t.Fatal("empty unit accepted")
+ }
+}
+
+func TestValidateStrictRefusesCarlosAppUnit(t *testing.T) {
+ if err := ValidateStrict([]byte(file(strings.Replace(entryA, "shop.service", "carlos-app@x.service", 1)))); err == nil {
+ t.Fatal("carlos-app unit accepted")
+ }
+}
+
+func TestValidateStrictRefusesUnitTwice(t *testing.T) {
+ dup := strings.Replace(entryB, "shop-b.service", "shop.service", 1)
+ if err := ValidateStrict([]byte(file(entryA, dup))); err == nil {
+ t.Fatal("duplicate unit accepted")
+ }
+}
+
+func TestValidateStrictRefusesHostTwice(t *testing.T) {
+ dup := strings.Replace(entryB, "b.example.com", "a.example.com", 1)
+ if err := ValidateStrict([]byte(file(entryA, dup))); err == nil {
+ t.Fatal("duplicate host accepted")
+ }
+}
+
+func TestValidateStrictRefusesOverCap(t *testing.T) {
+ raw := file(entryA)
+ raw += strings.Repeat(" ", MaxBytes+1-len(raw))
+ if len(raw) != 4097 {
+ t.Fatalf("len %d", len(raw))
+ }
+ if err := ValidateStrict([]byte(raw)); err == nil {
+ t.Fatal("over-cap file accepted")
+ }
+ if err := ValidateStrict([]byte(raw[:MaxBytes])); err != nil {
+ t.Fatalf("at-cap file refused: %v", err)
+ }
+}
+
+func TestValidateStrictAcceptsTrailingWhitespace(t *testing.T) {
+ if err := ValidateStrict([]byte(file(entryA, entryB) + "\r\n")); err != nil {
+ t.Fatal(err)
+ }
+}
+
+func TestValidateStrictAcceptsEmptyUnits(t *testing.T) {
+ if err := ValidateStrict([]byte(`{"schema":1,"units":[]}`)); err != nil {
+ t.Fatal(err)
+ }
+}
+
+func TestHosts(t *testing.T) {
+ es, err := Decode([]byte(file(entryA, entryB)))
+ if err != nil {
+ t.Fatal(err)
+ }
+ h := Hosts(es)
+ if len(h) != 2 || h[0] != "a.example.com" || h[1] != "b.example.com" {
+ t.Fatalf("hosts %v", h)
+ }
+}