Skip to content

Commit de7168a

Browse files
authored
Set http_logging_policy in Configuration (Azure#12218)
* allow user to set http_logging_policy in azure core * add tests for setting http logging policy in azure core * allow user to set http_logging_policy in azure mgmt core * fix default allowed headers for ARMHttpLoggingPolicy * add tests for setting http logging policy in azure mgmt core * deprecate WHITELIST, switch to ALLOWLIST in HttpLoggingPolicy * deprecate WHITELIST, switch to ALLOWLIST in ARMHttpLoggingPolicy * udpate changelog * change fix for ARMHttpLoggingPolicy default allowed headers * update version * Revert "deprecate WHITELIST, switch to ALLOWLIST in ARMHttpLoggingPolicy" This reverts commit 4175acd. * Revert "deprecate WHITELIST, switch to ALLOWLIST in HttpLoggingPolicy" This reverts commit 64b3246. * switch keyword docstring to ivar for most config policies * removed __init__ in azure-mgmt-core async tests * use the current class attribute to get the default allowed headers
1 parent 72d461c commit de7168a

File tree

14 files changed

+155
-18
lines changed

14 files changed

+155
-18
lines changed

sdk/core/azure-core/CHANGELOG.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11

22
# Release History
33

4-
## 1.6.1 (Unreleased)
4+
## 1.7.0 (Unreleased)
55

66
### Bug fixes
77

88
- `AzureKeyCredentialPolicy` will now accept (and ignore) passed in kwargs #11963
99
- Better error messages if passed endpoint is incorrect #12106
1010
- Do not JSON encore a string if content type is "text" #12137
1111

12+
### Features
13+
14+
- Added `http_logging_policy` property on the `Configuration` object, allowing users to individually
15+
set the http logging policy of the config #12218
16+
1217
## 1.6.0 (2020-06-03)
1318

1419
### Bug fixes

sdk/core/azure-core/azure/core/_pipeline_client.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def _build_pipeline(self, config, **kwargs): # pylint: disable=no-self-use
114114
config.custom_hook_policy,
115115
config.logging_policy,
116116
DistributedTracingPolicy(**kwargs),
117-
HttpLoggingPolicy(**kwargs)
117+
config.http_logging_policy or HttpLoggingPolicy(**kwargs)
118118
]
119119

120120
if not transport:

sdk/core/azure-core/azure/core/_pipeline_client_async.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def _build_pipeline(self, config, **kwargs): # pylint: disable=no-self-use
113113
config.custom_hook_policy,
114114
config.logging_policy,
115115
DistributedTracingPolicy(**kwargs),
116-
HttpLoggingPolicy(**kwargs),
116+
config.http_logging_policy or HttpLoggingPolicy(**kwargs)
117117
]
118118

119119
if not transport:

sdk/core/azure-core/azure/core/_version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
# regenerated.
1010
# --------------------------------------------------------------------------
1111

12-
VERSION = "1.6.1"
12+
VERSION = "1.7.0"

sdk/core/azure-core/azure/core/configuration.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,16 @@ class Configuration(object):
3434
Configuration to construct the pipeline correctly, as well as inserting any
3535
unexposed/non-configurable policies.
3636
37-
:keyword headers_policy: Provides parameters for custom or additional headers to be sent with the request.
38-
:keyword proxy_policy: Provides configuration parameters for proxy.
39-
:keyword redirect_policy: Provides configuration parameters for redirects.
40-
:keyword retry_policy: Provides configuration parameters for retries in the pipeline.
41-
:keyword custom_hook_policy: Provides configuration parameters for a custom hook.
42-
:keyword logging_policy: Provides configuration parameters for logging.
43-
:keyword user_agent_policy: Provides configuration parameters to append custom values to the
37+
:ivar headers_policy: Provides parameters for custom or additional headers to be sent with the request.
38+
:ivar proxy_policy: Provides configuration parameters for proxy.
39+
:ivar redirect_policy: Provides configuration parameters for redirects.
40+
:ivar retry_policy: Provides configuration parameters for retries in the pipeline.
41+
:ivar custom_hook_policy: Provides configuration parameters for a custom hook.
42+
:ivar logging_policy: Provides configuration parameters for logging.
43+
:ivar http_logging_policy: Provides configuration parameters for HTTP specific logging.
44+
:ivar user_agent_policy: Provides configuration parameters to append custom values to the
4445
User-Agent header.
45-
:keyword authentication_policy: Provides configuration parameters for adding a bearer token Authorization
46+
:ivar authentication_policy: Provides configuration parameters for adding a bearer token Authorization
4647
header to requests.
4748
:keyword polling_interval: Polling interval while doing LRO operations, if Retry-After is not set.
4849
@@ -74,6 +75,9 @@ def __init__(self, **kwargs):
7475
# Logger configuration
7576
self.logging_policy = None
7677

78+
# Http logger configuration
79+
self.http_logging_policy = None
80+
7781
# User Agent configuration
7882
self.user_agent_policy = None
7983

