flaupretre/terraform-ssh-tunnel

SSH tunnel isn't working everytime

Closed this issue · 3 comments

Hi,
After everything up, it I try to play apply again I get :

Error: Unable to create client for remote [XXXXXX]: Get "https://localhost:33517/1.0": Unable to connect to: localhost:33517

Nothing change that except delete terraform.tfstate file

Thanks for your attention,
Regards,

Hi, After everything up, it I try to play apply again I get :

Error: Unable to create client for remote [XXXXXX]: Get "https://localhost:33517/1.0": Unable to connect to: localhost:33517

Nothing change that except delete terraform.tfstate file

Thanks for your attention, Regards,

Try to place module db_tunnel at the beginnig of the main.tf in your root path.module and then remove timeout variable from main.tf, variables.tf and tunnel.sh:

# root main.tf

module "ssh-tunnel" {
  source = "./modules/ssh-tunnel"

  target_host = $your_target_host 
  target_port = $port
  gateway_host = $your_gateway_host
  gateway_user = $user
} 
...

# main.tf

locals {
  gw_prefix = (var.gateway_user == "" ? "" : "${var.gateway_user}@")
}
  

data external free_port {
  program = [
    var.python_cmd,
    "-c",
    "import socket; s=socket.socket(); s.bind((\"\", 0)); print(\"{ \\\"port\\\": \\\"\" + str(s.getsockname()[1]) + \"\\\" }\"); s.close()"
  ]
}

data external ssh_tunnel {
  count = (var.create ? 1 : 0)
  program = [
    var.shell_cmd,
    "${path.module}/tunnel.sh",
    var.ssh_cmd,
    data.external.free_port.result.port,
    var.target_host,
    var.target_port,
    "${local.gw_prefix}${var.gateway_host}",
    var.gateway_port,
    var.shell_cmd
  ]
}
# tunnel.sh

SSH="$1"
LOCAL_PORT="$2"
TARGET_HOST="$3"
TARGET_PORT="$4"
GATEWAY="$5"
GATEWAY_PORT="$6"
SHELL="$7"
MPID="$8"

ABSPATH=$(cd "$(dirname "$0")"; pwd -P)

if [ -z "$MPID" ] ; then
  echo '{}'
  p=`ps -p $PPID -o "ppid="`
  nohup $SHELL "$ABSPATH/tunnel.sh" $@ $p <&- >&- 2>&- &
  exit 0
fi

$SSH -N -L $LOCAL_PORT:$TARGET_HOST:$TARGET_PORT -p $GATEWAY_PORT $GATEWAY &
CPID=$!

while true ; do
  if ! ps -p $MPID &>/dev/null; then
    break
  fi
  sleep 5
done

kill $CPID

exit 0
# variables.tf

variable "create" {
  type = bool
  description = "If false, do nothing"
  default = true
}

variable "python_cmd" {
  type = string
  description = "Command to run python"
  default = "python"
}

variable "shell_cmd" {
  type = string
  description = "Command to run a shell"
  default = "bash"
}

variable "ssh_cmd" {
  type = string
  description = "Shell command to use to start ssh client"
  default = "ssh"
}

variable "target_host" {
  type = string
  description = "The target host. Name will be resolved by gateway"
}

variable "target_port" {
  type = number
  description = "Target port number"
}

variable "gateway_host" {
  type = any
  description = "Name or IP of SSH gateway"
}

variable "gateway_user" {
  type = any
  description = "User to use on SSH gateway (default = current username)"
  default = ""
}

variable "gateway_port" {
  type = number
  description = "Gateway port"
  default = 22
}

Hi, thanks for your messages. Please confirm that the version I just released fixes the issue.

Note : you should just need to bump module version and use the 'host' output variable as target host in your provider configuration instead of "localhost" (something like 'module.db_tunnel.host', see the terraform-ssh-tunnel-databases project for an example).

Closing issue as I think it's fixed now. Please reopen if it is not the case.