-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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 ssl certificate outputs resources for cloud sql postgres #635
Comments
It'd be great start to export the |
Expose the 'ServerCaCert' object from the Cloud SQL Instance[1] Related: hashicorp#635 - Solves first point [1] https://cloud.google.com/sql/docs/mysql/admin-api/v1beta4/instances#resource
Add support for Cloud SQL CA Cert (#635)
Hey folks, Just wanted to confirm, we still can't automate client certs creation, right? Thanks, |
I believe we are still missing the necessary resource for that. I do not believe anyone is working on that at the moment. PRs would be accepted, or we'll get to it as soon as we can. Unfortunately, there's a lot going on in the provider right now, and we have only a handful of maintainers, so sometimes we can take longer to get to features than we'd like. |
I've been trying to automate handover from google_cloud_sql to postgres provider and came up with the solution below, I hope this workaround helps somebody until proper solution is in place.
# Adjust file names to match Terraform files
PGSSLCERT=terraform-client-cert.pem
PGSSLKEY=terraform-client-key.pe
PGSSLROOTCERT=terraform-ca-cert.pem Shell script is required due to hashicorp/terraform-provider-postgresql#49
# Create Terraform user
resource "google_sql_user" "terraform" {
name = "terraform"
project = "${module.project.project_id}"
instance = "${google_sql_database_instance.cluster_db.id}"
password = "${random_string.tf_tmp.result}"
}
# Specify location of cert/key files
data "null_data_source" "tf_sql_ssl_files" {
inputs {
client_key = "${path.root}/${google_sql_user.terraform.name}-client-key.pem"
client_key_sha = "${path.root}/${google_sql_user.terraform.name}-client-key.sha1"
client_cert = "${path.root}/${google_sql_user.terraform.name}-client-cert.pem"
ca_cert = "${path.root}/${google_sql_user.terraform.name}-ca-cert.pem"
}
}
# Save cert/key files
resource "null_resource" "db_certs" {
provisioner "local-exec" {
command = "gcloud sql ssl client-certs create terraform ${data.null_data_source.tf_sql_ssl_files.outputs.client_key} --instance=${google_sql_database_instance.cluster_db.id} --project=${module.project.project_id} --format='value(sha1_fingerprint)' > ${data.null_data_source.tf_sql_ssl_files.outputs.client_key_sha}"
}
provisioner "local-exec" {
command = "gcloud sql ssl client-certs describe terraform --instance=${google_sql_database_instance.cluster_db.id} --project=${module.project.project_id} --format='value(cert)' > ${data.null_data_source.tf_sql_ssl_files.outputs.client_cert}"
}
provisioner "local-exec" {
command = "gcloud sql instances describe ${google_sql_database_instance.cluster_db.id} --project=${module.project.project_id} --format='value(serverCaCert.cert)' > ${data.null_data_source.tf_sql_ssl_files.outputs.ca_cert}"
}
triggers {
user = "${google_sql_user.terraform.id}"
}
}
provider "postgresql" {
alias = "ssl"
host = "${var.auth_host}"
username = "${var.auth_user}"
password = "${var.auth_pass}"
sslmode = "verify-ca"
} File .sha1 contains cert fingerprint and is not necessary for connection but useful for rotation. |
@silvpol Have you managed to get this to work with I have not been able to get the postgres provider to connect to the cloud sql instance without running the cloud sql proxy alongside terraform and have to remove the postgres provider if the |
@olib963 I have tried provisioning from scratch again on a separate environment and you're right, this needs to be done in two steps:
Our top level file is split into main env module and then series of namespaces, so our workflow is now like this:
We automatically enable current terraform IP for SSL connection with a snippet like this inside our environment module: locals {
terraform_local_cidr = "${chomp(data.http.local_ip.body)}/32"
}
data "http" "local_ip" {
url = "https://ifconfig.co/ip"
}
resource "google_sql_database_instance" "cluster_db" {
//...
settings {
ip_configuration {
authorized_networks {
name = "Terraform local IP"
value = "${locals.terraform_local_cidr}"
expiration_time = "${replace(timeadd(timestamp(), "70m"),"/\\d+:\\d+Z/","00:00.000Z")}"
}
}
}
}
It fetches current public IP of terraform machine, adds 1h 10m to current timestamp and rounds down to full hour (to avoid constant diffs). This means that authorized IP will be in the list for only between 10 mins and 1h. Caveat with this approach is that you need to run apply on env module before actual run. |
@silvpol Awesome, thank you so much. I had been having trouble figuring out how to connect terraform without running the proxy. It is a bit frustrating that we need to run it in two separate steps, but it will do for now. |
…#635) Signed-off-by: Modular Magician <[email protected]>
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 feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 [email protected]. Thanks! |
A feature request for:
The text was updated successfully, but these errors were encountered: