From 7f705208687be4ab9399cf82d89312094daca896 Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Wed, 24 Mar 2021 15:55:00 -0700 Subject: [PATCH 01/17] Add AzureNamedKeyCredential --- sdk/core/azure-core/CHANGELOG.md | 6 ++- sdk/core/azure-core/azure/core/_version.py | 2 +- sdk/core/azure-core/azure/core/credentials.py | 48 ++++++++++++++++++- .../azure/core/pipeline/policies/__init__.py | 3 +- .../core/pipeline/policies/_authentication.py | 17 +++++++ .../azure-core/tests/test_authentication.py | 45 ++++++++++++++++- 6 files changed, 115 insertions(+), 6 deletions(-) diff --git a/sdk/core/azure-core/CHANGELOG.md b/sdk/core/azure-core/CHANGELOG.md index cac731af04f0..2d5f7500018b 100644 --- a/sdk/core/azure-core/CHANGELOG.md +++ b/sdk/core/azure-core/CHANGELOG.md @@ -1,6 +1,10 @@ # Release History -## 1.12.1 (Unreleased) +## 1.13.0 (Unreleased) + +### Features + +- Added `azure.core.credentials.AzureNamedKeyCredential` credential. ### Bug fixes diff --git a/sdk/core/azure-core/azure/core/_version.py b/sdk/core/azure-core/azure/core/_version.py index e89e943b0813..7a5c38e4c326 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.12.1" +VERSION = "1.13.0" diff --git a/sdk/core/azure-core/azure/core/credentials.py b/sdk/core/azure-core/azure/core/credentials.py index 21aeb433ac81..00319b94b875 100644 --- a/sdk/core/azure-core/azure/core/credentials.py +++ b/sdk/core/azure-core/azure/core/credentials.py @@ -30,7 +30,7 @@ def get_token(self, *scopes, **kwargs): AccessToken = namedtuple("AccessToken", ["token", "expires_on"]) -__all__ = ["AzureKeyCredential", "AzureSasCredential", "AccessToken"] +__all__ = ["AzureKeyCredential", "AzureSasCredential", "AccessToken", "AzureNamedKeyCredential"] class AzureKeyCredential(object): @@ -111,3 +111,49 @@ def update(self, signature): if not isinstance(signature, six.string_types): raise TypeError("The signature used for updating must be a string.") self._signature = signature + + +class AzureNamedKeyCredential(object): + """Credential type used for working with any service needing a named key that follows patterns + established by the other credential types. + + :param str name: The name of the credential used to authenticate to an Azure service. + :param str key: The key used to authenticate to an Azure service. + :raises: TypeError + """ + def __init__(self, name, key): + # type: (str, str) -> None + if not isinstance(name, six.string_types) or not isinstance(key, six.string_types): + raise TypeError("Both name and key must be Strings.") + self._name = name + self._key = key + + @property + def name(self): + # type () -> str + """The value of the configured name. + + :rtype: str + """ + return self._name + + @property + def key(self): + # type () -> str + """The value of the configured key. + + :rtype: str + """ + return self._key + + def update(self, name, key): + # type: (str, str) -> None + """Update the named key credential. + + Both name and key must be provided in order to update the named key credential. + Individual attributes cannot be updated. + """ + if not isinstance(name, six.string_types) or not isinstance(key, six.string_types): + raise TypeError("Both name and key must be Strings.") + self._name = name + self._key = key diff --git a/sdk/core/azure-core/azure/core/pipeline/policies/__init__.py b/sdk/core/azure-core/azure/core/pipeline/policies/__init__.py index a0e81b13cef5..74079e477ccd 100644 --- a/sdk/core/azure-core/azure/core/pipeline/policies/__init__.py +++ b/sdk/core/azure-core/azure/core/pipeline/policies/__init__.py @@ -25,7 +25,7 @@ # -------------------------------------------------------------------------- from ._base import HTTPPolicy, SansIOHTTPPolicy, RequestHistory -from ._authentication import BearerTokenCredentialPolicy, AzureKeyCredentialPolicy, AzureSasCredentialPolicy +from ._authentication import BearerTokenCredentialPolicy, AzureKeyCredentialPolicy, AzureSasCredentialPolicy, AzureNamedKeyCredentialPolicy from ._custom_hook import CustomHookPolicy from ._redirect import RedirectPolicy from ._retry import RetryPolicy, RetryMode @@ -45,6 +45,7 @@ 'SansIOHTTPPolicy', 'BearerTokenCredentialPolicy', 'AzureKeyCredentialPolicy', + 'AzureNamedKeyCredentialPolicy', 'AzureSasCredentialPolicy', 'HeadersPolicy', 'UserAgentPolicy', diff --git a/sdk/core/azure-core/azure/core/pipeline/policies/_authentication.py b/sdk/core/azure-core/azure/core/pipeline/policies/_authentication.py index 929920033cdf..14feb08f0e3e 100644 --- a/sdk/core/azure-core/azure/core/pipeline/policies/_authentication.py +++ b/sdk/core/azure-core/azure/core/pipeline/policies/_authentication.py @@ -145,3 +145,20 @@ def on_request(self, request): else: url = url + "?" + signature request.http_request.url = url + + +class AzureNamedKeyCredentialPolicy(SansIOHTTPPolicy): + """Adds a key header for the provided credential. + + :param credential: The credential used to authenticate requests. + :type credential: ~azure.core.credentials.AzureNamedKeyCredential + :raises: ValueError or TypeError + """ + def __init__(self, credential, **kwargs): # pylint: disable=unused-argument + # type: (AzureNamedKeyCredential, **Any) -> None + super(AzureNamedKeyCredentialPolicy, self).__init__() + self._key = credential.key + self._name = credential.name + + def on_request(self, request): + request.http_request.headers[self._name] = self._key diff --git a/sdk/core/azure-core/tests/test_authentication.py b/sdk/core/azure-core/tests/test_authentication.py index ddfcc314fcd9..c8a8af6fb800 100644 --- a/sdk/core/azure-core/tests/test_authentication.py +++ b/sdk/core/azure-core/tests/test_authentication.py @@ -6,10 +6,13 @@ import time import azure.core -from azure.core.credentials import AccessToken, AzureKeyCredential, AzureSasCredential +from azure.core.credentials import AccessToken, AzureKeyCredential, AzureSasCredential, AzureNamedKeyCredential from azure.core.exceptions import ServiceRequestError from azure.core.pipeline import Pipeline -from azure.core.pipeline.policies import BearerTokenCredentialPolicy, SansIOHTTPPolicy, AzureKeyCredentialPolicy, AzureSasCredentialPolicy +from azure.core.pipeline.policies import ( + BearerTokenCredentialPolicy, SansIOHTTPPolicy, AzureKeyCredentialPolicy, + AzureSasCredentialPolicy, AzureNamedKeyCredentialPolicy +) from azure.core.pipeline.transport import HttpRequest import pytest @@ -230,3 +233,41 @@ def test_azure_sas_credential_policy_raises(): sas = 1234 with pytest.raises(TypeError): credential = AzureSasCredential(sas) + +def test_azure_named_key_credential(): + cred = AzureNamedKeyCredential("sample_name", "samplekey") + + assert cred.name == "sample_name" + assert cred.key == "samplekey" + + cred.update("newname", "newkey") + assert cred.name == "newname" + assert cred.key == "newkey" + + +def test_azure_named_key_credential_raises(): + with pytest.raises(TypeError, match="Both name and key must be Strings."): + cred = AzureNamedKeyCredential("sample_name", 123345) + + cred = AzureNamedKeyCredential("sample_name", "samplekey") + assert cred.name == "sample_name" + assert cred.key == "samplekey" + + with pytest.raises(TypeError, match="Both name and key must be Strings."): + cred.update(1234, "newkey") + +def test_azure_named_key_credential_policy(): + """Tests to see if we can create an AzureKeyCredentialPolicy""" + + key_name = "api_key" + api_key = "test_key" + + def verify_authorization_header(request): + assert request.headers[key_name] == api_key + + transport=Mock(send=verify_authorization_header) + credential = AzureNamedKeyCredential(key_name, api_key) + credential_policy = AzureNamedKeyCredentialPolicy(credential=credential) + pipeline = Pipeline(transport=transport, policies=[credential_policy]) + + pipeline.run(HttpRequest("GET", "https://test_key_credential")) From 3db2ef26ec7e3ffb25b5cfc9d3c693ad6615e095 Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Wed, 24 Mar 2021 17:12:43 -0700 Subject: [PATCH 02/17] lint --- .../azure-core/azure/core/pipeline/policies/__init__.py | 5 ++++- .../azure/core/pipeline/policies/_authentication.py | 8 +++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/sdk/core/azure-core/azure/core/pipeline/policies/__init__.py b/sdk/core/azure-core/azure/core/pipeline/policies/__init__.py index 74079e477ccd..a3aa02dd03f1 100644 --- a/sdk/core/azure-core/azure/core/pipeline/policies/__init__.py +++ b/sdk/core/azure-core/azure/core/pipeline/policies/__init__.py @@ -25,7 +25,10 @@ # -------------------------------------------------------------------------- from ._base import HTTPPolicy, SansIOHTTPPolicy, RequestHistory -from ._authentication import BearerTokenCredentialPolicy, AzureKeyCredentialPolicy, AzureSasCredentialPolicy, AzureNamedKeyCredentialPolicy +from ._authentication import ( + BearerTokenCredentialPolicy, AzureKeyCredentialPolicy, AzureSasCredentialPolicy, + AzureNamedKeyCredentialPolicy + ) from ._custom_hook import CustomHookPolicy from ._redirect import RedirectPolicy from ._retry import RetryPolicy, RetryMode diff --git a/sdk/core/azure-core/azure/core/pipeline/policies/_authentication.py b/sdk/core/azure-core/azure/core/pipeline/policies/_authentication.py index 14feb08f0e3e..2c2622899ea5 100644 --- a/sdk/core/azure-core/azure/core/pipeline/policies/_authentication.py +++ b/sdk/core/azure-core/azure/core/pipeline/policies/_authentication.py @@ -17,7 +17,13 @@ if TYPE_CHECKING: # pylint:disable=unused-import from typing import Any, Dict, Optional - from azure.core.credentials import AccessToken, TokenCredential, AzureKeyCredential, AzureSasCredential + from azure.core.credentials import ( + AccessToken, + TokenCredential, + AzureKeyCredential, + AzureSasCredential, + AzureNamedKeyCredential + ) from azure.core.pipeline import PipelineRequest From 87530e982055b7f8c53856f060238e1e53368c5c Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Fri, 26 Mar 2021 09:58:34 -0700 Subject: [PATCH 03/17] Update sdk/core/azure-core/CHANGELOG.md Co-authored-by: swathipil <76007337+swathipil@users.noreply.github.com> --- sdk/core/azure-core/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/core/azure-core/CHANGELOG.md b/sdk/core/azure-core/CHANGELOG.md index 1e80588d8282..ce23c8899c8d 100644 --- a/sdk/core/azure-core/CHANGELOG.md +++ b/sdk/core/azure-core/CHANGELOG.md @@ -4,7 +4,7 @@ ### Features -- Added `azure.core.credentials.AzureNamedKeyCredential` credential #17548. +- Added `azure.core.credentials.AzureNamedKeyCredential` credential and its respective policy #17548. - Supported adding custom policies #16519 ### Bug fixes From 8be7ce5f3bc763abdbedf15855e1b524c521dcfd Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Wed, 14 Apr 2021 15:46:01 -0700 Subject: [PATCH 04/17] some changes --- sdk/core/azure-core/azure/core/credentials.py | 25 ++++++------------- .../core/pipeline/policies/_authentication.py | 8 +++--- .../azure-core/tests/test_authentication.py | 14 +++++------ .../azure/eventgrid/_helpers.py | 5 ++++ 4 files changed, 24 insertions(+), 28 deletions(-) diff --git a/sdk/core/azure-core/azure/core/credentials.py b/sdk/core/azure-core/azure/core/credentials.py index 00319b94b875..5419f5dec7e9 100644 --- a/sdk/core/azure-core/azure/core/credentials.py +++ b/sdk/core/azure-core/azure/core/credentials.py @@ -4,6 +4,7 @@ # license information. # ------------------------------------------------------------------------- from typing import TYPE_CHECKING +from collections import namedtuple import six @@ -26,10 +27,11 @@ def get_token(self, *scopes, **kwargs): else: - from collections import namedtuple - AccessToken = namedtuple("AccessToken", ["token", "expires_on"]) +AzureNamedKey = namedtuple("AzureNamedKey", ["name", "key"]) + + __all__ = ["AzureKeyCredential", "AzureSasCredential", "AccessToken", "AzureNamedKeyCredential"] @@ -125,26 +127,16 @@ def __init__(self, name, key): # type: (str, str) -> None if not isinstance(name, six.string_types) or not isinstance(key, six.string_types): raise TypeError("Both name and key must be Strings.") - self._name = name - self._key = key + self._credential = AzureNamedKey(name, key) @property - def name(self): + def credential(self): # type () -> str """The value of the configured name. :rtype: str """ - return self._name - - @property - def key(self): - # type () -> str - """The value of the configured key. - - :rtype: str - """ - return self._key + return self._credential def update(self, name, key): # type: (str, str) -> None @@ -155,5 +147,4 @@ def update(self, name, key): """ if not isinstance(name, six.string_types) or not isinstance(key, six.string_types): raise TypeError("Both name and key must be Strings.") - self._name = name - self._key = key + self._credential = AzureNamedKey(name, key) diff --git a/sdk/core/azure-core/azure/core/pipeline/policies/_authentication.py b/sdk/core/azure-core/azure/core/pipeline/policies/_authentication.py index 2c2622899ea5..df7bdd050a3b 100644 --- a/sdk/core/azure-core/azure/core/pipeline/policies/_authentication.py +++ b/sdk/core/azure-core/azure/core/pipeline/policies/_authentication.py @@ -160,11 +160,11 @@ class AzureNamedKeyCredentialPolicy(SansIOHTTPPolicy): :type credential: ~azure.core.credentials.AzureNamedKeyCredential :raises: ValueError or TypeError """ - def __init__(self, credential, **kwargs): # pylint: disable=unused-argument + def __init__(self, credential, name, **kwargs): # pylint: disable=unused-argument # type: (AzureNamedKeyCredential, **Any) -> None super(AzureNamedKeyCredentialPolicy, self).__init__() - self._key = credential.key - self._name = credential.name + self._credential = credential + self._name = name def on_request(self, request): - request.http_request.headers[self._name] = self._key + request.http_request.headers[self._name] = self._credential.credential.key diff --git a/sdk/core/azure-core/tests/test_authentication.py b/sdk/core/azure-core/tests/test_authentication.py index c8a8af6fb800..e1bf5ef2b980 100644 --- a/sdk/core/azure-core/tests/test_authentication.py +++ b/sdk/core/azure-core/tests/test_authentication.py @@ -237,12 +237,12 @@ def test_azure_sas_credential_policy_raises(): def test_azure_named_key_credential(): cred = AzureNamedKeyCredential("sample_name", "samplekey") - assert cred.name == "sample_name" - assert cred.key == "samplekey" + assert cred.credential.name == "sample_name" + assert cred.credential.key == "samplekey" cred.update("newname", "newkey") - assert cred.name == "newname" - assert cred.key == "newkey" + assert cred.credential.name == "newname" + assert cred.credential.key == "newkey" def test_azure_named_key_credential_raises(): @@ -250,8 +250,8 @@ def test_azure_named_key_credential_raises(): cred = AzureNamedKeyCredential("sample_name", 123345) cred = AzureNamedKeyCredential("sample_name", "samplekey") - assert cred.name == "sample_name" - assert cred.key == "samplekey" + assert cred.credential.name == "sample_name" + assert cred.credential.key == "samplekey" with pytest.raises(TypeError, match="Both name and key must be Strings."): cred.update(1234, "newkey") @@ -267,7 +267,7 @@ def verify_authorization_header(request): transport=Mock(send=verify_authorization_header) credential = AzureNamedKeyCredential(key_name, api_key) - credential_policy = AzureNamedKeyCredentialPolicy(credential=credential) + credential_policy = AzureNamedKeyCredentialPolicy(credential=credential, name=key_name) pipeline = Pipeline(transport=transport, policies=[credential_policy]) pipeline.run(HttpRequest("GET", "https://test_key_credential")) diff --git a/sdk/eventgrid/azure-eventgrid/azure/eventgrid/_helpers.py b/sdk/eventgrid/azure-eventgrid/azure/eventgrid/_helpers.py index bc7b15bf089f..486036ab6a34 100644 --- a/sdk/eventgrid/azure-eventgrid/azure/eventgrid/_helpers.py +++ b/sdk/eventgrid/azure-eventgrid/azure/eventgrid/_helpers.py @@ -77,6 +77,11 @@ def _get_authentication_policy(credential): return AzureKeyCredentialPolicy( credential=credential, name=constants.EVENTGRID_KEY_HEADER ) + elif isinstance(credential, AzureNamedKeyCredential): + return AzureNamedKeyCredentialPolicy( + name=constants.EVENTGRID_KEY_HEADER, + credential=credential + ) if isinstance(credential, AzureSasCredential): return EventGridSasCredentialPolicy( credential=credential, name=constants.EVENTGRID_TOKEN_HEADER From 8b215145a62806dd897488ff040e3738bf36b479 Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Wed, 14 Apr 2021 15:47:30 -0700 Subject: [PATCH 05/17] oops --- sdk/eventgrid/azure-eventgrid/azure/eventgrid/_helpers.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/sdk/eventgrid/azure-eventgrid/azure/eventgrid/_helpers.py b/sdk/eventgrid/azure-eventgrid/azure/eventgrid/_helpers.py index 486036ab6a34..bc7b15bf089f 100644 --- a/sdk/eventgrid/azure-eventgrid/azure/eventgrid/_helpers.py +++ b/sdk/eventgrid/azure-eventgrid/azure/eventgrid/_helpers.py @@ -77,11 +77,6 @@ def _get_authentication_policy(credential): return AzureKeyCredentialPolicy( credential=credential, name=constants.EVENTGRID_KEY_HEADER ) - elif isinstance(credential, AzureNamedKeyCredential): - return AzureNamedKeyCredentialPolicy( - name=constants.EVENTGRID_KEY_HEADER, - credential=credential - ) if isinstance(credential, AzureSasCredential): return EventGridSasCredentialPolicy( credential=credential, name=constants.EVENTGRID_TOKEN_HEADER From 05c4205d4dee9e16d6bb4ee11174eb86286b741d Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Wed, 14 Apr 2021 15:51:25 -0700 Subject: [PATCH 06/17] version --- sdk/core/azure-core/azure/core/_version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/core/azure-core/azure/core/_version.py b/sdk/core/azure-core/azure/core/_version.py index 36514ce9837d..656148f88d2b 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.13.1" +VERSION = "1.14.0" From d8ba6de40fa26ae762461f944764be6d807cde21 Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Wed, 14 Apr 2021 16:10:18 -0700 Subject: [PATCH 07/17] mypy fix --- .../azure-core/azure/core/pipeline/policies/_authentication.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/core/azure-core/azure/core/pipeline/policies/_authentication.py b/sdk/core/azure-core/azure/core/pipeline/policies/_authentication.py index df7bdd050a3b..9285179304fa 100644 --- a/sdk/core/azure-core/azure/core/pipeline/policies/_authentication.py +++ b/sdk/core/azure-core/azure/core/pipeline/policies/_authentication.py @@ -161,7 +161,7 @@ class AzureNamedKeyCredentialPolicy(SansIOHTTPPolicy): :raises: ValueError or TypeError """ def __init__(self, credential, name, **kwargs): # pylint: disable=unused-argument - # type: (AzureNamedKeyCredential, **Any) -> None + # type: (AzureNamedKeyCredential, str, **Any) -> None super(AzureNamedKeyCredentialPolicy, self).__init__() self._credential = credential self._name = name From 1555f1df063e17ab4c3fb10522f6de076d30dc01 Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Wed, 14 Apr 2021 18:07:09 -0700 Subject: [PATCH 08/17] lint --- sdk/core/azure-core/azure/core/credentials.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sdk/core/azure-core/azure/core/credentials.py b/sdk/core/azure-core/azure/core/credentials.py index 5419f5dec7e9..da83603cf8bf 100644 --- a/sdk/core/azure-core/azure/core/credentials.py +++ b/sdk/core/azure-core/azure/core/credentials.py @@ -3,11 +3,10 @@ # Licensed under the MIT License. See LICENSE.txt in the project root for # license information. # ------------------------------------------------------------------------- -from typing import TYPE_CHECKING from collections import namedtuple +from typing import TYPE_CHECKING import six - if TYPE_CHECKING: from typing import Any, NamedTuple from typing_extensions import Protocol From 6ab090c75eb2baf874e949fd425646c9cbfb333b Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Fri, 16 Apr 2021 10:50:21 -0700 Subject: [PATCH 09/17] remove policy --- sdk/core/azure-core/azure/core/credentials.py | 10 +++++----- .../core/pipeline/policies/_authentication.py | 18 ------------------ .../azure-core/tests/test_authentication.py | 18 +----------------- 3 files changed, 6 insertions(+), 40 deletions(-) diff --git a/sdk/core/azure-core/azure/core/credentials.py b/sdk/core/azure-core/azure/core/credentials.py index da83603cf8bf..3c70983d2b3d 100644 --- a/sdk/core/azure-core/azure/core/credentials.py +++ b/sdk/core/azure-core/azure/core/credentials.py @@ -125,15 +125,15 @@ class AzureNamedKeyCredential(object): def __init__(self, name, key): # type: (str, str) -> None if not isinstance(name, six.string_types) or not isinstance(key, six.string_types): - raise TypeError("Both name and key must be Strings.") + raise TypeError("Both name and key must be strings.") self._credential = AzureNamedKey(name, key) @property - def credential(self): - # type () -> str + def named_key(self): + # type () -> AzureNamedKey """The value of the configured name. - :rtype: str + :rtype: AzureNamedKey """ return self._credential @@ -145,5 +145,5 @@ def update(self, name, key): Individual attributes cannot be updated. """ if not isinstance(name, six.string_types) or not isinstance(key, six.string_types): - raise TypeError("Both name and key must be Strings.") + raise TypeError("Both name and key must be strings.") self._credential = AzureNamedKey(name, key) diff --git a/sdk/core/azure-core/azure/core/pipeline/policies/_authentication.py b/sdk/core/azure-core/azure/core/pipeline/policies/_authentication.py index 9285179304fa..4e9c8bd47b48 100644 --- a/sdk/core/azure-core/azure/core/pipeline/policies/_authentication.py +++ b/sdk/core/azure-core/azure/core/pipeline/policies/_authentication.py @@ -22,7 +22,6 @@ TokenCredential, AzureKeyCredential, AzureSasCredential, - AzureNamedKeyCredential ) from azure.core.pipeline import PipelineRequest @@ -151,20 +150,3 @@ def on_request(self, request): else: url = url + "?" + signature request.http_request.url = url - - -class AzureNamedKeyCredentialPolicy(SansIOHTTPPolicy): - """Adds a key header for the provided credential. - - :param credential: The credential used to authenticate requests. - :type credential: ~azure.core.credentials.AzureNamedKeyCredential - :raises: ValueError or TypeError - """ - def __init__(self, credential, name, **kwargs): # pylint: disable=unused-argument - # type: (AzureNamedKeyCredential, str, **Any) -> None - super(AzureNamedKeyCredentialPolicy, self).__init__() - self._credential = credential - self._name = name - - def on_request(self, request): - request.http_request.headers[self._name] = self._credential.credential.key diff --git a/sdk/core/azure-core/tests/test_authentication.py b/sdk/core/azure-core/tests/test_authentication.py index e1bf5ef2b980..b83291d51ee0 100644 --- a/sdk/core/azure-core/tests/test_authentication.py +++ b/sdk/core/azure-core/tests/test_authentication.py @@ -11,7 +11,7 @@ from azure.core.pipeline import Pipeline from azure.core.pipeline.policies import ( BearerTokenCredentialPolicy, SansIOHTTPPolicy, AzureKeyCredentialPolicy, - AzureSasCredentialPolicy, AzureNamedKeyCredentialPolicy + AzureSasCredentialPolicy ) from azure.core.pipeline.transport import HttpRequest @@ -255,19 +255,3 @@ def test_azure_named_key_credential_raises(): with pytest.raises(TypeError, match="Both name and key must be Strings."): cred.update(1234, "newkey") - -def test_azure_named_key_credential_policy(): - """Tests to see if we can create an AzureKeyCredentialPolicy""" - - key_name = "api_key" - api_key = "test_key" - - def verify_authorization_header(request): - assert request.headers[key_name] == api_key - - transport=Mock(send=verify_authorization_header) - credential = AzureNamedKeyCredential(key_name, api_key) - credential_policy = AzureNamedKeyCredentialPolicy(credential=credential, name=key_name) - pipeline = Pipeline(transport=transport, policies=[credential_policy]) - - pipeline.run(HttpRequest("GET", "https://test_key_credential")) From 6a98f0ba60a7acd9c184a378763f40c9ed6d5a62 Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Fri, 16 Apr 2021 10:51:13 -0700 Subject: [PATCH 10/17] doc --- sdk/core/azure-core/azure/core/credentials.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sdk/core/azure-core/azure/core/credentials.py b/sdk/core/azure-core/azure/core/credentials.py index 3c70983d2b3d..e5146d7f947a 100644 --- a/sdk/core/azure-core/azure/core/credentials.py +++ b/sdk/core/azure-core/azure/core/credentials.py @@ -143,6 +143,9 @@ def update(self, name, key): Both name and key must be provided in order to update the named key credential. Individual attributes cannot be updated. + + :param str name: The name of the credential used to authenticate to an Azure service. + :param str key: The key used to authenticate to an Azure service. """ if not isinstance(name, six.string_types) or not isinstance(key, six.string_types): raise TypeError("Both name and key must be strings.") From 5f94691050c10285f07ded2098cd1f75dbb029a6 Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Fri, 16 Apr 2021 10:53:06 -0700 Subject: [PATCH 11/17] Update sdk/core/azure-core/azure/core/pipeline/policies/_authentication.py --- .../azure/core/pipeline/policies/_authentication.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/sdk/core/azure-core/azure/core/pipeline/policies/_authentication.py b/sdk/core/azure-core/azure/core/pipeline/policies/_authentication.py index 4e9c8bd47b48..929920033cdf 100644 --- a/sdk/core/azure-core/azure/core/pipeline/policies/_authentication.py +++ b/sdk/core/azure-core/azure/core/pipeline/policies/_authentication.py @@ -17,12 +17,7 @@ if TYPE_CHECKING: # pylint:disable=unused-import from typing import Any, Dict, Optional - from azure.core.credentials import ( - AccessToken, - TokenCredential, - AzureKeyCredential, - AzureSasCredential, - ) + from azure.core.credentials import AccessToken, TokenCredential, AzureKeyCredential, AzureSasCredential from azure.core.pipeline import PipelineRequest From 53fab2e65909ca8f613768b92033608b22a45ecf Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Fri, 16 Apr 2021 10:55:43 -0700 Subject: [PATCH 12/17] Update sdk/core/azure-core/azure/core/pipeline/policies/__init__.py --- sdk/core/azure-core/azure/core/pipeline/policies/__init__.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/sdk/core/azure-core/azure/core/pipeline/policies/__init__.py b/sdk/core/azure-core/azure/core/pipeline/policies/__init__.py index a3aa02dd03f1..a390ee201ca6 100644 --- a/sdk/core/azure-core/azure/core/pipeline/policies/__init__.py +++ b/sdk/core/azure-core/azure/core/pipeline/policies/__init__.py @@ -25,10 +25,7 @@ # -------------------------------------------------------------------------- from ._base import HTTPPolicy, SansIOHTTPPolicy, RequestHistory -from ._authentication import ( - BearerTokenCredentialPolicy, AzureKeyCredentialPolicy, AzureSasCredentialPolicy, - AzureNamedKeyCredentialPolicy - ) +from ._authentication import BearerTokenCredentialPolicy, AzureKeyCredentialPolicy, AzureSasCredentialPolicy from ._custom_hook import CustomHookPolicy from ._redirect import RedirectPolicy from ._retry import RetryPolicy, RetryMode From 0865f90ccfc25fee2583fe7fccf3c4792129576f Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Fri, 16 Apr 2021 10:56:13 -0700 Subject: [PATCH 13/17] Update sdk/core/azure-core/azure/core/pipeline/policies/__init__.py --- sdk/core/azure-core/azure/core/pipeline/policies/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/sdk/core/azure-core/azure/core/pipeline/policies/__init__.py b/sdk/core/azure-core/azure/core/pipeline/policies/__init__.py index a390ee201ca6..a0e81b13cef5 100644 --- a/sdk/core/azure-core/azure/core/pipeline/policies/__init__.py +++ b/sdk/core/azure-core/azure/core/pipeline/policies/__init__.py @@ -45,7 +45,6 @@ 'SansIOHTTPPolicy', 'BearerTokenCredentialPolicy', 'AzureKeyCredentialPolicy', - 'AzureNamedKeyCredentialPolicy', 'AzureSasCredentialPolicy', 'HeadersPolicy', 'UserAgentPolicy', From 4a6736ff9db74c34cab946e9d7940381fa9af5d1 Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Fri, 16 Apr 2021 11:41:07 -0700 Subject: [PATCH 14/17] changelog --- sdk/core/azure-core/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/core/azure-core/CHANGELOG.md b/sdk/core/azure-core/CHANGELOG.md index 65f85132f74b..4f5feaa9a18a 100644 --- a/sdk/core/azure-core/CHANGELOG.md +++ b/sdk/core/azure-core/CHANGELOG.md @@ -4,7 +4,7 @@ ### New Features -- Added `azure.core.credentials.AzureNamedKeyCredential` credential and its respective policy #17548. +- Added `azure.core.credentials.AzureNamedKeyCredential` credential #17548. ## 1.13.0 (2021-04-02) From 4c2143d0b2fbfb1b392aba66d133ba6155297ce8 Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Fri, 16 Apr 2021 14:28:40 -0700 Subject: [PATCH 15/17] update tests --- sdk/core/azure-core/tests/test_authentication.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/sdk/core/azure-core/tests/test_authentication.py b/sdk/core/azure-core/tests/test_authentication.py index b83291d51ee0..e73fcdeb1b9c 100644 --- a/sdk/core/azure-core/tests/test_authentication.py +++ b/sdk/core/azure-core/tests/test_authentication.py @@ -237,12 +237,12 @@ def test_azure_sas_credential_policy_raises(): def test_azure_named_key_credential(): cred = AzureNamedKeyCredential("sample_name", "samplekey") - assert cred.credential.name == "sample_name" - assert cred.credential.key == "samplekey" + assert cred.named_key.name == "sample_name" + assert cred.named_key.key == "samplekey" cred.update("newname", "newkey") - assert cred.credential.name == "newname" - assert cred.credential.key == "newkey" + assert cred.named_key.name == "newname" + assert cred.named_key.key == "newkey" def test_azure_named_key_credential_raises(): @@ -250,8 +250,8 @@ def test_azure_named_key_credential_raises(): cred = AzureNamedKeyCredential("sample_name", 123345) cred = AzureNamedKeyCredential("sample_name", "samplekey") - assert cred.credential.name == "sample_name" - assert cred.credential.key == "samplekey" + assert cred.named_key.name == "sample_name" + assert cred.named_key.key == "samplekey" with pytest.raises(TypeError, match="Both name and key must be Strings."): cred.update(1234, "newkey") From 71f0b35d56e7577b1cadf544f33eb609c47647b9 Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Fri, 16 Apr 2021 15:22:06 -0700 Subject: [PATCH 16/17] fix --- sdk/core/azure-core/tests/test_authentication.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sdk/core/azure-core/tests/test_authentication.py b/sdk/core/azure-core/tests/test_authentication.py index e73fcdeb1b9c..2bc7555344e6 100644 --- a/sdk/core/azure-core/tests/test_authentication.py +++ b/sdk/core/azure-core/tests/test_authentication.py @@ -246,12 +246,12 @@ def test_azure_named_key_credential(): def test_azure_named_key_credential_raises(): - with pytest.raises(TypeError, match="Both name and key must be Strings."): + with pytest.raises(TypeError, match="Both name and key must be strings."): cred = AzureNamedKeyCredential("sample_name", 123345) cred = AzureNamedKeyCredential("sample_name", "samplekey") assert cred.named_key.name == "sample_name" assert cred.named_key.key == "samplekey" - with pytest.raises(TypeError, match="Both name and key must be Strings."): + with pytest.raises(TypeError, match="Both name and key must be strings."): cred.update(1234, "newkey") From 6bc4c0a963ccaec50752de4257cb51a3a5d3d4fb Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Sun, 25 Apr 2021 14:40:30 -0700 Subject: [PATCH 17/17] update test --- sdk/core/azure-core/tests/test_authentication.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sdk/core/azure-core/tests/test_authentication.py b/sdk/core/azure-core/tests/test_authentication.py index 2bc7555344e6..6c112aa5926e 100644 --- a/sdk/core/azure-core/tests/test_authentication.py +++ b/sdk/core/azure-core/tests/test_authentication.py @@ -239,11 +239,12 @@ def test_azure_named_key_credential(): assert cred.named_key.name == "sample_name" assert cred.named_key.key == "samplekey" + assert isinstance(cred.named_key, tuple) cred.update("newname", "newkey") assert cred.named_key.name == "newname" assert cred.named_key.key == "newkey" - + assert isinstance(cred.named_key, tuple) def test_azure_named_key_credential_raises(): with pytest.raises(TypeError, match="Both name and key must be strings."):