-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathoptions.go
More file actions
476 lines (417 loc) · 13.3 KB
/
options.go
File metadata and controls
476 lines (417 loc) · 13.3 KB
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
package app
import (
"fmt"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/cli-runtime/pkg/genericclioptions"
cliflag "k8s.io/component-base/cli/flag"
"github.com/jetstack/version-checker/pkg/api"
"github.com/jetstack/version-checker/pkg/client"
"github.com/jetstack/version-checker/pkg/client/selfhosted"
)
const (
envPrefix = "VERSION_CHECKER"
envACRUsername = "ACR_USERNAME"
envACRPassword = "ACR_PASSWORD" // #nosec G101
envACRRefreshToken = "ACR_REFRESH_TOKEN" // #nosec G101
envACRJWKSURI = "ACR_JWKS_URI"
envDockerUsername = "DOCKER_USERNAME"
envDockerPassword = "DOCKER_PASSWORD" // #nosec G101
envDockerToken = "DOCKER_TOKEN" // #nosec G101
envECRIamRoleArn = "ECR_IAM_ROLE_ARN"
envECRAccessKeyID = "ECR_ACCESS_KEY_ID" // #nosec G101
envECRSecretAccessKey = "ECR_SECRET_ACCESS_KEY" // #nosec G101
envECRSessionToken = "ECR_SESSION_TOKEN" // #nosec G101
envGCRAccessToken = "GCR_TOKEN" // #nosec G101
envGHCRAccessToken = "GHCR_TOKEN" // #nosec G101
envGHCRHostname = "GHCR_HOSTNAME"
envQuayToken = "QUAY_TOKEN" // #nosec G101
envSelfhostedPrefix = "SELFHOSTED"
envSelfhostedUsername = "USERNAME"
envSelfhostedPassword = "PASSWORD"
envSelfhostedHost = "HOST"
envSelfhostedBearer = "TOKEN" // #nosec G101
envSelfhostedTokenPath = "TOKEN_PATH"
envSelfhostedInsecure = "INSECURE"
envSelfhostedCAPath = "CA_PATH"
)
var (
selfhostedHostReg = regexp.MustCompile("^VERSION_CHECKER_SELFHOSTED_HOST_(.*)")
selfhostedUsernameReg = regexp.MustCompile("^VERSION_CHECKER_SELFHOSTED_USERNAME_(.*)")
selfhostedPasswordReg = regexp.MustCompile("^VERSION_CHECKER_SELFHOSTED_PASSWORD_(.*)")
selfhostedTokenPath = regexp.MustCompile("^VERSION_CHECKER_SELFHOSTED_TOKEN_PATH_(.*)")
selfhostedTokenReg = regexp.MustCompile("^VERSION_CHECKER_SELFHOSTED_TOKEN_(.*)")
selfhostedCAPath = regexp.MustCompile("^VERSION_CHECKER_SELFHOSTED_CA_PATH_(.*)")
selfhostedInsecureReg = regexp.MustCompile("^VERSION_CHECKER_SELFHOSTED_INSECURE_(.*)")
)
// Options is a struct to hold options for the version-checker.
type Options struct {
MetricsServingAddress string
PprofBindAddress string
DefaultTestAll bool
LogLevel string
CacheTimeout time.Duration
GracefulShutdownTimeout time.Duration
CacheSyncPeriod time.Duration
RequeueDuration time.Duration
kubeConfigFlags *genericclioptions.ConfigFlags
selfhosted selfhosted.Options
Client client.Options
}
type envMatcher struct {
re *regexp.Regexp
action func(matches []string, value string)
}
func (o *Options) addFlags(cmd *cobra.Command) {
var nfs cliflag.NamedFlagSets
o.addAppFlags(nfs.FlagSet("App"))
o.addAuthFlags(nfs.FlagSet("Auth"))
o.kubeConfigFlags = genericclioptions.NewConfigFlags(true)
o.kubeConfigFlags.AddFlags(nfs.FlagSet("Kubernetes"))
usageFmt := "Usage:\n %s\n"
cmd.SetUsageFunc(func(cmd *cobra.Command) error {
_, _ = fmt.Fprintf(cmd.OutOrStderr(), usageFmt, cmd.UseLine())
cliflag.PrintSections(cmd.OutOrStderr(), nfs, 0)
return nil
})
cmd.SetHelpFunc(func(cmd *cobra.Command, _ []string) {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s\n\n"+usageFmt, cmd.Long, cmd.UseLine())
cliflag.PrintSections(cmd.OutOrStdout(), nfs, 0)
})
fs := cmd.Flags()
for _, f := range nfs.FlagSets {
fs.AddFlagSet(f)
}
}
func (o *Options) addAppFlags(fs *pflag.FlagSet) {
fs.StringVarP(&o.MetricsServingAddress,
"metrics-serving-address", "m", "0.0.0.0:8080",
"Address to serve metrics on at the /metrics path.")
fs.StringVarP(&o.PprofBindAddress,
"pprof-serving-address", "", "",
"Address to serve pprof on for profiling.")
fs.BoolVarP(&o.DefaultTestAll,
"test-all-containers", "a", false,
"If enabled, all containers will be tested, unless they have the "+
fmt.Sprintf(`annotation "%s/${my-container}=false".`, api.EnableAnnotationKey))
fs.DurationVarP(&o.CacheTimeout,
"image-cache-timeout", "c", time.Minute*30,
"The time for an image version in the cache to be considered fresh. Images "+
"will be rechecked after this interval.")
fs.StringVarP(&o.LogLevel,
"log-level", "v", "info",
"Log level (debug, info, warn, error, fatal, panic).")
fs.DurationVarP(&o.GracefulShutdownTimeout,
"graceful-shutdown-timeout", "", 10*time.Second,
"Time that the manager should wait for all controller to shutdown.")
fs.DurationVarP(&o.RequeueDuration,
"requeue-duration", "r", time.Hour,
"The time a pod will be re-checked for new versions/tags")
fs.DurationVarP(&o.CacheSyncPeriod,
"cache-sync-period", "", 5*time.Hour,
"The time in which all resources should be updated.")
}
func (o *Options) addAuthFlags(fs *pflag.FlagSet) {
/// ACR
fs.StringVar(&o.Client.ACR.Username,
"acr-username", "",
fmt.Sprintf(
"Username to authenticate with azure container registry (%s_%s).",
envPrefix, envACRUsername,
))
fs.StringVar(&o.Client.ACR.Password,
"acr-password", "",
fmt.Sprintf(
"Password to authenticate with azure container registry (%s_%s).",
envPrefix, envACRPassword,
))
fs.StringVar(&o.Client.ACR.RefreshToken,
"acr-refresh-token", "",
fmt.Sprintf(
"Refresh token to authenticate with azure container registry. Cannot be used with "+
"username/password (%s_%s).",
envPrefix, envACRRefreshToken,
))
fs.StringVar(&o.Client.ACR.JWKSURI,
"acr-jwks-uri", "",
fmt.Sprintf(
"JWKS URI to verify the JWT access token received. If left blank, JWT token will not be verified. (%s_%s)",
envPrefix, envACRJWKSURI,
))
///
// Docker
fs.StringVar(&o.Client.Docker.Username,
"docker-username", "",
fmt.Sprintf(
"Username to authenticate with docker registry (%s_%s).",
envPrefix, envDockerUsername,
))
fs.StringVar(&o.Client.Docker.Password,
"docker-password", "",
fmt.Sprintf(
"Password to authenticate with docker registry (%s_%s).",
envPrefix, envDockerPassword,
))
fs.StringVar(&o.Client.Docker.Token,
"docker-token", "",
fmt.Sprintf(
"Token to authenticate with docker registry. Cannot be used with "+
"username/password (%s_%s).",
envPrefix, envDockerToken,
))
///
/// ECR
fs.StringVar(&o.Client.ECR.IamRoleArn,
"ecr-iam-role-arn", "",
fmt.Sprintf(
"IAM role ARN for read access to private registries, can not be used with access-key/secret-key/session-token (%s_%s).",
envPrefix, envECRIamRoleArn,
))
fs.StringVar(&o.Client.ECR.AccessKeyID,
"ecr-access-key-id", "",
fmt.Sprintf(
"ECR access key ID for read access to private registries (%s_%s).",
envPrefix, envECRAccessKeyID,
))
fs.StringVar(&o.Client.ECR.SecretAccessKey,
"ecr-secret-access-key", "",
fmt.Sprintf(
"ECR secret access key for read access to private registries (%s_%s).",
envPrefix, envECRSecretAccessKey,
))
fs.StringVar(&o.Client.ECR.SessionToken,
"ecr-session-token", "",
fmt.Sprintf(
"ECR session token for read access to private registries (%s_%s).",
envPrefix, envECRSessionToken,
))
///
/// GCR
fs.StringVar(&o.Client.GCR.Token,
"gcr-token", "",
fmt.Sprintf(
"Access token for read access to private GCR registries (%s_%s).",
envPrefix, envGCRAccessToken,
))
///
/// GHCR
fs.StringVar(&o.Client.GHCR.Token,
"gchr-token", "",
fmt.Sprintf(
"Personal Access token for read access to GHCR releases (%s_%s).",
envPrefix, envGHCRAccessToken,
))
fs.StringVar(&o.Client.GHCR.Hostname,
"gchr-hostname", "",
fmt.Sprintf(
"Override hostname for Github Enterprise instances (%s_%s).",
envPrefix, envGHCRHostname,
))
///
/// Quay
fs.StringVar(&o.Client.Quay.Token,
"quay-token", "",
fmt.Sprintf(
"Access token for read access to private Quay registries (%s_%s).",
envPrefix, envQuayToken,
))
///
/// Selfhosted
fs.StringVar(&o.selfhosted.Username,
"selfhosted-username", "",
fmt.Sprintf(
"Username is authenticate with a selfhosted registry (%s_%s_%s).",
envPrefix, envSelfhostedPrefix, envSelfhostedUsername,
))
fs.StringVar(&o.selfhosted.Password,
"selfhosted-password", "",
fmt.Sprintf(
"Password is authenticate with a selfhosted registry (%s_%s_%s).",
envPrefix, envSelfhostedPrefix, envSelfhostedPassword,
))
fs.StringVar(&o.selfhosted.Bearer,
"selfhosted-token", "",
fmt.Sprintf(
"Token to authenticate to a selfhosted registry. Cannot be used with "+
"username/password (%s_%s_%s).",
envPrefix, envSelfhostedPrefix, envSelfhostedBearer,
))
fs.StringVar(&o.selfhosted.TokenPath,
"selfhosted-token-path", "",
fmt.Sprintf(
"Override the default selfhosted registry's token auth path. "+
"(%s_%s_%s).",
envPrefix, envSelfhostedPrefix, envSelfhostedTokenPath,
))
fs.StringVar(&o.selfhosted.Host,
"selfhosted-registry-host", "",
fmt.Sprintf(
"Full host of the selfhosted registry. Include http[s] scheme (%s_%s_%s)",
envPrefix, envSelfhostedPrefix, envSelfhostedHost,
))
fs.StringVar(&o.selfhosted.CAPath,
"selfhosted-registry-ca-path", "",
fmt.Sprintf(
"Absolute path to a PEM encoded x509 certificate chain. (%s_%s_%s)",
envPrefix, envSelfhostedPrefix, envSelfhostedCAPath,
))
fs.BoolVarP(&o.selfhosted.Insecure,
"selfhosted-insecure", "", false,
fmt.Sprintf(
"Enable/Disable SSL Certificate Validation. WARNING: "+
"THIS IS NOT RECOMMENDED AND IS INTENDED FOR DEBUGGING (%s_%s_%s)",
envPrefix, envSelfhostedPrefix, envSelfhostedInsecure,
))
// if !validSelfHostedOpts(o) {
// panic(fmt.Errorf("invalid self hosted configuration"))
// }
}
func (o *Options) complete() {
o.Client.Selfhosted = make(map[string]*selfhosted.Options)
envs := os.Environ()
for _, opt := range []struct {
key string
assign *string
}{
{envACRUsername, &o.Client.ACR.Username},
{envACRPassword, &o.Client.ACR.Password},
{envACRRefreshToken, &o.Client.ACR.RefreshToken},
{envACRJWKSURI, &o.Client.ACR.JWKSURI},
{envDockerUsername, &o.Client.Docker.Username},
{envDockerPassword, &o.Client.Docker.Password},
{envDockerToken, &o.Client.Docker.Token},
{envECRIamRoleArn, &o.Client.ECR.IamRoleArn},
{envECRAccessKeyID, &o.Client.ECR.AccessKeyID},
{envECRSessionToken, &o.Client.ECR.SessionToken},
{envECRSecretAccessKey, &o.Client.ECR.SecretAccessKey},
{envGCRAccessToken, &o.Client.GCR.Token},
{envGHCRAccessToken, &o.Client.GHCR.Token},
{envGHCRHostname, &o.Client.GHCR.Hostname},
{envQuayToken, &o.Client.Quay.Token},
} {
for _, env := range envs {
if o.assignEnv(env, opt.key, opt.assign) {
break
}
}
}
o.assignSelfhosted(envs)
}
func (o *Options) assignEnv(env, key string, assign *string) bool {
pair := strings.SplitN(env, "=", 2)
if len(pair) < 2 {
return false
}
if envPrefix+"_"+key == pair[0] && len(*assign) == 0 {
*assign = pair[1]
return true
}
return false
}
func (o *Options) assignSelfhosted(envs []string) {
if o.Client.Selfhosted == nil {
o.Client.Selfhosted = make(map[string]*selfhosted.Options)
}
initOptions := func(name string) {
if o.Client.Selfhosted[name] == nil {
o.Client.Selfhosted[name] = new(selfhosted.Options)
}
}
// Go maps iterate in random order - Using a slice to consistency
regexActions := []envMatcher{
{
re: selfhostedTokenPath,
action: func(matches []string, value string) {
initOptions(matches[1])
o.Client.Selfhosted[matches[1]].TokenPath = value
},
},
{
re: selfhostedTokenReg,
action: func(matches []string, value string) {
initOptions(matches[1])
o.Client.Selfhosted[matches[1]].Bearer = value
},
},
// All your other patterns (host, username, password, insecure, capath...)
{
re: selfhostedHostReg,
action: func(matches []string, value string) {
initOptions(matches[1])
o.Client.Selfhosted[matches[1]].Host = value
},
},
{
re: selfhostedUsernameReg,
action: func(matches []string, value string) {
initOptions(matches[1])
o.Client.Selfhosted[matches[1]].Username = value
},
},
{
re: selfhostedPasswordReg,
action: func(matches []string, value string) {
initOptions(matches[1])
o.Client.Selfhosted[matches[1]].Password = value
},
},
{
re: selfhostedInsecureReg,
action: func(matches []string, value string) {
initOptions(matches[1])
if b, err := strconv.ParseBool(value); err == nil {
o.Client.Selfhosted[matches[1]].Insecure = b
}
},
},
{
re: selfhostedCAPath,
action: func(matches []string, value string) {
initOptions(matches[1])
o.Client.Selfhosted[matches[1]].CAPath = value
},
},
}
for _, env := range envs {
parts := strings.SplitN(env, "=", 2)
if len(parts) != 2 || parts[1] == "" {
continue
}
key := strings.ToUpper(parts[0])
val := parts[1]
for _, p := range regexActions {
if match := p.re.FindStringSubmatch(key); len(match) == 2 {
p.action(match, val)
break
}
}
}
// If we have some selfhosted flags, lets set them here...
if len(o.selfhosted.Host) > 0 {
o.Client.Selfhosted[o.selfhosted.Host] = &o.selfhosted
}
if !validSelfHostedOpts(o) {
panic(fmt.Errorf("invalid self hosted configuration"))
}
}
func validSelfHostedOpts(opts *Options) bool {
// opts set using env vars
if opts.Client.Selfhosted != nil {
for _, selfHostedOpts := range opts.Client.Selfhosted {
return isValidOption(selfHostedOpts.Host, "")
}
}
// opts set using flags
if opts.selfhosted != (selfhosted.Options{}) {
return isValidOption(opts.selfhosted.Host, "")
}
return true
}
func isValidOption(option, invalid string) bool {
return option != invalid
}