Skip to content

Fix unsafe type assertions in provider data handling #847

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

Merged
merged 4 commits into from
Jun 12, 2025
Merged
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
5 changes: 5 additions & 0 deletions .changes/unreleased/fixed-20250605-151206.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: fixed
body: Fixed unsafe type assertions in provider data handling that could cause panics and misleading error messages
time: 2025-06-05T15:12:06.705294178Z
custom:
Issue: "841"
12 changes: 5 additions & 7 deletions internal/services/data_record/datasource_data_record.go
Original file line number Diff line number Diff line change
@@ -190,18 +190,16 @@ func (d *DataRecordDataSource) Configure(ctx context.Context, req datasource.Con
return
}

client := req.ProviderData.(*api.ProviderClient).Api

if client == nil {
providerClient, ok := req.ProviderData.(*api.ProviderClient)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
"Unexpected DataSource Configure Type",
fmt.Sprintf("Expected *api.ProviderClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)

return
}

d.DataRecordClient = newDataRecordClient(client)
d.DataRecordClient = newDataRecordClient(providerClient.Api)
}

func (d *DataRecordDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
9 changes: 4 additions & 5 deletions internal/services/data_record/resource_data_record.go
Original file line number Diff line number Diff line change
@@ -112,16 +112,15 @@ func (r *DataRecordResource) Configure(ctx context.Context, req resource.Configu
return
}

clientApi := req.ProviderData.(*api.ProviderClient).Api
if clientApi == nil {
providerClient, ok := req.ProviderData.(*api.ProviderClient)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
fmt.Sprintf("Expected *api.ProviderClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)

return
}
r.DataRecordClient = newDataRecordClient(clientApi)
r.DataRecordClient = newDataRecordClient(providerClient.Api)
}

func (r *DataRecordResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
Original file line number Diff line number Diff line change
@@ -270,15 +270,15 @@ func (r *environmentGroupRuleSetResource) Configure(ctx context.Context, req res
return
}

client := req.ProviderData.(*api.ProviderClient).Api
if client == nil {
providerClient, ok := req.ProviderData.(*api.ProviderClient)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
fmt.Sprintf("Expected *api.ProviderClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
r.EnvironmentGroupRuleSetClient = NewEnvironmentGroupRuleSetClient(client, tenant.NewTenantClient(client))
r.EnvironmentGroupRuleSetClient = NewEnvironmentGroupRuleSetClient(providerClient.Api, tenant.NewTenantClient(providerClient.Api))
}

func (r *environmentGroupRuleSetResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
Original file line number Diff line number Diff line change
@@ -298,18 +298,16 @@ func (r *EnvironmentSettingsResource) Configure(ctx context.Context, req resourc
return
}

client := req.ProviderData.(*api.ProviderClient).Api

if client == nil {
providerClient, ok := req.ProviderData.(*api.ProviderClient)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
fmt.Sprintf("Expected *api.ProviderClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)

return
}

r.EnvironmentSettingClient = newEnvironmentSettingsClient(client)
r.EnvironmentSettingClient = newEnvironmentSettingsClient(providerClient.Api)
}

func (r *EnvironmentSettingsResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
11 changes: 5 additions & 6 deletions internal/services/languages/datasource_languages.go
Original file line number Diff line number Diff line change
@@ -98,16 +98,15 @@ func (d *DataSource) Configure(ctx context.Context, req datasource.ConfigureRequ
// ProviderData will be null when Configure is called from ValidateConfig. It's ok.
return
}
clientApi := req.ProviderData.(*api.ProviderClient).Api
if clientApi == nil {
providerClient, ok := req.ProviderData.(*api.ProviderClient)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
"Unexpected DataSource Configure Type",
fmt.Sprintf("Expected *api.ProviderClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)

return
}
d.LanguagesClient = newLanguagesClient(clientApi)
d.LanguagesClient = newLanguagesClient(providerClient.Api)
}

func (d *DataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
Original file line number Diff line number Diff line change
@@ -68,17 +68,14 @@ func (r *ManagedEnvironmentResource) Configure(ctx context.Context, req resource
)
return
}
clientApi := client.Api

if clientApi == nil {
if client.Api == nil {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
"Nil Api client",
"ProviderData contained a *api.ProviderClient but with nil Api. Please check provider initialization and credentials.",
)

return
}
r.ManagedEnvironmentClient = newManagedEnvironmentClient(clientApi)
r.ManagedEnvironmentClient = newManagedEnvironmentClient(client.Api)
}

func (r *ManagedEnvironmentResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
10 changes: 4 additions & 6 deletions internal/services/tenant_settings/resource_tenant_settings.go
Original file line number Diff line number Diff line change
@@ -344,18 +344,16 @@ func (r *TenantSettingsResource) Configure(ctx context.Context, req resource.Con
return
}

client := req.ProviderData.(*api.ProviderClient).Api

if client == nil {
providerClient, ok := req.ProviderData.(*api.ProviderClient)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
fmt.Sprintf("Expected *api.ProviderClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)

return
}

r.TenantSettingClient = newTenantSettingsClient(client)
r.TenantSettingClient = newTenantSettingsClient(providerClient.Api)
}

func (r *TenantSettingsResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {