azure-pipelines-terraform: Terraform apply fails with clientCredentialsToken already expired.
I have a pretty strange issue. Initially it was failed randomly, now it’s failing consistently. It appears the issue is a token was valid 2 hours ago before it was requested?!
2023-12-06T00:09:01.5213760Z ... Original Error: clientCredentialsToken: received HTTP status 401 with response: {
"error": "invalid_client",
"error_description": "AADSTS700024: Client assertion is not within its valid time range. Current time: 2023-12-06T00:05:01.4821348Z, assertion valid from 2023-12-04T22:36:40.0000000Z, expiry time of assertion 2023-12-04T22:41:40.0000000Z. Review the documentation at https://docs.microsoft.com/azure/active-directory/develop/active-directory-certificate-credentials . Trace ID: 883fdc44-ecca-43a1-b06a-814700b29800 Correlation ID: a0e44aa3-ef9a-4c5f-a199-3472dfd6b988 Timestamp: 2023-12-06 00:05:01Z",
"error_codes": [700024],
"timestamp": "2023-12-06 00:05:01Z",
"trace_id": "883fdc44-ecca-43a1-b06a-814700b29800",
"correlation_id": "a0e44aa3-ef9a-4c5f-a199-3472dfd6b988",
"error_uri": "https://login.microsoftonline.com/error?code=700024"
}
The pipeline is running under Azure Container Instance, and I have checked the log timestamp is matching with my local machine (give or take 10 seconds)
Strangely the first auth token negotiation earlier in the code seems to be successful. See timestamp 2023-12-06T00:01:25.5091505Z vs 2023-12-06T00:01:31.0398817Z
Which leads me to another question whether the entire thing was attempted twice?
Full debug trace:
2023-12-06T00:01:19.8537115Z ##[section]Starting: Terraform apply
2023-12-06T00:01:19.8560363Z ==============================================================================
2023-12-06T00:01:19.8560793Z Task : Terraform
2023-12-06T00:01:19.8561278Z Description : Execute terraform commands to manage resources on AzureRM, Amazon Web Services(AWS) and Google Cloud Platform(GCP)
2023-12-06T00:01:19.8561762Z Version : 4.227.24
2023-12-06T00:01:19.8562033Z Author : Microsoft Corporation
2023-12-06T00:01:19.8562427Z Help : [Learn more about this task](https://aka.ms/AAf0uqr)
2023-12-06T00:01:19.8562856Z ==============================================================================
2023-12-06T00:01:20.9658456Z [command]/azp/_work/_tool/terraform/1.6.3/x64/terraform providers
2023-12-06T00:01:25.4979252Z 2023-12-06T00:01:21.445Z [INFO] Terraform version: 1.6.3
2023-12-06T00:01:25.4979658Z
2023-12-06T00:01:25.4981474Z 2023-12-06T00:01:21.445Z [DEBUG] using github.com/hashicorp/go-tfe v1.36.0
2023-12-06T00:01:25.4982178Z Providers required by configuration:
2023-12-06T00:01:25.4983379Z 2023-12-06T00:01:21.445Z [DEBUG] using github.com/hashicorp/hcl/v2 v2.19.1
2023-12-06T00:01:25.4983821Z .
2023-12-06T00:01:25.5006396Z 2023-12-06T00:01:21.445Z [DEBUG] using github.com/hashicorp/terraform-svchost v0.1.1
2023-12-06T00:01:25.5009358Z 2023-12-06T00:01:21.445Z [DEBUG] using github.com/zclconf/go-cty v1.14.1
2023-12-06T00:01:25.5010323Z 2023-12-06T00:01:21.445Z [INFO] Go runtime version: go1.21.3
2023-12-06T00:01:25.5011269Z 2023-12-06T00:01:21.446Z [INFO] CLI args: []string{"/azp/_work/_tool/terraform/1.6.3/x64/terraform", "providers"}
2023-12-06T00:01:25.5022714Z 2023-12-06T00:01:21.446Z [DEBUG] Attempting to open CLI config file: /root/.terraformrc
2023-12-06T00:01:25.5023701Z 2023-12-06T00:01:21.446Z [DEBUG] File doesn't exist, but doesn't need to. Ignoring.
2023-12-06T00:01:25.5024991Z 2023-12-06T00:01:21.446Z [DEBUG] ignoring non-existing provider search directory terraform.d/plugins
2023-12-06T00:01:25.5026037Z 2023-12-06T00:01:21.446Z [DEBUG] ignoring non-existing provider search directory /root/.terraform.d/plugins
2023-12-06T00:01:25.5027118Z 2023-12-06T00:01:21.446Z [DEBUG] ignoring non-existing provider search directory /root/.local/share/terraform/plugins
2023-12-06T00:01:25.5028175Z 2023-12-06T00:01:21.446Z [DEBUG] ignoring non-existing provider search directory /usr/local/share/terraform/plugins
2023-12-06T00:01:25.5029226Z 2023-12-06T00:01:21.446Z [DEBUG] ignoring non-existing provider search directory /usr/share/terraform/plugins
2023-12-06T00:01:25.5030126Z 2023-12-06T00:01:21.446Z [INFO] CLI command args: []string{"providers"}
2023-12-06T00:01:25.5031110Z 2023-12-06T00:01:21.495Z [INFO] Testing if Service Principal / Client Certificate is applicable for Authentication..
2023-12-06T00:01:25.5032358Z 2023-12-06T00:01:21.495Z [INFO] Testing if Multi Tenant Service Principal / Client Secret is applicable for Authentication..
2023-12-06T00:01:25.5033447Z 2023-12-06T00:01:21.495Z [INFO] Testing if Service Principal / Client Secret is applicable for Authentication..
2023-12-06T00:01:25.5082425Z 2023-12-06T00:01:21.495Z [INFO] Testing if OIDC is applicable for Authentication..
2023-12-06T00:01:25.5083438Z 2023-12-06T00:01:21.495Z [INFO] Using OIDC for Authentication
2023-12-06T00:01:25.5087464Z 2023-12-06T00:01:21.495Z [INFO] Getting OAuth config for endpoint https://login.microsoftonline.com/ with tenant e3c***73c
2023-12-06T00:01:25.5088692Z 2023-12-06T00:01:21.495Z [DEBUG] Obtaining an MSAL / Microsoft Graph token for Resource Manager..
2023-12-06T00:01:25.5089579Z 2023-12-06T00:01:24.536Z [DEBUG] checking for provisioner in "."
2023-12-06T00:01:25.5090478Z 2023-12-06T00:01:24.536Z [DEBUG] checking for provisioner in "/azp/_work/_tool/terraform/1.6.3/x64"
2023-12-06T00:01:25.5091505Z 2023-12-06T00:01:24.536Z [DEBUG] Building the Blob Client from an Access Token (using user credentials)
2023-12-06T00:01:25.5092364Z 2023-12-06T00:01:24.695Z [DEBUG] Azure Backend Request:
2023-12-06T00:01:25.5104681Z POST /subscriptions/2d0***43e/resourceGroups/itsi-***-rg/providers/Microsoft.Storage/storageAccounts/itsi***ops/listKeys?api-version=2021-01-01 HTTP/1.1
2023-12-06T00:01:25.5105667Z Host: management.azure.com
2023-12-06T00:01:25.5106816Z User-Agent: HashiCorp Terraform/1.6.3 (+https://www.terraform.io) VSTS_e1c450d9-5476-4946-a244-d4470a6409ac_Release__2866_12648_9
2023-12-06T00:01:25.5107603Z Content-Length: 0
2023-12-06T00:01:25.5108173Z X-Ms-Authorization-Auxiliary:
2023-12-06T00:01:25.5108713Z Accept-Encoding: gzip
2023-12-06T00:01:25.5110172Z 2023-12-06T00:01:25.416Z [DEBUG] Azure Backend Response for https://management.azure.com/subscriptions/2d0***43e/resourceGroups/itsi-***-rg/providers/Microsoft.Storage/storageAccounts/itsi***ops/listKeys?api-version=2021-01-01:
2023-12-06T00:01:25.5111439Z HTTP/2.0 200 OK
2023-12-06T00:01:25.5111978Z Content-Length: 288
2023-12-06T00:01:25.5112501Z Cache-Control: no-cache
2023-12-06T00:01:25.5113087Z Content-Type: application/json
2023-12-06T00:01:25.5113503Z Date: Wed, 06 Dec 2023 00:01:25 GMT
2023-12-06T00:01:25.5124805Z Expires: -1
2023-12-06T00:01:25.5125526Z Pragma: no-cache
2023-12-06T00:01:25.5126239Z Strict-Transport-Security: max-age=31536000; includeSubDomains
2023-12-06T00:01:25.5126890Z X-Cache: CONFIG_NOCACHE
2023-12-06T00:01:25.5127453Z X-Content-Type-Options: nosniff
2023-12-06T00:01:25.5128185Z X-Ms-Correlation-Request-Id: 3806b7a0-be08-49c6-af58-f285ddcea7e7
2023-12-06T00:01:25.5128961Z X-Ms-Ratelimit-Remaining-Subscription-Resource-Requests: 11999
2023-12-06T00:01:25.5129728Z X-Ms-Request-Id: 221bc474-2cad-4d3f-8ec3-d74e221ef906
2023-12-06T00:01:25.5130629Z X-Ms-Routing-Request-Id: WESTEUROPE:20231206T000125Z:3806b7a0-be08-49c6-af58-f285ddcea7e7
2023-12-06T00:01:25.5131668Z X-Msedge-Ref: Ref A: 5D22AE16D9B44265979E815FF78724F4 Ref B: AMS231032607017 Ref C: 2023-12-06T00:01:24Z
2023-12-06T00:01:25.5132058Z
2023-12-06T00:01:25.5132947Z {"keys":[{"keyName":"key1","value":"Mw***g==","permissions":"FULL"},{"keyName":"key2","value":"SLB***w==","permissions":"FULL"}]}
2023-12-06T00:01:25.5145025Z 2023-12-06T00:01:25.418Z [DEBUG] Azure Backend Request:
2023-12-06T00:01:25.5145647Z GET /devops/tfstateenv%3Adev_canon HTTP/1.1
2023-12-06T00:01:25.5146151Z Host: itsi***ops.blob.core.windows.net
2023-12-06T00:01:25.5147228Z User-Agent: HashiCorp Terraform/1.6.3 (+https://www.terraform.io) VSTS_e1c450d9-5476-4946-a244-d4470a6409ac_Release__2866_12648_9
2023-12-06T00:01:25.5148151Z X-Ms-Date: Wed, 06 Dec 2023 00:01:25 GMT
2023-12-06T00:01:25.5148788Z X-Ms-Version: 2018-11-09
2023-12-06T00:01:25.5149322Z Accept-Encoding: gzip
2023-12-06T00:01:25.5150285Z 2023-12-06T00:01:25.470Z [DEBUG] Azure Backend Response for https://itsi***ops.blob.core.windows.net/devops/tfstateenv%3Adev_canon:
2023-12-06T00:01:25.5150928Z HTTP/1.1 200 OK
2023-12-06T00:01:25.5151430Z Content-Length: 25915
2023-12-06T00:01:25.5151961Z Accept-Ranges: bytes
2023-12-06T00:01:25.5152532Z Content-Md5: qdKhSwT7nJ2h+kNw/m42Sg==
2023-12-06T00:01:25.5153136Z Content-Type: application/json
2023-12-06T00:01:25.5153564Z Date: Wed, 06 Dec 2023 00:01:25 GMT
2023-12-06T00:01:25.5153956Z Etag: "0x8DBF51991D2C9B5"
2023-12-06T00:01:25.5191550Z Last-Modified: Mon, 04 Dec 2023 22:37:17 GMT
2023-12-06T00:01:25.5192285Z Server: Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0
2023-12-06T00:01:25.5192919Z X-Ms-Blob-Type: BlockBlob
2023-12-06T00:01:25.5193579Z X-Ms-Creation-Time: Mon, 27 Nov 2023 04:44:43 GMT
2023-12-06T00:01:25.5194535Z X-Ms-Lease-State: available
2023-12-06T00:01:25.5195123Z X-Ms-Lease-Status: unlocked
2023-12-06T00:01:25.5311662Z X-Ms-Request-Id: ff4ccb0e-b01e-0064-19d7-271b36000000
2023-12-06T00:01:25.5312510Z X-Ms-Server-Encrypted: true
2023-12-06T00:01:25.5313094Z X-Ms-Version: 2018-11-09
2023-12-06T00:01:25.5313280Z
2023-12-06T00:01:25.5314286Z { ... tfstate content ... }
2023-12-06T00:01:25.7426386Z 2023-12-06T00:01:25.479Z [DEBUG] Azure Backend Request:
2023-12-06T00:01:25.7426900Z GET /devops/tfstateenv%3Adev_canon HTTP/1.1
2023-12-06T00:01:25.7427358Z Host: itsi***ops.blob.core.windows.net
2023-12-06T00:01:25.7428379Z User-Agent: HashiCorp Terraform/1.6.3 (+https://www.terraform.io) VSTS_e1c450d9-5476-4946-a244-d4470a6409ac_Release__2866_12648_9
2023-12-06T00:01:25.7429267Z X-Ms-Date: Wed, 06 Dec 2023 00:01:25 GMT
2023-12-06T00:01:25.7429865Z X-Ms-Version: 2018-11-09
2023-12-06T00:01:25.7430417Z Accept-Encoding: gzip
2023-12-06T00:01:25.7431363Z 2023-12-06T00:01:25.491Z [DEBUG] Azure Backend Response for https://itsi***ops.blob.core.windows.net/devops/tfstateenv%3Adev_canon:
2023-12-06T00:01:25.7432002Z HTTP/1.1 200 OK
2023-12-06T00:01:25.7432521Z Content-Length: 25915
2023-12-06T00:01:25.7433040Z Accept-Ranges: bytes
2023-12-06T00:01:25.7433676Z Content-Md5: qdKhSwT7nJ2h+kNw/m42Sg==
2023-12-06T00:01:25.7434579Z Content-Type: application/json
2023-12-06T00:01:25.7435021Z Date: Wed, 06 Dec 2023 00:01:25 GMT
2023-12-06T00:01:25.7435421Z Etag: "0x8DBF51991D2C9B5"
2023-12-06T00:01:25.7436065Z Last-Modified: Mon, 04 Dec 2023 22:37:17 GMT
2023-12-06T00:01:25.7436778Z Server: Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0
2023-12-06T00:01:25.7437392Z X-Ms-Blob-Type: BlockBlob
2023-12-06T00:01:25.7438053Z X-Ms-Creation-Time: Mon, 27 Nov 2023 04:44:43 GMT
2023-12-06T00:01:25.7438669Z X-Ms-Lease-State: available
2023-12-06T00:01:25.7439236Z X-Ms-Lease-Status: unlocked
2023-12-06T00:01:25.7439912Z X-Ms-Request-Id: ff4ccb43-b01e-0064-4bd7-271b36000000
2023-12-06T00:01:25.7440531Z X-Ms-Server-Encrypted: true
2023-12-06T00:01:25.7441098Z X-Ms-Version: 2018-11-09
2023-12-06T00:01:25.7441283Z
2023-12-06T00:01:25.7441550Z { ... tfstate content ... }
2023-12-06T00:01:25.9678396Z ├── provider[registry.terraform.io/hashicorp/azurerm] 3.77.0
2023-12-06T00:01:25.9679059Z └── module.itsi-clients-instance
2023-12-06T00:01:25.9679711Z └── provider[registry.terraform.io/hashicorp/azurerm]
2023-12-06T00:01:25.9679970Z
2023-12-06T00:01:25.9680280Z Providers required by state:
2023-12-06T00:01:25.9680461Z
2023-12-06T00:01:25.9680989Z provider[registry.terraform.io/hashicorp/azurerm]
2023-12-06T00:01:25.9681237Z
2023-12-06T00:01:28.3182800Z [command]/azp/_work/_tool/terraform/1.6.3/x64/terraform apply -auto-approve -no-color dev.tfplan
2023-12-06T00:01:28.3729789Z 2023-12-06T00:01:28.372Z [INFO] Terraform version: 1.6.3
2023-12-06T00:01:28.3736987Z 2023-12-06T00:01:28.373Z [DEBUG] using github.com/hashicorp/go-tfe v1.36.0
2023-12-06T00:01:28.3743755Z 2023-12-06T00:01:28.374Z [DEBUG] using github.com/hashicorp/hcl/v2 v2.19.1
2023-12-06T00:01:28.3749566Z 2023-12-06T00:01:28.374Z [DEBUG] using github.com/hashicorp/terraform-svchost v0.1.1
2023-12-06T00:01:28.3752576Z 2023-12-06T00:01:28.375Z [DEBUG] using github.com/zclconf/go-cty v1.14.1
2023-12-06T00:01:28.3757010Z 2023-12-06T00:01:28.375Z [INFO] Go runtime version: go1.21.3
2023-12-06T00:01:28.3761825Z 2023-12-06T00:01:28.375Z [INFO] CLI args: []string{"/azp/_work/_tool/terraform/1.6.3/x64/terraform", "apply", "-auto-approve", "-no-color", "dev.tfplan"}
2023-12-06T00:01:28.3767972Z 2023-12-06T00:01:28.376Z [DEBUG] Attempting to open CLI config file: /root/.terraformrc
2023-12-06T00:01:28.3827333Z 2023-12-06T00:01:28.382Z [DEBUG] File doesn't exist, but doesn't need to. Ignoring.
2023-12-06T00:01:28.3836064Z 2023-12-06T00:01:28.383Z [DEBUG] ignoring non-existing provider search directory terraform.d/plugins
2023-12-06T00:01:28.3843243Z 2023-12-06T00:01:28.383Z [DEBUG] ignoring non-existing provider search directory /root/.terraform.d/plugins
2023-12-06T00:01:28.3849430Z 2023-12-06T00:01:28.384Z [DEBUG] ignoring non-existing provider search directory /root/.local/share/terraform/plugins
2023-12-06T00:01:28.3855014Z 2023-12-06T00:01:28.385Z [DEBUG] ignoring non-existing provider search directory /usr/local/share/terraform/plugins
2023-12-06T00:01:28.3862171Z 2023-12-06T00:01:28.385Z [DEBUG] ignoring non-existing provider search directory /usr/share/terraform/plugins
2023-12-06T00:01:28.3870380Z 2023-12-06T00:01:28.386Z [INFO] CLI command args: []string{"apply", "-auto-approve", "-no-color", "dev.tfplan"}
2023-12-06T00:01:28.4399422Z 2023-12-06T00:01:28.439Z [INFO] Testing if Service Principal / Client Certificate is applicable for Authentication..
2023-12-06T00:01:28.4405177Z 2023-12-06T00:01:28.440Z [INFO] Testing if Multi Tenant Service Principal / Client Secret is applicable for Authentication..
2023-12-06T00:01:28.4409470Z 2023-12-06T00:01:28.440Z [INFO] Testing if Service Principal / Client Secret is applicable for Authentication..
2023-12-06T00:01:28.4414909Z 2023-12-06T00:01:28.441Z [INFO] Testing if OIDC is applicable for Authentication..
2023-12-06T00:01:28.4419667Z 2023-12-06T00:01:28.441Z [INFO] Using OIDC for Authentication
2023-12-06T00:01:28.4424821Z 2023-12-06T00:01:28.442Z [INFO] Getting OAuth config for endpoint https://login.microsoftonline.com/ with tenant e3c***73c
2023-12-06T00:01:28.4429990Z 2023-12-06T00:01:28.442Z [DEBUG] Obtaining an MSAL / Microsoft Graph token for Resource Manager..
2023-12-06T00:01:30.9746131Z 2023-12-06T00:01:30.973Z [DEBUG] checking for provisioner in "."
2023-12-06T00:01:30.9753691Z 2023-12-06T00:01:30.974Z [DEBUG] checking for provisioner in "/azp/_work/_tool/terraform/1.6.3/x64"
2023-12-06T00:01:31.0389911Z 2023-12-06T00:01:31.038Z [INFO] backend/local: starting Apply operation
2023-12-06T00:01:31.0398817Z 2023-12-06T00:01:31.039Z [DEBUG] Building the Blob Client from an Access Token (using user credentials)
2023-12-06T00:09:01.5206578Z
2023-12-06T00:09:01.5213760Z Error: error loading state: Error retrieving keys for Storage Account "itsi***ops": autorest/Client#Do: Preparing request failed: StatusCode=0 -- Original Error: clientCredentialsToken: received HTTP status 401 with response: {"error":"invalid_client","error_description":"AADSTS700024: Client assertion is not within its valid time range. Current time: 2023-12-06T00:05:01.4821348Z, assertion valid from 2023-12-04T22:36:40.0000000Z, expiry time of assertion 2023-12-04T22:41:40.0000000Z. Review the documentation at https://docs.microsoft.com/azure/active-directory/develop/active-directory-certificate-credentials . Trace ID: 883fdc44-ecca-43a1-b06a-814700b29800 Correlation ID: a0e44aa3-ef9a-4c5f-a199-3472dfd6b988 Timestamp: 2023-12-06 00:05:01Z","error_codes":[700024],"timestamp":"2023-12-06 00:05:01Z","trace_id":"883fdc44-ecca-43a1-b06a-814700b29800","correlation_id":"a0e44aa3-ef9a-4c5f-a199-3472dfd6b988","error_uri":"https://login.microsoftonline.com/error?code=700024"}
2023-12-06T00:09:01.5222157Z
2023-12-06T00:09:01.5334485Z ##[error]Error: The process '/azp/_work/_tool/terraform/1.6.3/x64/terraform' failed with exit code 1
2023-12-06T00:09:01.5410436Z ##[section]Finishing: Terraform apply
Terraform Apply task v4 definition:
steps:
- task: ms-devlabs.custom-terraform-tasks.custom-terraform-release-task.TerraformTaskV4@4
displayName: 'Terraform apply'
inputs:
command: apply
workingDirectory: '$(System.DefaultWorkingDirectory)/environments/$(prefix)/$(project_name)/$(client_name)'
commandOptions: '-no-color $(project_name).tfplan'
environmentServiceNameAzureRM: '$(azure_service)'
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 2
- Comments: 16 (8 by maintainers)
Are there any workarounds for this? We’d like to introduce approval step between creating terraform plan and applying it, but everytime there’s more than like 10 minutes (not an hour) between plan and apply, the token stored within the plan is already expired. There seems to be no way to force using fresh token instead the one stored within the plan.
To be clear. This is really a limitation of the Terraform backend auth implementation as opposed to these tasks. We are trying our best work around this limitation in the tasks, but at the end of the day, the Terraform backend (and providers) are responsible for token management. If the backend supported Azure CLI auth like the providers, then it would be much easier to work around these timeout problems. As such, I suggest you upvote this issue: https://github.com/hashicorp/terraform/issues/34322
Also, the backend and providers request a new access token for certain operations rather than using a cached one. If there was a way to supply an access token directly to the provider / backend or tell it to cache an access token, then these timeout problems would go away. For other methods (MSI and Client Secret) we don’t see the same problem since the source creds do not have a timeout or have a long time out. But behind the scenes they are doing the same thing and getting a new access token. The access token timeout is much longer than the id token, but we see these timeout issues because the provider / backend keeps requesting new tokens throughout the run. This is not something that can be handled by the task since it is the inner working of the provider / Terraform CLI which the task has no control over. For Azure CLI, it is responsible for caching the access token, so that solves the problem, because the provider / backend just asks it for the token rather than trying to generate a new one each time.
I keep running into this issue as well with azurerm_api_management, and still after updating to Terraform 1.9.2 and azurerm provider 3.112.0. APIM should take 8-15 min max but instead times out after 40+ min, resulting in OP’s error.
Having expired token issues with workload identity in both tasks: TerraformTaskV4 and AzureCLI. When you enable addSpnToEnvironment the token is only valid for 10 minutes.
src: https://learn.microsoft.com/en-us/azure/devops/pipelines/release/troubleshoot-workload-identity?view=azure-devops#error-messages
I’m running into the same issue.
I don’t want to manage my service connections by hand; that’s why I’m using this task. Handling token expiry is something I expect this task to handle.
I just switched to the Azure CLI task to be honest