carlos / platform Public

Regional domains (#447) Task 6: carlos add refuses a regional host

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev b53764c454a3d37cdcf185951ba04776ced8c9b8 parent d1ec32c
2 files changed, +202 −0
  • cli.go +44 −0
  • cli_add_regional_test.go +158 −0
diff --git a/cli.go b/cli.go
index c11772cd3..9e80c6662 100644
--- a/cli.go
+++ b/cli.go
@@ -26,6 +26,7 @@ import (
"github.com/carlosframework/platform/internal/activator"
"github.com/carlosframework/platform/internal/appconfig"
"github.com/carlosframework/platform/internal/caps"
+ "github.com/carlosframework/platform/internal/domains"
"github.com/carlosframework/platform/internal/edge"
"github.com/carlosframework/platform/internal/edgesfile"
"github.com/carlosframework/platform/internal/host"
@@ -150,6 +151,37 @@ func addMintsNested(ctx context.Context, store release.Store, boxLabel func() st
return false, nil
}
+// addRegionalCheck is `carlos add`'s side of W12 (#447): a host a regional
+// claim holds cannot be a route. For host "X" the claim read is X's; for
+// "*.Y" it is Y's, and the wildcard is refused whether or not that claim has
+// Catchall.
+//
+// One claim object is read by key through the store the caller's
+// deploymentReadSeam returned; nothing is listed. skipped means the check
+// could not be made: no store, or a read that failed other than not-found
+// (not-found is an answer, "no claim"). The caller proceeds on skipped, since
+// this verb runs at provision time on boxes with no store and the edge's
+// Reload denies a regional key regardless.
+func addRegionalCheck(ctx context.Context, store release.Store, host string) (refused, skipped bool) {
+ if store == nil {
+ return false, true
+ }
+ c, err := domains.GetClaim(ctx, store, regionalClaimHost(host))
+ if errors.Is(err, os.ErrNotExist) {
+ return false, false
+ }
+ if err != nil {
+ return false, true
+ }
+ return c.Regional(), false
+}
+
+// regionalClaimHost is the host whose claim decides whether `carlos add
+// <host>` is allowed: the host itself, or the parent of a "*." wildcard.
+func regionalClaimHost(host string) string {
+ return strings.ToLower(strings.TrimPrefix(host, "*."))
+}
+
// boxLabelForAdd is the label this box's status publishes under, which is
// what a layout intent's scope names. A function value so runAdd's `host`
// string does not shadow the package.
@@ -180,6 +212,18 @@ func runAdd(args []string) {
if err := addRail(host, *kind); err != nil {
log.Fatalf("add %s: %v", host, err)
}
+ // W12 (#447): a host a regional claim holds is not a route here. One
+ // claim read through the same store seam the checks below use; when it
+ // cannot be read the verb says so and goes on (the edge's Reload denies a
+ // regional key whatever this row says).
+ regionalStore, _ := deploymentReadSeam()
+ refused, skipped := addRegionalCheck(context.Background(), regionalStore, host)
+ if refused {
+ log.Fatalf("%s belongs to a regional domain, so it cannot be a route here. detach the domain first.", host)
+ }
+ if skipped {
+ fmt.Fprintf(os.Stderr, "could not read the domain claim for %s, so the regional domain check was skipped.\n", regionalClaimHost(host))
+ }
if err := appRail(*app); err != nil {
log.Fatalf("add %s: %v", host, err)
}
diff --git a/cli_add_regional_test.go b/cli_add_regional_test.go
new file mode 100644
index 000000000..7517afbd2
--- /dev/null
+++ b/cli_add_regional_test.go
@@ -0,0 +1,158 @@
+package main
+
+import (
+ "context"
+ "os"
+ "os/exec"
+ "path/filepath"
+ "strings"
+ "testing"
+
+ "github.com/carlosframework/platform/internal/domains"
+ "github.com/carlosframework/platform/internal/registry"
+ "github.com/carlosframework/platform/internal/release"
+)
+
+// W12: a root `carlos add` of a host a regional claim holds is refused,
+// whether the host is the claim's own (X) or a wildcard under it (*.X). The
+// claim is read as ONE object through the deployment-store seam. When it
+// cannot be read (no store, or a read that fails other than not-found) the
+// verb prints the skip line and proceeds; the edge's Reload deny is the
+// backstop.
+func TestAddRefusesARegionalHost(t *testing.T) {
+ bin := buildCarlos(t)
+ storeDir := t.TempDir()
+ _, pubB64, err := release.GenerateKey()
+ if err != nil {
+ t.Fatal(err)
+ }
+ store := release.NewDirStore(storeDir)
+ const claimed = "shop.example.com"
+ if _, err := domains.SetClaimPlacements(context.Background(), store, domains.Claim{
+ Schema: 2, Host: claimed, Account: "bab", App: "shop",
+ Route: "shop-ie.example.net",
+ CreatedAt: "2026-10-29T00:00:00Z", CreatedBy: "owner@example.com",
+ Placements: []domains.Placement{
+ {Region: "", Host: "shop-ie.example.net"},
+ {Region: "uk", Host: "shop-uk.example.net"},
+ },
+ }, nil, ""); err != nil {
+ t.Fatal(err)
+ }
+ scratch := t.TempDir()
+ regPath := filepath.Join(t.TempDir(), "registry.db")
+ run := func(extraEnv []string, host string) (string, error) {
+ t.Helper()
+ cmd := exec.Command(bin, "add", "-registry", regPath, "-app", "shop", host)
+ cmd.Env = carlosTestEnv(t, extraEnv...)
+ // pick() walks cwd ancestors for .carlos/config, so the subprocess must not run from the real checkout.
+ cmd.Dir = scratch
+ out, err := cmd.CombinedOutput()
+ return string(out), err
+ }
+ withStore := []string{"CARLOS_DEPLOYMENT_DIR=" + storeDir, "CARLOS_RELEASE_PUBKEY=" + pubB64}
+ const refusal = "belongs to a regional domain, so it cannot be a route here. detach the domain first."
+ const skipLine = "could not read the domain claim for " + claimed + ", so the regional domain check was skipped."
+
+ reg, err := registry.Open(regPath)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer reg.Close()
+ has := func(h string) bool {
+ t.Helper()
+ got, err := reg.Get(h)
+ if err != nil {
+ t.Fatal(err)
+ }
+ return got != nil
+ }
+
+ for _, h := range []string{claimed, "*." + claimed} {
+ out, err := run(withStore, h)
+ if err == nil {
+ t.Fatalf("add %s under a regional claim: want non-zero exit, got 0\n%s", h, out)
+ }
+ if !strings.Contains(out, h+" "+refusal) {
+ t.Errorf("add %s: not the regional refusal:\n%s", h, out)
+ }
+ if strings.Contains(out, skipLine) {
+ t.Errorf("add %s: printed the skip line though the claim was read:\n%s", h, out)
+ }
+ if has(h) {
+ t.Errorf("add %s: refused, yet the registry has a row", h)
+ }
+ }
+
+ // Neighbours the claim does not hold: a sub-host (no wildcard claim of its
+ // own to read) and a wildcard over a parent with no claim.
+ for _, h := range []string{"alice." + claimed, "*.example.com"} {
+ out, err := run(withStore, h)
+ if err != nil {
+ t.Fatalf("add %s: %v\n%s", h, err, out)
+ }
+ if strings.Contains(out, refusal) || strings.Contains(out, "could not read the domain claim") {
+ t.Errorf("add %s: printed a regional line:\n%s", h, out)
+ }
+ if !has(h) {
+ t.Errorf("add %s: no registry row", h)
+ }
+ }
+
+ // Cannot read the claim: no store at all.
+ out, err := run(nil, claimed)
+ if err != nil {
+ t.Fatalf("add %s with no store: %v\n%s", claimed, err, out)
+ }
+ if !strings.Contains(out, skipLine) {
+ t.Errorf("add %s with no store: no skip line:\n%s", claimed, out)
+ }
+ if !has(claimed) {
+ t.Errorf("add %s with no store: no registry row", claimed)
+ }
+ if err := reg.Delete(claimed); err != nil {
+ t.Fatal(err)
+ }
+
+ // Cannot read the claim: the store is there and the object is not readable.
+ claimFile := filepath.Join(storeDir, domains.ClaimKey(claimed))
+ if os.Geteuid() == 0 {
+ t.Log("root reads past chmod 000; the unreadable-object case uses a malformed claim instead")
+ if err := os.WriteFile(claimFile, []byte("not json"), 0o600); err != nil {
+ t.Fatal(err)
+ }
+ } else if err := os.Chmod(claimFile, 0); err != nil {
+ t.Fatal(err)
+ }
+ out, err = run(withStore, claimed)
+ if err != nil {
+ t.Fatalf("add %s over an unreadable claim: %v\n%s", claimed, err, out)
+ }
+ if !strings.Contains(out, skipLine) {
+ t.Errorf("add %s over an unreadable claim: no skip line:\n%s", claimed, out)
+ }
+ if !has(claimed) {
+ t.Errorf("add %s over an unreadable claim: no registry row", claimed)
+ }
+}
+
+// A claim that is not regional (a plain alias claim) does not stop `carlos
+// add`: only a regional one does.
+func TestAddRegionalCheckPassesAPlainClaim(t *testing.T) {
+ store := release.NewDirStore(t.TempDir())
+ ctx := context.Background()
+ if _, _, err := domains.AttachClaim(ctx, store, domains.Claim{
+ Schema: 1, Host: "shop.example.com", Account: "bab", App: "shop",
+ Route: "shop-ie.example.net", CreatedAt: "2026-10-29T00:00:00Z",
+ }); err != nil {
+ t.Fatal(err)
+ }
+ for _, h := range []string{"shop.example.com", "*.shop.example.com"} {
+ if refused, skipped := addRegionalCheck(ctx, store, h); refused || skipped {
+ t.Errorf("addRegionalCheck(%s) over a plain claim = refused %v, skipped %v, want neither", h, refused, skipped)
+ }
+ }
+ if refused, skipped := addRegionalCheck(ctx, nil, "shop.example.com"); refused || !skipped {
+ t.Errorf("addRegionalCheck with no store = refused %v, skipped %v, want skipped only", refused, skipped)
+ }
+}