@@ -25,37 +25,28 @@ function get_random_ephemeral_port() {
2525}
2626
2727function get_gateway_address() {
28- TELEPORT_CLUSTER=${1:? }
29- TELEPORT_GATEWAY_NAME=${2:? }
30-
31- NODE_HOST=$(
32- tsh ls --cluster " ${TELEPORT_CLUSTER} " \
33- --query=" labels[\" service\" ] == \" ${TELEPORT_GATEWAY_NAME} \" " \
28+ TP_PROXY=${1:? }
29+ TP_CLUSTER=${2:? }
30+ TP_GATEWAY_NODE=${3:? }
31+ TP_GATEWAY_USER=${4:- root}
32+
33+ TUNNEL_GATEWAY_HOST=$(
34+ tsh ls --proxy " ${TP_PROXY} " --cluster " ${TP_CLUSTER} " \
35+ --query=" labels[\" service\" ] == \" ${TP_GATEWAY_NODE} \" " \
3436 --format names | head -n 1
3537 )
36- echo " root@${NODE_HOST} "
37- }
38-
39- function open_tunnel() {
40- TSH_CLUSTER_NAME=${1:? }
41- TUNNEL_LOCAL_PORT=${2:? }
42- TUNNEL_TARGET_HOST=${3:? }
43- TUNNEL_TARGET_PORT=${4:? }
44- TUNNEL_GATEWAY_ADDRESS=${5:? }
45-
46- tsh ssh --cluster " ${TSH_CLUSTER_NAME} " \
47- -N -L " ${TUNNEL_LOCAL_PORT} :${TUNNEL_TARGET_HOST} :${TUNNEL_TARGET_PORT} " \
48- " ${TUNNEL_GATEWAY_ADDRESS} "
38+ echo " ${TP_GATEWAY_USER} @${TUNNEL_GATEWAY_HOST} "
4939}
5040
5141function open_background_tunnel() {
52- TSH_CLUSTER_NAME=${1:? }
53- TUNNEL_LOCAL_PORT=${2:? }
54- TUNNEL_TARGET_HOST=${3:? }
55- TUNNEL_TARGET_PORT=${4:? }
56- TUNNEL_GATEWAY_ADDRESS=${5:? }
57-
58- tsh ssh --cluster " ${TSH_CLUSTER_NAME} " \
42+ TP_PROXY=${1:? }
43+ TP_CLUSTER=${2:? }
44+ TUNNEL_LOCAL_PORT=${3:? }
45+ TUNNEL_TARGET_HOST=${4:? }
46+ TUNNEL_TARGET_PORT=${5:? }
47+ TUNNEL_GATEWAY_ADDRESS=${6:? }
48+
49+ tsh ssh --proxy " ${TP_PROXY} " --cluster " ${TP_CLUSTER} " \
5950 -N -L " ${TUNNEL_LOCAL_PORT} :${TUNNEL_TARGET_HOST} :${TUNNEL_TARGET_PORT} " \
6051 " ${TUNNEL_GATEWAY_ADDRESS} " &
6152 TUNNEL_PID=$!
@@ -74,11 +65,12 @@ function open_background_tunnel() {
7465}
7566
7667function open_background_tunnel_with_timeout() {
77- TSH_CLUSTER_NAME=${1:? }
78- TUNNEL_LOCAL_PORT=${2:? }
79- TUNNEL_TARGET_HOST=${3:? }
80- TUNNEL_TARGET_PORT=${4:? }
81- TUNNEL_GATEWAY_ADDRESS=${5:? }
68+ TP_PROXY=${1:? }
69+ TP_CLUSTER=${2:? }
70+ TUNNEL_LOCAL_PORT=${3:? }
71+ TUNNEL_TARGET_HOST=${4:? }
72+ TUNNEL_TARGET_PORT=${5:? }
73+ TUNNEL_GATEWAY_ADDRESS=${6:? }
8274 TUNNEL_TIMEOUT=${6:- $TUNNEL_TIMEOUT }
8375
8476 PARENT_PROCESS_ID=" $( ps -p " ${PPID} " -o " ppid=" ) "
@@ -87,7 +79,8 @@ function open_background_tunnel_with_timeout() {
8779 nohup timeout " ${TUNNEL_TIMEOUT} " \
8880 " ${SCRIPT_ROOT} /tunneler.sh" \
8981 " open_background_tunnel" \
90- " ${TSH_CLUSTER_NAME} " \
82+ " ${TP_PROXY} " \
83+ " ${TP_CLUSTER} " \
9184 " ${TUNNEL_LOCAL_PORT} " \
9285 " ${TUNNEL_TARGET_HOST} " \
9386 " ${TUNNEL_TARGET_PORT} " \
@@ -109,16 +102,18 @@ function open_background_tunnel_with_timeout() {
109102# --------------------------------------------------------------------- main ---
110103
111104function create() {
112- TELEPORT_CLUSTER=${1:? }
113- TELEPORT_GATEWAY_NAME=${2:? }
114- TUNNEL_TARGET_HOST=${3:? }
115- TUNNEL_TARGET_PORT=${4:? }
105+ TP_PROXY=${1:? }
106+ TP_CLUSTER=${2:? }
107+ TP_GATEWAY_NODE=${3:? }
108+ TUNNEL_TARGET_HOST=${4:? }
109+ TUNNEL_TARGET_PORT=${5:? }
116110
117111 TUNNEL_LOCAL_PORT=$( get_random_ephemeral_port)
118- TUNNEL_GATEWAY_ADDRESS=$( get_gateway_address " ${TELEPORT_CLUSTER } " " ${TELEPORT_GATEWAY_NAME } " )
112+ TUNNEL_GATEWAY_ADDRESS=$( get_gateway_address " ${TP_PROXY } " " ${TP_CLUSTER} " " ${TP_GATEWAY_NODE }" )
119113
120114 open_background_tunnel_with_timeout \
121- " ${TELEPORT_CLUSTER} " \
115+ " ${TP_PROXY} " \
116+ " ${TP_CLUSTER} " \
122117 " ${TUNNEL_LOCAL_PORT} " \
123118 " ${TUNNEL_TARGET_HOST} " \
124119 " ${TUNNEL_TARGET_PORT} " \
@@ -134,22 +129,23 @@ if [[ "${1}" == "create" && "${2}" == "stdin" ]]; then
134129 # handler if input is stdin (e.g. from terraform)
135130
136131 INPUT=" $( dd 2> /dev/null) "
137- TELEPORT_CLUSTER=$( echo " ${INPUT} " | jq -r .teleport_cluster)
138- TELEPORT_GATEWAY_NAME=$( echo " ${INPUT} " | jq -r .teleport_gateway_name)
139- TUNNEL_TARGET_HOST=$( echo " ${INPUT} " | jq -r .target_host)
140- TUNNEL_TARGET_PORT=$( echo " ${INPUT} " | jq -r .target_port)
132+ TP_PROXY=$( echo " ${INPUT} " | jq -r .tp_proxy)
133+ TP_CLUSTER=$( echo " ${INPUT} " | jq -r .tp_cluster)
134+ TP_GATEWAY_NODE=$( echo " ${INPUT} " | jq -r .tp_gateway_node)
135+ TARGET_HOST=$( echo " ${INPUT} " | jq -r .target_host)
136+ TARGET_PORT=$( echo " ${INPUT} " | jq -r .target_port)
141137
142- TUNNEL_LOCAL_PORT =$( create " ${TELEPORT_CLUSTER } " " ${TELEPORT_GATEWAY_NAME } " " ${TUNNEL_TARGET_HOST } " " ${TUNNEL_TARGET_PORT } " )
143- echo " {\" host\" :\" localhost\" ,\" port\" :\" ${TUNNEL_LOCAL_PORT } \" }"
138+ LOCAL_PORT =$( create " ${TP_PROXY } " " ${TP_CLUSTER } " " ${TP_GATEWAY_NODE } " " ${TARGET_HOST} " " ${TARGET_PORT }" )
139+ echo " {\" host\" :\" localhost\" ,\" port\" :\" ${LOCAL_PORT } \" }"
144140
145141elif [[ " ${1} " == " create" ]]; then
146142
147143 # handler for normal cli calls
148144
149145 shift
150146
151- TUNNEL_LOCAL_PORT =$( create " ${@ } " )
152- echo " localhost:${TUNNEL_LOCAL_PORT } "
147+ LOCAL_PORT =$( create " ${@ } " )
148+ echo " localhost:${LOCAL_PORT } "
153149
154150else
155151
0 commit comments