Skip to content

Commit 844e16d

Browse files
Rakshith Bhyravabhotlaswathipil
andauthored
Add AzureNamedKeyCredential (Azure#17548)
* Add AzureNamedKeyCredential * lint * Update sdk/core/azure-core/CHANGELOG.md Co-authored-by: swathipil <76007337+swathipil@users.noreply.github.com> * some changes * oops * version * mypy fix * lint * remove policy * doc * Update sdk/core/azure-core/azure/core/pipeline/policies/_authentication.py * Update sdk/core/azure-core/azure/core/pipeline/policies/__init__.py * Update sdk/core/azure-core/azure/core/pipeline/policies/__init__.py * changelog * update tests * fix * update test Co-authored-by: swathipil <76007337+swathipil@users.noreply.github.com>
1 parent 0f81bed commit 844e16d

File tree

4 files changed

+76
-8
lines changed

4 files changed

+76
-8
lines changed

sdk/core/azure-core/CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# Release History
22

3-
## 1.13.1 (Unreleased)
3+
## 1.14.0 (Unreleased)
44

5+
### New Features
6+
7+
- Added `azure.core.credentials.AzureNamedKeyCredential` credential #17548.
58

69
## 1.13.0 (2021-04-02)
710

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
# regenerated.
1010
# --------------------------------------------------------------------------
1111

12-
VERSION = "1.13.1"
12+
VERSION = "1.14.0"

sdk/core/azure-core/azure/core/credentials.py

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
# Licensed under the MIT License. See LICENSE.txt in the project root for
44
# license information.
55
# -------------------------------------------------------------------------
6+
from collections import namedtuple
67
from typing import TYPE_CHECKING
78
import six
89

9-
1010
if TYPE_CHECKING:
1111
from typing import Any, NamedTuple
1212
from typing_extensions import Protocol
@@ -26,11 +26,12 @@ def get_token(self, *scopes, **kwargs):
2626

2727

2828
else:
29-
from collections import namedtuple
30-
3129
AccessToken = namedtuple("AccessToken", ["token", "expires_on"])
3230

33-
__all__ = ["AzureKeyCredential", "AzureSasCredential", "AccessToken"]
31+
AzureNamedKey = namedtuple("AzureNamedKey", ["name", "key"])
32+
33+
34+
__all__ = ["AzureKeyCredential", "AzureSasCredential", "AccessToken", "AzureNamedKeyCredential"]
3435

3536

3637
class AzureKeyCredential(object):
@@ -111,3 +112,41 @@ def update(self, signature):
111112
if not isinstance(signature, six.string_types):
112113
raise TypeError("The signature used for updating must be a string.")
113114
self._signature = signature
115+
116+
117+
class AzureNamedKeyCredential(object):
118+
"""Credential type used for working with any service needing a named key that follows patterns
119+
established by the other credential types.
120+
121+
:param str name: The name of the credential used to authenticate to an Azure service.
122+
:param str key: The key used to authenticate to an Azure service.
123+
:raises: TypeError
124+
"""
125+
def __init__(self, name, key):
126+
# type: (str, str) -> None
127+
if not isinstance(name, six.string_types) or not isinstance(key, six.string_types):
128+
raise TypeError("Both name and key must be strings.")
129+
self._credential = AzureNamedKey(name, key)
130+
131+
@property
132+
def named_key(self):
133+
# type () -> AzureNamedKey
134+
"""The value of the configured name.
135+
136+
:rtype: AzureNamedKey
137+
"""
138+
return self._credential
139+
140+
def update(self, name, key):
141+
# type: (str, str) -> None
142+
"""Update the named key credential.
143+
144+
Both name and key must be provided in order to update the named key credential.
145+
Individual attributes cannot be updated.
146+
147+
:param str name: The name of the credential used to authenticate to an Azure service.
148+
:param str key: The key used to authenticate to an Azure service.
149+
"""
150+
if not isinstance(name, six.string_types) or not isinstance(key, six.string_types):
151+
raise TypeError("Both name and key must be strings.")
152+
self._credential = AzureNamedKey(name, key)

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
import time
77

88
import azure.core
9-
from azure.core.credentials import AccessToken, AzureKeyCredential, AzureSasCredential
9+
from azure.core.credentials import AccessToken, AzureKeyCredential, AzureSasCredential, AzureNamedKeyCredential
1010
from azure.core.exceptions import ServiceRequestError
1111
from azure.core.pipeline import Pipeline
12-
from azure.core.pipeline.policies import BearerTokenCredentialPolicy, SansIOHTTPPolicy, AzureKeyCredentialPolicy, AzureSasCredentialPolicy
12+
from azure.core.pipeline.policies import (
13+
BearerTokenCredentialPolicy, SansIOHTTPPolicy, AzureKeyCredentialPolicy,
14+
AzureSasCredentialPolicy
15+
)
1316
from azure.core.pipeline.transport import HttpRequest
1417

1518
import pytest
@@ -230,3 +233,26 @@ def test_azure_sas_credential_policy_raises():
230233
sas = 1234
231234
with pytest.raises(TypeError):
232235
credential = AzureSasCredential(sas)
236+
237+
def test_azure_named_key_credential():
238+
cred = AzureNamedKeyCredential("sample_name", "samplekey")
239+
240+
assert cred.named_key.name == "sample_name"
241+
assert cred.named_key.key == "samplekey"
242+
assert isinstance(cred.named_key, tuple)
243+
244+
cred.update("newname", "newkey")
245+
assert cred.named_key.name == "newname"
246+
assert cred.named_key.key == "newkey"
247+
assert isinstance(cred.named_key, tuple)
248+
249+
def test_azure_named_key_credential_raises():
250+
with pytest.raises(TypeError, match="Both name and key must be strings."):
251+
cred = AzureNamedKeyCredential("sample_name", 123345)
252+
253+
cred = AzureNamedKeyCredential("sample_name", "samplekey")
254+
assert cred.named_key.name == "sample_name"
255+
assert cred.named_key.key == "samplekey"
256+
257+
with pytest.raises(TypeError, match="Both name and key must be strings."):
258+
cred.update(1234, "newkey")

0 commit comments

Comments
 (0)