terraform-provider-snowflake: Error: Private key not recognized, key is not in PEM format
Terraform CLI and Provider Versions
terraform {
required_version = ">= 1.3.0"
required_providers {
snowflake = {
source = "Snowflake-Labs/snowflake"
version = "0.84.1"
}
}
backend "s3" {}
}
Terraform Configuration
provider "snowflake" {
account = jsondecode(module.aws-secrets-manager-get-secret.snowflake-credentials-secret-string)["SnowflakeAccount"]
user = jsondecode(module.aws-secrets-manager-get-secret.snowflake-credentials-secret-string)["SnowflakeUser"]
authenticator = "JWT"
private_key = local.my-rsa-key # Stored locally for testing. The key itself is represented in the Expected Behavior section.
role = var.default-snowflake-role
warehouse = var.default-snowflake-warehouse
params = {
query_tag = "Snowflake CI/CD"
}
}
Expected Behavior
The behavior I am expecting is that the private key is accepted by the Snowflake provider. The structure of my private key is as follows (This is a dummy key, but follows the exact same structure as the actual private key that I am providing to the provider) :
The key is of length: 4096
-----BEGIN PRIVATE KEY-----
MIIJJwIBAAKCAgBbKusUIihl3myn27+OKsV1xpuRv9ik9wTNhbJOPl8+I5b1zjsQ
F4CZ3kdJCyi/gHRPGEVn7NV5d066v8dMvkXzy1mRIH9Lysy2RVjEBjO0J+Gk9kDL
DciTdkkYMdI+9p016vE7UdxbyIJRT5uJ9KsdZMkkt5CS9sUiDophDw4b7WT7ijOR
...
yfHY0fgAO9+6d5l5d5u4iNowbGGPEOkqSeM9qVj4VamIpauHVhO0R8v4sySaukF4
KWKKojFMSNUCYGk8f0v6+RYeUAIteNCJq5xgmEqZ+sYyyLCit38dSve9TZ8erBGg
GwZ92w+osBZvR0qia2xYuJUpoS8NaJSlS2GCYigNKoldaN3QDEfHvm7Kxw==
-----END PRIVATE KEY-----
Actual Behavior
Below is the output I receive when performing terraform apply.
│ Error: could not retrieve private key: could not parse private key, key is not in PEM format
│
│ with provider["[registry.terraform.io/snowflake-labs/snowflake](http://registry.terraform.io/snowflake-labs/snowflake)"],
│ on [main.tf](http://main.tf/) line 34, in provider "snowflake":
│ 34: provider "snowflake" {
I have read through some open issues regarding private keys with the Snowflake provider and have adhered to the suggestions found, such as providing the Snowflake provider with the authenticator = "JWT" parameter.
Steps to Reproduce
- Create an RSA key pair in Snowflake.
- Store the private key in an AWS Secrets Manager secret.
- Run
terraform plan.
How much impact is this issue causing?
High
Logs
No response
Additional Information
Using a password works just fine, but is no viable for our production environment.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 16
This is how I reconstruct the private key
locals { processed-secret-string = jsondecode(data.aws_secretsmanager_secret_version.snowflake-secret-version.secret_string) 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-----”]) }
Sorry for the bad formatting, hope that helps!
On Mon, Jun 24, 2024, 6:34 AM Naveen kumar @.***> wrote:
This shows how the Snowflake terraform provider is expecting the Private Key: https://github.com/snowflakedb/gosnowflake/blob/master/secret_detector.go
privateKeyPattern =
(?im)-----BEGIN PRIVATE KEY-----\\n([a-z0-9/+=\\n]{32,})\\n-----END PRIVATE KEY-----If I recall correctly, the AWS Terraform provider returns the private key/rsa key with a bunch of new lines and return carriages.
On Sun, Jun 23, 2024 at 11:16 PM Cameron Gibson @.***> wrote:
Hey, I will get back to you in the morning, it has to do with the way the private key is formatted so I had to use a couple of regex functions to deconstruct the private key after I pulled it from AWS and then put it back together in the correct format.
I’ll send you the actual terraform when I get to work.
Also, if you don’t want to wait, the way I figured this out was I looked at the go backend for the go-snowflake package which the Snowflake terraform provider uses as its back end. There is some code in there (don’t remember where) that shows an exact regular expression that the private key is retrieved with which will tell you exactly how the key needs to be formatted.
On Sun, Jun 23, 2024, 7:00 PM Naveen kumar @.***> wrote:
Thank you for the response @sfc-gh-asawicki, I literally just figured out what was wrong and it had everything to do with how the private key was being stored in AWS Secrets Manager and all is working as expected now!
Moving this issue to closed.
Hey @ToxicCypher. Thanks for reporting the issue.
At first glance, it does not look like a problem with the provider. The error code 08004 indicates that the connection was rejected. The error 390144 is mentioned here: https://docs.snowflake.com/en/user-guide/key-pair-auth-troubleshooting#list-of-errors with possible troubleshooting:
Please make sure that your key is generated correctly according to docs. You should copy the exact content from the generated key file.
Also, you can try using
private_keywith terraformfilefunction like:where
<filepath>points to your private key file.