-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.py
More file actions
252 lines (220 loc) · 6.66 KB
/
base.py
File metadata and controls
252 lines (220 loc) · 6.66 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
"""Abstract Kadmin class for kerberos api server."""
from abc import ABC, abstractmethod
import backoff
import httpx
from .exceptions import (
KRBAPISetupConfigsError,
KRBAPISetupStashError,
KRBAPISetupTreeError,
KRBAPIStatusNotFoundError,
)
from .utils import log, logger_wraps
class AbstractKadmin(ABC):
"""Stub client for non set up dirs."""
client: httpx.AsyncClient
def __init__(self, client: httpx.AsyncClient) -> None:
"""Set client.
:param httpx.AsyncClient client: httpx
"""
self.client = client
@logger_wraps()
async def setup_configs(
self,
krb5_config: str,
kdc_config: str,
) -> None:
"""Request Setup."""
log.info("Setting up configs")
response = await self.client.post(
"/setup/configs",
json={
"krb5_config": krb5_config.encode().hex(),
"kdc_config": kdc_config.encode().hex(),
},
)
if response.status_code != 201:
raise KRBAPISetupConfigsError(response.text)
@logger_wraps()
async def setup_stash(
self,
domain: str,
admin_dn: str,
services_dn: str,
krbadmin_dn: str,
krbadmin_password: str,
admin_password: str,
stash_password: str,
) -> None:
"""Set up stash."""
log.info("Setting up stash")
response = await self.client.post(
"/setup/stash",
json={
"domain": domain,
"admin_dn": admin_dn,
"services_dn": services_dn,
"krbadmin_dn": krbadmin_dn,
"krbadmin_password": krbadmin_password,
"admin_password": admin_password,
"stash_password": stash_password,
},
)
if response.status_code != 201:
raise KRBAPISetupStashError(response.text)
@logger_wraps()
async def setup_subtree(
self,
domain: str,
admin_dn: str,
services_dn: str,
krbadmin_dn: str,
krbadmin_password: str,
admin_password: str,
stash_password: str,
) -> None:
"""Set up subtree."""
log.info("Setting up subtree")
response = await self.client.post(
"/setup/subtree",
json={
"domain": domain,
"admin_dn": admin_dn,
"services_dn": services_dn,
"krbadmin_dn": krbadmin_dn,
"krbadmin_password": krbadmin_password,
"admin_password": admin_password,
"stash_password": stash_password,
},
)
if response.status_code != 201:
raise KRBAPISetupTreeError(response.text)
@logger_wraps()
async def reset_setup(self) -> None:
"""Reset setup."""
log.warning("Setup reset")
await self.client.post("/setup/reset")
async def setup(
self,
domain: str,
admin_dn: str,
services_dn: str,
krbadmin_dn: str,
krbadmin_password: str,
admin_password: str,
stash_password: str,
krb5_config: str,
kdc_config: str,
ldap_keytab_path: str,
) -> None:
"""Request Setup."""
await self.setup_configs(krb5_config, kdc_config)
await self.setup_stash(
domain,
admin_dn,
services_dn,
krbadmin_dn,
krbadmin_password,
admin_password,
stash_password,
)
await self.setup_subtree(
domain,
admin_dn,
services_dn,
krbadmin_dn,
krbadmin_password,
admin_password,
stash_password,
)
status = await self.get_status(wait_for_positive=True)
if status:
await self.ldap_principal_setup(
f"ldap/{domain}",
ldap_keytab_path,
)
@abstractmethod
async def add_principal(
self,
principal_name: str,
password: str | None = None,
algorithms: list[str] | None = None,
timeout: int | float = 1,
) -> None: ...
@abstractmethod
async def get_principal(self, name: str) -> dict: ...
@abstractmethod
async def del_principal(self, name: str) -> None: ...
@abstractmethod
async def change_principal_password(
self,
name: str,
password: str,
) -> None: ...
@abstractmethod
async def create_or_update_principal_pw(
self,
name: str,
password: str,
) -> None: ...
@abstractmethod
async def rename_princ(
self,
name: str,
new_name: str,
algorithms: list[str] | None = None,
password: str | None = None,
) -> None: ...
@backoff.on_exception(
backoff.constant,
(
httpx.ConnectError,
httpx.ConnectTimeout,
httpx.RemoteProtocolError,
KRBAPIStatusNotFoundError,
),
jitter=None,
raise_on_giveup=False,
max_tries=30,
)
async def get_status(self, wait_for_positive: bool = False) -> bool:
"""Get status of setup."""
response = await self.client.get("/setup/status")
status = response.json()
if wait_for_positive and not status:
raise KRBAPIStatusNotFoundError
return status
@abstractmethod
async def ktadd(
self,
names: list[str],
is_rand_key: bool,
) -> httpx.Response: ...
@abstractmethod
async def lock_principal(self, name: str) -> None: ...
@abstractmethod
async def force_princ_pw_change(self, name: str) -> None: ...
@logger_wraps()
async def ldap_principal_setup(self, name: str, path: str) -> None:
"""LDAP principal setup.
:param str ldap_principal_name: ldap principal name
:param str ldap_keytab_path: ldap keytab path
"""
response = await self.client.get("/principal", params={"name": name})
if response.status_code == 200:
return
response = await self.client.post(
"/principal",
json={"principal_name": name},
)
if response.status_code != 201:
log.error(f"Error creating ldap principal: {response.text}")
return
response = await self.client.post(
"/principal/ktadd",
json={"names": [name], "is_rand_key": True},
)
if response.status_code != 200:
log.error(f"Error getting keytab: {response.text}")
return
with open(path, "wb") as f:
f.write(response.read())