-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[aks-preview] az aks bastion: fix tunnel when Bastion is in a different subscription #9953
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
Open
FumingZhang
wants to merge
2
commits into
Azure:main
Choose a base branch
from
FumingZhang:fix-aks-bastion-cross-subscription
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+134
−6
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
src/aks-preview/azext_aks_preview/tests/latest/test_aks_bastion.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
| import unittest | ||
| from unittest.mock import AsyncMock, MagicMock, patch | ||
|
|
||
| from azext_aks_preview.bastion.bastion import ( | ||
| BastionResource, | ||
| _aks_bastion_launch_tunnel, | ||
| aks_bastion_parse_bastion_resource, | ||
| ) | ||
|
|
||
|
|
||
| class TestAksBastionParseResource(unittest.TestCase): | ||
| def test_cross_subscription_resource_id_preserves_bastion_subscription(self): | ||
| # the bastion lives in a different (hub) subscription than the cluster | ||
| bastion_id = ( | ||
| "/subscriptions/hub-sub-id/resourceGroups/bastion-rg/" | ||
| "providers/Microsoft.Network/bastionHosts/my-bastion" | ||
| ) | ||
| resource = aks_bastion_parse_bastion_resource( | ||
| bastion_id, ["node-rg"], subscription_id="aks-sub-id" | ||
| ) | ||
| self.assertEqual(resource.name, "my-bastion") | ||
| self.assertEqual(resource.resource_group, "bastion-rg") | ||
| # the subscription from the bastion resource ID must win over the cluster subscription | ||
| self.assertEqual(resource.subscription, "hub-sub-id") | ||
|
|
||
| def test_resource_id_without_subscription_falls_back_to_cluster_subscription(self): | ||
| # parse_resource_id may not yield a subscription; fall back to the cluster one | ||
| bastion_id = ( | ||
| "/subscriptions/hub-sub-id/resourceGroups/bastion-rg/" | ||
| "providers/Microsoft.Network/bastionHosts/my-bastion" | ||
| ) | ||
| with patch( | ||
| "azext_aks_preview.bastion.bastion.parse_resource_id", | ||
| return_value={"name": "my-bastion", "resource_group": "bastion-rg"}, | ||
| ): | ||
| resource = aks_bastion_parse_bastion_resource( | ||
| bastion_id, | ||
| ["node-rg"], | ||
| subscription_id="aks-sub-id", | ||
| ) | ||
| self.assertEqual(resource.subscription, "aks-sub-id") | ||
|
|
||
|
|
||
| class TestAksBastionLaunchTunnel(unittest.IsolatedAsyncioTestCase): | ||
| async def test_tunnel_uses_bastion_subscription(self): | ||
| # regression guard: the inner `az network bastion tunnel` must be scoped to the | ||
| # bastion's subscription, not the cluster subscription (see azure-cli#33579) | ||
| bastion_resource = BastionResource( | ||
| name="my-bastion", | ||
| resource_group="bastion-rg", | ||
| subscription="hub-sub-id", | ||
| ) | ||
| mock_process = MagicMock() | ||
| mock_process.wait = AsyncMock(return_value=0) | ||
| mock_process.returncode = 0 | ||
|
|
||
| with patch( | ||
| "azext_aks_preview.bastion.bastion.asyncio.create_subprocess_exec", | ||
| new=AsyncMock(return_value=mock_process), | ||
| ) as mock_exec: | ||
| await _aks_bastion_launch_tunnel( | ||
| bastion_resource, | ||
| port=12345, | ||
| mc_id="/subscriptions/aks-sub-id/resourceGroups/aks-rg/" | ||
| "providers/Microsoft.ContainerService/managedClusters/cluster", | ||
| subscription_id="aks-sub-id", | ||
| ) | ||
|
|
||
| args = mock_exec.call_args.args | ||
| self.assertIn("--subscription", args) | ||
| sub_index = args.index("--subscription") | ||
| self.assertEqual(args[sub_index + 1], "hub-sub-id") | ||
| self.assertNotIn("aks-sub-id", args[sub_index + 1]) | ||
|
|
||
| async def test_tunnel_falls_back_to_cluster_subscription(self): | ||
| # when the bastion has no subscription of its own (name-based discovery in the | ||
| # node resource group), the cluster subscription is used | ||
| bastion_resource = BastionResource( | ||
| name="my-bastion", | ||
| resource_group="node-rg", | ||
| subscription=None, | ||
| ) | ||
| mock_process = MagicMock() | ||
| mock_process.wait = AsyncMock(return_value=0) | ||
| mock_process.returncode = 0 | ||
|
|
||
| with patch( | ||
| "azext_aks_preview.bastion.bastion.asyncio.create_subprocess_exec", | ||
| new=AsyncMock(return_value=mock_process), | ||
| ) as mock_exec: | ||
| await _aks_bastion_launch_tunnel( | ||
| bastion_resource, | ||
| port=12345, | ||
| mc_id="/subscriptions/aks-sub-id/resourceGroups/aks-rg/" | ||
| "providers/Microsoft.ContainerService/managedClusters/cluster", | ||
| subscription_id="aks-sub-id", | ||
| ) | ||
|
|
||
| args = mock_exec.call_args.args | ||
| self.assertIn("--subscription", args) | ||
| sub_index = args.index("--subscription") | ||
| self.assertEqual(args[sub_index + 1], "aks-sub-id") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.