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

Terraform Crash #35641

Closed
Bibz87 opened this issue Aug 26, 2024 · 9 comments · Fixed by #35666
Closed

Terraform Crash #35641

Bibz87 opened this issue Aug 26, 2024 · 9 comments · Fixed by #35666
Labels
backend/k8s bug new new issue not yet triaged

Comments

@Bibz87
Copy link

Bibz87 commented Aug 26, 2024

Terraform Version

Terraform v1.9.5
on windows_amd64
+ provider registry.terraform.io/hashicorp/random v3.6.2

Terraform Configuration Files

terraform {
  backend "kubernetes" {
    config_path="~/.kube/config"
    namespace="terraform-tests"
    secret_suffix="test-01"
  }
}

resource "random_string" "test" {
  length = 8
  special = false
}

Debug Output

https://gist.github.com/Bibz87/2c68fea5383e932e99451051d1bc10d9

Expected Behavior

Terraform doesn't crash during initialisation

Actual Behavior

Attempting to initialise Terraform configuration results in crash

Steps to Reproduce

  1. terraform init

Additional Context

After some testing, it looks like ending the secret suffix with a number causes the crash. Changing secret_suffix from test-01 to test makes the initialisation work properly without crashing.

Note: Terraform does create the Kubernetes secret before crashing.

References

No response

@Bibz87 Bibz87 added bug new new issue not yet triaged labels Aug 26, 2024
@Bibz87
Copy link
Author

Bibz87 commented Aug 26, 2024

I definitely don't feel comfortable downloading and installing random patches from untrusted sources.

@crw
Copy link
Collaborator

crw commented Aug 26, 2024

Looks like spam. Will report to GitHub.

@hashicorp hashicorp deleted a comment from Saschl Aug 26, 2024
@hashicorp hashicorp deleted a comment Aug 26, 2024
@hashicorp hashicorp deleted a comment Aug 26, 2024
@crw
Copy link
Collaborator

crw commented Aug 26, 2024

@Bibz87 Thanks for your report!

@Bibz87
Copy link
Author

Bibz87 commented Aug 26, 2024

Let me know if you need anything from me to help with debugging. 😀

@hashicorp hashicorp deleted a comment Aug 26, 2024
@Saschl
Copy link

Saschl commented Aug 26, 2024

Looks like spam. Will report to GitHub.

Very sorry about that (one of the comments came from my account). Account got compromised and posted this malicious stuff. Should not happen again

@bschaatsbergen
Copy link
Member

Similar to #33995 cc @crw

@bschaatsbergen
Copy link
Member

bschaatsbergen commented Sep 2, 2024

When the secret_suffix is "test-44", the backend attempts to retrieve a secret named "<secret>-test-44". It then uses the Atoi function to parse the last segment of the name, assuming it's an integer index. This is what leads to errors if the suffix isn't actually an index, resulting in the out of bound error.

This assumption seems to be related to how the backend manages Terraform state files. Since state files can be too large to fit into a single Kubernetes secret, the backend chunks the state into multiple secrets, see this part of the secret creation implementation. This chunking mechanism leads to the use of numeric suffixes to differentiate between chunks. However, relying on the assumption that the last part of the secret name is potentially an index can cause issues if the suffix is not intended to be an index when set by the user.

Given the complexity introduced by the existing chunking mechanism, supporting secret suffixes with a trailing -<int> becomes extremely challenging. To avoid potential issues, I recommend not allowing trailing -<int> values in the secret_suffix moving forward.

@bschaatsbergen
Copy link
Member

bschaatsbergen commented Sep 2, 2024

I’ve opened a PR (#35666) to add extra validation for the secret_suffix, preventing it from ending with a trailing -<number>. This helps avoid conflicts with the backend’s chunking mechanism, which appends numeric indices to secret names.

Copy link

github-actions bot commented Nov 4, 2024

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 4, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
backend/k8s bug new new issue not yet triaged
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants