Skip to content

Commit

Permalink
Added explicit check for handling missing env variables for login (#1065
Browse files Browse the repository at this point in the history
)

* Added explicit check for handling missing env variables for login

* Minor changes

* Minor update to test

* Updated test case

* Updated test case
  • Loading branch information
duedares-rvj authored Oct 31, 2024
1 parent 152ca2d commit 4a0e957
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 30 deletions.
11 changes: 11 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
7 changes: 4 additions & 3 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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",
},
Expand All @@ -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",
},
},
},
Expand Down
28 changes: 1 addition & 27 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
}

0 comments on commit 4a0e957

Please sign in to comment.