Skip to content

Commit ff66413

Browse files
committed
Remove the region environment variable
1 parent 37b8aa0 commit ff66413

File tree

2 files changed

+60
-31
lines changed

2 files changed

+60
-31
lines changed

README.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,27 @@
1-
# OCI_Scripts
2-
Miscellaneous scripts related to Oracle Cloud Infrastructure (OCI)
1+
# Miscellaneous Oracle Cloud Infrastructure (OCI) scripts
2+
3+
## `ssh-oci-bastion.sh` ##
4+
5+
Configure and ssh or create a tunnel to an Oracle Cloud Infrastructure host via the bastion.
6+
7+
### Setup ###
8+
9+
0. Bash shell, SSH CLI client, `sed`, `sleep`, etc.
10+
* (macOS, Linux) Out-of-the-box
11+
* (Windows) Install [WSL](https://learn.microsoft.com/en-us/windows/wsl/) or [Cygwin](https://www.cygwin.com/)
12+
1. Install [OCI CLI](https://docs.oracle.com/en-us/iaas/Content/API/SDKDocs/cliinstall.htm).
13+
2. Install [`jq`](https://stedolan.github.io/jq/).
14+
3. Define the following environment variables. OCI menus below are as of October 2022.
15+
1. `OCI_INSTANCE_IP`: OCI host IP. See `Compute` > `Instances` > {host} > `Primary VNIC` > `Private IP address`
16+
2. `OCI_INSTANCE_OCID`. See `Compute` > `Instances` > {host} > `General information` > `OCID`
17+
3. `OCI_BASTION_OCID`. See `Identity & Security` > `Bastion` > {bastion} > `Bastion information` > `OCID`
18+
* If you're working with the single OCI host, setting them globally in your environment will work well.
19+
* If you're working with multiple hosts, you can pass these vars on-the-fly: see the `Usage Examples` section.
20+
21+
### Usage Examples ###
22+
23+
* Create a bastion session and ssh using system environment vars: `ssh-oci-bastion.sh joe`
24+
* Create a bastion session and ssh:
25+
`OCI_INSTANCE_IP=10.0.xx OCI_INSTANCE_OCID=ocid1.instance.xx OCI_BASTION_OCID=ocid1.bastion.xx ssh-oci-bastion.sh joe`
26+
* Create a bastion port-forwarding session and launch the tunnel for the port 1234: `ssh-oci-bastion.sh -p 1234`
27+

ssh-oci-bastion.sh

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,20 @@ DESCRIPTION
3636
3737
ENVIRONMENT
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
5653
Credits: George Chacko, Oracle
5754
HEREDOC
5855
exit 1
@@ -153,13 +150,19 @@ shift $((OPTIND - 1))
153150
# Process positional parameters
154151
readonly 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+
156159
if ! command -v jq >/dev/null; then
157160
# shellcheck disable=SC2016
158161
echo >&2 'Please install `jq`'
159162
exit 1
160163
fi
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
@@ -169,7 +172,6 @@ done
169172
# `${USER:-${USERNAME:-${LOGNAME}}}` might not be available inside Docker containers
170173
echo -e "\n# oci-bastion.sh: running under $(whoami)@${HOSTNAME} in ${PWD} #"
171174

172-
readonly BASTION_HOST="host.bastion.${OCI_BASTION_REGION}.oci.oraclecloud.com"
173175
readonly MAX_TTL=$((3 * 60 * 60))
174176
readonly CHECK_INTERVAL_SEC=5
175177
readonly SSH_PUB_KEY=~/.ssh/id_rsa.pub
@@ -179,26 +181,22 @@ readonly AFTER_SESSION_CREATION_WAIT=5
179181
if [[ $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
204202
fi
@@ -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

Comments
 (0)