elastic / terraform-provider-elasticstack

Terraform provider for Elastic Stack
https://registry.terraform.io/providers/elastic/elasticstack/latest/docs
Apache License 2.0
170 stars 92 forks source link

Kibana action connectors crash when the Kibana endpoint is misconfigured. #391

Open hahny93 opened 1 year ago

hahny93 commented 1 year ago

Pointing the Kibana configuration to a non-Kibana http server leads to provider crashes when managing action connectors.

This code correctly parses the response code, but obviously the body doesn't necessarily meet the expected form and the Error/Message fields remain nil.

    if resp.JSON400 != nil {
        return "", diag.Errorf("%s: %s", *resp.JSON400.Error, *resp.JSON400.Message)
    }

We should definitely fix this specific issue, but I wonder if it's worthwhile putting a product style check early in the provider code. That way we can provide a targeted, relevant error to users. Something like: "It doesn't look like the provider kibana.endpoint is actually pointing to a Kibana instance."

Original issue

Describe the bug When trying to create a connector to MS Teams the provider crashes with a runtime error.

To Reproduce Steps to reproduce the behavior:

  1. TF configuration used

    resource "elasticstack_kibana_action_connector" "action_connector_teams" {
    name              = "Teams Application Log Failure Connector"
    connector_type_id = ".teams"
    secrets = jsonencode({
    webhookUrl = var.elastic_teams_connector_url
    })
    }

    and

    
    provider "elasticstack" {
    elasticsearch {
    endpoints = [ec_deployment.elastic.elasticsearch.https_endpoint]
    username  = ec_deployment.elastic.elasticsearch_username
    password  = ec_deployment.elastic.elasticsearch_password
    }
    
    kibana {
    endpoints = [ec_deployment.elastic.elasticsearch.https_endpoint]
    username  = ec_deployment.elastic.elasticsearch_username
    password  = ec_deployment.elastic.elasticsearch_password
    }
    }
... with the variables declared as CICD Pipeline config.
2. TF operations to execute to get the error 'terraform apply'
3. The operation fails

**Expected behavior**
The operation creates the action connector in the given environment.

**Debug output**

