|
3 | 3 | # Licensed under the MIT License. See License.txt in the project root for license information.
|
4 | 4 | # --------------------------------------------------------------------------------------------
|
5 | 5 |
|
| 6 | +class _Track1Credential: # pylint: disable=too-few-public-methods |
| 7 | + |
| 8 | + def __init__(self, credential, resource): |
| 9 | + """Track 1 credential that can be fed into Track 1 SDK clients. Exposes signed_session protocol. |
| 10 | + :param credential: Track 2 credential that exposes get_token protocol |
| 11 | + :param resource: AAD resource |
| 12 | + """ |
| 13 | + self._credential = credential |
| 14 | + self._resource = resource |
| 15 | + |
| 16 | + def signed_session(self, session=None): |
| 17 | + import requests |
| 18 | + from azure.cli.core.auth.util import resource_to_scopes |
| 19 | + session = session or requests.Session() |
| 20 | + token = self._credential.get_token(*resource_to_scopes(self._resource)) |
| 21 | + header = "{} {}".format('Bearer', token.token) |
| 22 | + session.headers['Authorization'] = header |
| 23 | + return session |
| 24 | + |
6 | 25 |
|
7 | 26 | def applicationinsights_data_plane_client(cli_ctx, _, subscription=None):
|
8 | 27 | """Initialize Log Analytics data client for use with CLI."""
|
9 | 28 | from .vendored_sdks.applicationinsights import ApplicationInsightsDataClient
|
10 | 29 | from azure.cli.core._profile import Profile
|
11 | 30 | profile = Profile(cli_ctx=cli_ctx)
|
12 |
| - cred, _, _ = profile.get_login_credentials( |
13 |
| - resource=cli_ctx.cloud.endpoints.app_insights_resource_id, |
14 |
| - subscription_id=subscription |
15 |
| - ) |
| 31 | + # Note: temporarily adapt track2 auth to track1 by the guidance: |
| 32 | + # https://github.com/Azure/azure-cli/pull/29631#issuecomment-2716799520 |
| 33 | + # need to be removed after migrated by codegen |
| 34 | + cred, _, _ = profile.get_login_credentials(subscription_id=subscription) |
16 | 35 | return ApplicationInsightsDataClient(
|
17 |
| - cred, |
| 36 | + _Track1Credential(cred, cli_ctx.cloud.endpoints.app_insights_resource_id), |
18 | 37 | base_url=f'{cli_ctx.cloud.endpoints.app_insights_resource_id}/v1'
|
19 | 38 | )
|
20 | 39 |
|
|
0 commit comments