From 0b050171c7cb4b50fae33b01ffccc1023febf1c8 Mon Sep 17 00:00:00 2001 From: ShocOne <62835948+ShocOne@users.noreply.github.com> Date: Thu, 28 Mar 2024 12:16:53 +0000 Subject: [PATCH 1/2] Add environment and client options validation*** --- httpclient/httpclient_client_configuration.go | 40 ++++++++++++++----- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/httpclient/httpclient_client_configuration.go b/httpclient/httpclient_client_configuration.go index 041a2a0..aa3f694 100644 --- a/httpclient/httpclient_client_configuration.go +++ b/httpclient/httpclient_client_configuration.go @@ -68,6 +68,12 @@ func LoadConfigFromEnv(config *ClientConfig) (*ClientConfig, error) { } // AuthConfig + config.Auth.Username = getEnvOrDefault("USERNAME", config.Auth.Username) + log.Printf("Username env value found and set to: %s", config.Auth.Username) + + config.Auth.Password = getEnvOrDefault("PASSWORD", config.Auth.Password) + log.Printf("Password env value found and set") + config.Auth.ClientID = getEnvOrDefault("CLIENT_ID", config.Auth.ClientID) log.Printf("ClientID env value found and set to: %s", config.Auth.ClientID) @@ -168,19 +174,15 @@ func parseDuration(value string, defaultVal time.Duration) time.Duration { func validateMandatoryConfiguration(config *ClientConfig) error { var missingFields []string - // Check for missing mandatory fields and add them to the missingFields slice if necessary. - if config.Auth.ClientID == "" { - missingFields = append(missingFields, "Auth.ClientID") - } - if config.Auth.ClientSecret == "" { - missingFields = append(missingFields, "Auth.ClientSecret") - } + // Check for mandatory fields related to the environment if config.Environment.InstanceName == "" { missingFields = append(missingFields, "Environment.InstanceName") } if config.Environment.APIType == "" { missingFields = append(missingFields, "Environment.APIType") } + + // Check for mandatory fields related to the client options if config.ClientOptions.LogLevel == "" { missingFields = append(missingFields, "ClientOptions.LogLevel") } @@ -191,12 +193,32 @@ func validateMandatoryConfiguration(config *ClientConfig) error { missingFields = append(missingFields, "ClientOptions.LogConsoleSeparator") } - // If there are missing fields, return an error detailing what is missing. + // Check for either OAuth credentials pair or Username and Password pair + usingOAuth := config.Auth.ClientID != "" && config.Auth.ClientSecret != "" + usingBasicAuth := config.Auth.Username != "" && config.Auth.Password != "" + + if !(usingOAuth || usingBasicAuth) { + if config.Auth.ClientID == "" { + missingFields = append(missingFields, "Auth.ClientID") + } + if config.Auth.ClientSecret == "" { + missingFields = append(missingFields, "Auth.ClientSecret") + } + if config.Auth.Username == "" { + missingFields = append(missingFields, "Auth.Username") + } + if config.Auth.Password == "" { + missingFields = append(missingFields, "Auth.Password") + } + return fmt.Errorf("either OAuth credentials (ClientID and ClientSecret) or Basic Auth credentials (Username and Password) must be provided") + } + + // If there are missing fields, return an error detailing what is missing if len(missingFields) > 0 { return fmt.Errorf("mandatory configuration missing: %s", strings.Join(missingFields, ", ")) } - // If no fields are missing, return nil indicating the configuration is complete. + // If no fields are missing, return nil indicating the configuration is complete return nil } From fd8afe53b65b9138de5361a874457dc9754fb711 Mon Sep 17 00:00:00 2001 From: ShocOne <62835948+ShocOne@users.noreply.github.com> Date: Thu, 28 Mar 2024 12:18:46 +0000 Subject: [PATCH 2/2] Refactor error message in validateMandatoryConfiguration function --- httpclient/httpclient_client_configuration.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/httpclient/httpclient_client_configuration.go b/httpclient/httpclient_client_configuration.go index aa3f694..63b1688 100644 --- a/httpclient/httpclient_client_configuration.go +++ b/httpclient/httpclient_client_configuration.go @@ -210,12 +210,12 @@ func validateMandatoryConfiguration(config *ClientConfig) error { if config.Auth.Password == "" { missingFields = append(missingFields, "Auth.Password") } - return fmt.Errorf("either OAuth credentials (ClientID and ClientSecret) or Basic Auth credentials (Username and Password) must be provided") } - // If there are missing fields, return an error detailing what is missing + // If there are missing fields, construct and return an error message detailing what is missing if len(missingFields) > 0 { - return fmt.Errorf("mandatory configuration missing: %s", strings.Join(missingFields, ", ")) + errorMessage := fmt.Sprintf("Mandatory configuration missing: %s. Ensure that either OAuth credentials (ClientID and ClientSecret) or Basic Auth credentials (Username and Password) are fully provided.", strings.Join(missingFields, ", ")) + return fmt.Errorf(errorMessage) } // If no fields are missing, return nil indicating the configuration is complete