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

Add Importer for tls_private_key #133

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Miouge1
Copy link

@Miouge1 Miouge1 commented Sep 29, 2021

Add Importer for tls_private_key.

I have tested it with the following Terraform file:

resource "tls_private_key" "rsa" {
  algorithm = "RSA"
}

resource "tls_private_key" "ecdsa" {
  algorithm = "ECDSA"
}

resource "tls_self_signed_cert" "rsa" {
  key_algorithm   = tls_private_key.rsa.algorithm
  private_key_pem = tls_private_key.rsa.private_key_pem
  validity_period_hours = 12
  allowed_uses = []
  subject {
      common_name  = "example.com"
  }
}

resource "tls_self_signed_cert" "ecdsa" {
  key_algorithm   = tls_private_key.ecdsa.algorithm
  private_key_pem = tls_private_key.ecdsa.private_key_pem
  validity_period_hours = 12
  allowed_uses = []
  subject {
      common_name  = "example.com"
  }
}
$ terraform plan
[...]
Plan: 4 to add, 0 to change, 0 to destroy.
[...]
$ terraform import tls_private_key.rsa rsa.key
tls_private_key.rsa: Importing from ID "rsa.key"...
tls_private_key.rsa: Import prepared!
  Prepared tls_private_key for import
tls_private_key.rsa: Refreshing state... [id=4c4dc28e87f9674022a0443506e6621f1ce0d3a2]

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.
$ import tls_private_key.ecdsa secp224r1.pem
tls_private_key.ecdsa: Importing from ID "secp224r1.pem"...
tls_private_key.ecdsa: Import prepared!
  Prepared tls_private_key for import
tls_private_key.ecdsa: Refreshing state... [id=2875f8a9af6e56b5d5c8dde67241afc1112aa355]

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.
$ terraform plan
[...]
Plan: 2 to add, 0 to change, 0 to destroy.
[...]
$ terraform apply
[...]
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.

Resolves !44

@hashicorp-cla
Copy link

hashicorp-cla commented Sep 29, 2021

CLA assistant check
All committers have signed the CLA.

@fcrespofastly
Copy link

Can anyone review this please? This is a very important feature solves #44 which many users are struggling with.

@dkulchinsky
Copy link

Thank you for this PR @Miouge1!

This fixes #44, and looks like a solid approach.

We really need this and were thinking of doing a similar PR ourselves, but if this is already in place what's the chance this gets reviewed and pulled in?

@dekimsey
Copy link

dekimsey commented Jul 6, 2022

I ran into needing to do this on a set of private keys today. Unfortunately the codebase has moved since this PR was written and it no longer functions as is. I was able to get it working and will try to open a new PR with the updated changes.

@aletournel
Copy link

aletournel commented Oct 25, 2022

We found a workaround by directly adding our private key in the state :

  • We first created the tls_private_key ressource in the terraform state by doing terraform apply. A private key is generated but we don't care about that one. We just need that the tls_private_key ressource exists in the terraform state
  • We pulled the state locally with terraform state pull > tmp.tfstate
  • We modified the fields of the tls_private_key ressource directly in the json and increased the serial of the state
  • We pushed the modified state containing our private key in it with terraform state push tmp.tfstate

A bit hacky but it worked in our case.
Nevertheless with the import, it would have been so much easier ! So would be nice to have this feature soon.

@jwaldrip
Copy link

jwaldrip commented Mar 6, 2023

Any update on this? Seems to be quite a long time open for a helpful feature. @Miouge1 could you bring it up to date with mainline?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants