@@ -36,23 +36,20 @@ DESCRIPTION
3636
3737ENVIRONMENT
3838
39- OCI host instance IP: \` $OCI_INSTANCE_IP \`
40- e.g., \` 10.0.1.xxx\`
41- OCI host instance OCID: \` $OCI_INSTANCE_OCID \` ,
42- e.g., \` ocid1.instance.oc1.iad.xxxxxxxxx\`
43- OCI bastion OCID: \` $OCI_BASTION_OCID \`
44- e.g., \` ocid1.bastion.oc1.iad.xxxxxxxxx\`
45- OCI bastion region domain: \` $OCI_BASTION_REGION \`
46- e.g., \` us-ashburn-1\`
39+ * OCI CLI is required to be installed.
4740
48- \` jq\` is required to be installed.
41+ * \` jq\` is required to be installed.
42+
43+ * Required environment variables:
44+ * \` $OCI_INSTANCE_IP \` OCI host instance IP: , e.g., \` 10.0.1.xx\`
45+ * \` $OCI_INSTANCE_OCID \` , e.g., \` ocid1.instance.oc1.iad.xx\`
46+ * \` $OCI_BASTION_OCID \` , e.g., \` ocid1.bastion.oc1.iad.xx\`
4947
5048 Limitations for the \` host_user\` mode:
5149 1. This is the only OCI bastion session proxy jump host that is being configured in the SSH config.
52-
5350 2. The private host IP is not yet configured in the SSH config before the first run of this script.
5451
55- v2 .0.0 October 2022 Created by Dima Korobskiy
52+ v1 .0.0 October 2022 Created by Dima Korobskiy
5653Credits: George Chacko, Oracle
5754HEREDOC
5855 exit 1
@@ -153,13 +150,19 @@ shift $((OPTIND - 1))
153150# Process positional parameters
154151readonly HOST_USER=$1
155152
153+ if ! command -v oci > /dev/null; then
154+ # shellcheck disable=SC2016
155+ echo >&2 ' Please install OCI CLI'
156+ exit 1
157+ fi
158+
156159if ! command -v jq > /dev/null; then
157160 # shellcheck disable=SC2016
158161 echo >&2 ' Please install `jq`'
159162 exit 1
160163fi
161164
162- for required_env_var in OCI_INSTANCE_IP OCI_INSTANCE_OCID OCI_BASTION_OCID OCI_BASTION_REGION ; do
165+ for required_env_var in OCI_INSTANCE_IP OCI_INSTANCE_OCID OCI_BASTION_OCID; do
163166 if [[ ! ${! required_env_var} ]]; then
164167 echo " Please define $required_env_var "
165168 exit 1
169172# `${USER:-${USERNAME:-${LOGNAME}}}` might not be available inside Docker containers
170173echo -e " \n# oci-bastion.sh: running under $( whoami) @${HOSTNAME} in ${PWD} #"
171174
172- readonly BASTION_HOST=" host.bastion.${OCI_BASTION_REGION} .oci.oraclecloud.com"
173175readonly MAX_TTL=$(( 3 * 60 * 60 ))
174176readonly CHECK_INTERVAL_SEC=5
175177readonly SSH_PUB_KEY=~ /.ssh/id_rsa.pub
@@ -179,26 +181,22 @@ readonly AFTER_SESSION_CREATION_WAIT=5
179181if [[ $port ]]; then
180182 echo -e " \nCreating a port forwarding tunnel for the port $port : this can take up to 20s to succeed ..."
181183 session_ocid=$( time oci bastion session create-port-forwarding --bastion-id " $OCI_BASTION_OCID " \
182- --target-resource-id " $OCI_INSTANCE_OCID " --target-private-ip " ${OCI_INSTANCE_IP} " --target-port $port \
184+ --target-resource-id " $OCI_INSTANCE_OCID " --target-private-ip " ${OCI_INSTANCE_IP} " --target-port " $port " \
183185 --session-ttl $MAX_TTL --ssh-public-key-file $SSH_PUB_KEY --wait-for-state SUCCEEDED --wait-for-state FAILED \
184186 --wait-interval-seconds $CHECK_INTERVAL_SEC | jq --raw-output ' .data.resources[0].identifier' )
185187 echo " Bastion Port Forwarding Session OCID=$session_ocid "
186- oci bastion session get --session-id " $session_ocid "
187- echo
188+ ssh_command=$( oci bastion session get --session-id " $session_ocid " | jq --raw-output ' .data["ssh-metadata"].command' )
189+ # Result: `ssh -i <privateKey> -N -L <localPort>:{HOST_IP}:5432 -p 22 [email protected] ` 190+ # Remove the placeholder
191+ ssh_command=" ${ssh_command/ -i <privateKey>/ } "
192+ # Replace the placeholder
193+ ssh_command=" ${ssh_command/ <localPort>/ " localhost:$port " } "
188194 sleep $AFTER_SESSION_CREATION_WAIT
189195
190196 echo -e " \nLaunching an SSH tunnel"
191197 set -x
192-
193- # `-N`: Do not execute a remote command. This is useful for just forwarding ports.
194- # `-L [bind_address:]port:host:hostport`: Specifies that connections to the given TCP port on the local (client) host
195- # are to be forwarded to the given host and port on the remote side. Port forwardings can also be specified in the
196- # configuration file. Only the superuser can forward privileged ports. IPv6 addresses can be specified by enclosing
197- # the address in square brackets. By default, the local port is bound in accordance with the `GatewayPorts` setting.
198- # However, an explicit `bind_address` may be used to bind the connection to a specific address. The bind_address of
199- # `localhost` indicates that the listening port be bound for local use only, while an empty address or `*' indcates
200- # that the port should be available from all interfaces.
201- ssh -N -L " localhost:$port :${OCI_INSTANCE_IP} :$port " " $session_ocid " @" $BASTION_HOST "
198+ # This only works assuming there are no internal quotes in the command
199+ $ssh_command
202200 set +x
203201 exit
204202fi
@@ -212,11 +210,17 @@ if [[ $HOST_USER ]]; then
212210 --ssh-public-key-file $SSH_PUB_KEY --wait-for-state SUCCEEDED --wait-for-state FAILED \
213211 --wait-interval-seconds $CHECK_INTERVAL_SEC | jq --raw-output ' .data.resources[0].identifier' )
214212 echo " Bastion Session OCID=$session_ocid "
215- oci bastion session get --session-id " $session_ocid "
216- echo
213+ ssh_command=$( oci bastion session get --session-id " $session_ocid " | jq --raw-output ' .data["ssh-metadata"].command' )
214+ # Result: `ssh -i <privateKey> -o ProxyCommand=\"ssh -i <privateKey> -W %h:%p -p 22
215+ # [email protected] \" -p 22 {HOST_USER}@{HOST_IP}` 216+ # Extract the bastion session SSH destination: the `[email protected] ` part 217+ # Remove the string head
218+ bastion_session_dest=${ssh_command#* ocid1.bastionsession.}
219+ # Remove the string tail and reconstruct `[email protected] ` 220+ bastion_session_dest=" ocid1.bastionsession.${bastion_session_dest%% oraclecloud.com* } oraclecloud.com"
217221
218222 upsert ~ /.ssh/config " Host ${OCI_INSTANCE_IP} "
219- upsert ~ /.ssh/config ' ProxyJump ocid1.bastionsession.' " ProxyJump ${session_ocid} @ ${BASTION_HOST }"
223+ upsert ~ /.ssh/config ' ProxyJump ocid1.bastionsession.' " ProxyJump ${bastion_session_dest } "
220224
221225 if [[ $SKIP_SSH ]]; then
222226 exit 0
0 commit comments