@@ -3,11 +3,11 @@ package agent
3
3
import (
4
4
"fmt"
5
5
"io"
6
- "log"
7
6
"net/url"
8
7
"os"
9
8
"time"
10
9
10
+ "github.com/go-logr/logr"
11
11
"github.com/hashicorp/go-multierror"
12
12
"github.com/pkg/errors"
13
13
"github.com/spf13/cobra"
@@ -355,32 +355,35 @@ type CombinedConfig struct {
355
355
// The error returned may be a multierror.Error. Use multierror.Prefix(err,
356
356
// "context:") rather than fmt.Errorf("context: %w", err) when wrapping the
357
357
// error.
358
- func ValidateAndCombineConfig (log * log .Logger , cfg Config , flags AgentCmdFlags ) (CombinedConfig , client.Client , error ) {
358
+ func ValidateAndCombineConfig (log logr .Logger , cfg Config , flags AgentCmdFlags ) (CombinedConfig , client.Client , error ) {
359
359
res := CombinedConfig {}
360
360
var errs error
361
361
362
362
{
363
- var mode AuthMode
363
+ var (
364
+ mode AuthMode
365
+ reason string
366
+ )
364
367
switch {
365
368
case flags .VenafiCloudMode && flags .CredentialsPath != "" :
366
369
mode = VenafiCloudKeypair
367
- log . Printf ("Using the %s auth mode since --venafi-cloud and --credentials-path were specified." , mode )
370
+ reason = fmt . Sprintf ("Using the %s auth mode since --venafi-cloud and --credentials-path were specified." , mode )
368
371
case flags .ClientID != "" && flags .PrivateKeyPath != "" :
369
372
mode = VenafiCloudKeypair
370
- log . Printf ("Using the %s auth mode since --client-id and --private-key-path were specified." , mode )
373
+ reason = fmt . Sprintf ("Using the %s auth mode since --client-id and --private-key-path were specified." , mode )
371
374
case flags .ClientID != "" :
372
375
return CombinedConfig {}, nil , fmt .Errorf ("if --client-id is specified, --private-key-path must also be specified" )
373
376
case flags .PrivateKeyPath != "" :
374
377
return CombinedConfig {}, nil , fmt .Errorf ("--private-key-path is specified, --client-id must also be specified" )
375
378
case flags .VenConnName != "" :
376
379
mode = VenafiCloudVenafiConnection
377
- log . Printf ("Using the %s auth mode since --venafi-connection was specified." , mode )
380
+ reason = fmt . Sprintf ("Using the %s auth mode since --venafi-connection was specified." , mode )
378
381
case flags .APIToken != "" :
379
382
mode = JetstackSecureAPIToken
380
- log . Printf ("Using the %s auth mode since --api-token was specified." , mode )
383
+ reason = fmt . Sprintf ("Using the %s auth mode since --api-token was specified." , mode )
381
384
case ! flags .VenafiCloudMode && flags .CredentialsPath != "" :
382
385
mode = JetstackSecureOAuth
383
- log . Printf ("Using the %s auth mode since --credentials-file was specified without --venafi-cloud." , mode )
386
+ reason = fmt . Sprintf ("Using the %s auth mode since --credentials-file was specified without --venafi-cloud." , mode )
384
387
default :
385
388
return CombinedConfig {}, nil , fmt .Errorf ("no auth mode specified. You can use one of four auth modes:\n " +
386
389
" - Use (--venafi-cloud with --credentials-file) or (--client-id with --private-key-path) to use the " + string (VenafiCloudKeypair ) + " mode.\n " +
@@ -389,6 +392,7 @@ func ValidateAndCombineConfig(log *log.Logger, cfg Config, flags AgentCmdFlags)
389
392
" - Use --api-token if you want to use the " + string (JetstackSecureAPIToken ) + " mode.\n " )
390
393
}
391
394
res .AuthMode = mode
395
+ log .Info (reason )
392
396
}
393
397
394
398
// Validation and defaulting of `server` and the deprecated `endpoint.path`.
@@ -403,10 +407,10 @@ func ValidateAndCombineConfig(log *log.Logger, cfg Config, flags AgentCmdFlags)
403
407
case hasServerField && hasEndpointField :
404
408
// The `server` field takes precedence over the deprecated
405
409
// `endpoint` field.
406
- log .Printf ("The `server` and `endpoint` fields are both set in the config; using the `server` field." )
410
+ log .Info ("The `server` and `endpoint` fields are both set in the config; using the `server` field." )
407
411
server = cfg .Server
408
412
case ! hasServerField && hasEndpointField :
409
- log .Printf ("Using deprecated Endpoint configuration. User Server instead." )
413
+ log .Info ("Using deprecated Endpoint configuration. User Server instead." )
410
414
if cfg .Endpoint .Protocol == "" && cfg .Server == "" {
411
415
cfg .Endpoint .Protocol = "http"
412
416
}
@@ -424,7 +428,7 @@ func ValidateAndCombineConfig(log *log.Logger, cfg Config, flags AgentCmdFlags)
424
428
errs = multierror .Append (errs , fmt .Errorf ("server %q is not a valid URL" , server ))
425
429
}
426
430
if res .AuthMode == VenafiCloudVenafiConnection && server != "" {
427
- log .Printf ( "ignoring the server field specified in the config file. In %s mode, this field is not needed." , VenafiCloudVenafiConnection )
431
+ log .Info ( fmt . Sprintf ( "ignoring the server field specified in the config file. In %s mode, this field is not needed." , VenafiCloudVenafiConnection ) )
428
432
server = ""
429
433
}
430
434
res .Server = server
@@ -454,7 +458,7 @@ func ValidateAndCombineConfig(log *log.Logger, cfg Config, flags AgentCmdFlags)
454
458
// change this value with the new --venafi-connection flag, and this
455
459
// field is simply ignored.
456
460
if cfg .VenafiCloud != nil && cfg .VenafiCloud .UploadPath != "" {
457
- log .Printf ( `ignoring the venafi-cloud.upload_path field in the config file. In %s mode, this field is not needed.` , res .AuthMode )
461
+ log .Info ( fmt . Sprintf ( `ignoring the venafi-cloud.upload_path field in the config file. In %s mode, this field is not needed.` , res .AuthMode ) )
458
462
}
459
463
uploadPath = ""
460
464
}
@@ -472,7 +476,7 @@ func ValidateAndCombineConfig(log *log.Logger, cfg Config, flags AgentCmdFlags)
472
476
// https://venafi.atlassian.net/browse/VC-35385 is done.
473
477
{
474
478
if cfg .VenafiCloud != nil && cfg .VenafiCloud .UploaderID != "" {
475
- log .Printf ( `ignoring the venafi-cloud.uploader_id field in the config file. This field is not needed in %s mode.` , res .AuthMode )
479
+ log .Info ( fmt . Sprintf ( `ignoring the venafi-cloud.uploader_id field in the config file. This field is not needed in %s mode.` , res .AuthMode ) )
476
480
}
477
481
}
478
482
@@ -524,13 +528,13 @@ func ValidateAndCombineConfig(log *log.Logger, cfg Config, flags AgentCmdFlags)
524
528
case flags .Period == 0 && cfg .Period == 0 :
525
529
errs = multierror .Append (errs , fmt .Errorf ("period must be set using --period or -p, or using the 'period' field in the config file" ))
526
530
case flags .Period == 0 && cfg .Period > 0 :
527
- log .Printf ("Using period from config %s " , cfg .Period )
531
+ log .Info ("Using period from config" , "period " , cfg .Period )
528
532
period = cfg .Period
529
533
case flags .Period > 0 && cfg .Period == 0 :
530
534
period = flags .Period
531
535
case flags .Period > 0 && cfg .Period > 0 :
532
536
// The flag takes precedence.
533
- log .Printf ("Both the 'period' field and --period are set. Using the value provided with --period." )
537
+ log .Info ("Both the 'period' field and --period are set. Using the value provided with --period." )
534
538
period = flags .Period
535
539
}
536
540
res .Period = period
@@ -599,7 +603,7 @@ func ValidateAndCombineConfig(log *log.Logger, cfg Config, flags AgentCmdFlags)
599
603
// The error returned may be a multierror.Error. Use multierror.Prefix(err,
600
604
// "context:") rather than fmt.Errorf("context: %w", err) when wrapping the
601
605
// error.
602
- func validateCredsAndCreateClient (log * log .Logger , flagCredentialsPath , flagClientID , flagPrivateKeyPath , flagAPIToken string , cfg CombinedConfig ) (client.Client , error ) {
606
+ func validateCredsAndCreateClient (log logr .Logger , flagCredentialsPath , flagClientID , flagPrivateKeyPath , flagAPIToken string , cfg CombinedConfig ) (client.Client , error ) {
603
607
var errs error
604
608
605
609
var preflightClient client.Client
@@ -719,7 +723,7 @@ func ValidateDataGatherers(dataGatherers []DataGatherer) error {
719
723
720
724
// The error returned may be a multierror.Error. Instead of adding context to
721
725
// the error with fmt.Errorf("%w", err), use multierror.Prefix(err, "context").
722
- func createCredentialClient (log * log .Logger , credentials client.Credentials , cfg CombinedConfig , agentMetadata * api.AgentMetadata ) (client.Client , error ) {
726
+ func createCredentialClient (log logr .Logger , credentials client.Credentials , cfg CombinedConfig , agentMetadata * api.AgentMetadata ) (client.Client , error ) {
723
727
switch creds := credentials .(type ) {
724
728
case * client.VenafiSvcAccountCredentials :
725
729
// The uploader ID isn't actually used in the backend, let's use an
@@ -730,7 +734,7 @@ func createCredentialClient(log *log.Logger, credentials client.Credentials, cfg
730
734
if cfg .AuthMode == VenafiCloudKeypair {
731
735
// We don't do this for the VenafiCloudVenafiConnection mode because
732
736
// the upload_path field is ignored in that mode.
733
- log .Println ("Loading upload_path from \" venafi-cloud\" configuration." )
737
+ log .Info ("Loading upload_path from \" venafi-cloud\" configuration." )
734
738
uploadPath = cfg .UploadPath
735
739
}
736
740
return client .NewVenafiCloudClient (agentMetadata , creds , cfg .Server , uploaderID , uploadPath , cfg .DisableCompression )
0 commit comments