Skip to content

Commit 42832a0

Browse files
committed
feat: add CLI authentication support for provider configuration
This commit adds support for CLI-based authentication in the Terraform provider, enabling users to authenticate using credentials from the STACKIT CLI without managing separate service account credentials. Changes: - Add cli_auth boolean attribute to enable CLI authentication - Add cli_profile string attribute for profile selection - Implement authentication priority: explicit credentials > CLI > env vars - Integrate with SDK's WithCLIProviderAuth() configuration option The implementation follows the explicit opt-in pattern requested in RFC #880, requiring users to set cli_auth = true to enable the feature. Profile resolution follows the standard precedence: explicit config > STACKIT_CLI_PROFILE env var > ~/.config/stackit/cli-profile.txt > default. This change depends on SDK PR stackitcloud/stackit-sdk-go#3865 which adds the core CLI authentication functionality, and CLI PR stackitcloud/stackit-cli#1130 which implements the provider credential storage. Closes #719 Related to #880
1 parent f1a49e6 commit 42832a0

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

stackit/provider.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ type providerModel struct {
159159
EnableBetaResources types.Bool `tfsdk:"enable_beta_resources"`
160160
ServiceEnablementCustomEndpoint types.String `tfsdk:"service_enablement_custom_endpoint"`
161161
Experiments types.List `tfsdk:"experiments"`
162+
CliAuth types.Bool `tfsdk:"cli_auth"`
163+
CliProfile types.String `tfsdk:"cli_profile"`
162164
}
163165

164166
// Schema defines the provider-level schema for configuration data.
@@ -202,6 +204,8 @@ func (p *Provider) Schema(_ context.Context, _ provider.SchemaRequest, resp *pro
202204
"token_custom_endpoint": "Custom endpoint for the token API, which is used to request access tokens when using the key flow",
203205
"enable_beta_resources": "Enable beta resources. Default is false.",
204206
"experiments": fmt.Sprintf("Enables experiments. These are unstable features without official support. More information can be found in the README. Available Experiments: %v", strings.Join(features.AvailableExperiments, ", ")),
207+
"cli_auth": "Enable authentication using STACKIT CLI credentials. When enabled, the provider will use credentials from 'stackit auth provider login' if no explicit service account credentials are provided. Default is false.",
208+
"cli_profile": "STACKIT CLI profile to use for authentication when cli_auth is enabled. If not specified, uses STACKIT_CLI_PROFILE environment variable, then ~/.config/stackit/cli-profile.txt, then 'default'.",
205209
}
206210

207211
resp.Schema = schema.Schema{
@@ -370,6 +374,14 @@ func (p *Provider) Schema(_ context.Context, _ provider.SchemaRequest, resp *pro
370374
Optional: true,
371375
Description: descriptions["experiments"],
372376
},
377+
"cli_auth": schema.BoolAttribute{
378+
Optional: true,
379+
Description: descriptions["cli_auth"],
380+
},
381+
"cli_profile": schema.StringAttribute{
382+
Optional: true,
383+
Description: descriptions["cli_profile"],
384+
},
373385
},
374386
}
375387
}
@@ -453,6 +465,40 @@ func (p *Provider) Configure(ctx context.Context, req provider.ConfigureRequest,
453465
providerData.Experiments = experimentValues
454466
}
455467

468+
// Setup authentication with priority order:
469+
// 1. Explicit provider configuration (service_account_key, token, etc.)
470+
// 2. CLI provider credentials (if cli_auth = true and authenticated via STACKIT CLI)
471+
// 3. Environment variables and credentials file (handled by sdkauth.SetupAuth)
472+
var err error
473+
474+
// Check if CLI auth is explicitly enabled
475+
cliAuthEnabled := !providerConfig.CliAuth.IsNull() && !providerConfig.CliAuth.IsUnknown() && providerConfig.CliAuth.ValueBool()
476+
477+
// Check if explicit authentication is configured
478+
hasExplicitAuth := (!providerConfig.ServiceAccountKey.IsNull() && !providerConfig.ServiceAccountKey.IsUnknown()) ||
479+
(!providerConfig.ServiceAccountKeyPath.IsNull() && !providerConfig.ServiceAccountKeyPath.IsUnknown()) ||
480+
(!providerConfig.Token.IsNull() && !providerConfig.Token.IsUnknown())
481+
482+
// Configure CLI provider authentication via SDK if enabled
483+
if !hasExplicitAuth && cliAuthEnabled {
484+
// Get CLI profile from config
485+
var cliProfile string
486+
if !providerConfig.CliProfile.IsNull() && !providerConfig.CliProfile.IsUnknown() {
487+
cliProfile = providerConfig.CliProfile.ValueString()
488+
}
489+
490+
// Apply CLI provider auth configuration option
491+
// The SDK will handle credential reading, token refresh, and authentication
492+
err := config.WithCLIProviderAuth(cliProfile)(sdkConfig)
493+
if err != nil {
494+
core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring provider",
495+
fmt.Sprintf("%v", err))
496+
return
497+
}
498+
}
499+
500+
// Setup authentication using the configured SDK
501+
// This respects explicit credentials, CLI auth (if enabled), or env vars/credentials file
456502
roundTripper, err := sdkauth.SetupAuth(sdkConfig)
457503
if err != nil {
458504
core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring provider", fmt.Sprintf("Setting up authentication: %v", err))

0 commit comments

Comments
 (0)