From 4a0e95746b530799e06a7fcc5b6c823cd12ecdcc Mon Sep 17 00:00:00 2001 From: Rajat Bajaj Date: Thu, 31 Oct 2024 12:19:50 +0530 Subject: [PATCH] Added explicit check for handling missing env variables for login (#1065) * Added explicit check for handling missing env variables for login * Minor changes * Minor update to test * Updated test case * Updated test case --- internal/config/config.go | 11 +++++++++++ internal/config/config_test.go | 7 ++++--- internal/provider/provider.go | 28 +--------------------------- 3 files changed, 16 insertions(+), 30 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index 29bc15f41..372f19ce5 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -62,6 +62,17 @@ func ConfigureProvider(terraformVersion *string) schema.ConfigureContextFunc { audience := data.Get("audience").(string) debug := data.Get("debug").(bool) + if apiToken == "" && (clientID == "" || clientSecret == "" || domain == "") { + return nil, diag.Diagnostics{ + { + Severity: diag.Error, + Summary: "Missing environment variables", + Detail: fmt.Sprintf("Either AUTH0_API_TOKEN or AUTH0_DOMAIN:AUTH0_CLIENT_ID:AUTH0_CLIENT_SECRET must be configured. " + + "Ref: https://registry.terraform.io/providers/auth0/auth0/latest/docs"), + }, + } + } + apiClient, err := management.New(domain, authenticationOption(clientID, clientSecret, apiToken, audience), management.WithDebug(debug), diff --git a/internal/config/config_test.go b/internal/config/config_test.go index c2f359777..ec3d5e75c 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -25,7 +25,7 @@ func TestConfigureProvider(t *testing.T) { name: "it can configure a provider with client credentials", givenTerraformConfig: map[string]interface{}{ "domain": "example.auth0.com", - "clientID": "1234567", + "client_id": "1234567", "client_secret": "secret", }, expectedDiagnostics: nil, @@ -34,7 +34,7 @@ func TestConfigureProvider(t *testing.T) { name: "it can configure a provider with client credentials and audience", givenTerraformConfig: map[string]interface{}{ "domain": "example.auth0.com", - "clientID": "1234567", + "client_id": "1234567", "client_secret": "secret", "audience": "myaudience", }, @@ -56,7 +56,8 @@ func TestConfigureProvider(t *testing.T) { expectedDiagnostics: diag.Diagnostics{ diag.Diagnostic{ Severity: diag.Error, - Summary: "parse \"https://example.com:path\": invalid port \":path\" after host", + Summary: "Missing environment variables", + Detail: "Either AUTH0_API_TOKEN or AUTH0_DOMAIN:AUTH0_CLIENT_ID:AUTH0_CLIENT_SECRET must be configured. Ref: https://registry.terraform.io/providers/auth0/auth0/latest/docs", }, }, }, diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 84c883409..f0c54c928 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -1,12 +1,8 @@ package provider import ( - "context" - "fmt" "os" - "github.com/hashicorp/terraform-plugin-sdk/v2/diag" - "github.com/auth0/terraform-provider-auth0/internal/auth0/flow" "github.com/auth0/terraform-provider-auth0/internal/auth0/form" @@ -176,29 +172,7 @@ func New() *schema.Provider { }, } - provider.ConfigureContextFunc = func(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) { - var diags diag.Diagnostics - - // Check required environment variables. - requiredEnvVars := []string{"AUTH0_DOMAIN", "AUTH0_CLIENT_ID", "AUTH0_CLIENT_SECRET"} - for _, varName := range requiredEnvVars { - value, exists := os.LookupEnv(varName) - if !exists || value == "" { - diags = append(diags, diag.Diagnostic{ - Severity: diag.Error, - Summary: fmt.Sprintf("Missing environment variable: %s", varName), - Detail: fmt.Sprintf("The environment variable %s must be set and cannot be empty.", varName), - }) - } - } - - if len(diags) > 0 { - return nil, diags - } - - // Call the original configuration function if no errors. - return config.ConfigureProvider(&provider.TerraformVersion)(ctx, d) - } + provider.ConfigureContextFunc = config.ConfigureProvider(&provider.TerraformVersion) return provider }