-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
176 lines (144 loc) · 5.01 KB
/
client.py
File metadata and controls
176 lines (144 loc) · 5.01 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
"""Ready to work kadmin client."""
import httpx
import ldap_protocol.kerberos.exceptions as krb_exc
from .base import AbstractKadmin
from .utils import logger_wraps
class KerberosMDAPIClient(AbstractKadmin):
"""KRB server integration."""
@logger_wraps(is_stub=True)
async def setup(*_, **__) -> None: # type: ignore
"""Stub method, setup is not needed."""
@logger_wraps()
async def add_principal(
self,
name: str,
password: str | None,
algorithms: list[str] | None = None,
timeout: int | float = 1,
) -> None:
"""Add request."""
response = await self.client.post(
"principal",
json={
"name": name,
"password": password,
"algorithms": algorithms,
},
timeout=timeout,
)
if response.status_code != 201:
raise krb_exc.KRBAPIAddPrincipalError(response.text)
@logger_wraps()
async def get_principal(self, name: str) -> dict:
"""Get request."""
response = await self.client.get("principal", params={"name": name})
if response.status_code == 404:
raise krb_exc.KRBAPIPrincipalNotFoundError
if response.status_code != 200:
raise krb_exc.KRBAPIGetPrincipalError(response.text)
return response.json()
@logger_wraps()
async def del_principal(self, name: str) -> None:
"""Delete principal."""
response = await self.client.delete("principal", params={"name": name})
if response.status_code == 404:
raise krb_exc.KRBAPIPrincipalNotFoundError
if response.status_code != 200:
raise krb_exc.KRBAPIDeletePrincipalError(response.text)
@logger_wraps()
async def change_principal_password(
self,
name: str,
password: str,
) -> None:
"""Change password request."""
response = await self.client.patch(
"principal",
json={"name": name, "password": password},
)
if response.status_code != 201:
raise krb_exc.KRBAPIChangePasswordError(response.text)
@logger_wraps()
async def create_or_update_principal_pw(
self,
name: str,
password: str,
) -> None:
"""Change password request."""
response = await self.client.post(
"/principal/create_or_update",
json={"name": name, "password": password},
)
if response.status_code == 404:
raise krb_exc.KRBAPIPrincipalNotFoundError
if response.status_code != 201:
raise krb_exc.KRBAPIChangePasswordError(response.text)
@logger_wraps()
async def rename_princ(
self,
name: str,
new_name: str,
algorithms: list[str] | None,
password: str | None,
) -> None:
"""Rename request."""
response = await self.client.put(
"principal",
json={
"name": name,
"new_name": new_name,
"algorithms": algorithms,
"password": password,
},
)
if response.status_code != 202:
raise krb_exc.KRBAPIRenamePrincipalError(response.text)
@logger_wraps()
async def ktadd(
self,
names: list[str],
is_rand_key: bool,
) -> httpx.Response:
"""Ktadd build request for stream and return response.
:param list[str] names: principals
:return httpx.Response: stream
"""
request = self.client.build_request(
"POST",
"/principal/ktadd",
json={"names": names, "is_rand_key": is_rand_key},
)
response = await self.client.send(request, stream=True)
if response.status_code == 404:
raise krb_exc.KRBAPIPrincipalNotFoundError
return response
@logger_wraps()
async def lock_principal(self, name: str) -> None:
"""Lock princ.
:param str name: upn
:raises KRBAPIPrincipalNotFoundError: on error
:raises KRBAPILockPrincipalError: on error
"""
response = await self.client.post(
"principal/lock",
json={"name": name},
)
if response.status_code == 404:
raise krb_exc.KRBAPIPrincipalNotFoundError
if response.status_code != 200:
raise krb_exc.KRBAPILockPrincipalError(response.text)
@logger_wraps()
async def force_princ_pw_change(self, name: str) -> None:
"""Force mark password change for principal.
:param str name: pw
:raises KRBAPIPrincipalNotFoundError: err
:raises KRBAPIForcePasswordChangeError: err
"""
response = await self.client.post(
"principal/force_reset",
json={"name": name},
)
if response.status_code == 404:
raise krb_exc.KRBAPIPrincipalNotFoundError
if response.status_code != 200:
raise krb_exc.KRBAPIForcePasswordChangeError(response.text)