-
Notifications
You must be signed in to change notification settings - Fork 420
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
[Bug]: private_key variable is not in PEM format #2899
Comments
I am not using the pem file since I can not input the file within the Terraform Cloud UI variables, instead i am using the value of the pem file. When using the value hardcoded as shown below it works. provider "snowflake" { However, when using the same exact value in Terraform Cloud UI variable TF_VAR_snowflake_private_key, it fails with the error "Error: could not retrieve private key: could not parse private key, key is not in PEM format". |
@NLPatras did you check the related issues in detail? The last one describes almost the same case with the solution (#2432 (comment)). |
Yes, I did, the pem key that I have used hard coded and it worked was already of the below format having a \n for every new line. |
Did you try the solution from #2432 (comment) ? It does not seem like a problem with the provider but with how it is passed to it (as you said, the key directly in the provider config works). |
The solution provided above, seems to not be suitable for my case. My private key is already in the format that he was trying to achieved with the following commands: pre-processed-private-key-body = regex("-----BEGIN PRIVATE KEY----- (.*)-----END PRIVATE KEY-----", local.processed-secret-string["PrivateKey"])[0] processed-private-key-body = replace(local.pre-processed-private-key-body, " ", "\n") processed-private-key = join("\n", ["-----BEGIN PRIVATE KEY-----", local.processed-private-key-body, "-----END PRIVATE KEY-----"]) |
What I can recommend to check is to set
(they will be taken from the above envs). It should not change anything but it's worth try. Otherwise, I would still claim that the format must be incorrect in some way (because we do not process it anyhow, and as you claim, setting it directly works). |
Getting same exact error! |
I currently see no other option than the key being formatted badly (assuming no hidden configs like envs are set additionally). For the current implementation, there is no difference between passing the key directly through the config and setting it as an environment variable. If you have already tried out the solution referenced above (this one: #2899 (comment)), then there is not much I can offer more. You can spin out a short test in Golang that checks the output of
|
I have tried asserting the private key using python script, and it passed the test. Would it make a difference asserting it with golang or python? |
Yes. The suggested |
The assertion of private key with the above script running in golang is a PASS. It is the same exact private key that was hard coded. To recall the private key has the below format. Just a remainder \n has a single backslash. -----BEGIN ENCRYPTED PRIVATE KEY-----\n...\n...\n...\n-----END ENCRYPTED PRIVATE KEY-----\n |
The actual issue is that the Terraform Cloud UI adds to the variables an extra back slash thus if my input has "\n" or "\t" , Terraform Cloud UI returns it as double backslash "\\n" or "\\t" . Therefore the solution is as follow:
With Terraform Cloud UI env variables named in this way: TF_VAR_snowflake_account |
@NLPatras, so as I understand you were able to make it work with the above solution, then? :) |
Yes, made it work. I had to print out the private_key and private_key_passphrase to see how Terraform Cloud UI is injecting these variables. It was observed that Terraform Cloud UI adds extra back slash to the above mentioned input values. Thus making the private_key not being PEM format. The solution was to replace "\\n" to "\n", and since my private_key_passphrase contained a "\t", also had to replace "\\t" to "\t". |
Great to hear that! I will add this to the documentation for future reference. I am closing the issue. |
Terraform CLI Version
1.8.5
Terraform Provider Version
0.92.0
Terraform Configuration
Category
category:provider_config
Object type(s)
resource:account_parameter
Expected Behavior
Successful run of terraform apply/plan
Actual Behavior
Error: could not retrieve private key: could not parse private key, key is not in PEM format
Steps to Reproduce
Terraform Cloud UI env variables :
TF_VAR_snowflake_account
TF_VAR_snowflake_user
TF_VAR_snowflake_authenticator
TF_VAR_snowflake_private_key - sensitive
TF_VAR_snowflake_private_key_passphrase - sensitive
provider "snowflake" {
account = var.snowflake_account
user = var.snowflake_user
authenticator = var.snowflake_authenticator
private_key = var.snowflake_private_key
private_key_passphrase = var.snowflake_private_key_passphrase
}
3. terraform plan
How much impact is this issue causing?
Medium
Logs
No response
Additional Information
No response
Would you like to implement a fix?
The text was updated successfully, but these errors were encountered: