microsoft / terraform-provider-azuredevops

Terraform Azure DevOps provider
https://www.terraform.io/docs/providers/azuredevops/
MIT License
380 stars 272 forks source link

Build definition import fails on invalid memory address #491

Open roryprimrose opened 2 years ago

roryprimrose commented 2 years ago

Community Note

Terraform (and Azure DevOps Provider) Version

Terraform: 1.0.11 Azure DevOps provider: 0.1.7

Affected Resource(s)

Terraform Configuration Files

resource "azuredevops_build_definition" "pipeline" {
  project_id      = var.repository.project_id
  name            = var.config.name
  path            = var.config.folderPath
  agent_pool_name = var.config.pool

  ci_trigger {
    use_yaml = true
  }

  repository {
    repo_type           = "TfsGit"
    repo_id             = var.repository.id
    branch_name         = var.repository.default_branch
    yml_path            = var.config.yamlPath
    report_build_status = var.config.reportBuildStatus
  }
}

Debug Output

module.azdo_pipeline["7d625088-2797-4329-a50c-e6c452456678"].azuredevops_build_definition.pipeline: Import prepared!
  Prepared azuredevops_build_definition for import
module.azdo_pipeline["7d625088-2797-4329-a50c-e6c452456678"].azuredevops_build_definition.pipeline: Refreshing state... [id=41]
β•·
β”‚ Error: Plugin did not respond
β”‚ 
β”‚ The plugin encountered an error, and failed to respond to the
β”‚ plugin.(*GRPCProvider).ReadResource call. The plugin logs may contain more
β”‚ details.
β•΅

Stack trace from the terraform-provider-azuredevops_v0.1.7 plugin:

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

goroutine 39 [running]:
github.com/microsoft/terraform-provider-azuredevops/azuredevops/internal/service/build.flattenRepository(0xc000192160, 0x11c9cf9, 0x4)
    github.com/microsoft/terraform-provider-azuredevops/azuredevops/internal/service/build/resource_build_definition.go:510 +0x3d7
github.com/microsoft/terraform-provider-azuredevops/azuredevops/internal/service/build.flattenBuildDefinition(0xc000596ee0, 0xc000192160, 0xc00027c480, 0x24)
    github.com/microsoft/terraform-provider-azuredevops/azuredevops/internal/service/build/resource_build_definition.go:328 +0x174
github.com/microsoft/terraform-provider-azuredevops/azuredevops/internal/service/build.resourceBuildDefinitionRead(0xc000596ee0, 0xfd5000, 0xc0001d6a00, 0xc000596ee0, 0x0)
    github.com/microsoft/terraform-provider-azuredevops/azuredevops/internal/service/build/resource_build_definition.go:417 +0x225
github.com/hashicorp/terraform-plugin-sdk/helper/schema.(*Resource).RefreshWithoutUpgrade(0xc0005bc460, 0xc0004805a0, 0xfd5000, 0xc0001d6a00, 0xc000266518, 0x0, 0x0)
    github.com/hashicorp/terraform-plugin-sdk@v1.17.2/helper/schema/resource.go:470 +0x12e
github.com/hashicorp/terraform-plugin-sdk/internal/helper/plugin.(*GRPCProviderServer).ReadResource(0xc00000f690, 0x13c8890, 0xc0006b10e0, 0xc0008d8960, 0xc00000f690, 0xc0006b10e0, 0xc00068bba0)
    github.com/hashicorp/terraform-plugin-sdk@v1.17.2/internal/helper/plugin/grpc_provider.go:535 +0x3dd
github.com/hashicorp/terraform-plugin-sdk/internal/tfplugin5._Provider_ReadResource_Handler(0x1182fe0, 0xc00000f690, 0x13c8890, 0xc0006b10e0, 0xc0008d8900, 0x0, 0x13c8890, 0xc0006b10e0, 0xc000af4a50, 0xe4)
    github.com/hashicorp/terraform-plugin-sdk@v1.17.2/internal/tfplugin5/tfplugin5.pb.go:3269 +0x214
google.golang.org/grpc.(*Server).processUnaryRPC(0xc00078c1c0, 0x13d6578, 0xc000585200, 0xc0001d6f00, 0xc000092ba0, 0x19b92d0, 0x0, 0x0, 0x0)
    google.golang.org/grpc@v1.32.0/server.go:1194 +0x52b
