Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added explicit check for handling missing env variables for login #1065

Merged
merged 5 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading