Skip to content

Commit a193a71

Browse files
validate selfhosted options (#361)
1 parent 0bec27f commit a193a71

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
@@ -252,49 +252,51 @@ func (o *Options) addAuthFlags(fs *pflag.FlagSet) {
252252
fs.StringVar(&o.selfhosted.Username,
253253
"selfhosted-username", "",
254254
fmt.Sprintf(
255-
"Username is authenticate with a selfhosted registry (%s_%s).",
256-
envPrefix, envSelfhostedUsername,
255+
"Username is authenticate with a selfhosted registry (%s_%s_%s).",
256+
envPrefix, envSelfhostedPrefix, envSelfhostedUsername,
257257
))
258258
fs.StringVar(&o.selfhosted.Password,
259259
"selfhosted-password", "",
260260
fmt.Sprintf(
261-
"Password is authenticate with a selfhosted registry (%s_%s).",
262-
envPrefix, envSelfhostedPassword,
261+
"Password is authenticate with a selfhosted registry (%s_%s_%s).",
262+
envPrefix, envSelfhostedPrefix, envSelfhostedPassword,
263263
))
264264
fs.StringVar(&o.selfhosted.Bearer,
265265
"selfhosted-token", "",
266266
fmt.Sprintf(
267267
"Token to authenticate to a selfhosted registry. Cannot be used with "+
268-
"username/password (%s_%s).",
269-
envPrefix, envSelfhostedBearer,
268+
"username/password (%s_%s_%s).",
269+
envPrefix, envSelfhostedPrefix, envSelfhostedBearer,
270270
))
271271
fs.StringVar(&o.selfhosted.TokenPath,
272272
"selfhosted-token-path", "",
273273
fmt.Sprintf(
274274
"Override the default selfhosted registry's token auth path. "+
275-
"(%s_%s).",
276-
envPrefix, envSelfhostedTokenPath,
275+
"(%s_%s_%s).",
276+
envPrefix, envSelfhostedPrefix, envSelfhostedTokenPath,
277277
))
278278
fs.StringVar(&o.selfhosted.Host,
279279
"selfhosted-registry-host", "",
280280
fmt.Sprintf(
281-
"Full host of the selfhosted registry. Include http[s] scheme (%s_%s)",
282-
envPrefix, envSelfhostedHost,
281+
"Full host of the selfhosted registry. Include http[s] scheme (%s_%s_%s)",
282+
envPrefix, envSelfhostedPrefix, envSelfhostedHost,
283283
))
284-
fs.StringVar(&o.selfhosted.Host,
284+
fs.StringVar(&o.selfhosted.CAPath,
285285
"selfhosted-registry-ca-path", "",
286286
fmt.Sprintf(
287-
"Absolute path to a PEM encoded x509 certificate chain. (%s_%s)",
288-
envPrefix, envSelfhostedCAPath,
287+
"Absolute path to a PEM encoded x509 certificate chain. (%s_%s_%s)",
288+
envPrefix, envSelfhostedPrefix, envSelfhostedCAPath,
289289
))
290290
fs.BoolVarP(&o.selfhosted.Insecure,
291291
"selfhosted-insecure", "", false,
292292
fmt.Sprintf(
293293
"Enable/Disable SSL Certificate Validation. WARNING: "+
294-
"THIS IS NOT RECOMMENDED AND IS INTENDED FOR DEBUGGING (%s_%s)",
295-
envPrefix, envSelfhostedInsecure,
294+
"THIS IS NOT RECOMMENDED AND IS INTENDED FOR DEBUGGING (%s_%s_%s)",
295+
envPrefix, envSelfhostedPrefix, envSelfhostedInsecure,
296296
))
297-
///
297+
// if !validSelfHostedOpts(o) {
298+
// panic(fmt.Errorf("invalid self hosted configuration"))
299+
// }
298300
}
299301

300302
func (o *Options) complete() {
@@ -414,4 +416,26 @@ func (o *Options) assignSelfhosted(envs []string) {
414416
if len(o.selfhosted.Host) > 0 {
415417
o.Client.Selfhosted[o.selfhosted.Host] = &o.selfhosted
416418
}
419+
if !validSelfHostedOpts(o) {
420+
panic(fmt.Errorf("invalid self hosted configuration"))
421+
}
422+
}
423+
424+
func validSelfHostedOpts(opts *Options) bool {
425+
// opts set using env vars
426+
if opts.Client.Selfhosted != nil {
427+
for _, selfHostedOpts := range opts.Client.Selfhosted {
428+
return isValidOption(selfHostedOpts.Host, "")
429+
}
430+
}
431+
432+
// opts set using flags
433+
if opts.selfhosted != (selfhosted.Options{}) {
434+
return isValidOption(opts.selfhosted.Host, "")
435+
}
436+
return true
437+
}
438+
439+
func isValidOption(option, invalid string) bool {
440+
return option != invalid
417441
}

cmd/app/options_test.go

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

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

0 commit comments

Comments
 (0)