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