Skip to content
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

Crash after upgrade to v5.1.0: Panic due to nil pointer dereference #1091

Closed
SND33 opened this issue Jan 24, 2025 · 11 comments · Fixed by #1097
Closed

Crash after upgrade to v5.1.0: Panic due to nil pointer dereference #1091

SND33 opened this issue Jan 24, 2025 · 11 comments · Fixed by #1097

Comments

@SND33
Copy link

SND33 commented Jan 24, 2025

Describe the bug

After upgrading to version 5.1.0, the plugin crashes with a panic caused by a nil pointer dereference. The issue appears to occur during the usage of setRealmData, within the Compare and GreaterThanOrEqual methods when handling a nil Keycloak version object. I initially observed the crash when the plugin attempted to refresh the keycloak_realm resource (see stack trace below), however, I tried to temporarily disable the realm resource and started getting the same crash on other resources, so there is probably another underlying issue. Happy to provide more details if needed.

Stack trace from the terraform-provider-keycloak_v5.1.0 plugin:

panic: runtime error: invalid memory address or nil pointer dereference

[signal SIGSEGV: segmentation violation code=0x1 addr=0x28 pc=0x88f613]

goroutine 42 [running]:

[github.com/hashicorp/go-version.(*Version](http://github.com/hashicorp/go-version.(*Version)).String(0x0)

	[github.com/hashicorp/[email protected]/version.go:386](http://github.com/hashicorp/[email protected]/version.go:386) +0x33

[github.com/hashicorp/go-version.(*Version](http://github.com/hashicorp/go-version.(*Version)).Compare(0x0, 0xc0005a0b90)

	[github.com/hashicorp/[email protected]/version.go:119](http://github.com/hashicorp/[email protected]/version.go:119) +0x25

[github.com/hashicorp/go-version.(*Version](http://github.com/hashicorp/go-version.(*Version)).GreaterThanOrEqual(...)

	[github.com/hashicorp/[email protected]/version.go:320](http://github.com/hashicorp/[email protected]/version.go:320)

[github.com/keycloak/terraform-provider-keycloak/provider.setRealmData](http://github.com/keycloak/terraform-provider-keycloak/provider.setRealmData)(0xc000198e80, 0xc0001b9408, 0x0)

	[github.com/keycloak/terraform-provider-keycloak/provider/resource_keycloak_realm.go:1314](http://github.com/keycloak/terraform-provider-keycloak/provider/resource_keycloak_realm.go:1314) +0x2cc6

[github.com/keycloak/terraform-provider-keycloak/provider.resourceKeycloakRealmRead](http://github.com/keycloak/terraform-provider-keycloak/provider.resourceKeycloakRealmRead)({0xfa8310, 0xc0002e62a0}, 0xc000198e80, {0xe40b40?, 0xc0004342a0})

	[github.com/keycloak/terraform-provider-keycloak/provider/resource_keycloak_realm.go:1438](http://github.com/keycloak/terraform-provider-keycloak/provider/resource_keycloak_realm.go:1438) +0x179

[github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource](http://github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource)).read(0xc00039f340, {0xfa8268, 0xc0005a9800}, 0xc000198e80, {0xe40b40, 0xc0004342a0})

	[github.com/hashicorp/terraform-plugin-sdk/[email protected]/helper/schema/resource.go:823](http://github.com/hashicorp/terraform-plugin-sdk/[email protected]/helper/schema/resource.go:823) +0x119

[github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource](http://github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource)).RefreshWithoutUpgrade(0xc00039f340, {0xfa8268, 0xc0005a9800}, 0xc0004f2ea0, {0xe40b40, 0xc0004342a0})

	[github.com/hashicorp/terraform-plugin-sdk/[email protected]/helper/schema/resource.go:1117](http://github.com/hashicorp/terraform-plugin-sdk/[email protected]/helper/schema/resource.go:1117) +0x529

[github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*GRPCProviderServer](http://github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*GRPCProviderServer)).ReadResource(0xc000386ba0, {0xfa8268?, 0xc0005a9740?}, 0xc0000c3800)

	[github.com/hashicorp/terraform-plugin-sdk/[email protected]/helper/schema/grpc_provider.go:710](http://github.com/hashicorp/terraform-plugin-sdk/[email protected]/helper/schema/grpc_provider.go:710) +0x6c5

[github.com/hashicorp/terraform-plugin-go/tfprotov5/tf5server.(*server](http://github.com/hashicorp/terraform-plugin-go/tfprotov5/tf5server.(*server)).ReadResource(0xc0002e2500, {0xfa8268?, 0xc0005a8f60?}, 0xc0002e78f0)

	[github.com/hashicorp/[email protected]/tfprotov5/tf5server/server.go:783](http://github.com/hashicorp/[email protected]/tfprotov5/tf5server/server.go:783) +0x309

[github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5._Provider_ReadResource_Handler](http://github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5._Provider_ReadResource_Handler)({0xe218c0, 0xc0002e2500}, {0xfa8268, 0xc0005a8f60}, 0xc000199900, 0x0)

	[github.com/hashicorp/[email protected]/tfprotov5/internal/tfplugin5/tfplugin5_grpc.pb.go:575](http://github.com/hashicorp/[email protected]/tfprotov5/internal/tfplugin5/tfplugin5_grpc.pb.go:575) +0x1a9

[google.golang.org/grpc.(*Server](http://google.golang.org/grpc.(*Server)).processUnaryRPC(0xc000194e00, {0xfa8268, 0xc0005a8ed0}, {0xfacdd0, 0xc0003f1040}, 0xc0005aa6c0, 0xc000410960, 0x15a7228, 0x0)

	[google.golang.org/[email protected]/server.go:1394](http://google.golang.org/[email protected]/server.go:1394) +0xe49

[google.golang.org/grpc.(*Server](http://google.golang.org/grpc.(*Server)).handleStream(0xc000194e00, {0xfacdd0, 0xc0003f1040}, 0xc0005aa6c0)

	[google.golang.org/[email protected]/server.go:1805](http://google.golang.org/[email protected]/server.go:1805) +0xe8b

[google.golang.org/grpc.(*Server](http://google.golang.org/grpc.(*Server)).serveStreams.func2.1()

	[google.golang.org/[email protected]/server.go:1029](http://google.golang.org/[email protected]/server.go:1029) +0x8b

created by [google.golang.org/grpc.(*Server](http://google.golang.org/grpc.(*Server)).serveStreams.func2 in goroutine 28

	[google.golang.org/[email protected]/server.go:1040](http://google.golang.org/[email protected]/server.go:1040) +0x125

Error: The terraform-provider-keycloak_v5.1.0 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.

2025-01-24T11:58:58.386Z [DEBUG] provider: plugin exited

Version

26.1.0

Expected behavior

No response

Actual behavior

No response

How to Reproduce?

No response

Anything else?

No response

@sebastienbaillet
Copy link

I have the same problem when I try to upgrade from 4.5.0 to 5.0.0 or to 5.1.0 with same stack trace.

@sschu
Copy link
Contributor

sschu commented Jan 24, 2025

@SND33 @sebastienbaillet Do you have a reproducer you could share? Does it also happen against KC 26.0.8 (which is the latest version we have in CI)?

@sebastienbaillet
Copy link

sebastienbaillet commented Jan 24, 2025

It happens against KC 26.0.5. I can test against 26.0.8 and 26.1.0 if you need.
I'll try to create a small example that causes the problem.
Something strange is that I have ~20% of non-error case if I do TF_LOG=debug terraform apply, and 100% of error with a simple terraform apply.

@sschu
Copy link
Contributor

sschu commented Jan 24, 2025

That would be good. CI is green and tests run fine locally also against 26.1.0.

@unbreakabl3
Copy link

unbreakabl3 commented Jan 24, 2025

I got the same problem with KC 24.x. No issues with provider 5.0.0.

@sebastienbaillet
Copy link

sebastienbaillet commented Jan 24, 2025

So, I've tested with the following KC version: 26.0.5, 26.0.8, 26.1.01.

Applying (for the first time) this terraform code crash:

terraform {
  required_providers {
    keycloak = {
      source  = "keycloak/keycloak"
      version = "= 5.1.0"
    }
  }
  required_version = ">= 1.6"
}

variable "keycloak_admin_password" {
  type = string
}

locals {
  keycloak_url = "https://my-url"
  realm_name   = "provider-crash-realm-test"
}

provider "keycloak" {
  client_id                = "admin-cli"
  username                 = "admin"
  password                 = var.keycloak_admin_password
  url                      = local.keycloak_url
  initial_login            = false
}

resource "keycloak_realm" "platform_realm" {
  realm             = local.realm_name
  enabled           = true
}

with the following stack trace:

keycloak_realm.platform_realm: Creating...
╷
│ Error: Plugin did not respond
│
│   with keycloak_realm.platform_realm,
│   on keycloak.tf line 28, in resource "keycloak_realm" "platform_realm":
│   28: resource "keycloak_realm" "platform_realm" {
│
│ The plugin encountered an error, and failed to respond to the plugin.(*GRPCProvider).ApplyResourceChange call. The plugin logs may contain more details.
╵

Stack trace from the terraform-provider-keycloak_v5.1.0 plugin:

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

goroutine 41 [running]:
github.com/hashicorp/go-version.(*Version).String(0x0)
        github.com/hashicorp/[email protected]/version.go:386 +0x33
github.com/hashicorp/go-version.(*Version).Compare(0x0, 0xc0005e8c80)
        github.com/hashicorp/[email protected]/version.go:119 +0x25
github.com/hashicorp/go-version.(*Version).GreaterThanOrEqual(...)
        github.com/hashicorp/[email protected]/version.go:320
github.com/keycloak/terraform-provider-keycloak/provider.setRealmFlowBindings(0xc00062ac00, 0xc000696a08, 0x0)
        github.com/keycloak/terraform-provider-keycloak/provider/resource_keycloak_realm.go:731 +0x5d5
github.com/keycloak/terraform-provider-keycloak/provider.getRealmFromData(0xc00062ac00, 0x0)
        github.com/keycloak/terraform-provider-keycloak/provider/resource_keycloak_realm.go:1030 +0x1e71
github.com/keycloak/terraform-provider-keycloak/provider.resourceKeycloakRealmCreate({0xfa8310, 0xc000128690}, 0xc00062ac00, {0xe40b40, 0xc000114c00})
        github.com/keycloak/terraform-provider-keycloak/provider/resource_keycloak_realm.go:1399 +0x6d
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource).create(0xc0003af340, {0xfa8268, 0xc000472c90}, 0xc00062ac00, {0xe40b40, 0xc000114c00})
        github.com/hashicorp/terraform-plugin-sdk/[email protected]/helper/schema/resource.go:806 +0x119
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource).Apply(0xc0003af340, {0xfa8268, 0xc000472c90}, 0xc000550dd0, 0xc00062a900, {0xe40b40, 0xc000114c00})
        github.com/hashicorp/terraform-plugin-sdk/[email protected]/helper/schema/resource.go:937 +0xa89
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*GRPCProviderServer).ApplyResourceChange(0xc000396bb8, {0xfa8268?, 0xc000472bd0?}, 0xc000476370)
        github.com/hashicorp/terraform-plugin-sdk/[email protected]/helper/schema/grpc_provider.go:1155 +0xd5c
github.com/hashicorp/terraform-plugin-go/tfprotov5/tf5server.(*server).ApplyResourceChange(0xc0002f4500, {0xfa8268?, 0xc0004721e0?}, 0xc000128070)
        github.com/hashicorp/[email protected]/tfprotov5/tf5server/server.go:865 +0x3d0
github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5._Provider_ApplyResourceChange_Handler({0xe218c0, 0xc0002f4500}, {0xfa8268, 0xc0004721e0}, 0xc00062a080, 0x0)
        github.com/hashicorp/[email protected]/tfprotov5/internal/tfplugin5/tfplugin5_grpc.pb.go:611 +0x1a6
google.golang.org/grpc.(*Server).processUnaryRPC(0xc0001b2e00, {0xfa8268, 0xc000472150}, {0xfacdd0, 0xc0004011e0}, 0xc00047c000, 0xc000422960, 0x15a7258, 0x0)
        google.golang.org/[email protected]/server.go:1394 +0xe49
google.golang.org/grpc.(*Server).handleStream(0xc0001b2e00, {0xfacdd0, 0xc0004011e0}, 0xc00047c000)
        google.golang.org/[email protected]/server.go:1805 +0xe8b
google.golang.org/grpc.(*Server).serveStreams.func2.1()
        google.golang.org/[email protected]/server.go:1029 +0x8b
created by google.golang.org/grpc.(*Server).serveStreams.func2 in goroutine 16
        google.golang.org/[email protected]/server.go:1040 +0x125

Error: The terraform-provider-keycloak_v5.1.0 plugin crashed!

For information, I've installed the 3 following plugins in Keycloak:

I'll see if I can try without those plugins.

Other informations, I'm running Terraform in a Ubuntu WSL on Windows 11.

@sschu
Copy link
Contributor

sschu commented Jan 24, 2025

OK, I can reproduce it locally using your example. I'll check whats wrong...

@sschu
Copy link
Contributor

sschu commented Jan 27, 2025

@sebastienbaillet @unbreakabl3 @SND33 I have found the problem, the reason is that the Keycloak version is not correctly initialized if initial_login is set to false in the provider settings. I'll come up wit the fix. Until then, would it work for you to set initial_login to true or do you need it set to false?

@unbreakabl3
Copy link

@sebastienbaillet @unbreakabl3 @SND33 I have found the problem, the reason is that the Keycloak version is not correctly initialized if initial_login is set to false in the provider settings. I'll come up wit the fix. Until then, would it work for you to set initial_login to true or do you need it set to false?

Sounds great. I can test both tomorrow and update.

@unbreakabl3
Copy link

@sebastienbaillet @unbreakabl3 @SND33 I have found the problem, the reason is that the Keycloak version is not correctly initialized if initial_login is set to false in the provider settings. I'll come up wit the fix. Until then, would it work for you to set initial_login to true or do you need it set to false?

Sounds great. I can test both tomorrow and update.

So, KC 24.x:

  1. initial_login = true - working
  2. initial_login = false - not working

@sebastienbaillet
Copy link

Nice !
I need initial_login = false, so I'll stay in 4.5.0 until the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants