-
Notifications
You must be signed in to change notification settings - Fork 214
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 jumphost with ssh-agent support #129
base: main
Are you sure you want to change the base?
Conversation
@cyrilgdn please let me know your opinion |
2171167
to
78be3cf
Compare
@cyrilgdn I updated the PR, tests are OK. Can you do the review, please? |
@jindrichskupa I'll take a look, sorry for the delay. |
No problem. there shouldn't be a problem, tests are updated without failing. Only merge conflicts I can resolve. |
The tests are passing locally. https://github.com/cyrilgdn/terraform-provider-postgresql/pull/129/checks?check_run_id=3701848416 |
Thanks for this long awaited feature ! Can't wait for it |
@jindrichskupa Could you merge master again please, the |
@jindrichskupa writing comments in my code for me + X months when I'll be able to implement this, I though about suggestions. To illustrate those, here's my use case scenario
In here, I can't be sure the bastion is up and running when I wanna create my database. I added a wait for it like this data "aws_instances" "bastions" {
instance_tags = {
"aws:autoscaling:groupName" = "${var.name}-bastion"
}
}
resource "null_resource" "wait_for_a_bastion" {
provisioner "local-exec" {
command = "wait-for-it ${data.aws_instances.bastions.public_ips[0]}:22000 --timeout 600; done"
interpreter = ["bash"]
}
} But I wish the proposed usage could just implement a retry mechanism, with juste a couple of parameters saying like every 5 sec for 600 sec. Second point, I don't saw how I could pass down the private key in your config snippet without going out of Terraform (writing the key in a file), which make this less secure ? |
I built the commit 78be3cf, then symlinked it like this: lrwxr-xr-x 47 a0s 14 Oct 9:51 /Users/a0s/.terraform.d/plugins/localhost/cookielab/postgresql/1.14.0-add-ssh-jumphost-support/darwin_amd64/terraform-provider-postgresql -> /Users/a0s/go/bin/terraform-provider-postgresql My config: terraform {
required_providers {
postgresql = {
source = "localhost/cookielab/postgresql"
version = "1.14.0-add-ssh-jumphost-support"
}
}
}
provider "postgresql" {
host = module.test[0].rds_cluster_endpoint
username = module.test[0].rds_cluster_master_username
password = module.test[0].rds_cluster_master_password
jumphost {
host = aws_route53_record.jumphost.name
user = local.jumphost_user
port = 22
local_port = 15432
private_key = tls_private_key.jumphost.private_key_pem
}
}
resource "postgresql_role" "test" {
name = "test"
login = false
password = "test"
} The result was:
Any suggestion how to debug it? :) |
postgresql/provider.go
Outdated
@@ -133,6 +135,45 @@ func Provider() terraform.ResourceProvider { | |||
Description: "Specify the expected version of PostgreSQL.", | |||
ValidateFunc: validateExpectedVersion, | |||
}, | |||
"jumphost": { | |||
Type: schema.TypeSet, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@a0s I had the same issue. I was able to fix it by changing this line to Type: schema.TypeList,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. I will update the PR.
you can use ssh-agent or as described under your question the private key is a string:
or
provided via ENV variables like |
I see. Yes, I can add retry and timeout params. There are in general two steps:
Will check the sshtunnel module. Should I add it now or in next version? |
@jindrichskupa Having timeout option would be very useful! I am getting timeout error in 50% of runs:
|
@a0s this could have different reason, but I will take a look on that
|
I'm not in a hurry, Meanwhile I came up with the following, until you're done ...
data "aws_instances" "bastions" {
depends_on = [module.bastion]
instance_tags = {
"aws:autoscaling:groupName" = "${var.name}-bastion"
}
}
# XXX: Remove the SSH tunnel once postgresql provider can do this itself https://github.com/cyrilgdn/terraform-provider-postgresql/pull/129, and maybe the wait for it if there's a retry mechanism
resource "null_resource" "wait_for_bastion" {
triggers = { always_run = "${timestamp()}" }
provisioner "local-exec" {
command = "wait-for-it ${data.aws_instances.bastions.public_ips[0]}:22000 --timeout=600"
}
}
# Half random local port for the tunnel
resource "random_integer" "tunnel_local_port" {
seed = var.name
min = 54300
max = 54399
keepers = {
bastion_ip = data.aws_instances.bastions.public_ips[0]
}
}
resource "null_resource" "open_tunnel_to_postgres_instance" {
depends_on = [null_resource.wait_for_bastion]
triggers = { always_run = "${timestamp()}" }
provisioner "local-exec" {
command = "screen -d -m ssh -i ${var.name}_bastion_private_key.pem -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -L '${random_integer.tunnel_local_port.result}:${module.rds-postgres-instance.instance_address}:5432' wazo@${data.aws_instances.bastions.public_ips[0]} -p 22000 sleep 600"
}
}
# https://registry.terraform.io/providers/cyrilgdn/postgresql/latest/docs
provider "postgresql" {
scheme = "postgres"
# host = module.rds-postgres-instance.instance_address
# port = 5432
host = "localhost"
port = random_integer.tunnel_local_port.result
username = module.rds-postgres-instance.admin_role
password = module.rds-postgres-instance.admin_password
sslmode = "disable" # Terraform goes through a tunnel, hence the certificate RDS hostname & tunnel's localhost don't match
connect_timeout = 15
# We're not superuser in AWS RDS Postgres
superuser = false
}
... |
@jindrichskupa We just found a strange bug. The plugin seems to check if there is a |
@cyrilgdn @jindrichskupa Are there any plans for the future of this feature / pull request? |
* Upgrade Terraform SDK to v2 * Rename Makefile
cc9d685
to
9597bd9
Compare
This would make using the provider feasible with recommended AWS network structures. It's great that so much work has been put into the PR! Is there still an unsupported requirement preventing this from being merged? |
This would be amazing! Any updates here? |
commit b276055 Merge: ac282ce ec71102 Author: Marvin Willms <marvin.willms@code.berlin> Date: Tue Apr 5 01:27:38 2022 +0200 Merge branch 'master' into add-ssh-jumphost-support commit ac282ce Author: Marvin Willms <marvin.willms@code.berlin> Date: Tue Apr 5 00:54:43 2022 +0200 ignore `.idea` commit fda6f16 Author: Marvin Willms <marvin.willms@code.berlin> Date: Tue Apr 5 00:17:56 2022 +0200 remove `vendor` dir commit 6046175 Author: Marvin Willms <marvin.willms@code.berlin> Date: Tue Apr 5 00:17:30 2022 +0200 Run `go mod tidy` commit 9597bd9 Author: Jindrich Skupa <jindrich.skupa@gmail.com> Date: Tue Nov 30 13:29:36 2021 +0100 updates commit fe82ecc Author: Jindrich Skupa <jindrich.skupa@gmail.com> Date: Tue Oct 19 17:34:51 2021 +0200 Fix for suggestion https://github.com/cyrilgdn/terraform-provider-postgresql/pull/129\#discussion_r728776913 commit 1d1deac Author: Jindrich Skupa <jindrich.skupa@gmail.com> Date: Thu Aug 26 09:23:06 2021 +0200 Add jumphost with ssh-agent and private key support commit 88d2c11 Author: Cyril Gaudin <cyril.gaudin@gmail.com> Date: Sat Sep 25 00:08:13 2021 +0200 Upgrade Terraform SDK to v2 (cyrilgdn#140) * Upgrade Terraform SDK to v2 * Rename Makefile commit 91568cb Author: Cyril Gaudin <cyril.gaudin@gmail.com> Date: Fri Sep 24 18:10:28 2021 +0200 Remove vendor directory and clean a bit Makefile (cyrilgdn#139) commit 89556ba Author: Jindrich Skupa <jindrich.skupa@gmail.com> Date: Thu Aug 26 09:23:06 2021 +0200 Add jumphost with ssh-agent and private key support
* Add jumphost with ssh-agent and private key support * Remove vendor directory and clean a bit Makefile (cyrilgdn#139) * Upgrade Terraform SDK to v2 (cyrilgdn#140) * Upgrade Terraform SDK to v2 * Rename Makefile * Add jumphost with ssh-agent and private key support * Fix for suggestion https://github.com/cyrilgdn/terraform-provider-postgresql/pull/129\#discussion_r728776913 * updates * Run `go mod tidy` * remove `vendor` dir * ignore `.idea` * log error when tunnel start returns error * run `go get -u github.com/cookielab/go-ssh-tunnel` * ignore `vendor` dir * move `golang.org/x/crypto` and `golang.org/x/sys` * Upgrade golangci-lint-action * Upgrade go version of test workflow * Add last line * Run `go mod tidy` * Remove `ulimits` * Make tests manually run Co-authored-by: Jindrich Skupa <jindrich.skupa@gmail.com> Co-authored-by: Cyril Gaudin <cyril.gaudin@gmail.com>
Hey @jindrichskupa @cyrilgdn, despite this being a pretty old PR, I'd still like to see this feature merged. I would be happy to volunteer to create a new PR if @jindrichskupa does not have the time to look into this. It would include changes such as the |
f2c2e47
to
dea1401
Compare
Issues
Resolves: #81
Native Go jumphost (ssh port forwarding) support
jumphost
- jumphost configuration blockhost
/$JUMPHOST
jumphost hostnameuser
/$JUMPHOST_USER
jumphost usernameport
/$JUMPHOST_PORT
jumphost custom ssh-portlocal_port
/$JUMPHOST_LOCALPORT
local port for postgresql accessprivate_key
$JUMPHOST_PRIVATE_KEY
private key for user auth as stringsql.Open
Example configuration
Possible extensions