Skip to main content

Through Microsoft Dev Tunnels

A relay you don’t have to run: the job connects out to Microsoft’s Dev Tunnels, and you open an https URL after signing in with the same GitHub or Microsoft account. Microsoft sees your traffic.

On the previous page, the relay was a server of yours. If you don’t have one, somebody else’s will do. Dev Tunnels is the Microsoft service behind VS Code’s tunnels, and it has a program of its own, devtunnel, which can publish any port, not just VS Code’s. The job connects out to it, and you get an https://...devtunnels.ms URL that only opens after you sign in with the same account the job used.

Once, on the login node

#!/bin/bash
# Once, on the login node:
#     ./setup.sh
# Downloads the devtunnel program and signs you in with a GitHub account, by
# showing a code to enter at a URL in any browser. The login is kept in your
# home directory, so jobs on the compute nodes can use it.
#
# DEVTUNNEL sets where to put the program (default ~/.local/bin/devtunnel).
# Add -m to the login line below to sign in with a Microsoft account instead.
set -euo pipefail
DEVTUNNEL="${DEVTUNNEL:-$HOME/.local/bin/devtunnel}"

case "$(uname -m)" in
    x86_64) platform=linux-x64 ;;
    aarch64) platform=linux-arm64 ;;
    *) echo "no devtunnel build for $(uname -m)" >&2 && exit 1 ;;
esac
if [[ ! -x $DEVTUNNEL ]]; then
    mkdir -p "$(dirname "$DEVTUNNEL")"
    curl -fsSL "https://aka.ms/TunnelsCliDownload/$platform" -o "$DEVTUNNEL"
    chmod +x "$DEVTUNNEL"
fi
"$DEVTUNNEL" user login -g -d
"$DEVTUNNEL" user show

It downloads devtunnel to ~/.local/bin, for the kind of CPU the login node has, and signs you in. -g is to sign in with a GitHub account, and -d with a device code: it shows a code and a URL, and you enter the code at the URL in a browser anywhere, such as the one on your laptop. Your sign-in is then kept in your home directory, where jobs on the compute nodes can use it.1

On the cluster

#!/bin/bash
#SBATCH --job-name=dev-tunnel
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --time=01:00:00
# Cluster side, after setup.sh. Submit from this directory:
#     sbatch launch.sh
# then run connect.sh on your laptop, or read the URL from the job's output.
#
# The app listens on loopback. devtunnel connects out to Microsoft's relay and
# serves it at an https://....devtunnels.ms URL that only your account can open.
set -euo pipefail
DEVTUNNEL="${DEVTUNNEL:-$(command -v devtunnel || echo "$HOME/.local/bin/devtunnel")}"

SESSION="$HOME/.interactive-apps/$SLURM_JOB_ID"
mkdir -p "$SESSION"
chmod 700 "$HOME/.interactive-apps" "$SESSION"
APP_PORT="$(python3 -c 'import socket; s = socket.socket(); s.bind(("127.0.0.1", 0)); print(s.getsockname()[1])')"
# A token for the app. Nothing else on the cluster network can reach the node's
# loopback, but every other job on the same node can.
APP_TOKEN="$(python3 -c 'import secrets; print(secrets.token_hex(24))')"
export APP_TOKEN

# --- your app, bound to 127.0.0.1:$APP_PORT, with $APP_TOKEN as its token ---
python3 "${DEMO_APP:-$SLURM_SUBMIT_DIR/../demo-app/demo_app.py}" --host 127.0.0.1 --port "$APP_PORT" &
# ----------------------------------------------------------------------------

# Without --allow-anonymous, the tunnel only lets in the account that made it.
# It is registered with the relay until it expires, so keep that short, and
# delete it when the job ends normally.
"$DEVTUNNEL" host -p "$APP_PORT" --protocol http --expiration 1d >"$SESSION/devtunnel.log" 2>&1 &
trap '"$DEVTUNNEL" delete "${TUNNEL_ID:-}" -f >/dev/null 2>&1; rm -rf "$SESSION"' EXIT
until grep -q '^Ready to accept connections' "$SESSION/devtunnel.log"; do sleep 1; done
TUNNEL_ID="$(sed -n 's/^Ready to accept connections for tunnel: //p' "$SESSION/devtunnel.log")"
URL="$(sed -n 's/^Connect via browser: //p' "$SESSION/devtunnel.log")"

