forked from cyberark/ark-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathark_api.py
More file actions
273 lines (210 loc) · 9.65 KB
/
Copy pathark_api.py
File metadata and controls
273 lines (210 loc) · 9.65 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
from __future__ import annotations
from typing import Dict, List, Optional, Type, cast
from ark_sdk_python.auth.ark_auth import ArkAuth
from ark_sdk_python.models.ark_exceptions import ArkServiceException
from ark_sdk_python.models.ark_profile import ArkProfile, ArkProfileLoader
from ark_sdk_python.services.ark_service import ArkService
class ArkAPI:
def __init__(self, authenticators: List[ArkAuth], profile: Optional[ArkProfile] = None) -> None:
self.__authenticators = authenticators
self.__lazy_loaded_services: Dict[str, ArkService] = {}
self.__profile = profile or ArkProfileLoader.load_default_profile()
def __lazy_load_service(self, service_type: Type[ArkService]) -> ArkService:
service_name = service_type.service_config().service_name
required_auth_names = service_type.service_config().required_authenticator_names
required_autheneticators = [auth for auth in self.__authenticators if auth.authenticator_name() in required_auth_names]
optional_authenticators = [
auth
for auth in self.__authenticators
if auth.authenticator_name() in service_type.service_config().optional_authenticator_names
]
if len(required_autheneticators) == len(required_auth_names):
authenticators = {f'{a.authenticator_name()}_auth': a for a in required_autheneticators + optional_authenticators}
self.__lazy_loaded_services[service_name] = service_type(**authenticators)
else:
raise ArkServiceException(
f'{service_name.title()} is not supported or missing fitting authenticators, please make sure you are logged in'
)
return self.__lazy_loaded_services[service_name]
def authenticator(self, authenticator_name: str) -> ArkAuth:
"""
Returns the specified authenticator.
Args:
authenticator_name (str): _description_
Raises:
ArkServiceException: _description_
Returns:
ArkAuth: _description_
"""
for auth in self.__authenticators:
if auth.authenticator_name() == authenticator_name:
return auth
raise ArkServiceException(f'{authenticator_name} is not supported or not found')
def service(self, service_type: Type[ArkService]) -> ArkService:
"""
Returns the specified service when the appropriate authenticators were provided.
Args:
service_type (Type[ArkService]): _description_
Raises:
ArkServiceException: _description_
Returns:
ArkService: _description_
"""
if not issubclass(service_type, ArkService):
raise ArkServiceException(f"Type {service_type} is invalid")
service_name = service_type.service_config().service_name
if service_name in self.__lazy_loaded_services:
return self.__lazy_loaded_services[service_name]
return self.__lazy_load_service(service_type)
@property
def profile(self) -> ArkProfile:
"""
Gets the API profile
Returns:
ArkProfile: _description_
"""
return self.__profile
@property
def identity_directories(self) -> "ArkIdentityDirectoriesService":
"""
Returns the Identity Directories Service if the appropriate authenticators were given
Returns:
ArkIdentityDirectoriesService: _description_
"""
from ark_sdk_python.services.identity.directories import ArkIdentityDirectoriesService
return cast(ArkIdentityDirectoriesService, self.service(ArkIdentityDirectoriesService))
@property
def identity_policies(self) -> "ArkIdentityPoliciesService":
"""
Returns the Identity Policies Service if the appropriate authenticators were given
Returns:
ArkIdentityPoliciesService: _description_
"""
from ark_sdk_python.services.identity.policies import ArkIdentityPoliciesService
return cast(ArkIdentityPoliciesService, self.service(ArkIdentityPoliciesService))
@property
def identity_roles(self) -> "ArkIdentityRolesService":
"""
Returns the Identity Roles Service if the appropriate authenticators were given
Returns:
ArkIdentityRolesService: _description_
"""
from ark_sdk_python.services.identity.roles import ArkIdentityRolesService
return cast(ArkIdentityRolesService, self.service(ArkIdentityRolesService))
@property
def identity_users(self) -> "ArkIdentityUsersService":
"""
Returns the Identity Users Service if the appropriate authenticators were given
Returns:
ArkIdentityUsersService: _description_
"""
from ark_sdk_python.services.identity.users import ArkIdentityUsersService
return cast(ArkIdentityUsersService, self.service(ArkIdentityUsersService))
@property
def dpa_workspaces_db(self) -> "ArkDPADBWorkspaceService":
"""
Returns the DPA DB Workspace service if the appropriate authenticators were provided.
Returns:
ArkDPADBWorkspaceService: _description_
"""
from ark_sdk_python.services.dpa.workspaces.db import ArkDPADBWorkspaceService
return cast(ArkDPADBWorkspaceService, self.service(ArkDPADBWorkspaceService))
@property
def dpa_policies_vm(self) -> "ArkDPAVMPoliciesService":
"""
Returns the DPA VM Policies service if the appropriate authenticators were provided.
Returns:
ArkDPAVMPoliciesService: _description_
"""
from ark_sdk_python.services.dpa.policies.vm import ArkDPAVMPoliciesService
return cast(ArkDPAVMPoliciesService, self.service(ArkDPAVMPoliciesService))
@property
def dpa_policies_db(self) -> "ArkDPADBPoliciesService":
"""
Returns the DPA DB Policies service if the appropriate authenticators were provided.
Returns:
ArkDPADBPoliciesService: _description_
"""
from ark_sdk_python.services.dpa.policies.db import ArkDPADBPoliciesService
return cast(ArkDPADBPoliciesService, self.service(ArkDPADBPoliciesService))
@property
def dpa_secrets_db(self) -> "ArkDPADBSecretsService":
"""
Returns the DPA DB Secrets service if the appropriate authenticators were provided.
Returns:
ArkDPADBSecretsService: _description_
"""
from ark_sdk_python.services.dpa.secrets.db import ArkDPADBSecretsService
return cast(ArkDPADBSecretsService, self.service(ArkDPADBSecretsService))
@property
def dpa_sso(self) -> "ArkDPASSOService":
"""
Returns the DPA sso service if the appropriate authenticators were provided.
Returns:
ArkDPASSOService: _description_
"""
from ark_sdk_python.services.dpa.sso import ArkDPASSOService
return cast(ArkDPASSOService, self.service(ArkDPASSOService))
@property
def dpa_db(self) -> "ArkDPADBService":
"""
Returns the DPA DB service if the appropriate authenticators were provided.
Returns:
ArkDPADBService: _description_
"""
from ark_sdk_python.services.dpa.db import ArkDPADBService
return cast(ArkDPADBService, self.service(ArkDPADBService))
@property
def dpa_certificates(self) -> "ArkDPACertificatesService":
"""
Returns DPA certificates service if the appropriate authenticators were provided.
Returns:
ArkDPACertificatesService: _description_
"""
from ark_sdk_python.services.dpa.certificates import ArkDPACertificatesService
return cast(ArkDPACertificatesService, self.service(ArkDPACertificatesService))
@property
def dpa_k8s(self) -> "ArkDPAK8SService":
"""
Returns the DPA Policies service if the appropriate authenticators were provided.
Returns:
ArkDPAK8SService: _description_
"""
from ark_sdk_python.services.dpa.k8s import ArkDPAK8SService
return cast(ArkDPAK8SService, self.service(ArkDPAK8SService))
@property
def sm(self) -> "ArkSMService":
"""
Returns the SM service if the appropriate authenticators were given
Returns:
ArkSMService: _description_
"""
from ark_sdk_python.services.sm import ArkSMService
return cast(ArkSMService, self.service(ArkSMService))
@property
def pcloud_accounts(self) -> "ArkPCloudAccountsService":
"""
Returns the PCloud Accounts service if the appropriate authenticators were given
Returns:
ArkPCloudAccountsService: _description_
"""
from ark_sdk_python.services.pcloud.accounts import ArkPCloudAccountsService
return cast(ArkPCloudAccountsService, self.service(ArkPCloudAccountsService))
@property
def pcloud_safes(self) -> "ArkPCloudSafesService":
"""
Returns the PCloud Safes service if the appropriate authenticators were given
Returns:
ArkPCloudSafesService: _description_
"""
from ark_sdk_python.services.pcloud.safes import ArkPCloudSafesService
return cast(ArkPCloudSafesService, self.service(ArkPCloudSafesService))
@property
def pcloud_platforms(self) -> "ArkPCloudPlatformsService":
"""
Returns the PCloud Platforms service if the appropriate authenticators were given
Returns:
ArkPCloudPlatformsService: _description_
"""
from ark_sdk_python.services.pcloud.platforms import ArkPCloudPlatformsService
return cast(ArkPCloudPlatformsService, self.service(ArkPCloudPlatformsService))