Skip to content

Commit 7ffc785

Browse files
committed
review changes
Signed-off-by: Mauritz Uphoff <mauritz.uphoff@stackit.cloud>
1 parent 6641b0c commit 7ffc785

File tree

6 files changed

+37
-14
lines changed

6 files changed

+37
-14
lines changed

stackit/internal/conversion/conversion.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,18 @@ func ParseProviderData(ctx context.Context, providerData any, diags *diag.Diagno
183183
}
184184
return stackitProviderData, true
185185
}
186+
187+
// TODO: write tests
188+
func ParseEphemeralProviderData(ctx context.Context, providerData any, diags *diag.Diagnostics) (core.EphemeralProviderData, bool) {
189+
// Prevent panic if the provider has not been configured.
190+
if providerData == nil {
191+
return core.EphemeralProviderData{}, false
192+
}
193+
194+
stackitProviderData, ok := providerData.(core.EphemeralProviderData)
195+
if !ok {
196+
core.LogAndAddError(ctx, diags, "Error configuring API client", fmt.Sprintf("Expected configure type stackit.ProviderData, got %T", providerData))
197+
return core.EphemeralProviderData{}, false
198+
}
199+
return stackitProviderData, true
200+
}

stackit/internal/core/core.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,19 @@ const (
2525
DatasourceRegionFallbackDocstring = "Uses the `default_region` specified in the provider configuration as a fallback in case no `region` is defined on datasource level."
2626
)
2727

28-
type ProviderData struct {
29-
RoundTripper http.RoundTripper
30-
ServiceAccountEmail string // Deprecated: ServiceAccountEmail is not required and will be removed after 12th June 2025.
28+
type EphemeralProviderData struct {
29+
ProviderData
3130

3231
PrivateKey string
3332
PrivateKeyPath string
3433
ServiceAccountKey string
3534
ServiceAccountKeyPath string
35+
TokenCustomEndpoint string
36+
}
37+
38+
type ProviderData struct {
39+
RoundTripper http.RoundTripper
40+
ServiceAccountEmail string // Deprecated: ServiceAccountEmail is not required and will be removed after 12th June 2025.
3641

3742
// Deprecated: Use DefaultRegion instead
3843
Region string
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package access_token

stackit/internal/services/access_token/ephemeral_access_token.go renamed to stackit/internal/services/access_token/ephemeral_resource.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,11 @@ import (
1010
"github.com/hashicorp/terraform-plugin-framework/ephemeral"
1111
"github.com/hashicorp/terraform-plugin-framework/ephemeral/schema"
1212
"github.com/hashicorp/terraform-plugin-framework/types"
13-
"github.com/hashicorp/terraform-plugin-log/tflog"
1413
"github.com/stackitcloud/stackit-sdk-go/core/clients"
1514
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
1615
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
1716
)
1817

19-
// #nosec G101 tokenUrl is a public endpoint, not a hardcoded credential
20-
const tokenUrl = "https://service-account.api.stackit.cloud/token"
21-
2218
var (
2319
_ ephemeral.EphemeralResource = &accessTokenEphemeralResource{}
2420
_ ephemeral.EphemeralResourceWithConfigure = &accessTokenEphemeralResource{}
@@ -33,10 +29,11 @@ type accessTokenEphemeralResource struct {
3329
serviceAccountKey string
3430
privateKeyPath string
3531
privateKey string
32+
tokenCustomEndpoint string
3633
}
3734

3835
func (e *accessTokenEphemeralResource) Configure(ctx context.Context, req ephemeral.ConfigureRequest, resp *ephemeral.ConfigureResponse) {
39-
providerData, ok := conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
36+
providerData, ok := conversion.ParseEphemeralProviderData(ctx, req.ProviderData, &resp.Diagnostics)
4037
if !ok {
4138
return
4239
}
@@ -45,6 +42,7 @@ func (e *accessTokenEphemeralResource) Configure(ctx context.Context, req epheme
4542
e.serviceAccountKeyPath = providerData.ServiceAccountKeyPath
4643
e.privateKey = providerData.PrivateKey
4744
e.privateKeyPath = providerData.PrivateKeyPath
45+
e.tokenCustomEndpoint = providerData.TokenCustomEndpoint
4846
}
4947

5048
type ephemeralTokenModel struct {
@@ -88,7 +86,7 @@ func (e *accessTokenEphemeralResource) Open(ctx context.Context, req ephemeral.O
8886
return
8987
}
9088

91-
client, diags := initKeyFlowClient(ctx, serviceAccountKey, privateKey)
89+
client, diags := initKeyFlowClient(ctx, serviceAccountKey, privateKey, e.tokenCustomEndpoint)
9290
resp.Diagnostics.Append(diags...)
9391
if resp.Diagnostics.HasError() {
9492
return
@@ -100,7 +98,6 @@ func (e *accessTokenEphemeralResource) Open(ctx context.Context, req ephemeral.O
10098
return
10199
}
102100

103-
ctx = tflog.SetField(ctx, "access_token", accessToken)
104101
model.AccessToken = types.StringValue(accessToken)
105102
resp.Diagnostics.Append(resp.Result.Set(ctx, model)...)
106103
}
@@ -181,14 +178,17 @@ func resolvePrivateKey(ctx context.Context, cfgValue, cfgPath string, key *clien
181178
}
182179

183180
// initKeyFlowClient configures and initializes a new KeyFlow client using the key and private key.
184-
func initKeyFlowClient(ctx context.Context, key *clients.ServiceAccountKeyResponse, privateKey string) (*clients.KeyFlow, diag.Diagnostics) {
181+
func initKeyFlowClient(ctx context.Context, key *clients.ServiceAccountKeyResponse, privateKey string, tokenCustomEndpoint string) (*clients.KeyFlow, diag.Diagnostics) {
185182
var diags diag.Diagnostics
186183

187184
client := &clients.KeyFlow{}
188185
cfg := &clients.KeyFlowConfig{
189186
ServiceAccountKey: key,
190187
PrivateKey: privateKey,
191-
TokenUrl: tokenUrl,
188+
}
189+
190+
if tokenCustomEndpoint != "" {
191+
cfg.TokenUrl = tokenCustomEndpoint
192192
}
193193

194194
if err := client.Init(cfg); err != nil {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package access_token

stackit/provider.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,12 +467,13 @@ func (p *Provider) Configure(ctx context.Context, req provider.ConfigureRequest,
467467
resp.DataSourceData = providerData
468468
resp.ResourceData = providerData
469469

470-
// Copy service account and private key credentials to support ephemeral access token generation
471-
ephemeralProviderData := providerData
470+
// Copy service account, private key credentials and custom-token endpoint to support ephemeral access token generation
471+
var ephemeralProviderData core.EphemeralProviderData
472472
setStringField(providerConfig.ServiceAccountKey, func(v string) { ephemeralProviderData.ServiceAccountKey = v })
473473
setStringField(providerConfig.ServiceAccountKeyPath, func(v string) { ephemeralProviderData.ServiceAccountKeyPath = v })
474474
setStringField(providerConfig.PrivateKey, func(v string) { ephemeralProviderData.PrivateKey = v })
475475
setStringField(providerConfig.PrivateKeyPath, func(v string) { ephemeralProviderData.PrivateKeyPath = v })
476+
setStringField(providerConfig.TokenCustomEndpoint, func(v string) { ephemeralProviderData.TokenCustomEndpoint = v })
476477
resp.EphemeralResourceData = ephemeralProviderData
477478

478479
providerData.Version = p.version

0 commit comments

Comments
 (0)