mrparkers / terraform-provider-keycloak

Terraform provider for Keycloak
https://registry.terraform.io/providers/mrparkers/keycloak/latest/docs
MIT License
634 stars 313 forks source link

terraform import fails for resources within a client #762

Closed naushadh closed 1 year ago

naushadh commented 1 year ago

Fantastic provider to manage Keycloak btw! I appreciate the thought and effort you've put into it.

Similar to https://github.com/mrparkers/terraform-provider-keycloak/issues/399; but I've confirmed login works for me, but it fails further down the chain.

I'm running into an error when importing a bunch of existing resources original created outside Terraform. It appears wide-spread across resources, permissions, etc perhaps because they share very similar conventions. So far I've only had luck importing a Client.

My command for the import

$ TF_LOG=debug terraform import keycloak_openid_client_authorization_resource.catalog_hive default/
286ebfcf-e924-43ad-9408-53c0526ed76b/25cece67-6a1c-4d33-bcf0-db9c4eddc044

Snippet from the logs when I run the import

2022-11-08T02:48:56.728Z [DEBUG] plugin.terraform-provider-keycloak_v3.10.0: Login request: tf_req_id=58427d42-11ef-ecef-d906-377fd4a6def5 @caller=github.com/mrparkers/terraform-provider-keycloak/keycloak/keycloak_client.go:108 @module=provider request=client_id=admin-cli&grant_type=password&password=${REDACTED}&username=${REDACTED} tf_provider_addr=provider tf_rpc=Configure timestamp=2022-11-08T02:48:56.727Z
b.com/mrparkers/terraform-provider-keycloak/keycloak/keycloak_client.go:139 tf_provider_addr=provider tf_req_id=58427d42-11ef-ecef-d906-377fd4a6def5 tf_rpc=Configure @module=provider timestamp=2022-11-08T02:48:57.131Z
2022-11-08T02:48:57.131Z [DEBUG] plugin.terraform-provider-keycloak_v3.10.0: Sending request: @caller=github.com/mrparkers/terraform-provider-keycloak/keycloak/keycloak_client.go:294 tf_req_id=58427d42-11ef-ecef-d906-377fd4a6def5 tf_rpc=Configure @module=provider method=GET path=/auth/admin/serverinfo tf_provider_addr=provider timestamp=2022-11-08T02:48:57.131Z
2022-11-08T02:48:57.662Z [DEBUG] plugin.terraform-provider-keycloak_v3.10.0: Received response: tf_provider_addr=provider tf_rpc=Configure @module=provider status="200 OK" tf_req_id=58427d42-11ef-ecef-d906-377fd4a6def5 @caller=github.com/mrparkers/terraform-provider-keycloak/keycloak/keycloak_client.go:341 timestamp=2022-11-08T02:48:57.661Z
keycloak_openid_client_authorization_resource.catalog_hive: Importing from ID "default/286ebfcf-e924-43ad-9408-53c0526ed76b/25cece67-6a1c-4d33-bcf0-db9c4eddc044"...

Error: rpc error: code = Canceled desc = context canceled


