-
Notifications
You must be signed in to change notification settings - Fork 83
/
config.go
279 lines (261 loc) · 8.65 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
package main
import (
"errors"
"io"
"os"
"time"
"gopkg.in/yaml.v3"
"github.com/regclient/regclient/config"
"github.com/regclient/regclient/pkg/template"
"github.com/regclient/regclient/types/mediatype"
)
// delay checking for at least 5 minutes when rate limit is exceeded
var rateLimitRetryMin = time.Minute * 5
var defaultMediaTypes = []string{
mediatype.Docker2Manifest,
mediatype.Docker2ManifestList,
mediatype.OCI1Manifest,
mediatype.OCI1ManifestList,
}
// Config is parsed configuration file for regsync
type Config struct {
Version int `yaml:"version" json:"version"`
Creds []config.Host `yaml:"creds" json:"creds"`
Defaults ConfigDefaults `yaml:"defaults" json:"defaults"`
Sync []ConfigSync `yaml:"sync" json:"sync"`
}
// ConfigDefaults is uses for general options and defaults for ConfigSync entries
type ConfigDefaults struct {
Backup string `yaml:"backup" json:"backup"`
Interval time.Duration `yaml:"interval" json:"interval"`
Schedule string `yaml:"schedule" json:"schedule"`
RateLimit ConfigRateLimit `yaml:"ratelimit" json:"ratelimit"`
Parallel int `yaml:"parallel" json:"parallel"`
DigestTags *bool `yaml:"digestTags" json:"digestTags"`
Referrers *bool `yaml:"referrers" json:"referrers"`
ReferrerFilters []ConfigReferrerFilter `yaml:"referrerFilters" json:"referrerFilters"`
FastCheck *bool `yaml:"fastCheck" json:"fastCheck"`
ForceRecursive *bool `yaml:"forceRecursive" json:"forceRecursive"`
IncludeExternal *bool `yaml:"includeExternal" json:"includeExternal"`
MediaTypes []string `yaml:"mediaTypes" json:"mediaTypes"`
Hooks ConfigHooks `yaml:"hooks" json:"hooks"`
// general options
BlobLimit int64 `yaml:"blobLimit" json:"blobLimit"`
CacheCount int `yaml:"cacheCount" json:"cacheCount"`
CacheTime time.Duration `yaml:"cacheTime" json:"cacheTime"`
SkipDockerConf bool `yaml:"skipDockerConfig" json:"skipDockerConfig"`
UserAgent string `yaml:"userAgent" json:"userAgent"`
}
// ConfigRateLimit is for rate limit settings
type ConfigRateLimit struct {
Min int `yaml:"min" json:"min"`
Retry time.Duration `yaml:"retry" json:"retry"`
}
// ConfigSync defines a source/target repository to sync
type ConfigSync struct {
Source string `yaml:"source" json:"source"`
Target string `yaml:"target" json:"target"`
Type string `yaml:"type" json:"type"`
Tags AllowDeny `yaml:"tags" json:"tags"`
Repos AllowDeny `yaml:"repos" json:"repos"`
DigestTags *bool `yaml:"digestTags" json:"digestTags"`
Referrers *bool `yaml:"referrers" json:"referrers"`
ReferrerFilters []ConfigReferrerFilter `yaml:"referrerFilters" json:"referrerFilters"`
Platform string `yaml:"platform" json:"platform"`
Platforms []string `yaml:"platforms" json:"platforms"`
FastCheck *bool `yaml:"fastCheck" json:"fastCheck"`
ForceRecursive *bool `yaml:"forceRecursive" json:"forceRecursive"`
IncludeExternal *bool `yaml:"includeExternal" json:"includeExternal"`
Backup string `yaml:"backup" json:"backup"`
Interval time.Duration `yaml:"interval" json:"interval"`
Schedule string `yaml:"schedule" json:"schedule"`
RateLimit ConfigRateLimit `yaml:"ratelimit" json:"ratelimit"`
MediaTypes []string `yaml:"mediaTypes" json:"mediaTypes"`
Hooks ConfigHooks `yaml:"hooks" json:"hooks"`
}
// AllowDeny is an allow and deny list of regex strings
type AllowDeny struct {
Allow []string `yaml:"allow" json:"allow"`
Deny []string `yaml:"deny" json:"deny"`
}
type ConfigReferrerFilter struct {
ArtifactType string `yaml:"artifactType" json:"artifactType"`
Annotations map[string]string `yaml:"annotations" json:"annotations"`
}
// ConfigHooks for commands that run during the sync
type ConfigHooks struct {
Pre *ConfigHook `yaml:"pre" json:"pre"`
Post *ConfigHook `yaml:"post" json:"post"`
Unchanged *ConfigHook `yaml:"unchanged" json:"unchanged"`
}
// ConfigHook identifies the hook type and params
type ConfigHook struct {
Type string `yaml:"type" json:"type"`
Params []string `yaml:"params" json:"params"`
}
// ConfigNew creates an empty configuration
func ConfigNew() *Config {
c := Config{
Creds: []config.Host{},
Sync: []ConfigSync{},
}
return &c
}
// ConfigLoadReader reads the config from an io.Reader
func ConfigLoadReader(r io.Reader) (*Config, error) {
c := ConfigNew()
if err := yaml.NewDecoder(r).Decode(c); err != nil && !errors.Is(err, io.EOF) {
return nil, err
}
// verify loaded version is not higher than supported version
if c.Version > 1 {
return c, ErrUnsupportedConfigVersion
}
// apply top level defaults
if c.Defaults.RateLimit.Retry < rateLimitRetryMin {
c.Defaults.RateLimit.Retry = rateLimitRetryMin
}
// apply defaults to each step
for i := range c.Sync {
syncSetDefaults(&c.Sync[i], c.Defaults)
}
err := configExpandTemplates(c)
if err != nil {
return nil, err
}
return c, nil
}
// ConfigLoadFile loads the config from a specified filename
func ConfigLoadFile(filename string) (*Config, error) {
_, err := os.Stat(filename)
if err == nil {
//#nosec G304 command is run by a user accessing their own files
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
c, err := ConfigLoadReader(file)
if err != nil {
return nil, err
}
return c, nil
}
return nil, err
}
// ConfigWrite outputs the processed config
func ConfigWrite(c *Config, w io.Writer) error {
return yaml.NewEncoder(w).Encode(c)
}
// expand templates in various parts of the config
func configExpandTemplates(c *Config) error {
dataSync := struct {
Sync ConfigSync
}{}
for i := range c.Creds {
val, err := template.String(c.Creds[i].Name, nil)
if err != nil {
return err
}
c.Creds[i].Name = val
val, err = template.String(c.Creds[i].User, nil)
if err != nil {
return err
}
c.Creds[i].User = val
val, err = template.String(c.Creds[i].Pass, nil)
if err != nil {
return err
}
c.Creds[i].Pass = val
val, err = template.String(c.Creds[i].RegCert, nil)
if err != nil {
return err
}
c.Creds[i].RegCert = val
val, err = template.String(c.Creds[i].ClientCert, nil)
if err != nil {
return err
}
c.Creds[i].ClientCert = val
val, err = template.String(c.Creds[i].ClientKey, nil)
if err != nil {
return err
}
c.Creds[i].ClientKey = val
}
for i := range c.Sync {
dataSync.Sync = c.Sync[i]
val, err := template.String(c.Sync[i].Source, dataSync)
if err != nil {
return err
}
c.Sync[i].Source = val
dataSync.Sync.Source = val
val, err = template.String(c.Sync[i].Target, dataSync)
if err != nil {
return err
}
c.Sync[i].Target = val
}
return nil
}
// updates sync entry with defaults
func syncSetDefaults(s *ConfigSync, d ConfigDefaults) {
if s.Backup == "" && d.Backup != "" {
s.Backup = d.Backup
}
if s.Schedule == "" && d.Schedule != "" {
s.Schedule = d.Schedule
}
if s.Interval == 0 && s.Schedule == "" && d.Interval != 0 {
s.Interval = d.Interval
}
if s.RateLimit.Min == 0 && d.RateLimit.Min != 0 {
s.RateLimit.Min = d.RateLimit.Min
}
if s.RateLimit.Retry == 0 {
s.RateLimit.Retry = d.RateLimit.Retry
} else if s.RateLimit.Retry < rateLimitRetryMin {
s.RateLimit.Retry = rateLimitRetryMin
}
if len(s.MediaTypes) == 0 {
if len(d.MediaTypes) > 0 {
s.MediaTypes = d.MediaTypes
} else {
s.MediaTypes = defaultMediaTypes
}
}
if s.DigestTags == nil {
b := (d.DigestTags != nil && *d.DigestTags)
s.DigestTags = &b
}
if s.Referrers == nil {
b := (d.Referrers != nil && *d.Referrers)
s.Referrers = &b
}
if s.ReferrerFilters == nil {
s.ReferrerFilters = d.ReferrerFilters
}
if s.FastCheck == nil {
b := (d.FastCheck != nil && *d.FastCheck)
s.FastCheck = &b
}
if s.ForceRecursive == nil {
b := (d.ForceRecursive != nil && *d.ForceRecursive)
s.ForceRecursive = &b
}
if s.IncludeExternal == nil {
b := (d.IncludeExternal != nil && *d.IncludeExternal)
s.IncludeExternal = &b
}
if s.Hooks.Pre == nil && d.Hooks.Pre != nil {
s.Hooks.Pre = d.Hooks.Pre
}
if s.Hooks.Post == nil && d.Hooks.Post != nil {
s.Hooks.Post = d.Hooks.Post
}
if s.Hooks.Unchanged == nil && d.Hooks.Unchanged != nil {
s.Hooks.Unchanged = d.Hooks.Unchanged
}
}