@@ -15,7 +15,6 @@ import (
15
15
16
16
"github.com/jetstack/version-checker/pkg/api"
17
17
"github.com/jetstack/version-checker/pkg/client"
18
- "github.com/jetstack/version-checker/pkg/client/selfhosted"
19
18
)
20
19
21
20
const (
@@ -50,6 +49,14 @@ const (
50
49
envSelfhostedTokenPath = "TOKEN_PATH"
51
50
envSelfhostedInsecure = "INSECURE"
52
51
envSelfhostedCAPath = "CA_PATH"
52
+
53
+ // Used for kubernetes Credential Discovery
54
+ envKeychainServiceAccountName = "AUTH_SERVICE_ACCOUNT_NAME"
55
+ envKeychainNamespace = "AUTH_SERVICE_ACCOUNT_NAMESPACE"
56
+ envKeychainImagePullSecrets = "AUTH_IMAGE_PULL_SECRETS"
57
+ envKeychainUseMountSecrets = "AUTH_USE_MOUNT_SECRETS"
58
+ // Duration in which to Refresh Credentials from Service Account
59
+ envKeychainRefreshDuration = "AUTH_REFRESH_DURATION"
53
60
)
54
61
55
62
var (
@@ -149,26 +156,63 @@ func (o *Options) addAppFlags(fs *pflag.FlagSet) {
149
156
}
150
157
151
158
func (o * Options ) addAuthFlags (fs * pflag.FlagSet ) {
159
+
160
+ /// KEYCHAIN
161
+ fs .StringVar (& o .Client .KeyChain .Namespace ,
162
+ "keychain-namespace" , "" ,
163
+ fmt .Sprintf (
164
+ "Namespace inside of which service account and imagepullsecrets belong too (%s_%s)." ,
165
+ envPrefix , envKeychainNamespace ,
166
+ ))
167
+
168
+ fs .StringVar (& o .Client .KeyChain .ServiceAccountName ,
169
+ "keychain-service-account" , "" ,
170
+ fmt .Sprintf (
171
+ "ServiceAccount used to fetch Image Pull Secrets from (%s_%s)." ,
172
+ envPrefix , envKeychainServiceAccountName ,
173
+ ))
174
+
175
+ fs .StringSliceVar (& o .Client .KeyChain .ImagePullSecrets ,
176
+ "keychain-image-pull-secrets" , []string {},
177
+ fmt .Sprintf (
178
+ "Set of image pull secrets to include during authentication (%s_%s)." ,
179
+ envPrefix , envKeychainImagePullSecrets ,
180
+ ))
181
+
182
+ fs .BoolVar (& o .Client .KeyChain .UseMountSecrets ,
183
+ "keychain-use-mount-secrets" , false ,
184
+ fmt .Sprintf ("Include Mount Secrets during discovery (%s_%s)." ,
185
+ envPrefix , envKeychainUseMountSecrets ,
186
+ ))
187
+ fs .DurationVar (& o .Client .AuthRefreshDuration ,
188
+ "keychain-refresh-duration" , time .Hour ,
189
+ fmt .Sprintf ("Duration credentials are refreshed (%s_%s)." ,
190
+ envPrefix , envKeychainRefreshDuration ,
191
+ ))
192
+
152
193
/// ACR
153
194
fs .StringVar (& o .Client .ACR .Username ,
154
195
"acr-username" , "" ,
155
196
fmt .Sprintf (
156
197
"Username to authenticate with azure container registry (%s_%s)." ,
157
198
envPrefix , envACRUsername ,
158
199
))
200
+ _ = fs .MarkDeprecated ("acr-username" , "use keychain instead" )
159
201
fs .StringVar (& o .Client .ACR .Password ,
160
202
"acr-password" , "" ,
161
203
fmt .Sprintf (
162
204
"Password to authenticate with azure container registry (%s_%s)." ,
163
205
envPrefix , envACRPassword ,
164
206
))
207
+ _ = fs .MarkDeprecated ("acr-password" , "use keychain instead" )
165
208
fs .StringVar (& o .Client .ACR .RefreshToken ,
166
209
"acr-refresh-token" , "" ,
167
210
fmt .Sprintf (
168
211
"Refresh token to authenticate with azure container registry. Cannot be used with " +
169
212
"username/password (%s_%s)." ,
170
213
envPrefix , envACRRefreshToken ,
171
214
))
215
+ _ = fs .MarkDeprecated ("acr-refresh-token" , "use keychain instead" )
172
216
fs .StringVar (& o .Client .ACR .JWKSURI ,
173
217
"acr-jwks-uri" , "" ,
174
218
fmt .Sprintf (
@@ -184,19 +228,22 @@ func (o *Options) addAuthFlags(fs *pflag.FlagSet) {
184
228
"Username to authenticate with docker registry (%s_%s)." ,
185
229
envPrefix , envDockerUsername ,
186
230
))
231
+ _ = fs .MarkDeprecated ("docker-username" , "use keychain instead" )
187
232
fs .StringVar (& o .Client .Docker .Password ,
188
233
"docker-password" , "" ,
189
234
fmt .Sprintf (
190
235
"Password to authenticate with docker registry (%s_%s)." ,
191
236
envPrefix , envDockerPassword ,
192
237
))
238
+ _ = fs .MarkDeprecated ("docker-password" , "use keychain instead" )
193
239
fs .StringVar (& o .Client .Docker .Token ,
194
240
"docker-token" , "" ,
195
241
fmt .Sprintf (
196
242
"Token to authenticate with docker registry. Cannot be used with " +
197
243
"username/password (%s_%s)." ,
198
244
envPrefix , envDockerToken ,
199
245
))
246
+ _ = fs .MarkDeprecated ("docker-token" , "use keychain instead" )
200
247
///
201
248
202
249
/// ECR
@@ -233,6 +280,7 @@ func (o *Options) addAuthFlags(fs *pflag.FlagSet) {
233
280
"Access token for read access to private GCR registries (%s_%s)." ,
234
281
envPrefix , envGCRAccessToken ,
235
282
))
283
+ _ = fs .MarkDeprecated ("gcr-token" , "use keychain instead" )
236
284
///
237
285
238
286
/// GHCR
@@ -242,6 +290,7 @@ func (o *Options) addAuthFlags(fs *pflag.FlagSet) {
242
290
"Personal Access token for read access to GHCR releases (%s_%s)." ,
243
291
envPrefix , envGHCRAccessToken ,
244
292
))
293
+ _ = fs .MarkDeprecated ("gchr-token" , "use keychain instead" )
245
294
fs .StringVar (& o .Client .GHCR .Hostname ,
246
295
"gchr-hostname" , "" ,
247
296
fmt .Sprintf (
@@ -257,6 +306,7 @@ func (o *Options) addAuthFlags(fs *pflag.FlagSet) {
257
306
"Access token for read access to private Quay registries (%s_%s)." ,
258
307
envPrefix , envQuayToken ,
259
308
))
309
+ _ = fs .MarkDeprecated ("quay-token" , "use keychain instead" )
260
310
///
261
311
262
312
/// Selfhosted
@@ -266,19 +316,22 @@ func (o *Options) addAuthFlags(fs *pflag.FlagSet) {
266
316
"Username is authenticate with a selfhosted registry (%s_%s_%s)." ,
267
317
envPrefix , envSelfhostedPrefix , envSelfhostedUsername ,
268
318
))
319
+ _ = fs .MarkDeprecated ("selfhosted-username" , "use keychain instead" )
269
320
fs .StringVar (& o .selfhosted .Password ,
270
321
"selfhosted-password" , "" ,
271
322
fmt .Sprintf (
272
323
"Password is authenticate with a selfhosted registry (%s_%s_%s)." ,
273
324
envPrefix , envSelfhostedPrefix , envSelfhostedPassword ,
274
325
))
326
+ _ = fs .MarkDeprecated ("selfhosted-password" , "use keychain instead" )
275
327
fs .StringVar (& o .selfhosted .Bearer ,
276
328
"selfhosted-token" , "" ,
277
329
fmt .Sprintf (
278
330
"Token to authenticate to a selfhosted registry. Cannot be used with " +
279
331
"username/password (%s_%s_%s)." ,
280
332
envPrefix , envSelfhostedPrefix , envSelfhostedBearer ,
281
333
))
334
+ _ = fs .MarkDeprecated ("selfhosted-token" , "use keychain instead" )
282
335
fs .StringVar (& o .selfhosted .TokenPath ,
283
336
"selfhosted-token-path" , "" ,
284
337
fmt .Sprintf (
@@ -305,12 +358,9 @@ func (o *Options) addAuthFlags(fs *pflag.FlagSet) {
305
358
"THIS IS NOT RECOMMENDED AND IS INTENDED FOR DEBUGGING (%s_%s_%s)" ,
306
359
envPrefix , envSelfhostedPrefix , envSelfhostedInsecure ,
307
360
))
308
- // if !validSelfHostedOpts(o) {
309
- // panic(fmt.Errorf("invalid self hosted configuration"))
310
- // }
311
361
}
312
362
313
- func (o * Options ) complete () {
363
+ func (o * Options ) complete () error {
314
364
o .Client .Selfhosted = make (map [string ]* selfhosted.Options )
315
365
316
366
envs := os .Environ ()
@@ -338,6 +388,9 @@ func (o *Options) complete() {
338
388
{envGHCRHostname , & o .Client .GHCR .Hostname },
339
389
340
390
{envQuayToken , & o .Client .Quay .Token },
391
+
392
+ {envKeychainNamespace , & o .Client .KeyChain .Namespace },
393
+ {envKeychainServiceAccountName , & o .Client .KeyChain .ServiceAccountName },
341
394
} {
342
395
for _ , env := range envs {
343
396
if o .assignEnv (env , opt .key , opt .assign ) {
@@ -346,7 +399,7 @@ func (o *Options) complete() {
346
399
}
347
400
}
348
401
349
- o .assignSelfhosted (envs )
402
+ return o .assignSelfhosted (envs )
350
403
}
351
404
352
405
func (o * Options ) assignEnv (env , key string , assign * string ) bool {
@@ -363,7 +416,24 @@ func (o *Options) assignEnv(env, key string, assign *string) bool {
363
416
return false
364
417
}
365
418
366
- func (o * Options ) assignSelfhosted (envs []string ) {
419
+ // assignSelfhosted processes a list of environment variables and assigns
420
+ // self-hosted configuration options to the Options struct. It parses the
421
+ // environment variables using predefined regular expressions to extract
422
+ // self-hosted configuration details such as token path, bearer token, host,
423
+ // username, password, insecure flag, and CA path.
424
+ //
425
+ // The function ensures that each self-hosted configuration is initialized
426
+ // before assigning values. It also validates the self-hosted options after
427
+ // processing all environment variables.
428
+ //
429
+ // Parameters:
430
+ // - envs: A slice of strings representing environment variables in the
431
+ // format "KEY=VALUE".
432
+ //
433
+ // Returns:
434
+ // - error: An error if validation of the self-hosted options fails, or nil
435
+ // if the operation is successful.
436
+ func (o * Options ) assignSelfhosted (envs []string ) error {
367
437
if o .Client .Selfhosted == nil {
368
438
o .Client .Selfhosted = make (map [string ]* selfhosted.Options )
369
439
}
@@ -451,26 +521,40 @@ func (o *Options) assignSelfhosted(envs []string) {
451
521
o .Client .Selfhosted [o .selfhosted .Host ] = & o .selfhosted
452
522
}
453
523
454
- if ! validSelfHostedOpts (o ) {
455
- panic (fmt .Errorf ("invalid self hosted configuration" ))
456
- }
524
+ return validateSelfHostedOpts (o )
457
525
}
458
526
459
- func validSelfHostedOpts (opts * Options ) bool {
527
+ // validateSelfHostedOpts validates the self-hosted options provided in the
528
+ // Options struct. It checks both the options set using environment variables
529
+ // and those set using flags.
530
+ //
531
+ // For options set using environment variables, it iterates through the list
532
+ // of self-hosted options and ensures that each host is valid.
533
+ //
534
+ // For options set using flags, it validates the host in the selfhosted.Options
535
+ // struct.
536
+ //
537
+ // Returns an error if any of the self-hosted options contain an invalid host,
538
+ // otherwise returns nil.
539
+ func validateSelfHostedOpts (opts * Options ) error {
460
540
// opts set using env vars
461
541
if opts .Client .Selfhosted != nil {
462
- for _ , selfHostedOpts := range opts .Client .Selfhosted {
463
- return isValidOption (selfHostedOpts .Host , "" )
542
+ for name , selfHostedOpts := range opts .Client .Selfhosted {
543
+ if err := isValidOption (selfHostedOpts .Host , "" ); ! err {
544
+ return fmt .Errorf ("invalid self-hosted option for: %s" , name )
545
+ }
464
546
}
465
547
}
466
548
467
549
// opts set using flags
468
550
if opts .selfhosted != (selfhosted.Options {}) {
469
- return isValidOption (opts .selfhosted .Host , "" )
551
+ if ! isValidOption (opts .selfhosted .Host , "" ) {
552
+ return fmt .Errorf ("invalid self-hosted option for host: %s" , opts .selfhosted .Host )
553
+ }
470
554
}
471
- return true
555
+ return nil
472
556
}
473
557
474
- func isValidOption (option , invalid string ) bool {
558
+ func isValidOption (option , invalid any ) bool {
475
559
return option != invalid
476
560
}
0 commit comments