google.golang.org/grpc.(*Server).handleStream(0xc00078c1c0, 0x13d6578, 0xc000585200, 0xc0001d6f00, 0x0)
    google.golang.org/grpc@v1.32.0/server.go:1517 +0xd0c

Panic Output

Expected Behavior

Build definition should be imported into state.

Actual Behavior

I ran about 40 imports into state via an AzDO pipeline. About 50% of them failed on this memory issue.

Steps to Reproduce

  1. terraform import -input=false *** 'module.azdo_pipeline[\"70109eaf-836e-4abe-a042-d5110b67f067\"].azuredevops_build_definition.pipeline' MyProject/PipelineId

Important Factoids

References

xuzhang3 commented 2 years ago

@roryprimrose I cannot reproduce this error, from the source code, this error is because the buildDefinition.repository cannot find. This could be some legacy changes from different API version result in different response body or there is some permissions to limit the repository read operation. I will enhance the null pointer check. You can check the the build definition details by API : https://dev.azure.com/{org}/{project}/_apis/build/definitions/{denifinitionID}?api-version=5.1. image

roryprimrose commented 2 years ago

Thanks @xuzhang3. I found that it only happened on import. If I renamed the existing pipeline and then ran apply it was able to create the pipeline fine. Not a great solution because now we have old pipelines we need to hang on to due to the pipeline history but I was able to move forward.

roryprimrose commented 2 years ago

Is there a reason you use the old build api rather than the new pipeline api?

https://docs.microsoft.com/en-us/rest/api/azure/devops/pipelines/pipelines/get?view=azure-devops-rest-6.0

I'm assuming it is so that you can support old build definitions as well as the newer pipelines.

tmeckel commented 2 years ago

@roryprimrose we're still using the 5.1 REST API because a newer one wasn't available so far for GO. @nechvatalp is working on the 6.0 REST API right now. There are still some issues with generation process; ref to #204

andreassiegel commented 2 years ago

I encounter a very similar problem with Terraform 1.1.0 (or 1.0.4 or 1.0.11) and the Azure DevOps Provider 0.1.7 and 0.1.8.:

When I run terraform plan in the project my colleague worked with before, the azuredevops plugin crashes:

