@@ -426,11 +426,9 @@ type CombinedConfig struct {
426
426
// error.
427
427
func ValidateAndCombineConfig (log logr.Logger , cfg Config , flags AgentCmdFlags ) (CombinedConfig , client.Client , error ) {
428
428
res := CombinedConfig {}
429
- var errs error
430
429
431
430
if flags .MachineHubMode {
432
- err := cfg .MachineHub .Validate ()
433
- if err != nil {
431
+ if err := cfg .MachineHub .Validate (); err != nil {
434
432
return CombinedConfig {}, nil , fmt .Errorf ("invalid MachineHub config provided: %w" , err )
435
433
}
436
434
@@ -453,14 +451,17 @@ func ValidateAndCombineConfig(log logr.Logger, cfg Config, flags AgentCmdFlags)
453
451
mode = VenafiCloudKeypair
454
452
reason = "--venafi-cloud and --credentials-path were specified"
455
453
keysAndValues = []any {"credentialsPath" , flags .CredentialsPath }
456
- case flags .ClientID != "" && flags .PrivateKeyPath != "" :
454
+ case flags .ClientID != "" || flags .PrivateKeyPath != "" :
455
+ if flags .PrivateKeyPath == "" {
456
+ return CombinedConfig {}, nil , fmt .Errorf ("if --client-id is specified, --private-key-path must also be specified" )
457
+ }
458
+ if flags .ClientID == "" {
459
+ return CombinedConfig {}, nil , fmt .Errorf ("--private-key-path is specified, --client-id must also be specified" )
460
+ }
461
+
457
462
mode = VenafiCloudKeypair
458
463
reason = "--client-id and --private-key-path were specified"
459
464
keysAndValues = []any {"clientID" , flags .ClientID , "privateKeyPath" , flags .PrivateKeyPath }
460
- case flags .ClientID != "" :
461
- return CombinedConfig {}, nil , fmt .Errorf ("if --client-id is specified, --private-key-path must also be specified" )
462
- case flags .PrivateKeyPath != "" :
463
- return CombinedConfig {}, nil , fmt .Errorf ("--private-key-path is specified, --client-id must also be specified" )
464
465
case flags .VenConnName != "" :
465
466
mode = VenafiCloudVenafiConnection
466
467
reason = "--venafi-connection was specified"
@@ -493,6 +494,8 @@ func ValidateAndCombineConfig(log logr.Logger, cfg Config, flags AgentCmdFlags)
493
494
res .TLSPKMode = mode
494
495
}
495
496
497
+ var errs error
498
+
496
499
// Validation and defaulting of `server` and the deprecated `endpoint.path`.
497
500
if res .TLSPKMode != Off {
498
501
// Only relevant if using TLSPK backends
@@ -584,12 +587,7 @@ func ValidateAndCombineConfig(log logr.Logger, cfg Config, flags AgentCmdFlags)
584
587
var clusterID string
585
588
var organizationID string // Only used by the old jetstack-secure mode.
586
589
switch res .TLSPKMode { // nolint:exhaustive
587
- case VenafiCloudKeypair :
588
- if cfg .ClusterID == "" {
589
- errs = multierror .Append (errs , fmt .Errorf ("cluster_id is required in %s mode" , res .TLSPKMode ))
590
- }
591
- clusterID = cfg .ClusterID
592
- case VenafiCloudVenafiConnection :
590
+ case VenafiCloudKeypair , VenafiCloudVenafiConnection :
593
591
if cfg .ClusterID == "" {
594
592
errs = multierror .Append (errs , fmt .Errorf ("cluster_id is required in %s mode" , res .TLSPKMode ))
595
593
}
@@ -609,8 +607,7 @@ func ValidateAndCombineConfig(log logr.Logger, cfg Config, flags AgentCmdFlags)
609
607
res .ClusterDescription = cfg .ClusterDescription
610
608
611
609
// Validation of `data-gatherers`.
612
- dgErr := ValidateDataGatherers (cfg .DataGatherers )
613
- if dgErr != nil {
610
+ if dgErr := ValidateDataGatherers (cfg .DataGatherers ); dgErr != nil {
614
611
errs = multierror .Append (errs , dgErr )
615
612
}
616
613
res .DataGatherers = cfg .DataGatherers
@@ -736,12 +733,12 @@ func validateCredsAndCreateClient(log logr.Logger, flagCredentialsPath, flagClie
736
733
break // Don't continue with the client if credentials file invalid.
737
734
}
738
735
739
- preflightClient , err = createCredentialClient ( log , creds , cfg , metadata )
736
+ preflightClient , err = client . NewOAuthClient ( metadata , creds , cfg . Server )
740
737
if err != nil {
741
738
errs = multierror .Append (errs , err )
742
739
}
743
740
case VenafiCloudKeypair :
744
- var creds client.Credentials
741
+ var creds * client.VenafiSvcAccountCredentials
745
742
746
743
if flagClientID != "" && flagCredentialsPath != "" {
747
744
errs = multierror .Append (errs , fmt .Errorf ("--client-id and --credentials-file cannot be used simultaneously" ))
@@ -779,8 +776,16 @@ func validateCredsAndCreateClient(log logr.Logger, flagCredentialsPath, flagClie
779
776
return nil , fmt .Errorf ("programmer mistake: --client-id and --private-key-path or --credentials-file must have been provided" )
780
777
}
781
778
779
+ // The uploader ID isn't actually used in the backend, let's use an
780
+ // arbitrary value.
781
+ uploaderID := "no"
782
+
783
+ // We don't do this for the VenafiCloudVenafiConnection mode because
784
+ // the upload_path field is ignored in that mode.
785
+ log .Info ("Loading upload_path from \" venafi-cloud\" configuration." )
786
+
782
787
var err error
783
- preflightClient , err = createCredentialClient ( log , creds , cfg , metadata )
788
+ preflightClient , err = client . NewVenafiCloudClient ( metadata , creds , cfg . Server , uploaderID , cfg . UploadPath )
784
789
if err != nil {
785
790
errs = multierror .Append (errs , err )
786
791
}
@@ -836,31 +841,6 @@ func ValidateDataGatherers(dataGatherers []DataGatherer) error {
836
841
return err
837
842
}
838
843
839
- // The error returned may be a multierror.Error. Instead of adding context to
840
- // the error with fmt.Errorf("%w", err), use multierror.Prefix(err, "context").
841
- func createCredentialClient (log logr.Logger , credentials client.Credentials , cfg CombinedConfig , agentMetadata * api.AgentMetadata ) (client.Client , error ) {
842
- switch creds := credentials .(type ) {
843
- case * client.VenafiSvcAccountCredentials :
844
- // The uploader ID isn't actually used in the backend, let's use an
845
- // arbitrary value.
846
- uploaderID := "no"
847
-
848
- var uploadPath string
849
- if cfg .TLSPKMode == VenafiCloudKeypair {
850
- // We don't do this for the VenafiCloudVenafiConnection mode because
851
- // the upload_path field is ignored in that mode.
852
- log .Info ("Loading upload_path from \" venafi-cloud\" configuration." )
853
- uploadPath = cfg .UploadPath
854
- }
855
- return client .NewVenafiCloudClient (agentMetadata , creds , cfg .Server , uploaderID , uploadPath )
856
-
857
- case * client.OAuthCredentials :
858
- return client .NewOAuthClient (agentMetadata , creds , cfg .Server )
859
- default :
860
- return nil , errors .New ("credentials file is in unknown format" )
861
- }
862
- }
863
-
864
844
// Inspired by the controller-runtime project.
865
845
func getInClusterNamespace () (string , error ) {
866
846
ns := os .Getenv ("POD_NAMESPACE" )
0 commit comments