Skip to content

Commit c82d8ba

Browse files
authored
Merge branch 'main' into issue-212
2 parents d4fd1af + a193a71 commit c82d8ba

File tree

2 files changed

+95
-16
lines changed

2 files changed

+95
-16
lines changed

cmd/app/options.go

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -263,49 +263,51 @@ func (o *Options) addAuthFlags(fs *pflag.FlagSet) {
263263
fs.StringVar(&o.selfhosted.Username,
264264
"selfhosted-username", "",
265265
fmt.Sprintf(
266-
"Username is authenticate with a selfhosted registry (%s_%s).",
267-
envPrefix, envSelfhostedUsername,
266+
"Username is authenticate with a selfhosted registry (%s_%s_%s).",
267+
envPrefix, envSelfhostedPrefix, envSelfhostedUsername,
268268
))
269269
fs.StringVar(&o.selfhosted.Password,
270270
"selfhosted-password", "",
271271
fmt.Sprintf(
272-
"Password is authenticate with a selfhosted registry (%s_%s).",
273-
envPrefix, envSelfhostedPassword,
272+
"Password is authenticate with a selfhosted registry (%s_%s_%s).",
273+
envPrefix, envSelfhostedPrefix, envSelfhostedPassword,
274274
))
275275
fs.StringVar(&o.selfhosted.Bearer,
276276
"selfhosted-token", "",
277277
fmt.Sprintf(
278278
"Token to authenticate to a selfhosted registry. Cannot be used with "+
279-
"username/password (%s_%s).",
280-
envPrefix, envSelfhostedBearer,
279+
"username/password (%s_%s_%s).",
280+
envPrefix, envSelfhostedPrefix, envSelfhostedBearer,
281281
))
282282
fs.StringVar(&o.selfhosted.TokenPath,
283283
"selfhosted-token-path", "",
284284
fmt.Sprintf(
285285
"Override the default selfhosted registry's token auth path. "+
286-
"(%s_%s).",
287-
envPrefix, envSelfhostedTokenPath,
286+
"(%s_%s_%s).",
287+
envPrefix, envSelfhostedPrefix, envSelfhostedTokenPath,
288288
))
289289
fs.StringVar(&o.selfhosted.Host,
290290
"selfhosted-registry-host", "",
291291
fmt.Sprintf(
292-
"Full host of the selfhosted registry. Include http[s] scheme (%s_%s)",
293-
envPrefix, envSelfhostedHost,
292+
"Full host of the selfhosted registry. Include http[s] scheme (%s_%s_%s)",
293+
envPrefix, envSelfhostedPrefix, envSelfhostedHost,
294294
))
295-
fs.StringVar(&o.selfhosted.Host,
295+
fs.StringVar(&o.selfhosted.CAPath,
296296
"selfhosted-registry-ca-path", "",
297297
fmt.Sprintf(
298-
"Absolute path to a PEM encoded x509 certificate chain. (%s_%s)",
299-
envPrefix, envSelfhostedCAPath,
298+
"Absolute path to a PEM encoded x509 certificate chain. (%s_%s_%s)",
299+
envPrefix, envSelfhostedPrefix, envSelfhostedCAPath,
300300
))
301301
fs.BoolVarP(&o.selfhosted.Insecure,
302302
"selfhosted-insecure", "", false,
303303
fmt.Sprintf(
304304
"Enable/Disable SSL Certificate Validation. WARNING: "+
305-
"THIS IS NOT RECOMMENDED AND IS INTENDED FOR DEBUGGING (%s_%s)",
306-
envPrefix, envSelfhostedInsecure,
305+
"THIS IS NOT RECOMMENDED AND IS INTENDED FOR DEBUGGING (%s_%s_%s)",
306+
envPrefix, envSelfhostedPrefix, envSelfhostedInsecure,
307307
))
308-
///
308+
// if !validSelfHostedOpts(o) {
309+
// panic(fmt.Errorf("invalid self hosted configuration"))
310+
// }
309311
}
310312

311313
func (o *Options) complete() {
@@ -450,4 +452,26 @@ func (o *Options) assignSelfhosted(envs []string) {
450452
if len(o.selfhosted.Host) > 0 {
451453
o.Client.Selfhosted[o.selfhosted.Host] = &o.selfhosted
452454
}
455+
if !validSelfHostedOpts(o) {
456+
panic(fmt.Errorf("invalid self hosted configuration"))
457+
}
458+
}
459+
460+
func validSelfHostedOpts(opts *Options) bool {
461+
// opts set using env vars
462+
if opts.Client.Selfhosted != nil {
463+
for _, selfHostedOpts := range opts.Client.Selfhosted {
464+
return isValidOption(selfHostedOpts.Host, "")
465+
}
466+
}
467+
468+
// opts set using flags
469+
if opts.selfhosted != (selfhosted.Options{}) {
470+
return isValidOption(opts.selfhosted.Host, "")
471+
}
472+
return true
473+
}
474+
475+
func isValidOption(option, invalid string) bool {
476+
return option != invalid
453477
}

cmd/app/options_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,61 @@ func TestComplete(t *testing.T) {
187187
}
188188
}
189189

190+
func TestInvalidSelfhostedPanic(t *testing.T) {
191+
tests := map[string]struct {
192+
envs []string
193+
}{
194+
"single host for all options should be included": {
195+
envs: []string{
196+
"VERSION_CHECKER_SELFHOSTED_INSECURE_FOO=true",
197+
},
198+
},
199+
}
200+
for name, test := range tests {
201+
t.Run(name, func(t *testing.T) {
202+
defer func() { recover() }()
203+
204+
o := new(Options)
205+
o.assignSelfhosted(test.envs)
206+
207+
t.Errorf("did not panic")
208+
})
209+
}
210+
}
211+
212+
func TestInvalidSelfhostedOpts(t *testing.T) {
213+
tests := map[string]struct {
214+
opts Options
215+
valid bool
216+
}{
217+
"no self hosted configuration": {
218+
opts: Options{},
219+
valid: true,
220+
},
221+
"no self hosted host provided": {
222+
opts: Options{
223+
Client: client.Options{
224+
Selfhosted: map[string]*selfhosted.Options{"foo": &selfhosted.Options{
225+
Insecure: true,
226+
}},
227+
},
228+
},
229+
valid: false,
230+
},
231+
}
232+
for name, test := range tests {
233+
t.Run(name, func(t *testing.T) {
234+
235+
valid := validSelfHostedOpts(&test.opts)
236+
237+
if !reflect.DeepEqual(test.valid, valid) {
238+
t.Errorf("unexpected selfhosted valid options, exp=%#+v got=%#+v",
239+
test.valid, valid)
240+
}
241+
})
242+
}
243+
}
244+
190245
func TestAssignSelfhosted(t *testing.T) {
191246
tests := map[string]struct {
192247
envs []string

0 commit comments

Comments
 (0)