Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix azdev style issues for bastion #7142

Merged
merged 7 commits into from
Apr 26, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions src/bastion/azext_bastion/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ def ssh_bastion_host(cmd, auth_type, target_resource_id, target_ip_address, reso
ip_connect = _is_ipconnect_request(bastion, target_ip_address)
if ip_connect:
if int(resource_port) not in [22, 3389]:
raise UnrecognizedArgumentError("Custom ports are not allowed. Allowed ports for Tunnel with IP connect is 22, 3389.")
raise UnrecognizedArgumentError("Custom ports are not allowed. Allowed ports for Tunnel with IP connect is \
22, 3389.")
target_resource_id = f"/subscriptions/{get_subscription_id(cmd.cli_ctx)}/resourceGroups/{resource_group_name}" \
f"/providers/Microsoft.Network/bh-hostConnect/{target_ip_address}"

Expand Down Expand Up @@ -255,18 +256,19 @@ def rdp_bastion_host(cmd, target_resource_id, target_ip_address, resource_group_
elif auth_type.lower() == "password":
# do nothing
logger.warning("No need to provide auth-type password for RDP connections.")
pass
elif auth_type.lower() == "aad":
enable_mfa = True

if disable_gateway or ip_connect:
raise UnrecognizedArgumentError("AAD login is not supported for Disable Gateway & IP Connect scenarios.")
else:
raise UnrecognizedArgumentError("Unknown auth type, support auth-types: aad. For non aad login, you dont need to provide auth-type flag.")
raise UnrecognizedArgumentError("Unknown auth type, support auth-types: aad. For non aad login, you dont need \
to provide auth-type flag.")

if ip_connect:
if int(resource_port) not in [22, 3389]:
raise UnrecognizedArgumentError("Custom ports are not allowed. Allowed ports for Tunnel with IP connect is 22, 3389.")
raise UnrecognizedArgumentError("Custom ports are not allowed. Allowed ports for Tunnel with IP connect is \
22, 3389.")

target_resource_id = f"/subscriptions/{get_subscription_id(cmd.cli_ctx)}/resourceGroups/{resource_group_name}" \
f"/providers/Microsoft.Network/bh-hostConnect/{target_ip_address}"
Expand All @@ -286,8 +288,7 @@ def rdp_bastion_host(cmd, target_resource_id, target_ip_address, resource_group_
launch_and_wait(command)
tunnel_server.cleanup()
else:
profile = Profile(cli_ctx=cmd.cli_ctx)
access_token = profile.get_raw_token()[0][2].get("accessToken")
access_token = Profile(cli_ctx=cmd.cli_ctx).get_raw_token()[0][2].get("accessToken")
logger.debug("Response %s", access_token)
web_address = f"https://{bastion_endpoint}/api/rdpfile?resourceId={target_resource_id}&format=rdp" \
f"&rdpport={resource_port}&enablerdsaad={enable_mfa}"
Expand All @@ -301,13 +302,9 @@ def rdp_bastion_host(cmd, target_resource_id, target_ip_address, resource_group_
}
response = requests.get(web_address, headers=headers)
if not response.ok:
errorMessage = json.loads(response.content).get('message', None)
if errorMessage:
raise ClientRequestError("Request failed with error: " + errorMessage)
raise ClientRequestError("Request to EncodingReservedUnitTypes v2 API endpoint failed.")
handle_error_response(response)

tempdir = os.path.realpath(tempfile.gettempdir())
rdpfilepath = os.path.join(tempdir, 'conn_{}.rdp'.format(uuid.uuid4().hex))
rdpfilepath = os.path.join(os.path.realpath(tempfile.gettempdir()), f'conn_{uuid.uuid4().hex}.rdp')
_write_to_file(response, rdpfilepath)

logger.warning("Saving RDP file to: %s", rdpfilepath)
Expand All @@ -328,6 +325,13 @@ def _is_ipconnect_request(bastion, target_ip_address):
return False


def handle_error_response(response):
errorMessage = json.loads(response.content).get('message', None)
if errorMessage:
raise ClientRequestError("Request failed with error: " + errorMessage)
raise ClientRequestError("Request to EncodingReservedUnitTypes v2 API endpoint failed.")
Tejaswikandula marked this conversation as resolved.
Show resolved Hide resolved


def _validate_resourceid(target_resource_id):
if not is_valid_resource_id(target_resource_id):
err_msg = "Please enter a valid resource ID. If this is not working, " \
Expand Down Expand Up @@ -371,16 +375,17 @@ def _tunnel_close_handler(tunnel):
sys.exit()


def create_bastion_tunnel(cmd, target_resource_id, target_ip_address, resource_group_name, bastion_host_name, resource_port, port,
timeout=None):
def create_bastion_tunnel(cmd, target_resource_id, target_ip_address, resource_group_name, bastion_host_name,
resource_port, port, timeout=None):

from .aaz.latest.network.bastion import Show
bastion = Show(cli_ctx=cmd.cli_ctx)(command_args={
"resource_group": resource_group_name,
"name": bastion_host_name
})

if bastion['sku']['name'] == BastionSku.Basic.value or bastion['sku']['name'] == BastionSku.Standard.value and bastion['enableTunneling'] is not True:
if bastion['sku']['name'] == BastionSku.Basic.value or bastion['sku']['name'] == BastionSku.Standard.value and \
bastion['enableTunneling'] is not True:
raise ClientRequestError('Bastion Host SKU must be Standard and Native Client must be enabled.')

ip_connect = _is_ipconnect_request(bastion, target_ip_address)
Expand All @@ -389,7 +394,8 @@ def create_bastion_tunnel(cmd, target_resource_id, target_ip_address, resource_g
f"{resource_group_name}/providers/Microsoft.Network/bh-hostConnect/{target_ip_address}"

if ip_connect and int(resource_port) not in [22, 3389]:
raise UnrecognizedArgumentError("Custom ports are not allowed. Allowed ports for Tunnel with IP connect is 22, 3389.")
raise UnrecognizedArgumentError("Custom ports are not allowed. Allowed ports for Tunnel with IP connect is \
22, 3389.")

_validate_resourceid(target_resource_id)
bastion_endpoint = _get_bastion_endpoint(cmd, bastion, resource_port, target_resource_id)
Expand Down
Loading