# Written aside and moved into place, so connect.sh never reads half of it.
cat >"$SESSION/info.env.tmp" <<INFO
JOBID=$SLURM_JOB_ID
TUNNEL_ID=$TUNNEL_ID
URL=$URL
APP_TOKEN=$APP_TOKEN
INFO
mv "$SESSION/info.env.tmp" "$SESSION/info.env"
echo "open $URL"
wait

The app listens on the compute node’s loopback, with a token, as on the previous pages. Then devtunnel host -p <port> connects out to Microsoft and publishes that port, and prints the URL. --protocol http says the app speaks plain HTTP, since it is devtunnel that adds HTTPS, between Microsoft and your browser.

Without --allow-anonymous, a tunnel is private: only the account that made it can open it. Never add that option. Then anyone who has the URL, or guesses the tunnel’s name, would reach your app, with its token as the only thing in the way.

--expiration 1d is because a tunnel stays registered with Microsoft until it is deleted or expires. The job deletes its own tunnel when it ends normally, but maybe not after scancel, so it expires after a day anyway.2

On your laptop

#!/bin/bash
# Laptop side. After `sbatch launch.sh` on the cluster:
#     LOGIN_HOST=<your login node> ./connect.sh <JOBID>
# There is no tunnel to hold open from here; this only looks up the URL, with
# the app's token, and opens it. Sign in with the same account you used in setup.sh.
set -euo pipefail
JOBID="${1:?usage: connect.sh JOBID}"
LOGIN_HOST="${LOGIN_HOST:?set LOGIN_HOST to the login node, as you would name it after ssh}"

eval "$(ssh -o BatchMode=yes "$LOGIN_HOST" "cat ~/.interactive-apps/$JOBID/info.env")"
echo "$URL/?token=$APP_TOKEN"
if command -v open >/dev/null; then open "$URL/?token=$APP_TOKEN"; elif command -v xdg-open >/dev/null; then xdg-open "$URL/?token=$APP_TOKEN"; fi
LOGIN_HOST=my-cluster ./connect.sh <job ID>

There is no tunnel to hold open on your laptop: it reads the URL and the token from the login node, and opens them in your browser. Sign in with the same account you used in setup.sh. The first time, Dev Tunnels shows a page warning you that you are about to connect to a developer tunnel. That is expected, and you can carry on, since nobody else can open your tunnel. If the app asks for a token after you sign in, it is the one at the end of the URL connect.sh printed.

What it means for security

The connections from the job to Microsoft and from your browser to Microsoft are both encrypted. But in between, Microsoft’s service decrypts the traffic: it is Microsoft’s certificate that your browser sees, and Microsoft’s servers that end the HTTPS and pass the HTTP on. So Microsoft can read everything, your token, your code and your data, and you are trusting it with all of that.3

You are also trusting your GitHub or Microsoft account. Whoever gets into it can open your tunnels, and whoever gets the sign-in kept in your home directory on the cluster can make tunnels as you.

So it comes down to whether your work may go through a third party. If your centre already recommends code tunnel for VS Code, it trusts the same relay.

Limitations

It needs the compute nodes to reach Microsoft’s servers, which depends on your cluster, as on the previous page. It needs a GitHub or Microsoft account. And there is a sign-in page and a warning page between you and your app, at least the first time.

The next page is a relay with less to set up, and no sign-in at all.

Footnotes

  1. If your login nodes and compute nodes have different kinds of CPU, run it again on a compute node, from a shell there, with DEVTUNNEL set to a path of its own.↩︎

  2. A tunnel can last from an hour to 30 days. devtunnel list shows yours, and devtunnel delete <tunnel ID> deletes one.↩︎

  3. VS Code’s own tunnel, code tunnel, is different here. It runs an SSH session inside the tunnel, so its traffic is encrypted from end to end, and the relay cannot read it. devtunnel host for a web app has nothing like that.↩︎