From de7168a07ec7476f589f316d2cbff6f42f8fda91 Mon Sep 17 00:00:00 2001 From: iscai-msft <43154838+iscai-msft@users.noreply.github.com> Date: Mon, 29 Jun 2020 18:58:32 -0400 Subject: [PATCH] Set http_logging_policy in Configuration (#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 4175acda7ef6ee4c724dbe17896ceb23972bf747. * Revert "deprecate WHITELIST, switch to ALLOWLIST in HttpLoggingPolicy" This reverts commit 64b3246fcc6926704b276f5740b1458b68a6c5cd. * 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 --- sdk/core/azure-core/CHANGELOG.md | 7 ++- .../azure-core/azure/core/_pipeline_client.py | 2 +- .../azure/core/_pipeline_client_async.py | 2 +- sdk/core/azure-core/azure/core/_version.py | 2 +- .../azure-core/azure/core/configuration.py | 20 ++++---- .../core/pipeline/policies/_universal.py | 2 +- .../azure_core_asynctests/test_pipeline.py | 27 ++++++++++- sdk/core/azure-core/tests/test_pipeline.py | 22 +++++++++ sdk/core/azure-mgmt-core/CHANGELOG.md | 12 +++++ .../azure/mgmt/core/_async_pipeline_client.py | 2 +- .../azure/mgmt/core/_pipeline_client.py | 2 +- .../azure/mgmt/core/_version.py | 2 +- .../tests/asynctests/test_policies_async.py | 46 +++++++++++++++++++ .../azure-mgmt-core/tests/test_policies.py | 25 +++++++++- 14 files changed, 155 insertions(+), 18 deletions(-) create mode 100644 sdk/core/azure-mgmt-core/tests/asynctests/test_policies_async.py diff --git a/sdk/core/azure-core/CHANGELOG.md b/sdk/core/azure-core/CHANGELOG.md index 4f6eda777df8..2cad230ece1b 100644 --- a/sdk/core/azure-core/CHANGELOG.md +++ b/sdk/core/azure-core/CHANGELOG.md @@ -1,7 +1,7 @@ # Release History -## 1.6.1 (Unreleased) +## 1.7.0 (Unreleased) ### Bug fixes @@ -9,6 +9,11 @@ - Better error messages if passed endpoint is incorrect #12106 - Do not JSON encore a string if content type is "text" #12137 +### Features + +- Added `http_logging_policy` property on the `Configuration` object, allowing users to individually +set the http logging policy of the config #12218 + ## 1.6.0 (2020-06-03) ### Bug fixes diff --git a/sdk/core/azure-core/azure/core/_pipeline_client.py b/sdk/core/azure-core/azure/core/_pipeline_client.py index b0a32564c717..a75271d14b2a 100644 --- a/sdk/core/azure-core/azure/core/_pipeline_client.py +++ b/sdk/core/azure-core/azure/core/_pipeline_client.py @@ -114,7 +114,7 @@ def _build_pipeline(self, config, **kwargs): # pylint: disable=no-self-use config.custom_hook_policy, config.logging_policy, DistributedTracingPolicy(**kwargs), - HttpLoggingPolicy(**kwargs) + config.http_logging_policy or HttpLoggingPolicy(**kwargs) ] if not transport: diff --git a/sdk/core/azure-core/azure/core/_pipeline_client_async.py b/sdk/core/azure-core/azure/core/_pipeline_client_async.py index 6f2eb9ebb5d1..3c6917a5e401 100644 --- a/sdk/core/azure-core/azure/core/_pipeline_client_async.py +++ b/sdk/core/azure-core/azure/core/_pipeline_client_async.py @@ -113,7 +113,7 @@ def _build_pipeline(self, config, **kwargs): # pylint: disable=no-self-use config.custom_hook_policy, config.logging_policy, DistributedTracingPolicy(**kwargs), - HttpLoggingPolicy(**kwargs), + config.http_logging_policy or HttpLoggingPolicy(**kwargs) ] if not transport: diff --git a/sdk/core/azure-core/azure/core/_version.py b/sdk/core/azure-core/azure/core/_version.py index 63b396f7f785..3e95c740099b 100644 --- a/sdk/core/azure-core/azure/core/_version.py +++ b/sdk/core/azure-core/azure/core/_version.py @@ -9,4 +9,4 @@ # regenerated. # -------------------------------------------------------------------------- -VERSION = "1.6.1" +VERSION = "1.7.0" diff --git a/sdk/core/azure-core/azure/core/configuration.py b/sdk/core/azure-core/azure/core/configuration.py index cfd8f58b518a..81148e10bf03 100644 --- a/sdk/core/azure-core/azure/core/configuration.py +++ b/sdk/core/azure-core/azure/core/configuration.py @@ -34,15 +34,16 @@ class Configuration(object): Configuration to construct the pipeline correctly, as well as inserting any unexposed/non-configurable policies. - :keyword headers_policy: Provides parameters for custom or additional headers to be sent with the request. - :keyword proxy_policy: Provides configuration parameters for proxy. - :keyword redirect_policy: Provides configuration parameters for redirects. - :keyword retry_policy: Provides configuration parameters for retries in the pipeline. - :keyword custom_hook_policy: Provides configuration parameters for a custom hook. - :keyword logging_policy: Provides configuration parameters for logging. - :keyword user_agent_policy: Provides configuration parameters to append custom values to the + :ivar headers_policy: Provides parameters for custom or additional headers to be sent with the request. + :ivar proxy_policy: Provides configuration parameters for proxy. + :ivar redirect_policy: Provides configuration parameters for redirects. + :ivar retry_policy: Provides configuration parameters for retries in the pipeline. + :ivar custom_hook_policy: Provides configuration parameters for a custom hook. + :ivar logging_policy: Provides configuration parameters for logging. + :ivar http_logging_policy: Provides configuration parameters for HTTP specific logging. + :ivar user_agent_policy: Provides configuration parameters to append custom values to the User-Agent header. - :keyword authentication_policy: Provides configuration parameters for adding a bearer token Authorization + :ivar authentication_policy: Provides configuration parameters for adding a bearer token Authorization header to requests. :keyword polling_interval: Polling interval while doing LRO operations, if Retry-After is not set. @@ -74,6 +75,9 @@ def __init__(self, **kwargs): # Logger configuration self.logging_policy = None + # Http logger configuration + self.http_logging_policy = None + # User Agent configuration self.user_agent_policy = None diff --git a/sdk/core/azure-core/azure/core/pipeline/policies/_universal.py b/sdk/core/azure-core/azure/core/pipeline/policies/_universal.py index a90f10a7bda4..10cb23bbcb43 100644 --- a/sdk/core/azure-core/azure/core/pipeline/policies/_universal.py +++ b/sdk/core/azure-core/azure/core/pipeline/policies/_universal.py @@ -371,7 +371,7 @@ def __init__(self, logger=None, **kwargs): # pylint: disable=unused-argument "azure.core.pipeline.policies.http_logging_policy" ) self.allowed_query_params = set() - self.allowed_header_names = set(HttpLoggingPolicy.DEFAULT_HEADERS_WHITELIST) + self.allowed_header_names = set(self.__class__.DEFAULT_HEADERS_WHITELIST) def _redact_query_param(self, key, value): lower_case_allowed_query_params = [ diff --git a/sdk/core/azure-core/tests/azure_core_asynctests/test_pipeline.py b/sdk/core/azure-core/tests/azure_core_asynctests/test_pipeline.py index b8bc6fa9b9a2..070b50368c5a 100644 --- a/sdk/core/azure-core/tests/azure_core_asynctests/test_pipeline.py +++ b/sdk/core/azure-core/tests/azure_core_asynctests/test_pipeline.py @@ -31,7 +31,9 @@ UserAgentPolicy, AsyncRedirectPolicy, AsyncHTTPPolicy, - AsyncRetryPolicy) + AsyncRetryPolicy, + HttpLoggingPolicy +) from azure.core.pipeline.transport import ( AsyncHttpTransport, HttpRequest, @@ -39,6 +41,9 @@ TrioRequestsTransport, AioHttpTransport ) + +from azure.core.configuration import Configuration +from azure.core import AsyncPipelineClient from azure.core.exceptions import AzureError import aiohttp @@ -143,6 +148,26 @@ async def do(): response = trio.run(do) +def test_default_http_logging_policy(): + config = Configuration() + pipeline_client = AsyncPipelineClient(base_url="test") + pipeline = pipeline_client._build_pipeline(config) + http_logging_policy = pipeline._impl_policies[-1]._policy + assert http_logging_policy.allowed_header_names == HttpLoggingPolicy.DEFAULT_HEADERS_WHITELIST + +def test_pass_in_http_logging_policy(): + config = Configuration() + http_logging_policy = HttpLoggingPolicy() + http_logging_policy.allowed_header_names.update( + {"x-ms-added-header"} + ) + config.http_logging_policy = http_logging_policy + + pipeline_client = AsyncPipelineClient(base_url="test") + pipeline = pipeline_client._build_pipeline(config) + http_logging_policy = pipeline._impl_policies[-1]._policy + assert http_logging_policy.allowed_header_names == HttpLoggingPolicy.DEFAULT_HEADERS_WHITELIST.union({"x-ms-added-header"}) + @pytest.mark.asyncio async def test_conf_async_requests(): diff --git a/sdk/core/azure-core/tests/test_pipeline.py b/sdk/core/azure-core/tests/test_pipeline.py index 7b4cff1753b4..f7a96f2372da 100644 --- a/sdk/core/azure-core/tests/test_pipeline.py +++ b/sdk/core/azure-core/tests/test_pipeline.py @@ -46,10 +46,12 @@ from azure.core.configuration import Configuration from azure.core.pipeline import Pipeline +from azure.core import PipelineClient from azure.core.pipeline.policies import ( SansIOHTTPPolicy, UserAgentPolicy, RedirectPolicy, + HttpLoggingPolicy ) from azure.core.pipeline.transport._base import PipelineClientBase from azure.core.pipeline.transport import ( @@ -60,6 +62,26 @@ from azure.core.exceptions import AzureError +def test_default_http_logging_policy(): + config = Configuration() + pipeline_client = PipelineClient(base_url="test") + pipeline = pipeline_client._build_pipeline(config) + http_logging_policy = pipeline._impl_policies[-1]._policy + assert http_logging_policy.allowed_header_names == HttpLoggingPolicy.DEFAULT_HEADERS_WHITELIST + +def test_pass_in_http_logging_policy(): + config = Configuration() + http_logging_policy = HttpLoggingPolicy() + http_logging_policy.allowed_header_names.update( + {"x-ms-added-header"} + ) + config.http_logging_policy = http_logging_policy + + pipeline_client = PipelineClient(base_url="test") + pipeline = pipeline_client._build_pipeline(config) + http_logging_policy = pipeline._impl_policies[-1]._policy + assert http_logging_policy.allowed_header_names == HttpLoggingPolicy.DEFAULT_HEADERS_WHITELIST.union({"x-ms-added-header"}) + def test_sans_io_exception(): class BrokenSender(HttpTransport): diff --git a/sdk/core/azure-mgmt-core/CHANGELOG.md b/sdk/core/azure-mgmt-core/CHANGELOG.md index 5ad025311ba7..1ca4a3f75ef9 100644 --- a/sdk/core/azure-mgmt-core/CHANGELOG.md +++ b/sdk/core/azure-mgmt-core/CHANGELOG.md @@ -1,6 +1,18 @@ # Release History +## 1.2.0 (Unreleased) + +### Bug Fixes + +- The `allowed_header_names` property of ARMHttpLoggingPolicy now includes the management plane specific +allowed headers #12218 + +### Features + +- Added `http_logging_policy` property on the `Configuration` object, allowing users to individually +set the http logging policy of the config #12218 + ## 1.1.0 (2020-05-04) ### Features diff --git a/sdk/core/azure-mgmt-core/azure/mgmt/core/_async_pipeline_client.py b/sdk/core/azure-mgmt-core/azure/mgmt/core/_async_pipeline_client.py index 01cb3b3097ab..2e71532e72be 100644 --- a/sdk/core/azure-mgmt-core/azure/mgmt/core/_async_pipeline_client.py +++ b/sdk/core/azure-mgmt-core/azure/mgmt/core/_async_pipeline_client.py @@ -65,5 +65,5 @@ def _default_policies(config, **kwargs): config.custom_hook_policy, config.logging_policy, DistributedTracingPolicy(**kwargs), - ARMHttpLoggingPolicy(**kwargs), + config.http_logging_policy or ARMHttpLoggingPolicy(**kwargs), ] diff --git a/sdk/core/azure-mgmt-core/azure/mgmt/core/_pipeline_client.py b/sdk/core/azure-mgmt-core/azure/mgmt/core/_pipeline_client.py index 39cdf9b5505f..0280e2408ac1 100644 --- a/sdk/core/azure-mgmt-core/azure/mgmt/core/_pipeline_client.py +++ b/sdk/core/azure-mgmt-core/azure/mgmt/core/_pipeline_client.py @@ -65,5 +65,5 @@ def _default_policies(config, **kwargs): config.custom_hook_policy, config.logging_policy, DistributedTracingPolicy(**kwargs), - ARMHttpLoggingPolicy(**kwargs), + config.http_logging_policy or ARMHttpLoggingPolicy(**kwargs), ] diff --git a/sdk/core/azure-mgmt-core/azure/mgmt/core/_version.py b/sdk/core/azure-mgmt-core/azure/mgmt/core/_version.py index 683eba90f4b8..3c9123ce45fc 100644 --- a/sdk/core/azure-mgmt-core/azure/mgmt/core/_version.py +++ b/sdk/core/azure-mgmt-core/azure/mgmt/core/_version.py @@ -9,4 +9,4 @@ # regenerated. # -------------------------------------------------------------------------- -VERSION = "1.1.0" +VERSION = "1.2.0" diff --git a/sdk/core/azure-mgmt-core/tests/asynctests/test_policies_async.py b/sdk/core/azure-mgmt-core/tests/asynctests/test_policies_async.py new file mode 100644 index 000000000000..f34c92cd514d --- /dev/null +++ b/sdk/core/azure-mgmt-core/tests/asynctests/test_policies_async.py @@ -0,0 +1,46 @@ +#-------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# +# The MIT License (MIT) +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the ""Software""), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. +# +#-------------------------------------------------------------------------- + +from azure.mgmt.core import AsyncARMPipelineClient +from azure.mgmt.core.policies import ARMHttpLoggingPolicy +from azure.core.configuration import Configuration + +def test_default_http_logging_policy(): + config = Configuration() + pipeline_client = AsyncARMPipelineClient(base_url="test", config=config) + http_logging_policy = pipeline_client._default_policies(config=config)[-1] + assert http_logging_policy.allowed_header_names == ARMHttpLoggingPolicy.DEFAULT_HEADERS_WHITELIST + +def test_pass_in_http_logging_policy(): + config = Configuration() + http_logging_policy = ARMHttpLoggingPolicy() + http_logging_policy.allowed_header_names.update( + {"x-ms-added-header"} + ) + config.http_logging_policy = http_logging_policy + + pipeline_client = AsyncARMPipelineClient(base_url="test", config=config) + http_logging_policy = pipeline_client._default_policies(config=config)[-1] + assert http_logging_policy.allowed_header_names == ARMHttpLoggingPolicy.DEFAULT_HEADERS_WHITELIST.union({"x-ms-added-header"}) \ No newline at end of file diff --git a/sdk/core/azure-mgmt-core/tests/test_policies.py b/sdk/core/azure-mgmt-core/tests/test_policies.py index f7f3b7ffd730..19bc2fbb93cd 100644 --- a/sdk/core/azure-mgmt-core/tests/test_policies.py +++ b/sdk/core/azure-mgmt-core/tests/test_policies.py @@ -35,13 +35,18 @@ import requests import httpretty +from azure.core.configuration import Configuration from azure.core.pipeline import Pipeline from azure.core.pipeline.transport import ( HttpRequest, RequestsTransport, ) -from azure.mgmt.core.policies import ARMAutoResourceProviderRegistrationPolicy +from azure.mgmt.core import ARMPipelineClient +from azure.mgmt.core.policies import ( + ARMAutoResourceProviderRegistrationPolicy, + ARMHttpLoggingPolicy +) @pytest.fixture def sleepless(monkeypatch): @@ -162,3 +167,21 @@ def test_register_failed_policy(): response = pipeline.run(request) assert response.http_response.status_code == 409 + +def test_default_http_logging_policy(): + config = Configuration() + pipeline_client = ARMPipelineClient(base_url="test", config=config) + http_logging_policy = pipeline_client._default_policies(config=config)[-1] + assert http_logging_policy.allowed_header_names == ARMHttpLoggingPolicy.DEFAULT_HEADERS_WHITELIST + +def test_pass_in_http_logging_policy(): + config = Configuration() + http_logging_policy = ARMHttpLoggingPolicy() + http_logging_policy.allowed_header_names.update( + {"x-ms-added-header"} + ) + config.http_logging_policy = http_logging_policy + + pipeline_client = ARMPipelineClient(base_url="test", config=config) + http_logging_policy = pipeline_client._default_policies(config=config)[-1] + assert http_logging_policy.allowed_header_names == ARMHttpLoggingPolicy.DEFAULT_HEADERS_WHITELIST.union({"x-ms-added-header"}) \ No newline at end of file