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

Error: Private key not recognized, key is not in PEM format #2432

Closed
ToxicCypher opened this issue Jan 26, 2024 · 11 comments
Closed

Error: Private key not recognized, key is not in PEM format #2432

ToxicCypher opened this issue Jan 26, 2024 · 11 comments
Labels
general-usage General help/usage questions

Comments

@ToxicCypher
Copy link

ToxicCypher commented Jan 26, 2024

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

  1. Create an RSA key pair in Snowflake.
  2. Store the private key in an AWS Secrets Manager secret.
  3. 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.

@ToxicCypher ToxicCypher added the bug Used to mark issues with provider's incorrect behavior label Jan 26, 2024
@ToxicCypher
Copy link
Author

I am wondering if there is some form of pre-processing that must be done to the private key before passing it to the Snowflake provider configuration. We typically perform some processing when using the python connector with a private key but to my knowledge this is using the go gosnowflake library.

@ToxicCypher
Copy link
Author

Removing new lines from the private key body stops complaining about the RSA key not being in a PEM format, but then compains about the JWT being invalid.

Sample Key:

-----BEGIN PRIVATE KEY-----
MIIJJwIBAAKCAgBbKusUIihl3myn27+OKsV1xpuRv9ik9wTNhbJOPl8+I5b1zjsQ...GwZ92w+osBZvR0qia2xYuJUpoS8NaJSlS2GCYigNKoldaN3QDEfHvm7Kxw==
-----END PRIVATE KEY-----

│ Error: open snowflake connection: 390144 (08004): JWT token is invalid. [ff4e4855-9e30-4213-b0b6-edcc5d94f200]
│ 
│   with provider["[registry.terraform.io/snowflake-labs/snowflake](http://registry.terraform.io/snowflake-labs/snowflake)"],
│   on [main.tf](http://main.tf/) line 36, in provider "snowflake":
│   36: provider "snowflake" {
│ 
╵

@sfc-gh-asawicki sfc-gh-asawicki added general-usage General help/usage questions and removed bug Used to mark issues with provider's incorrect behavior labels Jan 29, 2024
@sfc-gh-asawicki
Copy link
Collaborator

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:

390144 JWT_TOKEN_INVALID There is a general issue with the JWT token. For possible solutions, see Common Errors and Solutions.

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_key with terraform file function like:

private_key = file("<filepath>")

where <filepath> points to your private key file.

@ToxicCypher
Copy link
Author

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.

@NaveenAutomate
Copy link

HI @ToxicCypher , I am having same issue, when I give private key directly into terraform provider it works but when I retrieve from aws secrets manager it doesnt work, looks like its not copying the newlines to secrets maanger.

How did you manage to resolve this ?

@ToxicCypher
Copy link
Author

ToxicCypher commented Jun 24, 2024 via email

@ToxicCypher
Copy link
Author

ToxicCypher commented Jun 24, 2024 via email

@NaveenAutomate
Copy link

@ToxicCypher It would be great if you can share the bits of code.
I tried different with different newlines but nothing worked! :(
TIA

@ToxicCypher
Copy link
Author

ToxicCypher commented Jun 24, 2024 via email

@NaveenAutomate
Copy link

Thanks @ToxicCypher it worked!

@ToxicCypher
Copy link
Author

ToxicCypher commented Jun 24, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
general-usage General help/usage questions
Projects
None yet
Development

No branches or pull requests

3 participants