skip to Main Content

I am trying locally to connect to a database.
using:

az network bastion tunnel --name name --resource-group group --target-resource-id subscriptionid --resource-port 22 --port 2222 --subscription subscription

The tunnel is successfully created: Opening tunnel on port: 2222
Tunnel is ready, connect on port 2222

But when I try the connection via SSH:

Exception in thread Thread-1 (_start_tunnel):
Traceback (most recent call last):
  File "/usr/local/Cellar/[email protected]/3.11.7_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/threading.py", line 1045, in _bootstrap_inner
    self.run()
  File "/usr/local/Cellar/[email protected]/3.11.7_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/threading.py", line 982, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/user/.azure/cliextensions/bastion/azext_bastion/custom.py", line 364, in _start_tunnel
    tunnel_server.start_server()
  File "/Users/user/.azure/cliextensions/bastion/azext_bastion/tunnel.py", line 195, in start_server
    self._listen()
  File "/Users/user/.azure/cliextensions/bastion/azext_bastion/tunnel.py", line 123, in _listen
    auth_token = self._get_auth_token()
                 ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/.azure/cliextensions/bastion/azext_bastion/tunnel.py", line 112, in _get_auth_token
    self.last_token = response_json["authToken"]
                      ~~~~~~~~~~~~~^^^^^^^^^^^^^
KeyError: 'authToken'

I have the versions up to date.

2

Answers


  1. Chosen as BEST ANSWER

    There was a missing set on the set up. So before creating the tunnel I had to set the subscription id to the account: az account set --subscription xxxx-xxx-xxxx-xxx-xxxxxx


  2. az network bastion tunnel KeyError: ‘authToken’:

    Theauthtoken error comes when you do not set the auth_token environment variable before opening a network tunnel in Azure cloud shell.

    Set auth_token as an environment variable:

    Generate an authorization token with az account get-access-token bash command and export it as shown below.

    authtoken=$(az account get-access-token --subscription xxxx)
    export authtoken=authtoken
    

    enter image description here

    Check if the network bastion is existed under your connected subscription with the below command.

    az network bastion show --name newbastion --resource-group xxx
    

    enter image description here

    Now execute the tunnel command to connect to it with Azure bash command as detailed below.

    az network bastion tunnel --name newbastion --resource-group xxxx --target-resource-id "/subscriptions/xxxx/resourcegroups/xxxx/providers/Microsoft.Compute/virtualMachines/newvm" --resource-port 22 --port 50022
    

    I tried in my environment, and it was connected successfully with the given port.

    enter image description here

    Note:

    • Bastion Host SKU must be Standard and Native Client must be enabled.
    • Also check if there is any update for bastion extension and update it with az extension update -n bastion command.
    • Verify that the network bastion you are currently using matches the one configured under the Virtual Machine. Make sure that both the network bastion and virtual machine are properly configured as shown below.

    enter image description here

    Reference blog by @Sensación Térmica for the same issue.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search