Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set http_logging_policy in Configuration #12218

Merged
merged 15 commits into from
Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 4 additions & 0 deletions sdk/core/azure-core/azure/core/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Configuration(object):
: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 http_logging_policy: Provides configuration parameters for HTTP specific logging.
:keyword 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
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
lmazuel marked this conversation as resolved.
Show resolved Hide resolved

# User Agent configuration
self.user_agent_policy = None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ class HttpLoggingPolicy(SansIOHTTPPolicy):
"""The Pipeline policy that handles logging of HTTP requests and responses.
"""

DEFAULT_HEADERS_WHITELIST = set([
DEFAULT_HEADERS_ALLOWLIST = set([
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest don't do this in this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on a conversation with @xiangyan99, I'm going to revert these changes in this PR and open another PR for these changes in core. Issue filed here #12219

"x-ms-request-id",
"x-ms-client-request-id",
"x-ms-return-client-request-id",
Expand All @@ -364,14 +364,18 @@ class HttpLoggingPolicy(SansIOHTTPPolicy):
"Transfer-Encoding",
"User-Agent",
])

# Deprecated
DEFAULT_HEADERS_WHITELIST = DEFAULT_HEADERS_ALLOWLIST

REDACTED_PLACEHOLDER = "REDACTED"

def __init__(self, logger=None, **kwargs): # pylint: disable=unused-argument
self.logger = logger or logging.getLogger(
"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.DEFAULT_HEADERS_ALLOWLIST)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes a bug in ARMHttpLoggingPolicy. Previously the allowed_header_names would be set to HttpLoggingPolicy.DEFAULT_HEADERS_ALLOWLIST instead of ARMHttpLoggingPolicy.DEFAULT_HEADERS_ALLOWLIST


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_ALLOWLIST

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_ALLOWLIST.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_ALLOWLIST

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_ALLOWLIST.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),
]
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ARMHttpLoggingPolicy(HttpLoggingPolicy):
"""HttpLoggingPolicy with ARM specific safe headers fopr loggers.
"""

DEFAULT_HEADERS_WHITELIST = HttpLoggingPolicy.DEFAULT_HEADERS_WHITELIST | set([
DEFAULT_HEADERS_ALLOWLIST = HttpLoggingPolicy.DEFAULT_HEADERS_ALLOWLIST | set([
# https://docs.microsoft.com/azure/azure-resource-manager/management/request-limits-and-throttling#remaining-requests
"x-ms-ratelimit-remaining-subscription-reads",
"x-ms-ratelimit-remaining-subscription-writes",
Expand All @@ -47,6 +47,9 @@ class ARMHttpLoggingPolicy(HttpLoggingPolicy):
"x-ms-request-charge",
])

# Deprecated
DEFAULT_HEADERS_WHITELIST = DEFAULT_HEADERS_ALLOWLIST


__all__ = ["ARMAutoResourceProviderRegistrationPolicy", "ARMHttpLoggingPolicy"]

Expand Down
Empty file.
46 changes: 46 additions & 0 deletions sdk/core/azure-mgmt-core/tests/asynctests/test_policies.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_ALLOWLIST

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_ALLOWLIST.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_ALLOWLIST

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_ALLOWLIST.union({"x-ms-added-header"})