Skip to content

Commit d1b2a62

Browse files
author
Rakshith Bhyravabhotla
authored
Add credential Scope keyword (Azure#20987)
* Support for audience * Update CHANGELOG.md * Update sdk/monitor/azure-monitor-query/azure/monitor/query/_metrics_query_client.py
1 parent 5c3c625 commit d1b2a62

File tree

8 files changed

+59
-23
lines changed

8 files changed

+59
-23
lines changed

sdk/monitor/azure-monitor-query/CHANGELOG.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Release History
22

3-
## 1.0.0b5 (Unreleased)
3+
## 1.0.0b5 (2021-10-05)
44

55
### Features Added
66

@@ -9,6 +9,7 @@
99
- Added `LogsQueryStatus` Enum to describe the status of a result.
1010
- Added a new `LogsTableRow` type that represents a single row in a table.
1111
- Items in `metrics` list in `MetricsResult` can now be accessed by metric names.
12+
- Added `audience` keyword to support providing credential scope when creating clients.
1213

1314
### Breaking Changes
1415

@@ -19,10 +20,6 @@
1920
- `query_batch` API now returns a union of `LogsQueryPartialResult`, `LogsQueryError` and `LogsQueryResult`.
2021
- `metric_namespace` is renamed to `namespace` and is a keyword-only argument in `list_metric_definitions` API.
2122

22-
### Bugs Fixed
23-
24-
### Other Changes
25-
2623
## 1.0.0b4 (2021-09-09)
2724

2825
### Features Added

sdk/monitor/azure-monitor-query/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ The Azure Monitor Query client library is used to execute read-only queries agai
1414
- [Samples][samples]
1515
- [Change log][changelog]
1616

17+
## _Disclaimer_
18+
19+
_Azure SDK Python packages support for Python 2.7 is ending 01 January 2022. For more information and questions, please refer to https://github.com/Azure/azure-sdk-for-python/issues/20691_
20+
1721
## Getting started
1822

1923
### Prerequisites

sdk/monitor/azure-monitor-query/azure/monitor/query/_helpers.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,38 @@
1515

1616

1717
def get_authentication_policy(
18-
credential, # type: TokenCredential
18+
credential, # type: "TokenCredential"
19+
audience=None # type: str
1920
):
2021
# type: (...) -> BearerTokenCredentialPolicy
2122
"""Returns the correct authentication policy"""
22-
23+
if not audience:
24+
audience = "https://api.loganalytics.io/"
25+
scope = audience.rstrip('/') + "/.default"
2326
if credential is None:
2427
raise ValueError("Parameter 'credential' must not be None.")
2528
if hasattr(credential, "get_token"):
2629
return BearerTokenCredentialPolicy(
27-
credential, "https://api.loganalytics.io/.default"
30+
credential, scope
2831
)
2932

3033
raise TypeError("Unsupported credential")
3134

3235

3336
def get_metrics_authentication_policy(
3437
credential, # type: TokenCredential
38+
audience=None # type: str
3539
):
3640
# type: (...) -> BearerTokenCredentialPolicy
3741
"""Returns the correct authentication policy"""
38-
42+
if not audience:
43+
audience = "https://management.azure.com/"
44+
scope = audience.rstrip('/') + "/.default"
3945
if credential is None:
4046
raise ValueError("Parameter 'credential' must not be None.")
4147
if hasattr(credential, "get_token"):
4248
return BearerTokenCredentialPolicy(
43-
credential, "https://management.azure.com/.default"
49+
credential, scope
4450
)
4551

4652
raise TypeError("Unsupported credential")

sdk/monitor/azure-monitor-query/azure/monitor/query/_logs_query_client.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,20 @@ class LogsQueryClient(object):
4949
:type credential: ~azure.core.credentials.TokenCredential
5050
:keyword endpoint: The endpoint to connect to. Defaults to 'https://api.loganalytics.io'.
5151
:paramtype endpoint: str
52+
:keyword audience: URL to use for credential authentication with AAD.
53+
:paramtype audience: str
5254
"""
5355

5456
def __init__(self, credential, **kwargs):
5557
# type: (TokenCredential, Any) -> None
56-
57-
self._endpoint = kwargs.pop("endpoint", "https://api.loganalytics.io/v1")
58+
audience = kwargs.pop("audience", None)
59+
endpoint = kwargs.pop("endpoint", "https://api.loganalytics.io/v1")
60+
if not endpoint.startswith("https://") and not endpoint.startswith("http://"):
61+
endpoint = "https://" + endpoint
62+
self._endpoint = endpoint
5863
self._client = MonitorQueryClient(
5964
credential=credential,
60-
authentication_policy=get_authentication_policy(credential),
65+
authentication_policy=get_authentication_policy(credential, audience),
6166
base_url=self._endpoint,
6267
**kwargs
6368
)

sdk/monitor/azure-monitor-query/azure/monitor/query/_metrics_query_client.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,21 @@ class MetricsQueryClient(object):
4444
:type credential: ~azure.core.credentials.TokenCredential
4545
:keyword endpoint: The endpoint to connect to. Defaults to 'https://management.azure.com'.
4646
:paramtype endpoint: str
47+
:keyword audience: URL to use for credential authentication with AAD.
48+
:paramtype audience: str
4749
"""
4850

4951
def __init__(self, credential, **kwargs):
5052
# type: (TokenCredential, Any) -> None
53+
audience = kwargs.pop("audience", None)
5154
endpoint = kwargs.pop("endpoint", "https://management.azure.com")
55+
if not endpoint.startswith("https://") and not endpoint.startswith("http://"):
56+
endpoint = "https://" + endpoint
57+
self._endpoint = endpoint
5258
self._client = MonitorQueryClient(
5359
credential=credential,
54-
base_url=endpoint,
55-
authentication_policy=get_metrics_authentication_policy(credential),
60+
base_url=self._endpoint,
61+
authentication_policy=get_metrics_authentication_policy(credential, audience),
5662
**kwargs
5763
)
5864
self._metrics_op = self._client.metrics

sdk/monitor/azure-monitor-query/azure/monitor/query/aio/_helpers_asyc.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,35 @@
1313

1414
def get_authentication_policy(
1515
credential: "AsyncTokenCredential",
16+
audience: str = None
1617
) -> AsyncBearerTokenCredentialPolicy:
1718
"""Returns the correct authentication policy"""
18-
19+
if not audience:
20+
audience = "https://api.loganalytics.io/"
21+
scope = audience.rstrip('/') + "/.default"
1922
if credential is None:
2023
raise ValueError("Parameter 'credential' must not be None.")
2124
if hasattr(credential, "get_token"):
2225
return AsyncBearerTokenCredentialPolicy(
23-
credential, "https://api.loganalytics.io/.default"
26+
credential, scope
2427
)
2528

2629
raise TypeError("Unsupported credential")
2730

2831

2932
def get_metrics_authentication_policy(
3033
credential: "AsyncTokenCredential",
34+
audience: str = None
3135
) -> AsyncBearerTokenCredentialPolicy:
3236
"""Returns the correct authentication policy"""
33-
37+
if not audience:
38+
audience = "https://management.azure.com/"
39+
scope = audience.rstrip('/') + "/.default"
3440
if credential is None:
3541
raise ValueError("Parameter 'credential' must not be None.")
3642
if hasattr(credential, "get_token"):
3743
return AsyncBearerTokenCredentialPolicy(
38-
credential, "https://management.azure.com/.default"
44+
credential, scope
3945
)
4046

4147
raise TypeError("Unsupported credential")

sdk/monitor/azure-monitor-query/azure/monitor/query/aio/_logs_query_client_async.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,19 @@ class LogsQueryClient(object):
3535
:type credential: ~azure.core.credentials_async.AsyncTokenCredential
3636
:keyword endpoint: The endpoint to connect to. Defaults to 'https://api.loganalytics.io/v1'.
3737
:paramtype endpoint: str
38+
:keyword audience: URL to use for credential authentication with AAD.
39+
:paramtype audience: str
3840
"""
3941

4042
def __init__(self, credential: "AsyncTokenCredential", **kwargs: Any) -> None:
41-
self._endpoint = kwargs.pop("endpoint", "https://api.loganalytics.io/v1")
43+
audience = kwargs.pop("audience", None)
44+
endpoint = kwargs.pop("endpoint", "https://api.loganalytics.io/v1")
45+
if not endpoint.startswith("https://") and not endpoint.startswith("http://"):
46+
endpoint = "https://" + endpoint
47+
self._endpoint = endpoint
4248
self._client = MonitorQueryClient(
4349
credential=credential,
44-
authentication_policy=get_authentication_policy(credential),
50+
authentication_policy=get_authentication_policy(credential, audience),
4551
base_url=self._endpoint,
4652
**kwargs
4753
)

sdk/monitor/azure-monitor-query/azure/monitor/query/aio/_metrics_query_client_async.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,20 @@ class MetricsQueryClient(object):
3232
:type credential: ~azure.core.credentials.TokenCredential
3333
:keyword endpoint: The endpoint to connect to. Defaults to 'https://management.azure.com'.
3434
:paramtype endpoint: str
35+
:keyword audience: URL to use for credential authentication with AAD.
36+
:paramtype audience: str
3537
"""
3638

3739
def __init__(self, credential: "AsyncTokenCredential", **kwargs: Any) -> None:
40+
audience = kwargs.pop("audience", None)
3841
endpoint = kwargs.pop("endpoint", "https://management.azure.com")
42+
if not endpoint.startswith("https://") and not endpoint.startswith("http://"):
43+
endpoint = "https://" + endpoint
44+
self._endpoint = endpoint
3945
self._client = MonitorQueryClient(
4046
credential=credential,
41-
base_url=endpoint,
42-
authentication_policy=get_metrics_authentication_policy(credential),
47+
base_url=self._endpoint,
48+
authentication_policy=get_metrics_authentication_policy(credential, audience),
4349
**kwargs
4450
)
4551
self._metrics_op = self._client.metrics

0 commit comments

Comments
 (0)