generated from cyberark/conjur-template
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Abstract authentication flow into new interface
- Loading branch information
Showing
16 changed files
with
216 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
AuthenticationStrategy Interface | ||
This class describes a shared interface for authenticating to Conjur | ||
""" | ||
|
||
# Builtins | ||
import abc | ||
|
||
from conjur_api.models.ssl.ssl_verification_metadata import SslVerificationMetadata | ||
|
||
# pylint: disable=too-few-public-methods | ||
class AuthenticationStrategyInterface(metaclass=abc.ABCMeta): # pragma: no cover | ||
""" | ||
AuthenticationStrategyInterface | ||
This class is an interface that outlines a shared interface for authentication strategies | ||
""" | ||
|
||
@abc.abstractmethod | ||
async def authenticate(self, api_key: str, ssl_verification_data: SslVerificationMetadata) -> str: | ||
""" | ||
Authenticate uses the api_key to fetch a short-lived conjur_api token that | ||
for a limited time will allow you to interact fully with the Conjur | ||
vault. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
""" | ||
AuthnTypes module | ||
This module is used to represent different authentication methods. | ||
""" | ||
|
||
from enum import Enum | ||
|
||
|
||
class AuthnTypes(Enum): # pragma: no cover | ||
""" | ||
Represent possible authn methods that can be used. | ||
""" | ||
AUTHN = 0 | ||
# Future: | ||
# LDAP = 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
AuthenticationStrategyFactory module | ||
This module job is to encapsulate the creation of SSLContext in the dependent of each os environment | ||
""" | ||
|
||
from conjur_api.errors.errors import UnknownAuthnTypeError | ||
from conjur_api.interface.authentication_strategy_interface import AuthenticationStrategyInterface | ||
from conjur_api.models.enums.authn_types import AuthnTypes | ||
from conjur_api.providers.authn_authentication_strategy import AuthnAuthenticationStrategy | ||
|
||
# pylint: disable=too-few-public-methods | ||
def create_authentication_strategy( | ||
authn_type: AuthnTypes, | ||
url: str, | ||
account: str, | ||
username: str = None | ||
) -> AuthenticationStrategyInterface: | ||
""" | ||
Factory method to create AuthenticationStrategyInterface | ||
@return: AuthenticationStrategyInterface | ||
""" | ||
|
||
if authn_type == AuthnTypes.AUTHN: | ||
return AuthnAuthenticationStrategy(url, account, username) | ||
|
||
raise UnknownAuthnTypeError(f"Unknown authentication type '{authn_type}'") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
""" | ||
AuthnAuthenticationStrategy module | ||
This module holds the AuthnAuthenticationStrategy class | ||
""" | ||
|
||
import logging | ||
from conjur_api.http.endpoints import ConjurEndpoint | ||
from conjur_api.interface.authentication_strategy_interface import AuthenticationStrategyInterface | ||
from conjur_api.wrappers.http_wrapper import HttpVerb, invoke_endpoint | ||
|
||
# pylint: disable=too-few-public-methods | ||
class AuthnAuthenticationStrategy(AuthenticationStrategyInterface): | ||
""" | ||
AuthnAuthenticationStrategy class | ||
This class implement the "authn" strategy of authentication | ||
""" | ||
def __init__( | ||
self, | ||
url: str, | ||
account: str, | ||
login_id: str | ||
): | ||
self._url = url | ||
self._account = account | ||
self._login_id = login_id | ||
|
||
async def authenticate(self, api_key: str, ssl_verification_data) -> str: | ||
""" | ||
Authenticate uses the api_key to fetch a short-lived conjur_api token that | ||
for a limited time will allow you to interact fully with the Conjur | ||
vault. | ||
""" | ||
|
||
# TODO: What if login_id is Nothing | ||
|
||
params = { | ||
'url': self._url, | ||
'account': self._account, | ||
'login': self._login_id | ||
} | ||
|
||
logging.debug("Authenticating to %s...", self._url) | ||
response = await invoke_endpoint( | ||
HttpVerb.POST, | ||
ConjurEndpoint.AUTHENTICATE, | ||
params, | ||
api_key, | ||
ssl_verification_metadata=ssl_verification_data) | ||
return response.text |
Empty file.
27 changes: 27 additions & 0 deletions
27
tests/authentication_strategy/test_unit_authentication_strategy_factory.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from unittest import TestCase | ||
from conjur_api.errors.errors import UnknownAuthnTypeError | ||
from conjur_api.models.enums.authn_types import AuthnTypes | ||
|
||
from conjur_api.providers.authentication_strategy_factory import create_authentication_strategy | ||
from conjur_api.providers.authn_authentication_strategy import AuthnAuthenticationStrategy | ||
|
||
class AuthenticationStrategyFactoryTest(TestCase): | ||
|
||
def test_vanilla_flow(self): | ||
provider = create_authentication_strategy( | ||
AuthnTypes.AUTHN, | ||
"https://conjur.example.com", | ||
"some_account", | ||
"some_username" | ||
) | ||
self.assertIsInstance(provider, AuthnAuthenticationStrategy) | ||
|
||
def test_unknown_type(self): | ||
with self.assertRaises(UnknownAuthnTypeError) as context: | ||
create_authentication_strategy( | ||
99, | ||
"https://conjur.example.com", | ||
"some_account", | ||
"some_username" | ||
) | ||
self.assertEqual(context.exception.message, "Unknown authentication type '99'") |
9 changes: 9 additions & 0 deletions
9
tests/authentication_strategy/test_unit_authn_authentication_provider.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from unittest import TestCase | ||
|
||
from conjur_api.providers.authn_authentication_strategy import AuthnAuthenticationStrategy | ||
|
||
class AuthnAuthenticationStrategyTest(TestCase): | ||
|
||
def test_vanilla_flow(self): | ||
# TODO: What can we test here that would not really be an integration test? | ||
return |
Oops, something went wrong.