generated from cyberark/conjur-template
-
Notifications
You must be signed in to change notification settings - Fork 4
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
17 changed files
with
243 additions
and
31 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
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,21 @@ | ||
""" | ||
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 | ||
|
||
def __str__(self): | ||
""" | ||
Return string representation of AuthnTypes. | ||
""" | ||
return self.name.lower() |
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,53 @@ | ||
""" | ||
AuthnAuthenticationStrategy module | ||
This module holds the AuthnAuthenticationStrategy class | ||
""" | ||
|
||
import logging | ||
from conjur_api.errors.errors import MissingRequiredParameterException | ||
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. | ||
""" | ||
|
||
if self._login_id is None: | ||
raise MissingRequiredParameterException("login_id is required") | ||
|
||
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.
Oops, something went wrong.