Stack trace from the terraform-provider-elasticstack_v0.6.2 plugin: panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x103295c] goroutine 56 [running]: github.com/elastic/terraform-provider-elasticstack/internal/clients/kibana.CreateConnector({0x14c7c48, 0xc00061ea20}, 0xc0005c4c08?, {{0x0, 0x0}, {0xc00004db98, 0x7}, {0xc0003c40f0, 0x27}, {0xc00004db4a, ...}, ...}) github.com/elastic/terraform-provider-elasticstack/internal/clients/kibana/connector.go:42 +0xa1c github.com/elastic/terraform-provider-elasticstack/internal/kibana.resourceConnectorCreate({0x14c7c48, 0xc00061ea20}, 0x0?, {0x124cd20, 0xc000238460}) github.com/elastic/terraform-provider-elasticstack/internal/kibana/connector.go:137 +0x1d0 github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(Resource).create(0xc000466000, {0x14c7c80, 0xc00027be30}, 0xd?, {0x124cd20, 0xc000238460}) github.com/hashicorp/terraform-plugin-sdk/v2@v2.26.1/helper/schema/resource.go:707 +0x12e github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(Resource).Apply(0xc000466000, {0x14c7c80, 0xc00027be30}, 0xc00037cd00, 0xc0007b6e80, {0x124cd20, 0xc000238460}) github.com/hashicorp/terraform-plugin-sdk/v2@v2.26.1/helper/schema/resource.go:837 +0xa85 github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(GRPCProviderServer).ApplyResourceChange(0xc0000100c0, {0x14c7c80?, 0xc00027bd10?}, 0xc0004818b0) github.com/hashicorp/terraform-plugin-sdk/v2@v2.26.1/helper/schema/grpc_provider.go:1021 +0xe8d github.com/hashicorp/terraform-plugin-mux/tf5muxserver.muxServer.ApplyResourceChange({0xc0004255c0, 0xc000425620, {0xc0004b6010, 0x1, 0x1}, {0x0, 0x0, 0x0}, {0x0, 0x0, ...}, ...}, ...) github.com/hashicorp/terraform-plugin-mux@v0.9.0/tf5muxserver/mux_server_ApplyResourceChange.go:27 +0x102 github.com/hashicorp/terraform-plugin-go/tfprotov5/tf5server.(server).ApplyResourceChange(0xc000000140, {0x14c7c80?, 0xc000373aa0?}, 0xc0007a6620) github.com/hashicorp/terraform-plugin-go@v0.15.0/tfprotov5/tf5server/server.go:818 +0x574 github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5._Provider_ApplyResourceChange_Handler({0x1291e40?, 0xc000000140}, {0x14c7c80, 0xc000373aa0}, 0xc0007a65b0, 0x0) github.com/hashicorp/terraform-plugin-go@v0.15.0/tfprotov5/internal/tfplugin5/tfplugin5_grpc.pb.go:419 +0x170 google.golang.org/grpc.(Server).processUnaryRPC(0xc0002c6000, {0x14cbd40, 0xc0002dc820}, 0xc000377440, 0xc00027b920, 0x1dd2980, 0x0) google.golang.org/grpc@v1.54.0/server.go:1345 +0xdf0 google.golang.org/grpc.(Server).handleStream(0xc0002c6000, {0x14cbd40, 0xc0002dc820}, 0xc000377440, 0x0) google.golang.org/grpc@v1.54.0/server.go:1722 +0xa2f google.golang.org/grpc.(Server).serveStreams.func1.2() google.golang.org/grpc@v1.54.0/server.go:966 +0x98 created by google.golang.org/grpc.(Server).serveStreams.func1 google.golang.org/grpc@v1.54.0/server.go:964 +0x28a panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x103295c] goroutine 68 [running]: github.com/elastic/terraform-provider-elasticstack/internal/clients/kibana.CreateConnector({0x14c7c48, 0xc000594120}, 0xc0006a6c08?, {{0x0, 0x0}, {0xc00079a718, 0x7}, {0xc00011ede0, 0x27}, {0xc00079a6ca, ...}, ...}) github.com/elastic/terraform-provider-elasticstack/internal/clients/kibana/connector.go:42 +0xa1c github.com/elastic/terraform-provider-elasticstack/internal/kibana.resourceConnectorCreate({0x14c7c48, 0xc000594120}, 0x0?, {0x124cd20, 0xc0001c8fa0}) github.com/elastic/terraform-provider-elasticstack/internal/kibana/connector.go:137 +0x1d0 github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(Resource).create(0xc0004b8700, {0x14c7c80, 0xc0007b5080}, 0xd?, {0x124cd20, 0xc0001c8fa0}) github.com/hashicorp/terraform-plugin-sdk/v2@v2.26.1/helper/schema/resource.go:707 +0x12e github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(Resource).Apply(0xc0004b8700, {0x14c7c80, 0xc0007b5080}, 0xc0007b2a90, 0xc0006fd200, {0x124cd20, 0xc0001c8fa0}) github.com/hashicorp/terraform-plugin-sdk/v2@v2.26.1/helper/schema/resource.go:837 +0xa85 github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(GRPCProviderServer).ApplyResourceChange(0xc00019f368, {0x14c7c80?, 0xc0007b4f60?}, 0xc0006b9860) github.com/hashicorp/terraform-plugin-sdk/v2@v2.26.1/helper/schema/grpc_provider.go:1021 +0xe8d github.com/hashicorp/terraform-plugin-mux/tf5muxserver.muxServer.ApplyResourceChange({0xc0004a89f0, 0xc0004a8a50, {0xc00052c210, 0x1, 0x1}, {0x0, 0x0, 0x0}, {0x0, 0x0, ...}, ...}, ...) github.com/hashicorp/terraform-plugin-mux@v0.9.0/tf5muxserver/mux_server_ApplyResourceChange.go:27 +0x102 github.com/hashicorp/terraform-plugin-go/tfprotov5/tf5server.(server).ApplyResourceChange(0xc00047a320, {0x14c7c80?, 0xc0007b4240?}, 0xc0007973b0) github.com/hashicorp/terraform-plugin-go@v0.15.0/tfprotov5/tf5server/server.go:818 +0x574 github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5._Provider_ApplyResourceChange_Handler({0x1291e40?, 0xc00047a320}, {0x14c7c80, 0xc0007b4240}, 0xc000797340, 0x0) github.com/hashicorp/terraform-plugin-go@v0.15.0/tfprotov5/internal/tfplugin5/tfplugin5_grpc.pb.go:419 +0x170 google.golang.org/grpc.(Server).processUnaryRPC(0xc000318000, {0x14cbd40, 0xc000103040}, 0xc00016ec60, 0xc000531e90, 0x1dd2980, 0x0) google.golang.org/grpc@v1.54.0/server.go:1345 +0xdf0 google.golang.org/grpc.(Server).handleStream(0xc000318000, {0x14cbd40, 0xc000103040}, 0xc00016ec60, 0x0) google.golang.org/grpc@v1.54.0/server.go:1722 +0xa2f google.golang.org/grpc.(Server).serveStreams.func1.2() google.golang.org/grpc@v1.54.0/server.go:966 +0x98 created by google.golang.org/grpc.(Server).serveStreams.func1 google.golang.org/grpc@v1.54.0/server.go:964 +0x28a Error: The terraform-provider-elasticstack_v0.6.2 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.



**Versions:**
 - Terraform Version 1.5.3
 - Provider version 0.6.2
 - Elasticsearch Version 8.8.1
tobio commented 1 year ago

It looks like you're configuring the kibana endpoint with the elasticsearch endpoint in your provider. You'll want to change it to:

  kibana {
    endpoints = [ec_deployment.elastic.kibana.https_endpoint]
    username  = ec_deployment.elastic.elasticsearch_username
    password  = ec_deployment.elastic.elasticsearch_password
  }

Kibana will also re-use the elasticsearch credentials, so you could just have:

  kibana {
    endpoints = [ec_deployment.elastic.kibana.https_endpoint]
  }

That said, obviously the segfault is undesirable.

hahny93 commented 1 year ago

Thanks a lot, connector was now created successfully.

I totally agree with that, it's absolutely not clear what went wrong with our config or that it even was the config was to blame in the first place.