forked from cyberark/ark-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathark_service.py
More file actions
66 lines (52 loc) · 2.26 KB
/
Copy pathark_service.py
File metadata and controls
66 lines (52 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from abc import ABC, abstractmethod
from typing import Any, List
from ark_sdk_python.auth.ark_auth import ArkAuth
from ark_sdk_python.common import get_logger
from ark_sdk_python.models import ArkNotFoundException, ArkValidationException
from ark_sdk_python.models.services import ArkServiceConfig
class ArkService(ABC):
def __init__(self, *authenticators: Any) -> None:
self._logger = get_logger(self.__class__.__name__)
self._authenticators = [auth for auth in authenticators if issubclass(type(auth), ArkAuth)]
given_auth_names = [auth.authenticator_name() for auth in self._authenticators]
if any(a not in given_auth_names for a in self.service_config().required_authenticator_names):
raise ArkValidationException(f'{self.service_config().service_name} missing required authenticators for service')
@property
def authenticators(self) -> List[ArkAuth]:
"""
Returns all the authenticators for the service.
Returns:
List[ArkAuth]: _description_
"""
return self._authenticators
def authenticator(self, auth_name: str) -> ArkAuth:
"""
Finds the appropriate Ark authenticator class for the specified authenticator.
Args:
auth_name (str): _description_
Raises:
ArkNotFoundException: _description_
Returns:
ArkAuth: _description_
"""
for auth in self.authenticators:
if auth.authenticator_name() == auth_name:
return auth
raise ArkNotFoundException(f'{self.service_config().service_name} Failed to find authenticator {auth_name}')
def has_authenticator(self, auth_name: str) -> bool:
"""
Checks whether the specified authenticator name exists.
Args:
auth_name (str): _description_
Returns:
bool: _description_
"""
return any(auth.authenticator_name() == auth_name for auth in self.authenticators)
@staticmethod
@abstractmethod
def service_config() -> ArkServiceConfig:
"""
Returns the service configuration, which includes the service name, and its required and optional authenticators.
Returns:
ArkServiceConfig: _description_
"""