Skip to content

Commit

Permalink
Prevent stack trace if token is expired
Browse files Browse the repository at this point in the history
Handle the expired token error properly to avoid a stack trace.

(We should return a better error - this can be done in the future
by modifying the request handler.)
  • Loading branch information
stuart-mclaren-hpe committed Nov 14, 2024
1 parent 81966ad commit 0ccc6fa
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
6 changes: 6 additions & 0 deletions internal/datasources/system/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ func getSystemByName(
Systems().
GetAsSystemsGetResponse(ctx, grc)

if err != nil {
tflog.Error(ctx, "failed to get system by name: "+name)

return nil, err
}

if systems.GetTotal() == nil {
msg := "total is nil"
tflog.Error(ctx, msg)
Expand Down
10 changes: 10 additions & 0 deletions internal/simulator/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,19 @@ func simulateSystemGetByName() {
gock.New("http://localhost").
Get("/private-cloud-business/v1beta1/systems").
MatchParam("filter", "name eq "+systemName).
MatchHeader("Authorization", "Bearer abcdefghijklmnopqrstuvwxyz-0123456789").
Reply(200).
SetHeader("Content-Type", "application/json").
BodyString(systemsByName)

gock.New("http://localhost").
Get("/private-cloud-business/v1beta1/systems").
MatchParam("filter", "name eq "+systemName).
MatchHeader("Authorization", "Bearer expired-token").
Reply(401).
SetHeader("Content-Type", "text/plain").
BodyString("Jwt is not in the form of Header.Payload.Signature " +
"with two dots and 3 sections")
}

func System() {
Expand Down
41 changes: 41 additions & 0 deletions test/system/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,44 @@ func TestAccSystemDataSourceMissingName(t *testing.T) {
},
})
}

func TestAccSystemDataSourceBadAuth(t *testing.T) {
providerConfigBadAuth := `
terraform {
required_providers {
hpegl = {
source = "github.com/HewlettPackard/hpegl-pcbe-terraform-resources"
}
}
}
provider "hpegl" {
pc {
host = "http://localhost:8080"
token = "expired-token"
http_dump = true
poll_interval = 0.001
max_polls = 10
}
}
`
config := providerConfigBadAuth + `
data "hpegl_pc_system" "test" {
name = "array-5305-grp"
}
`
// TODO: return more informative error message - including
// http response code (requires change to request handler)
expected := `text does not support structured data`
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: config,
ExpectError: regexp.MustCompile(expected),
PlanOnly: true,
},
},
})
}

0 comments on commit 0ccc6fa

Please sign in to comment.