Skip to content

Commit e01e0e4

Browse files
authored
Merge branch 'Azure:dev' into dev
2 parents 09ca820 + 604774c commit e01e0e4

260 files changed

Lines changed: 25208 additions & 27130 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/azure-cli-core/azure/cli/core/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def __init__(self, **kwargs):
6161
from azure.cli.core.breaking_change import register_upcoming_breaking_change_info
6262
from azure.cli.core.commands import register_cache_arguments
6363
from azure.cli.core.commands.arm import (
64-
register_ids_argument, register_global_subscription_argument)
64+
register_ids_argument, register_global_subscription_argument, register_global_policy_argument)
6565
from azure.cli.core.cloud import get_active_cloud
6666
from azure.cli.core.commands.transform import register_global_transforms
6767
from azure.cli.core._session import ACCOUNT, CONFIG, SESSION, INDEX, VERSIONS
@@ -90,6 +90,7 @@ def __init__(self, **kwargs):
9090
register_global_transforms(self)
9191
register_global_subscription_argument(self)
9292
register_ids_argument(self) # global subscription must be registered first!
93+
register_global_policy_argument(self)
9394
register_cache_arguments(self)
9495
register_upcoming_breaking_change_info(self)
9596

src/azure-cli-core/azure/cli/core/aaz/_client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ def __init__(self, ctx, credential, **kwargs):
7373
base_url = self._build_base_url(ctx, **kwargs)
7474
if not base_url:
7575
raise CloudEndpointNotSetException()
76+
if not kwargs.get('custom_hook_policy'):
77+
from azure.cli.core.sdk.policies import get_custom_hook_policy
78+
kwargs['custom_hook_policy'] = get_custom_hook_policy(ctx.cli_ctx)
7679
super().__init__(
7780
base_url=base_url,
7881
config=self._build_configuration(ctx, credential, **kwargs),

src/azure-cli-core/azure/cli/core/commands/arm.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,52 @@ def resource_exists(cli_ctx, subscription, resource_group, name, namespace, type
187187
return existing
188188

189189

190+
def register_global_policy_argument(cli_ctx):
191+
192+
def add_global_policy_argument(_, **kwargs):
193+
command_table = kwargs.get('commands_loader').command_table
194+
195+
if not command_table:
196+
return
197+
198+
class ChangeReferenceAction(argparse.Action): # pylint:disable=too-few-public-methods
199+
200+
def __call__(self, parser, namespace, value, option_string=None):
201+
# save change reference to CLI context
202+
cmd = getattr(namespace, 'cmd', None) or getattr(namespace, '_cmd', None)
203+
cmd.cli_ctx.data['_change_reference'] = value
204+
205+
class AcquirePolicyTokenAction(argparse.Action): # pylint:disable=too-few-public-methods
206+
207+
def __call__(self, parser, namespace, value, option_string=None):
208+
# save change reference to CLI context
209+
cmd = getattr(namespace, 'cmd', None) or getattr(namespace, '_cmd', None)
210+
cmd.cli_ctx.data['_acquire_policy_token'] = True
211+
212+
for command in command_table.values():
213+
if command.name.split()[-1] in ['list', 'show']:
214+
continue
215+
216+
change_reference_kwargs = {
217+
'help': 'The related change reference ID for this resource operation',
218+
'arg_group': 'Global Policy',
219+
'action': ChangeReferenceAction,
220+
}
221+
acquire_policy_token_kwargs = {
222+
'help': 'Acquiring an Azure Policy token automatically for this resource operation',
223+
'arg_group': 'Global Policy',
224+
'nargs': 0,
225+
'action': AcquirePolicyTokenAction,
226+
}
227+
command.add_argument('_change_reference', '--change-reference', **change_reference_kwargs)
228+
command.add_argument('_acquire_policy_token', '--acquire-policy-token', **acquire_policy_token_kwargs)
229+
230+
policy_token_feature_enabled = cli_ctx.config.getboolean('core', 'enable_policy_token', False)
231+
if policy_token_feature_enabled:
232+
from knack import events
233+
cli_ctx.register_event(events.EVENT_INVOKER_POST_CMD_TBL_CREATE, add_global_policy_argument)
234+
235+
190236
# pylint: disable=too-many-statements
191237
def register_ids_argument(cli_ctx):
192238

src/azure-cli-core/azure/cli/core/commands/client_factory.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ def _prepare_client_kwargs_track2(cli_ctx):
166166
from azure.cli.core.sdk.policies import RecordTelemetryUserAgentPolicy
167167
client_kwargs['user_agent_policy'] = RecordTelemetryUserAgentPolicy(**client_kwargs)
168168

169+
from azure.cli.core.sdk.policies import get_custom_hook_policy
170+
client_kwargs['custom_hook_policy'] = get_custom_hook_policy(cli_ctx)
171+
169172
# Replace NetworkTraceLoggingPolicy to redact 'Authorization' and 'x-ms-authorization-auxiliary' headers.
170173
# NetworkTraceLoggingPolicy: log raw network trace, with all headers.
171174
from azure.cli.core.sdk.policies import SafeNetworkTraceLoggingPolicy

src/azure-cli-core/azure/cli/core/sdk/policies.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,56 @@ def on_request(self, request):
103103
super().on_request(request)
104104
from azure.cli.core.telemetry import set_user_agent
105105
set_user_agent(request.http_request.headers[self._USERAGENT])
106+
107+
108+
# pylint: disable=line-too-long
109+
def get_custom_hook_policy(cli_ctx):
110+
def _acquire_policy_token_request_hook(request):
111+
http_request = request.http_request
112+
if getattr(http_request, 'method', '') == 'GET':
113+
return
114+
ACQUIRE_POLICY_TOKEN_URL = '/subscriptions/{subscriptionId}/providers/Microsoft.Authorization/acquirePolicyToken?api-version=2025-03-01'
115+
policy_token = None
116+
117+
from azure.cli.core.azclierror import ServiceError
118+
try:
119+
import json
120+
from azure.cli.core.util import send_raw_request
121+
uri = getattr(http_request, 'url')
122+
method = getattr(http_request, 'method')
123+
content = getattr(http_request, 'content') if hasattr(http_request, 'content') else getattr(http_request, 'body')
124+
if content and isinstance(content, str):
125+
try:
126+
content = json.loads(content)
127+
except json.decoder.JSONDecodeError:
128+
pass
129+
130+
acquire_policy_token_body = {
131+
"operation": {
132+
"uri": uri,
133+
"httpMethod": method,
134+
"content": content
135+
},
136+
"changeReference": cli_ctx.data.get('_change_reference', None)
137+
}
138+
acquire_policy_token_response = send_raw_request(cli_ctx, 'POST',
139+
ACQUIRE_POLICY_TOKEN_URL,
140+
headers=['Content-Type=application/json',
141+
'x-ms-force-sync=true'],
142+
body=json.dumps(acquire_policy_token_body))
143+
if acquire_policy_token_response.status_code == 200 and acquire_policy_token_response.content:
144+
response_content = json.loads(acquire_policy_token_response.content)
145+
policy_token = response_content.get('token', None)
146+
if not policy_token:
147+
raise ServiceError(f"No token returned. Response:{acquire_policy_token_response.content}")
148+
except Exception as ex:
149+
raise ServiceError(f"Failed to acquire policy token, exception: {ex}")
150+
if policy_token:
151+
request.http_request.headers['x-ms-policy-external-evaluations'] = policy_token
152+
153+
acquire_policy_token = cli_ctx.data.get('_acquire_policy_token', False)
154+
change_reference = cli_ctx.data.get('_change_reference', None)
155+
if change_reference or acquire_policy_token:
156+
from azure.core.pipeline.policies import CustomHookPolicy
157+
return CustomHookPolicy(raw_request_hook=_acquire_policy_token_request_hook)
158+
return None

src/azure-cli/azure/cli/command_modules/acs/tests/latest/recordings/test_aks_abort.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ interactions:
1515
User-Agent:
1616
- AZURECLI/2.72.0 azsdk-python-core/1.31.0 Python/3.10.11 (Windows-10-10.0.26100-SP0)
1717
method: GET
18-
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest000001/providers/Microsoft.ContainerService/managedClusters/cliakstest000002?api-version=2025-05-01
18+
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest000001/providers/Microsoft.ContainerService/managedClusters/cliakstest000002?api-version=2025-07-01
1919
response:
2020
body:
2121
string: '{"error":{"code":"ResourceNotFound","message":"The Resource ''Microsoft.ContainerService/managedClusters/cliakstest000002''
@@ -125,7 +125,7 @@ interactions:
125125
User-Agent:
126126
- AZURECLI/2.72.0 azsdk-python-core/1.31.0 Python/3.10.11 (Windows-10-10.0.26100-SP0)
127127
method: PUT
128-
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest000001/providers/Microsoft.ContainerService/managedClusters/cliakstest000002?api-version=2025-05-01
128+
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest000001/providers/Microsoft.ContainerService/managedClusters/cliakstest000002?api-version=2025-07-01
129129
response:
130130
body:
131131
string: "{\n \"id\": \"/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/clitest000001/providers/Microsoft.ContainerService/managedClusters/cliakstest000002\",\n
@@ -221,7 +221,7 @@ interactions:
221221
User-Agent:
222222
- AZURECLI/2.72.0 azsdk-python-core/1.31.0 Python/3.10.11 (Windows-10-10.0.26100-SP0)
223223
method: GET
224-
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest000001/providers/Microsoft.ContainerService/managedClusters/cliakstest000002?api-version=2025-05-01
224+
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest000001/providers/Microsoft.ContainerService/managedClusters/cliakstest000002?api-version=2025-07-01
225225
response:
226226
body:
227227
string: "{\n \"id\": \"/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/clitest000001/providers/Microsoft.ContainerService/managedClusters/cliakstest000002\",\n
@@ -313,7 +313,7 @@ interactions:
313313
User-Agent:
314314
- AZURECLI/2.72.0 azsdk-python-core/1.31.0 Python/3.10.11 (Windows-10-10.0.26100-SP0)
315315
method: POST
316-
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest000001/providers/Microsoft.ContainerService/managedclusters/cliakstest000002/abort?api-version=2025-05-01
316+
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest000001/providers/Microsoft.ContainerService/managedclusters/cliakstest000002/abort?api-version=2025-07-01
317317
response:
318318
body:
319319
string: "{\n \"id\": \"/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/clitest000001/providers/Microsoft.ContainerService/managedClusters/cliakstest000002\",\n
@@ -565,7 +565,7 @@ interactions:
565565
User-Agent:
566566
- AZURECLI/2.72.0 azsdk-python-core/1.31.0 Python/3.10.11 (Windows-10-10.0.26100-SP0)
567567
method: GET
568-
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest000001/providers/Microsoft.ContainerService/managedClusters/cliakstest000002?api-version=2025-05-01
568+
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest000001/providers/Microsoft.ContainerService/managedClusters/cliakstest000002?api-version=2025-07-01
569569
response:
570570
body:
571571
string: "{\n \"id\": \"/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/clitest000001/providers/Microsoft.ContainerService/managedClusters/cliakstest000002\",\n

0 commit comments

Comments
 (0)