-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
375 lines (317 loc) · 10.4 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package main
import (
"context"
"errors"
"fmt"
"os"
"path"
"reflect"
"slices"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/utilitywarehouse/git-mirror/pkg/giturl"
"github.com/utilitywarehouse/git-mirror/pkg/mirror"
"gopkg.in/yaml.v3"
)
const (
defaultGitGC = "always"
defaultInterval = 30 * time.Second
defaultMirrorTimeout = 2 * time.Minute
defaultSSHKeyPath = "/etc/git-secret/ssh"
defaultSSHKnownHostsPath = "/etc/git-secret/known_hosts"
)
var (
defaultRoot = path.Join(os.TempDir(), "git-mirror")
configSuccess = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "git_mirror_config_last_reload_successful",
Help: "Whether the last configuration reload attempt was successful.",
})
configSuccessTime = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "git_mirror_config_last_reload_success_timestamp_seconds",
Help: "Timestamp of the last successful configuration reload.",
})
allowedRepoPoolConfig = getAllowedKeys(mirror.RepoPoolConfig{})
allowedDefaults = getAllowedKeys(mirror.DefaultConfig{})
allowedAuthKeys = getAllowedKeys(mirror.Auth{})
allowedRepoKeys = getAllowedKeys(mirror.RepositoryConfig{})
allowedWorktreeKeys = getAllowedKeys(mirror.WorktreeConfig{})
)
// WatchConfig polls the config file every interval and reloads if modified
func WatchConfig(ctx context.Context, path string, watchConfig bool, interval time.Duration, onChange func(*mirror.RepoPoolConfig) bool) {
var lastModTime time.Time
var success bool
for {
lastModTime, success = loadConfig(path, lastModTime, onChange)
if success {
configSuccess.Set(1)
configSuccessTime.SetToCurrentTime()
} else {
configSuccess.Set(0)
}
if !watchConfig {
return
}
t := time.NewTimer(interval)
select {
case <-t.C:
case <-ctx.Done():
return
}
}
}
func loadConfig(path string, lastModTime time.Time, onChange func(*mirror.RepoPoolConfig) bool) (time.Time, bool) {
fileInfo, err := os.Stat(path)
if err != nil {
logger.Error("Error checking config file", "err", err)
return lastModTime, false
}
modTime := fileInfo.ModTime()
if modTime.Equal(lastModTime) {
return lastModTime, true
}
logger.Info("reloading config file...")
newConfig, err := parseConfigFile(path)
if err != nil {
logger.Error("failed to reload config", "err", err)
// update modTime to re-evaluate after an update
return modTime, false
}
return modTime, onChange(newConfig)
}
// ensureConfig will do the diff between current repoPool state and new config
// and based on that diff it will add/remove repositories and worktrees
func ensureConfig(repoPool *mirror.RepoPool, newConfig *mirror.RepoPoolConfig) bool {
success := true
// add default values
applyGitDefaults(newConfig)
// validate and apply defaults to new config before compare
if err := newConfig.ValidateAndApplyDefaults(); err != nil {
logger.Error("failed to validate new config", "err", err)
return false
}
newRepos, removedRepos := diffRepositories(repoPool, newConfig)
for _, repo := range removedRepos {
if err := repoPool.RemoveRepository(repo); err != nil {
logger.Error("failed to remove repository", "remote", repo, "err", err)
success = false
}
}
for _, repo := range newRepos {
if err := repoPool.AddRepository(repo); err != nil {
logger.Error("failed to add new repository", "remote", repo.Remote, "err", err)
success = false
}
}
// find matched repos and check for worktree diffs
for _, newRepoConf := range newConfig.Repositories {
repo, err := repoPool.Repository(newRepoConf.Remote)
if err != nil {
logger.Error("unable to check worktree changes", "remote", newRepoConf.Remote, "err", err)
success = false
continue
}
newWTs, removedWTs := diffWorktrees(repo, &newRepoConf)
// 1st remove then add new in case new one has same link with diff reference
for _, wt := range removedWTs {
if err := repoPool.RemoveWorktreeLink(newRepoConf.Remote, wt); err != nil {
logger.Error("failed to remove worktree", "remote", newRepoConf.Remote, "link", wt, "err", err)
success = false
}
}
for _, wt := range newWTs {
if err := repoPool.AddWorktreeLink(newRepoConf.Remote, wt); err != nil {
logger.Error("failed to add worktree", "remote", newRepoConf.Remote, "link", wt.Link, "err", err)
success = false
}
}
}
// start mirror Loop on newly added repos
repoPool.StartLoop()
return success
}
func applyGitDefaults(mirrorConf *mirror.RepoPoolConfig) {
if mirrorConf.Defaults.Root == "" {
mirrorConf.Defaults.Root = defaultRoot
}
if mirrorConf.Defaults.GitGC == "" {
mirrorConf.Defaults.GitGC = defaultGitGC
}
if mirrorConf.Defaults.Interval == 0 {
mirrorConf.Defaults.Interval = defaultInterval
}
if mirrorConf.Defaults.MirrorTimeout == 0 {
mirrorConf.Defaults.MirrorTimeout = defaultMirrorTimeout
}
if mirrorConf.Defaults.Auth.SSHKeyPath == "" {
mirrorConf.Defaults.Auth.SSHKeyPath = defaultSSHKeyPath
}
if mirrorConf.Defaults.Auth.SSHKnownHostsPath == "" {
mirrorConf.Defaults.Auth.SSHKnownHostsPath = defaultSSHKnownHostsPath
}
}
func parseConfigFile(path string) (*mirror.RepoPoolConfig, error) {
yamlFile, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("unable to read file err:%w", err)
}
err = validateConfigYaml(yamlFile)
if err != nil {
return nil, fmt.Errorf("invalid config err:%w", err)
}
conf := &mirror.RepoPoolConfig{}
err = yaml.Unmarshal(yamlFile, conf)
if err != nil {
return nil, fmt.Errorf("unable to decode config err:%w", err)
}
return conf, nil
}
func validateConfigYaml(yamlData []byte) error {
var raw map[string]interface{}
if err := yaml.Unmarshal(yamlData, &raw); err != nil {
return fmt.Errorf("unable to decode config err:%w", err)
}
// check all root config sections for unexpected keys
if key := findUnexpectedKey(raw, allowedRepoPoolConfig); key != "" {
return fmt.Errorf("unexpected key: .%v", key)
}
// check ".defaults" if it's not empty
if raw["defaults"] != nil {
defaultsMap, ok := raw["defaults"].(map[string]interface{})
if !ok {
return fmt.Errorf(".defaults config is not valid")
}
if key := findUnexpectedKey(defaultsMap, allowedDefaults); key != "" {
return fmt.Errorf("unexpected key: .defaults.%v", key)
}
// check ".defaults.auth"
if authMap, ok := defaultsMap["auth"].(map[string]interface{}); ok {
if key := findUnexpectedKey(authMap, allowedAuthKeys); key != "" {
return fmt.Errorf("unexpected key: .defaults.auth.%v", key)
}
}
}
// skip further config checks if ".repositories" is empty
if raw["repositories"] == nil {
return nil
}
// check ".repositories"
reposInterface, ok := raw["repositories"].([]interface{})
if !ok {
return fmt.Errorf(".repositories config must be an array")
}
// check each repository in ".repositories"
for _, repoInterface := range reposInterface {
repoMap, ok := repoInterface.(map[string]interface{})
if !ok {
return fmt.Errorf(".repositories config is not valid")
}
if key := findUnexpectedKey(repoMap, allowedRepoKeys); key != "" {
return fmt.Errorf("unexpected key: .repositories[%v].%v", repoMap["remote"], key)
}
// skip further repository checks if "worktrees" is empty
if repoMap["worktrees"] == nil {
continue
}
// check "worktrees" in each repository
worktreesInterface, ok := repoMap["worktrees"].([]interface{})
if !ok {
return fmt.Errorf("worktrees config must be an array in .repositories[%v]", repoMap["remote"])
}
for i, worktreeInterface := range worktreesInterface {
worktreeMap, ok := worktreeInterface.(map[string]interface{})
if !ok {
return fmt.Errorf("worktrees config is not valid in .repositories[%v]", repoMap["remote"])
}
if key := findUnexpectedKey(worktreeMap, allowedWorktreeKeys); key != "" {
return fmt.Errorf("unexpected key: .repositories[%v].worktrees[%v].%v", repoMap["remote"], i, key)
}
// Check "pathspecs" in each worktree
if pathspecsInterface, exists := worktreeMap["pathspecs"]; exists {
if _, ok := pathspecsInterface.([]interface{}); !ok {
return fmt.Errorf("pathspecs config must be an array in .repositories[%v].worktrees[%v]", repoMap["remote"], i)
}
}
}
}
return nil
}
// getAllowedKeys retrieves a list of allowed keys from the specified struct
func getAllowedKeys(config interface{}) []string {
var allowedKeys []string
val := reflect.ValueOf(config)
typ := reflect.TypeOf(config)
for i := 0; i < val.NumField(); i++ {
field := typ.Field(i)
yamlTag := field.Tag.Get("yaml")
if yamlTag != "" {
allowedKeys = append(allowedKeys, yamlTag)
}
}
return allowedKeys
}
func findUnexpectedKey(raw map[string]interface{}, allowedKeys []string) string {
for key := range raw {
if !slices.Contains(allowedKeys, key) {
return key
}
}
return ""
}
// diffRepositories will do the diff between current state and new config and
// return new repositories config and list of remote url which are not found in config
func diffRepositories(repoPool *mirror.RepoPool, newConfig *mirror.RepoPoolConfig) (
newRepos []mirror.RepositoryConfig,
removedRepos []string,
) {
for _, newRepo := range newConfig.Repositories {
if _, err := repoPool.Repository(newRepo.Remote); errors.Is(err, mirror.ErrNotExist) {
newRepos = append(newRepos, newRepo)
}
}
for _, currentRepoURL := range repoPool.RepositoriesRemote() {
var found bool
for _, newRepo := range newConfig.Repositories {
if currentRepoURL == giturl.NormaliseURL(newRepo.Remote) {
found = true
break
}
}
if !found {
removedRepos = append(removedRepos, currentRepoURL)
}
}
return
}
// diffWorktrees will do the diff between current repo's worktree state and new worktree config
// it will return new worktree configs and link names of the link not found in new config
func diffWorktrees(repo *mirror.Repository, newRepoConf *mirror.RepositoryConfig) (
newWTCs []mirror.WorktreeConfig,
removedWTs []string,
) {
currentWTLinks := repo.WorktreeLinks()
for _, newWTC := range newRepoConf.Worktrees {
if _, ok := currentWTLinks[newWTC.Link]; !ok {
newWTCs = append(newWTCs, newWTC)
}
}
// for existing worktree
for cLink, wt := range currentWTLinks {
var found bool
for _, newWTC := range newRepoConf.Worktrees {
if newWTC.Link == cLink {
// wt link name is matching so make sure other
// config match as well if not replace it
if !wt.Equals(newWTC) {
newWTCs = append(newWTCs, newWTC)
break
}
found = true
break
}
}
if !found {
removedWTs = append(removedWTs, cLink)
}
}
return
}