sdk/core/azure-core/azure/core/pipeline/policies/_universal.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ def __init__(self, logger=None, **kwargs): # pylint: disable=unused-argument
371371
"azure.core.pipeline.policies.http_logging_policy"
372372
)
373373
self.allowed_query_params = set()
374-
self.allowed_header_names = set(HttpLoggingPolicy.DEFAULT_HEADERS_WHITELIST)
374+
self.allowed_header_names = set(self.__class__.DEFAULT_HEADERS_WHITELIST)
375375

376376
def _redact_query_param(self, key, value):
377377
lower_case_allowed_query_params = [

sdk/core/azure-core/tests/azure_core_asynctests/test_pipeline.py

+26-1
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,19 @@
3131
UserAgentPolicy,
3232
AsyncRedirectPolicy,
3333
AsyncHTTPPolicy,
34-
AsyncRetryPolicy)
34+
AsyncRetryPolicy,
35+
HttpLoggingPolicy
36+
)
3537
from azure.core.pipeline.transport import (
3638
AsyncHttpTransport,
3739
HttpRequest,
3840
AsyncioRequestsTransport,
3941
TrioRequestsTransport,
4042
AioHttpTransport
4143
)
44+
45+
from azure.core.configuration import Configuration
46+
from azure.core import AsyncPipelineClient
4247
from azure.core.exceptions import AzureError
4348

4449
import aiohttp
@@ -143,6 +148,26 @@ async def do():
143148

144149
response = trio.run(do)
145150

151+
def test_default_http_logging_policy():
152+
config = Configuration()
153+
pipeline_client = AsyncPipelineClient(base_url="test")
154+
pipeline = pipeline_client._build_pipeline(config)
155+
http_logging_policy = pipeline._impl_policies[-1]._policy
156+
assert http_logging_policy.allowed_header_names == HttpLoggingPolicy.DEFAULT_HEADERS_WHITELIST
157+
158+
def test_pass_in_http_logging_policy():
159+
config = Configuration()
160+
http_logging_policy = HttpLoggingPolicy()
161+
http_logging_policy.allowed_header_names.update(
162+
{"x-ms-added-header"}
163+
)
164+
config.http_logging_policy = http_logging_policy
165+
166+
pipeline_client = AsyncPipelineClient(base_url="test")
167+
pipeline = pipeline_client._build_pipeline(config)
168+
http_logging_policy = pipeline._impl_policies[-1]._policy
169+
assert http_logging_policy.allowed_header_names == HttpLoggingPolicy.DEFAULT_HEADERS_WHITELIST.union({"x-ms-added-header"})
170+
146171
@pytest.mark.asyncio
147172
async def test_conf_async_requests():
148173

sdk/core/azure-core/tests/test_pipeline.py

+22
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@
4646

4747
from azure.core.configuration import Configuration
4848
from azure.core.pipeline import Pipeline
49+
from azure.core import PipelineClient
4950
from azure.core.pipeline.policies import (
5051
SansIOHTTPPolicy,
5152
UserAgentPolicy,
5253
RedirectPolicy,
54+
HttpLoggingPolicy
5355
)
5456
from azure.core.pipeline.transport._base import PipelineClientBase
5557
from azure.core.pipeline.transport import (
@@ -60,6 +62,26 @@
6062

6163
from azure.core.exceptions import AzureError
6264

65+
def test_default_http_logging_policy():
66+
config = Configuration()
67+
pipeline_client = PipelineClient(base_url="test")
68+
pipeline = pipeline_client._build_pipeline(config)
69+
http_logging_policy = pipeline._impl_policies[-1]._policy
70+
assert http_logging_policy.allowed_header_names == HttpLoggingPolicy.DEFAULT_HEADERS_WHITELIST
71+
72+
def test_pass_in_http_logging_policy():
73+
config = Configuration()
74+
http_logging_policy = HttpLoggingPolicy()
75+
http_logging_policy.allowed_header_names.update(
76+
{"x-ms-added-header"}
77+
)
78+
config.http_logging_policy = http_logging_policy
79+
80+
pipeline_client = PipelineClient(base_url="test")
81+
pipeline = pipeline_client._build_pipeline(config)
82+
http_logging_policy = pipeline._impl_policies[-1]._policy
83+
assert http_logging_policy.allowed_header_names == HttpLoggingPolicy.DEFAULT_HEADERS_WHITELIST.union({"x-ms-added-header"})
84+
6385

6486
def test_sans_io_exception():
6587
class BrokenSender(HttpTransport):

sdk/core/azure-mgmt-core/CHANGELOG.md

+12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11

22
# Release History
33

4+
## 1.2.0 (Unreleased)
5+
6+
### Bug Fixes
7+
8+
- The `allowed_header_names` property of ARMHttpLoggingPolicy now includes the management plane specific
9+
allowed headers #12218
10+
11+
### Features
12+
13+
- Added `http_logging_policy` property on the `Configuration` object, allowing users to individually
14+
set the http logging policy of the config #12218
15+
416
## 1.1.0 (2020-05-04)
517

618
### Features

sdk/core/azure-mgmt-core/azure/mgmt/core/_async_pipeline_client.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,5 @@ def _default_policies(config, **kwargs):
6565
config.custom_hook_policy,
6666
config.logging_policy,
6767
DistributedTracingPolicy(**kwargs),
68-
ARMHttpLoggingPolicy(**kwargs),
68+
config.http_logging_policy or ARMHttpLoggingPolicy(**kwargs),
6969
]

