1package app
2
3import (
4 "errors"
5 "fmt"
6 "os"
7 "path/filepath"
8
9 "github.com/bnema/vev/internal/adapters/brokerconfig"
10 "github.com/bnema/vev/internal/adapters/ipc"
11 "github.com/bnema/vev/internal/domain"
12 "github.com/bnema/vev/internal/platform"
13 "github.com/bnema/vev/internal/ports"
14 "github.com/bnema/vev/internal/protocol"
15 "github.com/bnema/vev/internal/protocol/catalogue"
16 "github.com/bnema/vev/internal/usecase/broker"
17 "github.com/bnema/vev/pkg/safedir"
18)
19
20// productionBrokerLayout is the single production broker filesystem contract.
21// Runtime ownership belongs to broker.Supervisor; state persists independently;
22// configuration remains under the user's vev configuration directory.
23func productionBrokerConfigPath() string {
24 return filepath.Join(filepath.Dir(platform.ConfigPath()), "broker.json")
25}
26
27// ensureProductionBrokerConfig creates the empty production bootstrap on first
28// use. Existing files are never rewritten: malformed or insecure operator
29// configuration must still fail closed in brokerconfig.LoadProduction.
30func ensureProductionBrokerConfig() error {
31 path := productionBrokerConfigPath()
32 if err := safedir.EnsurePrivate(filepath.Dir(path)); err != nil {
33 return fmt.Errorf("vev: secure broker configuration directory: %w", err)
34 }
35 file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600)
36 if errors.Is(err, os.ErrExist) {
37 return nil
38 }
39 if err != nil {
40 return fmt.Errorf("vev: create broker configuration: %w", err)
41 }
42 _, writeErr := file.WriteString(`{"marker":"vev.broker.offline/v1","registrations":[]}`)
43 closeErr := file.Close()
44 if writeErr == nil && closeErr == nil {
45 return nil
46 }
47 removeErr := os.Remove(path)
48 if writeErr != nil {
49 return errors.Join(fmt.Errorf("vev: write broker configuration: %w", writeErr), closeErr, removeErr)
50 }
51 return errors.Join(fmt.Errorf("vev: close broker configuration: %w", closeErr), removeErr)
52}
53
54func remoteBrokerPolicy(transport string) ports.BrokerPolicy {
55 if transport == "stdio" {
56 transport = "ssh-stdio"
57 } else {
58 transport = "ssh-quic"
59 }
60 return ports.BrokerPolicy{
61 ProtocolVersion: protocol.Version, CatalogSchemaVersion: catalogue.RemoteCatalogSchemaVersion,
62 EnvironmentPolicy: protocol.EnvironmentPolicyDaemonOwned, Transport: transport,
63 Trust: "openssh-config-v1", Launch: broker.DaemonLaunchAuthority, Isolation: "per-user",
64 }
65}
66
67// upgradeBrokerHostVersions moves durable host policies to this build's
68// protocol and catalogue versions. Those fields describe the running binary,
69// not a user choice, so hosts pinned by an older build must follow an upgrade;
70// otherwise the unchanged daemon identity is refused as a conflicting policy.
71func upgradeBrokerHostVersions(store ports.BrokerHostStore) error {
72 hosts, err := store.LoadHosts()
73 if err != nil {
74 return fmt.Errorf("vev: load broker hosts: %w", err)
75 }
76 changed := false
77 records := ports.CloneBrokerHostRecords(hosts.Hosts)
78 for i := range records {
79 policy := &records[i].Policy
80 if policy.ProtocolVersion == protocol.Version && policy.CatalogSchemaVersion == catalogue.RemoteCatalogSchemaVersion {
81 continue
82 }
83 // A policy change advances the generation so stale observations are fenced.
84 if records[i].Registration.Generation == ^domain.RemoteGeneration(0) {
85 return ports.ErrBrokerRevisionExhausted
86 }
87 records[i].Registration.Generation++
88 policy.ProtocolVersion = protocol.Version
89 policy.CatalogSchemaVersion = catalogue.RemoteCatalogSchemaVersion
90 changed = true
91 }
92 if !changed {
93 return nil
94 }
95 if err := store.ReplaceHosts(hosts.Revision, records); err != nil {
96 return fmt.Errorf("vev: upgrade broker host versions: %w", err)
97 }
98 return nil
99}
100
101func localDaemonPolicy() ports.BrokerPolicy {
102 return ports.BrokerPolicy{
103 ProtocolVersion: protocol.Version, CatalogSchemaVersion: catalogue.RemoteCatalogSchemaVersion,
104 EnvironmentPolicy: protocol.EnvironmentPolicyClientOwned, Transport: "unix-mux",
105 Trust: "same-user", Launch: broker.DaemonLaunchAuthority, Isolation: "per-user",
106 }
107}
108
109func productionBrokerLayout() brokerconfig.Layout {
110 root := filepath.Join(platform.StateDir(), "broker")
111 runtime := filepath.Join(ipc.SocketDir(), "broker")
112 return brokerconfig.Layout{
113 Root: root,
114 Runtime: runtime,
115 State: filepath.Join(root, brokerconfig.StateDirName),
116 Log: filepath.Join(root, brokerconfig.LogDirName),
117 Spawn: filepath.Join(runtime, brokerconfig.SpawnDirName),
118 }
119}