β•·
β”‚ Error: Plugin did not respond
β”‚
β”‚   with module.azure-devops.azuredevops_serviceendpoint_github_enterprise.ghe,
β”‚   on azure-devops/service_connection.tf line 1, in resource "azuredevops_serviceendpoint_github_enterprise" "ghe":
β”‚    1: resource "azuredevops_serviceendpoint_github_enterprise" "ghe" {
β”‚
β”‚ The plugin encountered an error, and failed to respond to the plugin.(*GRPCProvider).ReadResource call. The plugin logs may contain more details.
β•΅
β•·
β”‚ Error: Request cancelled
β”‚
β”‚   with module.azure-devops.azuredevops_serviceendpoint_azurerm.infrastructure_subscription,
β”‚   on azure-devops/service_connection.tf line 12, in resource "azuredevops_serviceendpoint_azurerm" "infrastructure_subscription":
β”‚   12: resource "azuredevops_serviceendpoint_azurerm" "infrastructure_subscription" {
β”‚
β”‚ The plugin.(*GRPCProvider).UpgradeResourceState request was cancelled.
β•΅
β•·
β”‚ Error: Request cancelled
β”‚
β”‚   with module.azure-devops.azuredevops_serviceendpoint_azurecr.container_registry,
β”‚   on azure-devops/service_connection.tf line 26, in resource "azuredevops_serviceendpoint_azurecr" "container_registry":
β”‚   26: resource "azuredevops_serviceendpoint_azurecr" "container_registry" {
β”‚
β”‚ The plugin.(*GRPCProvider).UpgradeResourceState request was cancelled.
β•΅

Stack trace from the terraform-provider-azuredevops_v0.1.8 plugin:

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

goroutine 41 [running]:
github.com/microsoft/terraform-provider-azuredevops/azuredevops/internal/service/serviceendpoint.flattenServiceEndpointGitHubEnterprise(0xc0001a6850, 0xc000166a80, 0xc00071ea90)
    github.com/microsoft/terraform-provider-azuredevops/azuredevops/internal/service/serviceendpoint/resource_serviceendpoint_github_enterprise.go:53 +0x4e
github.com/microsoft/terraform-provider-azuredevops/azuredevops/internal/service/serviceendpoint.genServiceEndpointReadFunc.func1(0xc0001a6850, 0x1bdd7e0, 0xc00069a000, 0xc0001a6850, 0x0)
    github.com/microsoft/terraform-provider-azuredevops/azuredevops/internal/service/serviceendpoint/commons.go:210 +0x278
github.com/hashicorp/terraform-plugin-sdk/helper/schema.(*Resource).RefreshWithoutUpgrade(0xc000322780, 0xc000744cd0, 0x1bdd7e0, 0xc00069a000, 0xc0000ac298, 0x0, 0x0)
    github.com/hashicorp/terraform-plugin-sdk@v1.17.2/helper/schema/resource.go:470 +0x12e
github.com/hashicorp/terraform-plugin-sdk/internal/helper/plugin.(*GRPCProviderServer).ReadResource(0xc000288be0, 0x1fd59b0, 0xc0007af380, 0xc000281140, 0xc000288be0, 0xc0007af380, 0xc00056bba0)
    github.com/hashicorp/terraform-plugin-sdk@v1.17.2/internal/helper/plugin/grpc_provider.go:535 +0x3dd
github.com/hashicorp/terraform-plugin-sdk/internal/tfplugin5._Provider_ReadResource_Handler(0x1d8d040, 0xc000288be0, 0x1fd59b0, 0xc0007af380, 0xc0002810e0, 0x0, 0x1fd59b0, 0xc0007af380, 0xc00013edc0, 0x2a4)
    github.com/hashicorp/terraform-plugin-sdk@v1.17.2/internal/tfplugin5/tfplugin5.pb.go:3269 +0x214
google.golang.org/grpc.(*Server).processUnaryRPC(0xc00034b6c0, 0x1fe3918, 0xc00009b380, 0xc0000b6a00, 0xc00035e8d0, 0x25ce550, 0x0, 0x0, 0x0)
    google.golang.org/grpc@v1.32.0/server.go:1194 +0x52b
google.golang.org/grpc.(*Server).handleStream(0xc00034b6c0, 0x1fe3918, 0xc00009b380, 0xc0000b6a00, 0x0)
    google.golang.org/grpc@v1.32.0/server.go:1517 +0xd0c
google.golang.org/grpc.(*Server).serveStreams.func1.2(0xc000290380, 0xc00034b6c0, 0x1fe3918, 0xc00009b380, 0xc0000b6a00)
    google.golang.org/grpc@v1.32.0/server.go:859 +0xab
created by google.golang.org/grpc.(*Server).serveStreams.func1
    google.golang.org/grpc@v1.32.0/server.go:857 +0x1fd

Error: The terraform-provider-azuredevops_v0.1.8 plugin crashed!
tmeckel commented 2 years ago

@andreassiegel How does the HCL code look like, that creates the Service Endpoint resource for GitHub Enterprise? The provider crashes at the location below. So something's wrong how the authorization settings are handled.

https://github.com/microsoft/terraform-provider-azuredevops/blob/ea770d4b10a20078f59693600af4b9be085b5fba/azuredevops/internal/service/serviceendpoint/resource_serviceendpoint_github_enterprise.go#L53

And because the error is related to a different resource, would you mind creating a new issue? Would be easier to track.

image

roryprimrose commented 2 years ago

I get that kind of error mostly testing on my local machine. I suspect it is usually http/networking failures and running it again works fine.

andreassiegel commented 2 years ago

I wish rerunning would fix it. I get the error all the time recently, and always in the same spot.

But you're right, I get the error running on the local machine. The script is not supposed to be run somewhere in a pipeline because it is setting up pipelines, etc. itself.

roryprimrose commented 2 years ago

Try running terraform through fiddler so you can see the https traffic. That might show something interesting.

eXXL commented 2 years ago

I have the same issue that the import of existing pipelines works in some cases and in some others not. I get the same error like this one.

I tested the link you provided to check the repo properties: https://dev.azure.com/{org}/{project}/_apis/build/definitions/{denifinitionID}?api-version=5.1.

In many cases there are no repository properties. Then the import fails.

A workaround is to add a property via API: https://stackoverflow.com/questions/65027909/how-to-update-azure-devops-build-definition-source-from-azure-repos-to-github-us