2022-11-08T02:48:57.669Z [DEBUG] plugin.terraform-provider-keycloak_v3.10.0: 
2022-11-08T02:48:57.669Z [DEBUG] plugin.terraform-provider-keycloak_v3.10.0: goroutine 60 [running]:
2022-11-08T02:48:57.669Z [DEBUG] plugin.terraform-provider-keycloak_v3.10.0: github.com/mrparkers/terraform-provider-keycloak/provider.resourceKeycloakOpenidClientAuthorizationResourceImport(0xfa2f00, 0xc000598f30, 0xc0003e3b80, 0xe757a0, 0xc00030ab40, 0x12, 0x0, 0x0, 0x0, 0x20)
2022-11-08T02:48:57.669Z [DEBUG] plugin.terraform-provider-keycloak_v3.10.0:    github.com/mrparkers/terraform-provider-keycloak/provider/resource_keycloak_openid_client_authorization_resource.go:190 +0x26d
2022-11-08T02:48:57.669Z [DEBUG] plugin.terraform-provider-keycloak_v3.10.0: github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Provider).ImportState(0xc000114300, 0xfa2f00, 0xc000598f30, 0xc000489770, 0xc00039c4e0, 0x51, 0xc000000001, 0xe82c69, 0x12, 0x0, ...)
2022-11-08T02:48:57.670Z [DEBUG] plugin.terraform-provider-keycloak_v3.10.0:    github.com/hashicorp/terraform-plugin-sdk/v2@v2.12.0/helper/schema/provider.go:385 +0x544
2022-11-08T02:48:57.670Z [DEBUG] plugin.terraform-provider-keycloak_v3.10.0: github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*GRPCProviderServer).ImportResourceState(0xc00000ce70, 0xfa2e58, 0xc000598f30, 0xc0005303e0, 0xe82c69, 0x12, 0x0)
2022-11-08T02:48:57.670Z [DEBUG] plugin.terraform-provider-keycloak_v3.10.0:    github.com/hashicorp/terraform-plugin-sdk/v2@v2.12.0/helper/schema/grpc_provider.go:1088 +0x11a
2022-11-08T02:48:57.670Z [DEBUG] plugin.terraform-provider-keycloak_v3.10.0: github.com/hashicorp/terraform-plugin-go/tfprotov5/tf5server.(*server).ImportResourceState(0xc00038a500, 0xfa2f00, 0xc000598330, 0xc000318d20, 0x0, 0x0, 0x0)
2022-11-08T02:48:57.670Z [DEBUG] plugin.terraform-provider-keycloak_v3.10.0:    github.com/hashicorp/terraform-plugin-go@v0.8.0/tfprotov5/tf5server/server.go:841 +0x2ed
2022-11-08T02:48:57.670Z [DEBUG] plugin.terraform-provider-keycloak_v3.10.0: github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5._Provider_ImportResourceState_Handler(0xe3d4e0, 0xc00038a500, 0xfa2f00, 0xc000598330, 0xc00064e1e0, 0x0, 0xfa2f00, 0xc000598330, 0xc000446000, 0x82)
2022-11-08T02:48:57.670Z [DEBUG] plugin.terraform-provider-keycloak_v3.10.0:    github.com/hashicorp/terraform-plugin-go@v0.8.0/tfprotov5/internal/tfplugin5/tfplugin5_grpc.pb.go:403 +0x214
2022-11-08T02:48:57.670Z [DEBUG] plugin.terraform-provider-keycloak_v3.10.0: google.golang.org/grpc.(*Server).processUnaryRPC(0xc00032a540, 0xfaa438, 0xc0002d61a0, 0xc0000a8480, 0xc0001edd40, 0x14834d8, 0x0, 0x0, 0x0)
2022-11-08T02:48:57.670Z [DEBUG] plugin.terraform-provider-keycloak_v3.10.0:    google.golang.org/grpc@v1.45.0/server.go:1282 +0x52b
2022-11-08T02:48:57.670Z [DEBUG] plugin.terraform-provider-keycloak_v3.10.0: google.golang.org/grpc.(*Server).handleStream(0xc00032a540, 0xfaa438, 0xc0002d61a0, 0xc0000a8480, 0x0)
2022-11-08T02:48:57.670Z [DEBUG] plugin.terraform-provider-keycloak_v3.10.0:    google.golang.org/grpc@v1.45.0/server.go:1619 +0xd0c
2022-11-08T02:48:57.670Z [DEBUG] plugin.terraform-provider-keycloak_v3.10.0: google.golang.org/grpc.(*Server).serveStreams.func1.2(0xc000310040, 0xc00032a540, 0xfaa438, 0xc0002d61a0, 0xc0000a8480)
2022-11-08T02:48:57.670Z [DEBUG] plugin.terraform-provider-keycloak_v3.10.0:    google.golang.org/grpc@v1.45.0/server.go:921 +0xab
2022-11-08T02:48:57.670Z [DEBUG] plugin.terraform-provider-keycloak_v3.10.0: created by google.golang.org/grpc.(*Server).serveStreams.func1
2022-11-08T02:48:57.670Z [DEBUG] plugin.terraform-provider-keycloak_v3.10.0:    google.golang.org/grpc@v1.45.0/server.go:919 +0x1fd
2022-11-08T02:48:57.672Z [DEBUG] plugin: plugin process exited: path=/root/.terraform.d/plugins/terraform-provider-keycloak_v3.10.0 pid=4259 error="exit status 2"
2022/11/08 02:48:57 [ERROR] <root>: eval: *terraform.EvalImportState, err: rpc error: code = Canceled desc = context canceled
2022/11/08 02:48:57 [ERROR] <root>: eval: *terraform.EvalSequence, err: rpc error: code = Canceled desc = context canceled

When I opened the source-code for the specific provider resource import function, here

I see the following, where it appears we're attempting to access an out of bounds element.

func resourceKeycloakOpenidClientAuthorizationResourceImport(_ context.Context, d *schema.ResourceData, _ interface{}) ([]*schema.ResourceData, error) {
    parts := strings.Split(d.Id(), "/")
    if len(parts) != 3 {
        return nil, fmt.Errorf("Invalid import. Supported import formats: {{realmId}}/{{resourceServerId}}/{{authorizationResourceId}}")
    }
    d.Set("realm_id", parts[0])
    d.Set("resource_server_id", parts[1])
    d.SetId(parts[3]) // <----- Shouldn't this be d.SetId(parts[2]) ?

    return []*schema.ResourceData{d}, nil
}

Could you please advice me on whether I'm doing something wrong with my import/configuration, or if there's a legitimate issue with the provider?

naushadh commented 1 year ago

Turns out it was an out-of-bounds error afterall; pushed a fix (linked above), and verified it works from my project. Here's the pre-release build that I've used to temporarily unblock myself: https://github.com/naushadh/terraform-provider-keycloak/releases/tag/v3.10.1