Skip to content

Commit df72cd9

Browse files
authored
Merge pull request #661 from jetstack/cleanup_part1
Cleanup: config.go
2 parents 5aab733 + 84570e7 commit df72cd9

File tree

1 file changed

+24
-44
lines changed

1 file changed

+24
-44
lines changed

pkg/agent/config.go

Lines changed: 24 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -426,11 +426,9 @@ type CombinedConfig struct {
426426
// error.
427427
func ValidateAndCombineConfig(log logr.Logger, cfg Config, flags AgentCmdFlags) (CombinedConfig, client.Client, error) {
428428
res := CombinedConfig{}
429-
var errs error
430429

431430
if flags.MachineHubMode {
432-
err := cfg.MachineHub.Validate()
433-
if err != nil {
431+
if err := cfg.MachineHub.Validate(); err != nil {
434432
return CombinedConfig{}, nil, fmt.Errorf("invalid MachineHub config provided: %w", err)
435433
}
436434

@@ -453,14 +451,17 @@ func ValidateAndCombineConfig(log logr.Logger, cfg Config, flags AgentCmdFlags)
453451
mode = VenafiCloudKeypair
454452
reason = "--venafi-cloud and --credentials-path were specified"
455453
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+
457462
mode = VenafiCloudKeypair
458463
reason = "--client-id and --private-key-path were specified"
459464
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")
464465
case flags.VenConnName != "":
465466
mode = VenafiCloudVenafiConnection
466467
reason = "--venafi-connection was specified"
@@ -493,6 +494,8 @@ func ValidateAndCombineConfig(log logr.Logger, cfg Config, flags AgentCmdFlags)
493494
res.TLSPKMode = mode
494495
}
495496

497+
var errs error
498+
496499
// Validation and defaulting of `server` and the deprecated `endpoint.path`.
497500
if res.TLSPKMode != Off {
498501
// Only relevant if using TLSPK backends
@@ -584,12 +587,7 @@ func ValidateAndCombineConfig(log logr.Logger, cfg Config, flags AgentCmdFlags)
584587
var clusterID string
585588
var organizationID string // Only used by the old jetstack-secure mode.
586589
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:
593591
if cfg.ClusterID == "" {
594592
errs = multierror.Append(errs, fmt.Errorf("cluster_id is required in %s mode", res.TLSPKMode))
595593
}
@@ -609,8 +607,7 @@ func ValidateAndCombineConfig(log logr.Logger, cfg Config, flags AgentCmdFlags)
609607
res.ClusterDescription = cfg.ClusterDescription
610608

611609
// Validation of `data-gatherers`.
612-
dgErr := ValidateDataGatherers(cfg.DataGatherers)
613-
if dgErr != nil {
610+
if dgErr := ValidateDataGatherers(cfg.DataGatherers); dgErr != nil {
614611
errs = multierror.Append(errs, dgErr)
615612
}
616613
res.DataGatherers = cfg.DataGatherers
@@ -736,12 +733,12 @@ func validateCredsAndCreateClient(log logr.Logger, flagCredentialsPath, flagClie
736733
break // Don't continue with the client if credentials file invalid.
737734
}
738735

739-
preflightClient, err = createCredentialClient(log, creds, cfg, metadata)
736+
preflightClient, err = client.NewOAuthClient(metadata, creds, cfg.Server)
740737
if err != nil {
741738
errs = multierror.Append(errs, err)
742739
}
743740
case VenafiCloudKeypair:
744-
var creds client.Credentials
741+
var creds *client.VenafiSvcAccountCredentials
745742

746743
if flagClientID != "" && flagCredentialsPath != "" {
747744
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
779776
return nil, fmt.Errorf("programmer mistake: --client-id and --private-key-path or --credentials-file must have been provided")
780777
}
781778

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+
782787
var err error
783-
preflightClient, err = createCredentialClient(log, creds, cfg, metadata)
788+
preflightClient, err = client.NewVenafiCloudClient(metadata, creds, cfg.Server, uploaderID, cfg.UploadPath)
784789
if err != nil {
785790
errs = multierror.Append(errs, err)
786791
}
@@ -836,31 +841,6 @@ func ValidateDataGatherers(dataGatherers []DataGatherer) error {
836841
return err
837842
}
838843

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-
864844
// Inspired by the controller-runtime project.
865845
func getInClusterNamespace() (string, error) {
866846
ns := os.Getenv("POD_NAMESPACE")

0 commit comments

Comments
 (0)