Skip to content

Commit

Permalink
Add AzureNamedKeyCredential (#17548)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
rakshith91 and swathipil authored Apr 25, 2021
1 parent 0f81bed commit 844e16d
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 8 deletions.
5 changes: 4 additions & 1 deletion sdk/core/azure-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Release History

## 1.13.1 (Unreleased)
## 1.14.0 (Unreleased)

### New Features

- Added `azure.core.credentials.AzureNamedKeyCredential` credential #17548.

## 1.13.0 (2021-04-02)

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.13.1"
VERSION = "1.14.0"
47 changes: 43 additions & 4 deletions sdk/core/azure-core/azure/core/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
# Licensed under the MIT License. See LICENSE.txt in the project root for
# license information.
# -------------------------------------------------------------------------
from collections import namedtuple
from typing import TYPE_CHECKING
import six


if TYPE_CHECKING:
from typing import Any, NamedTuple
from typing_extensions import Protocol
Expand All @@ -26,11 +26,12 @@ def get_token(self, *scopes, **kwargs):


else:
from collections import namedtuple

AccessToken = namedtuple("AccessToken", ["token", "expires_on"])

__all__ = ["AzureKeyCredential", "AzureSasCredential", "AccessToken"]
AzureNamedKey = namedtuple("AzureNamedKey", ["name", "key"])


__all__ = ["AzureKeyCredential", "AzureSasCredential", "AccessToken", "AzureNamedKeyCredential"]


class AzureKeyCredential(object):
Expand Down Expand Up @@ -111,3 +112,41 @@ 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._credential = AzureNamedKey(name, key)

@property
def named_key(self):
# type () -> AzureNamedKey
"""The value of the configured name.
:rtype: AzureNamedKey
"""
return self._credential

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.
: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.")
self._credential = AzureNamedKey(name, key)
30 changes: 28 additions & 2 deletions sdk/core/azure-core/tests/test_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
from azure.core.pipeline.transport import HttpRequest

import pytest
Expand Down Expand Up @@ -230,3 +233,26 @@ 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.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."):
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."):
cred.update(1234, "newkey")

0 comments on commit 844e16d

Please sign in to comment.