package activator import ( "errors" "os" "path/filepath" "strings" "testing" "time" "golang.org/x/sys/unix" "github.com/carlosframework/platform/internal/instancectl" "github.com/carlosframework/platform/internal/provision" "github.com/carlosframework/platform/internal/registry" ) // The nested prepare mints the key into the instance directory's own env // file and hands it to the role in the same env it returns: the run role // and the seed role (coverage, Astra round 1). func TestPrepareMintsTheInstanceKeyIntoTheNestedEnvFile(t *testing.T) { for _, role := range []instancectl.Role{instancectl.RoleRun, instancectl.RoleSeed} { t.Run(string(role), func(t *testing.T) { b, rt := nestedBackend(t, &fakeControl{inv: "inv1"}, &fakeUnit{}) _, ctl, logs, env, err := b.prepare(rt, role) if err != nil { t.Fatalf("prepare: %v", err) } logs.Close() if ctl != nil { ctl.Close() } own, err := readEnvFileBeneath(provision.InstanceDir(b.DataDir, rt.Host), rt.Host+".env") if err != nil { t.Fatal(err) } if !wellFormedInstanceKey(own[InstanceKeyVar]) || env[InstanceKeyVar] != own[InstanceKeyVar] { t.Fatal("prepare's env does not carry the key minted into the nested env file") } }) } } // Finding 3: the nested backfill opens an existing file with mode 0 // (openat2 refuses a mode without O_CREAT), appends, and sets 0600 on the // descriptor. func TestBackfillInstanceKeyAppendsToANestedEnvFile(t *testing.T) { b, rt := nestedBackend(t, &fakeControl{inv: "inv1"}, &fakeUnit{}) dir := provision.InstanceDir(b.DataDir, rt.Host) if err := os.MkdirAll(dir, 0o700); err != nil { t.Fatal(err) } envPath := filepath.Join(dir, rt.Host+".env") tok := "CARLOS_ADMIN_TOKEN=" + strings.Repeat("e", 64) + "\n" if err := os.WriteFile(envPath, []byte(tok), 0o644); err != nil { t.Fatal(err) } if got, err := b.BackfillInstanceKey(t.Context(), rt); err != nil || got != InstanceKeyMinted { t.Fatalf("nested backfill = %v, %v; want minted", got, err) } raw, _ := os.ReadFile(envPath) if !strings.HasPrefix(string(raw), tok) || len(keyLines(raw)) != 1 || !wellFormedInstanceKey(keyLines(raw)[0]) { t.Fatalf("nested env file after the backfill: %d bytes, %d keys", len(raw), len(keyLines(raw))) } if st, _ := os.Stat(envPath); st.Mode().Perm() != 0o600 { t.Fatalf("mode = %o, want 600", st.Mode().Perm()) } if got, err := b.BackfillInstanceKey(t.Context(), rt); err != nil || got != InstanceKeyPresent { t.Fatalf("second nested backfill = %v, %v; want present", got, err) } if raw2, _ := os.ReadFile(envPath); string(raw2) != string(raw) { t.Fatal("the second backfill changed the file") } } // A nested own env file is on tenant-writable ground: a symlink planted at // its name is refused, and the file it points at is not touched. func TestEnsureInstanceKeyBeneathRefusesASymlink(t *testing.T) { dir := t.TempDir() target := filepath.Join(t.TempDir(), "elsewhere.env") if err := os.WriteFile(target, []byte("X=1\n"), 0o644); err != nil { t.Fatal(err) } if err := os.Symlink(target, filepath.Join(dir, "shop.example.com.env")); err != nil { t.Fatal(err) } if _, err := ensureInstanceKeyBeneathNoConfig(dir, "shop.example.com.env"); err == nil { t.Fatal("ensure followed a symlink planted at the env file's name") } if raw, _ := os.ReadFile(target); string(raw) != "X=1\n" { t.Fatalf("the symlink's target changed: %q", raw) } } // Review focus 4 and the final review's I3: a FIFO planted at an env // file's name fails the backfill fast and is refused; it never blocks // agent start. The nested row's files are opened through instancefs; the // flat row's own file and its rendered configuration through // openOwnEnvNoFollow, which opens O_NONBLOCK and accepts only a regular // file. The startup backfill runs under adminTokenMu before the activator // starts, so a blocking open here would keep every instance on the box // from waking. func TestBackfillInstanceKeyRefusesAFIFOWithoutBlocking(t *testing.T) { for _, tc := range []struct { name string flat bool file string // the file the FIFO is planted at, relative to the row's directory }{ {"nested own file", false, ".env"}, {"flat own file", true, ".env"}, {"flat configuration", true, ".config.env"}, } { t.Run(tc.name, func(t *testing.T) { var b *ExecBackend var rt registry.Route var dir string if tc.flat { b = testExecBackend(t) rt = registry.Route{Host: "shop.example.com", Kind: "instance"} dir = b.DataDir // A flat own file exists, so the backfill reaches the // configuration read when the FIFO is the configuration. if tc.file != ".env" { if err := os.WriteFile(filepath.Join(dir, rt.Host+".env"), []byte("A=1\n"), 0o600); err != nil { t.Fatal(err) } } } else { b, rt = nestedBackend(t, &fakeControl{inv: "inv1"}, &fakeUnit{}) dir = provision.InstanceDir(b.DataDir, rt.Host) if err := os.MkdirAll(dir, 0o700); err != nil { t.Fatal(err) } } if err := unix.Mkfifo(filepath.Join(dir, rt.Host+tc.file), 0o600); err != nil { t.Fatal(err) } done := make(chan error, 1) go func() { _, err := b.BackfillInstanceKey(t.Context(), rt) done <- err }() select { case err := <-done: if err == nil { t.Fatal("backfill over a FIFO succeeded, want an error") } if tc.flat && !errors.Is(err, errOwnEnvNotRegular) { t.Fatalf("backfill over a flat FIFO = %v, want errOwnEnvNotRegular", err) } case <-time.After(2 * time.Second): t.Fatal("backfill blocked on a FIFO") } if tc.file == ".env" { return // the own file is the FIFO: nothing to read back } if raw, err := os.ReadFile(filepath.Join(dir, rt.Host+".env")); err != nil || string(raw) != "A=1\n" { t.Fatalf("the own file changed beside a refused configuration: %q, %v", raw, err) } }) } }