Skip to content

Commit 5872de6

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 stackitcloud#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 stackitcloud#719 Related to stackitcloud#880
1 parent 24b7387 commit 5872de6

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

stackit/provider.go

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,10 @@ type providerModel struct {
160160
SqlServerFlexCustomEndpoint types.String `tfsdk:"sqlserverflex_custom_endpoint"`
161161
TokenCustomEndpoint types.String `tfsdk:"token_custom_endpoint"`
162162

163-
EnableBetaResources types.Bool `tfsdk:"enable_beta_resources"`
164-
Experiments types.List `tfsdk:"experiments"`
163+
EnableBetaResources types.Bool `tfsdk:"enable_beta_resources"`
164+
Experiments types.List `tfsdk:"experiments"`
165+
CliAuth types.Bool `tfsdk:"cli_auth"`
166+
CliProfile types.String `tfsdk:"cli_profile"`
165167
}
166168

167169
// Schema defines the provider-level schema for configuration data.
@@ -205,6 +207,8 @@ func (p *Provider) Schema(_ context.Context, _ provider.SchemaRequest, resp *pro
205207
"token_custom_endpoint": "Custom endpoint for the token API, which is used to request access tokens when using the key flow",
206208
"enable_beta_resources": "Enable beta resources. Default is false.",
207209
"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, ", ")),
210+
"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.",
211+
"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'.",
208212
}
209213

210214
resp.Schema = schema.Schema{
@@ -374,6 +378,14 @@ func (p *Provider) Schema(_ context.Context, _ provider.SchemaRequest, resp *pro
374378
Optional: true,
375379
Description: descriptions["token_custom_endpoint"],
376380
},
381+
"cli_auth": schema.BoolAttribute{
382+
Optional: true,
383+
Description: descriptions["cli_auth"],
384+
},
385+
"cli_profile": schema.StringAttribute{
386+
Optional: true,
387+
Description: descriptions["cli_profile"],
388+
},
377389
},
378390
}
379391
}
@@ -460,6 +472,40 @@ func (p *Provider) Configure(ctx context.Context, req provider.ConfigureRequest,
460472
providerData.Experiments = experimentValues
461473
}
462474

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

0 commit comments

Comments
 (0)