BlueGit

timfa bluegit

Files

bluegit / internal / config / settings.go

settings.go

settings.go 7.9 KiB Download Raw
1package config
2
3import (
4 "fmt"
5 "os"
6 "path/filepath"
7 "strings"
8
9 "gopkg.in/yaml.v3"
10)
11
12// Persisted is het deel van de machineconfiguratie dat in settings.yml
13// staat. Pointers onderscheiden "niet gezet" (terugval op config.yml) van
14// een expliciete waarde, inclusief een lege lijst of false.
15// state_path hoort hier niet in: die bepaalt waar dit bestand ligt.
16type Persisted struct {
17 BaseURL *string `yaml:"base_url,omitempty"`
18 Listen *string `yaml:"listen,omitempty"`
19 TrustedProxies *[]string `yaml:"trusted_proxies,omitempty"`
20 Auth *PersistedAuth `yaml:"auth,omitempty"`
21 Git *PersistedGit `yaml:"git,omitempty"`
22 SSH *PersistedSSH `yaml:"ssh,omitempty"`
23 Runner *PersistedRunner `yaml:"runner,omitempty"`
24 CI *PersistedCI `yaml:"ci,omitempty"`
25}
26
27// PersistedAuth is het auth-blok in settings.yml.
28type PersistedAuth struct {
29 Mode *string `yaml:"mode,omitempty"`
30 BaseURL *string `yaml:"base_url,omitempty"`
31 ClientID *string `yaml:"client_id,omitempty"`
32 ClientSecret *string `yaml:"client_secret,omitempty"`
33 ClientSecretFile *string `yaml:"client_secret_file,omitempty"`
34 RedirectPath *string `yaml:"redirect_path,omitempty"`
35 UserAgent *string `yaml:"user_agent,omitempty"`
36 DevUser *string `yaml:"dev_user,omitempty"`
37 DevAuthID *int64 `yaml:"dev_auth_id,omitempty"`
38 TenantID *string `yaml:"tenant_id,omitempty"`
39 Issuer *string `yaml:"issuer,omitempty"`
40}
41
42// PersistedGit is het git-blok in settings.yml.
43type PersistedGit struct {
44 Binary *string `yaml:"binary,omitempty"`
45 DefaultBranch *string `yaml:"default_branch,omitempty"`
46 HookBinary *string `yaml:"hook_binary,omitempty"`
47 MaintenanceInterval *string `yaml:"maintenance_interval,omitempty"`
48}
49
50// PersistedSSH is het ssh-blok in settings.yml.
51type PersistedSSH struct {
52 Enabled *bool `yaml:"enabled,omitempty"`
53 Listen *string `yaml:"listen,omitempty"`
54 PublicHost *string `yaml:"public_host,omitempty"`
55 PublicPort *int `yaml:"public_port,omitempty"`
56}
57
58// PersistedRunner is het runner-blok in settings.yml.
59type PersistedRunner struct {
60 Name *string `yaml:"name,omitempty"`
61}
62
63// PersistedCI is het ci-blok in settings.yml. Presets blijven in
64// config.yml / de runner-UI (runner-settings.json); die zitten hier
65// expres niet, om geen tweede bron te maken.
66type PersistedCI struct {
67 Enabled *bool `yaml:"enabled,omitempty"`
68 Docker *string `yaml:"docker_binary,omitempty"`
69 Concurrency *int `yaml:"concurrency,omitempty"`
70 Network *string `yaml:"network,omitempty"`
71 TimeoutSec *int `yaml:"timeout_seconds,omitempty"`
72 UpdateMaxWaitMin *int `yaml:"update_max_wait_minutes,omitempty"`
73}
74
75// LoadSettings leest settings.yml. (nil, nil) als het bestand er niet is.
76func LoadSettings(statePath string) (*Persisted, error) {
77 path := filepath.Join(statePath, SettingsFile)
78 b, err := os.ReadFile(path)
79 if err != nil {
80 if os.IsNotExist(err) {
81 return nil, nil
82 }
83 return nil, err
84 }
85 var p Persisted
86 if err := yaml.Unmarshal(b, &p); err != nil {
87 return nil, fmt.Errorf("%s: %w", path, err)
88 }
89 return &p, nil
90}
91
92// SaveSettings schrijft settings.yml atomair met mode 0600.
93func SaveSettings(statePath string, p Persisted) error {
94 b, err := yaml.Marshal(p)
95 if err != nil {
96 return err
97 }
98 return atomicWrite(filepath.Join(statePath, SettingsFile), b, 0o600)
99}
100
101func ptr[T any](v T) *T { return &v }
102
103// SettingsFrom maakt de opgeslagen vorm van de huidige configuratie.
104func SettingsFrom(c Config) Persisted {
105 return persistFrom(c)
106}
107
108func persistFrom(c Config) Persisted {
109 proxies := append([]string(nil), c.TrustedProxies...)
110 auth := PersistedAuth{
111 Mode: ptr(c.Auth.Mode),
112 BaseURL: ptr(c.Auth.BaseURL),
113 ClientID: ptr(c.Auth.ClientID),
114 RedirectPath: ptr(c.Auth.RedirectPath),
115 UserAgent: ptr(c.Auth.UserAgent),
116 DevUser: ptr(c.Auth.DevUser),
117 DevAuthID: ptr(c.Auth.DevAuthID),
118 TenantID: ptr(c.Auth.TenantID),
119 Issuer: ptr(c.Auth.Issuer),
120 ClientSecretFile: ptr(c.Auth.ClientSecretFile),
121 }
122 if strings.TrimSpace(c.Auth.ClientSecret) != "" {
123 auth.ClientSecret = ptr(c.Auth.ClientSecret)
124 }
125 return Persisted{
126 BaseURL: ptr(c.BaseURL),
127 Listen: ptr(c.Listen),
128 TrustedProxies: &proxies,
129 Auth: &auth,
130 Git: &PersistedGit{
131 Binary: ptr(c.Git.Binary),
132 DefaultBranch: ptr(c.Git.DefaultBranch),
133 HookBinary: ptr(c.Git.HookBinary),
134 MaintenanceInterval: ptr(c.Git.MaintenanceInterval),
135 },
136 SSH: &PersistedSSH{
137 Enabled: ptr(c.SSH.Enabled),
138 Listen: ptr(c.SSH.Listen),
139 PublicHost: ptr(c.SSH.PublicHost),
140 PublicPort: ptr(c.SSH.PublicPort),
141 },
142 Runner: &PersistedRunner{Name: ptr(c.Runner.Name)},
143 CI: &PersistedCI{
144 Enabled: ptr(c.CI.Enabled),
145 Docker: ptr(c.CI.Docker),
146 Concurrency: ptr(c.CI.Concurrency),
147 Network: ptr(c.CI.Network),
148 TimeoutSec: ptr(c.CI.TimeoutSec),
149 UpdateMaxWaitMin: ptr(c.CI.UpdateMaxWaitMin),
150 },
151 }
152}
153
154func applyPersisted(c *Config, p *Persisted) {
155 if p == nil {
156 return
157 }
158 if p.BaseURL != nil {
159 c.BaseURL = strings.TrimRight(*p.BaseURL, "/")
160 }
161 if p.Listen != nil {
162 c.Listen = *p.Listen
163 }
164 if p.TrustedProxies != nil {
165 c.TrustedProxies = append([]string(nil), *p.TrustedProxies...)
166 }
167 if a := p.Auth; a != nil {
168 if a.Mode != nil {
169 c.Auth.Mode = *a.Mode
170 }
171 if a.BaseURL != nil {
172 c.Auth.BaseURL = strings.TrimRight(*a.BaseURL, "/")
173 }
174 if a.ClientID != nil {
175 c.Auth.ClientID = *a.ClientID
176 }
177 if a.ClientSecret != nil && strings.TrimSpace(*a.ClientSecret) != "" {
178 c.Auth.ClientSecret = *a.ClientSecret
179 }
180 if a.ClientSecretFile != nil {
181 c.Auth.ClientSecretFile = *a.ClientSecretFile
182 }
183 if a.RedirectPath != nil {
184 c.Auth.RedirectPath = *a.RedirectPath
185 }
186 if a.UserAgent != nil {
187 c.Auth.UserAgent = *a.UserAgent
188 }
189 if a.DevUser != nil {
190 c.Auth.DevUser = *a.DevUser
191 }
192 if a.DevAuthID != nil {
193 c.Auth.DevAuthID = *a.DevAuthID
194 }
195 if a.TenantID != nil {
196 c.Auth.TenantID = strings.TrimSpace(*a.TenantID)
197 }
198 if a.Issuer != nil {
199 c.Auth.Issuer = strings.TrimRight(strings.TrimSpace(*a.Issuer), "/")
200 }
201 }
202 if g := p.Git; g != nil {
203 if g.Binary != nil {
204 c.Git.Binary = *g.Binary
205 }
206 if g.DefaultBranch != nil {
207 c.Git.DefaultBranch = *g.DefaultBranch
208 }
209 if g.HookBinary != nil {
210 c.Git.HookBinary = *g.HookBinary
211 }
212 if g.MaintenanceInterval != nil {
213 c.Git.MaintenanceInterval = *g.MaintenanceInterval
214 }
215 }
216 if s := p.SSH; s != nil {
217 if s.Enabled != nil {
218 c.SSH.Enabled = *s.Enabled
219 }
220 if s.Listen != nil {
221 c.SSH.Listen = *s.Listen
222 }
223 if s.PublicHost != nil {
224 c.SSH.PublicHost = *s.PublicHost
225 }
226 if s.PublicPort != nil {
227 c.SSH.PublicPort = *s.PublicPort
228 }
229 }
230 if r := p.Runner; r != nil && r.Name != nil {
231 c.Runner.Name = strings.TrimSpace(*r.Name)
232 }
233 if ci := p.CI; ci != nil {
234 if ci.Enabled != nil {
235 c.CI.Enabled = *ci.Enabled
236 }
237 if ci.Docker != nil {
238 c.CI.Docker = *ci.Docker
239 }
240 if ci.Concurrency != nil {
241 c.CI.Concurrency = *ci.Concurrency
242 }
243 if ci.Network != nil {
244 c.CI.Network = *ci.Network
245 }
246 if ci.TimeoutSec != nil {
247 c.CI.TimeoutSec = *ci.TimeoutSec
248 }
249 if ci.UpdateMaxWaitMin != nil {
250 c.CI.UpdateMaxWaitMin = *ci.UpdateMaxWaitMin
251 }
252 }
253}
254
255// MergeUpdate past een wijziging uit de UI/API toe op de huidige config
256// en bouwt de nieuwe Persisted-staat (leeg gelaten geheim blijft behouden).
257func MergeUpdate(cur Config, patch Persisted) (Config, Persisted) {
258 next := cur
259 // Een leeg nieuw geheim betekent "ongewijzigd".
260 if patch.Auth != nil && patch.Auth.ClientSecret != nil && strings.TrimSpace(*patch.Auth.ClientSecret) == "" {
261 patch.Auth.ClientSecret = nil
262 }
263 applyPersisted(&next, &patch)
264 next.applyDefaults()
265 saved := persistFrom(next)
266 return next, saved
267}