sdk/core/azure-mgmt-core/azure/mgmt/core/_pipeline_client.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,5 @@ def _default_policies(config, **kwargs):
6565
config.custom_hook_policy,
6666
config.logging_policy,
6767
DistributedTracingPolicy(**kwargs),
68-
ARMHttpLoggingPolicy(**kwargs),
68+
config.http_logging_policy or ARMHttpLoggingPolicy(**kwargs),
6969
]

sdk/core/azure-mgmt-core/azure/mgmt/core/_version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
# regenerated.
1010
# --------------------------------------------------------------------------
1111

12-
VERSION = "1.1.0"
12+
VERSION = "1.2.0"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#--------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
#
4+
# The MIT License (MIT)
5+
#
6+
# Permission is hereby granted, free of charge, to any person obtaining a copy
7+
# of this software and associated documentation files (the ""Software""), to deal
8+
# in the Software without restriction, including without limitation the rights
9+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
# copies of the Software, and to permit persons to whom the Software is
11+
# furnished to do so, subject to the following conditions:
12+
#
13+
# The above copyright notice and this permission notice shall be included in
14+
# all copies or substantial portions of the Software.
15+
#
16+
# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
# THE SOFTWARE.
23+
#
24+
#--------------------------------------------------------------------------
25+
26+
from azure.mgmt.core import AsyncARMPipelineClient
27+
from azure.mgmt.core.policies import ARMHttpLoggingPolicy
28+
from azure.core.configuration import Configuration
29+
30+
def test_default_http_logging_policy():
31+
config = Configuration()
32+
pipeline_client = AsyncARMPipelineClient(base_url="test", config=config)
33+
http_logging_policy = pipeline_client._default_policies(config=config)[-1]
34+
assert http_logging_policy.allowed_header_names == ARMHttpLoggingPolicy.DEFAULT_HEADERS_WHITELIST
35+
36+
def test_pass_in_http_logging_policy():
37+
config = Configuration()
38+
http_logging_policy = ARMHttpLoggingPolicy()
39+
http_logging_policy.allowed_header_names.update(
40+
{"x-ms-added-header"}
41+
)
42+
config.http_logging_policy = http_logging_policy
43+
44+
pipeline_client = AsyncARMPipelineClient(base_url="test", config=config)
45+
http_logging_policy = pipeline_client._default_policies(config=config)[-1]
46+
assert http_logging_policy.allowed_header_names == ARMHttpLoggingPolicy.DEFAULT_HEADERS_WHITELIST.union({"x-ms-added-header"})

sdk/core/azure-mgmt-core/tests/test_policies.py

+24-1
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,18 @@
3535
import requests
3636
import httpretty
3737

38+
from azure.core.configuration import Configuration
3839
from azure.core.pipeline import Pipeline
3940
from azure.core.pipeline.transport import (
4041
HttpRequest,
4142
RequestsTransport,
4243
)
4344

44-
from azure.mgmt.core.policies import ARMAutoResourceProviderRegistrationPolicy
45+
from azure.mgmt.core import ARMPipelineClient
46+
from azure.mgmt.core.policies import (
47+
ARMAutoResourceProviderRegistrationPolicy,
48+
ARMHttpLoggingPolicy
49+
)
4550

4651
@pytest.fixture
4752
def sleepless(monkeypatch):
@@ -162,3 +167,21 @@ def test_register_failed_policy():
162167
response = pipeline.run(request)
163168

164169
assert response.http_response.status_code == 409
170+
171+
def test_default_http_logging_policy():
172+
config = Configuration()
173+
pipeline_client = ARMPipelineClient(base_url="test", config=config)
174+
http_logging_policy = pipeline_client._default_policies(config=config)[-1]
175+
assert http_logging_policy.allowed_header_names == ARMHttpLoggingPolicy.DEFAULT_HEADERS_WHITELIST
176+
177+
def test_pass_in_http_logging_policy():
178+
config = Configuration()
179+
http_logging_policy = ARMHttpLoggingPolicy()
180+
http_logging_policy.allowed_header_names.update(
181+
{"x-ms-added-header"}
182+
)
183+
config.http_logging_policy = http_logging_policy
184+
185+
pipeline_client = ARMPipelineClient(base_url="test", config=config)
186+
http_logging_policy = pipeline_client._default_policies(config=config)[-1]
187+
assert http_logging_policy.allowed_header_names == ARMHttpLoggingPolicy.DEFAULT_HEADERS_WHITELIST.union({"x-ms-added-header"})

0 commit comments

Comments
 (0)