Skip to content

Commit

Permalink
Set http_logging_policy in Configuration (Azure#12218)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
iscai-msft authored Jun 29, 2020
1 parent 72d461c commit de7168a
Show file tree
Hide file tree
Showing 14 changed files with 155 additions and 18 deletions.
7 changes: 6 additions & 1 deletion sdk/core/azure-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@

# Release History

## 1.6.1 (Unreleased)
## 1.7.0 (Unreleased)

### Bug fixes

- `AzureKeyCredentialPolicy` will now accept (and ignore) passed in kwargs #11963
- 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
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/azure-core/azure/core/_pipeline_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/azure-core/azure/core/_pipeline_client_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/azure-core/azure/core/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
# regenerated.
# --------------------------------------------------------------------------

VERSION = "1.6.1"
VERSION = "1.7.0"
20 changes: 12 additions & 8 deletions sdk/core/azure-core/azure/core/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
27 changes: 26 additions & 1 deletion sdk/core/azure-core/tests/azure_core_asynctests/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,19 @@
UserAgentPolicy,
AsyncRedirectPolicy,
AsyncHTTPPolicy,
AsyncRetryPolicy)
AsyncRetryPolicy,
HttpLoggingPolicy
)
from azure.core.pipeline.transport import (
AsyncHttpTransport,
HttpRequest,
AsyncioRequestsTransport,
TrioRequestsTransport,
AioHttpTransport
)

from azure.core.configuration import Configuration
from azure.core import AsyncPipelineClient
from azure.core.exceptions import AzureError

import aiohttp
Expand Down Expand Up @@ -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():

Expand Down
22 changes: 22 additions & 0 deletions sdk/core/azure-core/tests/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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):
Expand Down
12 changes: 12 additions & 0 deletions sdk/core/azure-mgmt-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]
2 changes: 1 addition & 1 deletion sdk/core/azure-mgmt-core/azure/mgmt/core/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
# regenerated.
# --------------------------------------------------------------------------

VERSION = "1.1.0"
VERSION = "1.2.0"
46 changes: 46 additions & 0 deletions sdk/core/azure-mgmt-core/tests/asynctests/test_policies_async.py
Original file line number Diff line number Diff line change
@@ -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"})
25 changes: 24 additions & 1 deletion sdk/core/azure-mgmt-core/tests/test_policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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"})

0 comments on commit de7168a

Please sign in to comment.