cockroachdb / terraform-provider-cockroach

Terraform provider for CockroachDB Cloud
Apache License 2.0
56 stars 10 forks source link

Provider crashes given a not uuid id to data cockroach_cluster #80

Closed noprysk-ua closed 1 year ago

noprysk-ua commented 1 year ago

Given an invalid id to data cockroach_cluster, the provider crashes.

E.g., here the id contains the name of the cluster, and not the uuid.

data "cockroach_cluster" "foo" {
  id = "sunset-laika"
}

The fix is to indicate the uuid as the value.

data "cockroach_cluster" "foo" {
  id = "6ac25c84-adff-11ed-afa1-0242ac120002"
}

Consider validating the id and returning an error.

It looks like a return is missing in internal/provider/cockroach_cluster_data_source.go near

    cockroachCluster, httpResp, err := d.provider.service.GetCluster(ctx, cluster.ID.ValueString())
    if httpResp != nil && httpResp.StatusCode == http.StatusNotFound {
        resp.Diagnostics.AddError(
            "Cluster not found",
            fmt.Sprintf("Couldn't find a cluster with ID %s", cluster.ID.ValueString()))
        return
    }
    if err != nil {
        resp.Diagnostics.AddError(
            "Error getting cluster info",
            fmt.Sprintf("Unexpected error while retrieving cluster info: %v", formatAPIErrorMessage(err)))
          // TODO(add the missing return to prevent crashes): return
    }

    cluster.Name = types.StringValue(cockroachCluster.Name)

Here is the panic.

Stack trace from the terraform-provider-cockroach_v0.4.1 plugin:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x70 pc=0xafb7ad]

goroutine 53 [running]:
github.com/cockroachdb/terraform-provider-cockroach/internal/provider.(*clusterDataSource).Read(0xc00011c030, {0xe25980, 0xc000588ab0}, {{{{0xe29268, 0xc0005cc1b0}, {0xbf3f40, 0xc000589b90}}, {0xe2abc0, 0xc00023b680}}, {{{0x0, ...}, ...}, ...}}, ...)
        github.com/cockroachdb/terraform-provider-cockroach/internal/provider/cockroach_cluster_data_source.go:174 +0x4cd
github.com/hashicorp/terraform-plugin-framework/internal/fwserver.(*Server).ReadDataSource(0xc000408160, {0xe25980, 0xc000588ab0}, 0xc000588ba0, 0xc000171618)
        github.com/hashicorp/terraform-plugin-framework@v1.1.1/internal/fwserver/server_readdatasource.go:75 +0x46d
github.com/hashicorp/terraform-plugin-framework/internal/proto6server.(*Server).ReadDataSource(0xc000408160, {0xe25980?, 0xc000588960?}, 0xc00011e120)
        github.com/hashicorp/terraform-plugin-framework@v1.1.1/internal/proto6server/server_readdatasource.go:52 +0x28d
github.com/hashicorp/terraform-plugin-go/tfprotov6/tf6server.(*server).ReadDataSource(0xc000312dc0, {0xe25980?, 0xc0005881b0?}, 0xc00058c0a0)
        github.com/hashicorp/terraform-plugin-go@v0.14.3/tfprotov6/tf6server/server.go:656 +0x3ef
github.com/hashicorp/terraform-plugin-go/tfprotov6/internal/tfplugin6._Provider_ReadDataSource_Handler({0xcb5620?, 0xc000312dc0}, {0xe25980, 0xc0005881b0}, 0xc000182000, 0x0)
        github.com/hashicorp/terraform-plugin-go@v0.14.3/tfprotov6/internal/tfplugin6/tfplugin6_grpc.pb.go:421 +0x170
google.golang.org/grpc.(*Server).processUnaryRPC(0xc00041c000, {0xe294a0, 0xc0004824e0}, 0xc000590000, 0xc0004189c0, 0x1315db0, 0x0)
        google.golang.org/grpc@v1.51.0/server.go:1340 +0xd13
google.golang.org/grpc.(*Server).handleStream(0xc00041c000, {0xe294a0, 0xc0004824e0}, 0xc000590000, 0x0)
        google.golang.org/grpc@v1.51.0/server.go:1713 +0xa1b
google.golang.org/grpc.(*Server).serveStreams.func1.2()
        google.golang.org/grpc@v1.51.0/server.go:965 +0x98
created by google.golang.org/grpc.(*Server).serveStreams.func1
        google.golang.org/grpc@v1.51.0/server.go:963 +0x28a

Error: The terraform-provider-cockroach_v0.4.1 plugin crashed!

This is always indicative of a bug within the plugin. It would be immensely
helpful if you could report the crash with the plugin's maintainers so that it
can be fixed. The output above should help diagnose the issue.
erademacher commented 1 year ago

Thanks for the detailed bug report! We'll work a fix into 0.4.3.

erademacher commented 1 year ago

Fixed in v0.4.3. Thanks again!