-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssm.py
More file actions
251 lines (207 loc) 路 8.67 KB
/
Copy pathssm.py
File metadata and controls
251 lines (207 loc) 路 8.67 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
import ctypes
import ctypes.util
from enum import IntEnum
from pathlib import Path
from typing import Callable, Iterator, List, Optional, Tuple, Union
class SSMStatus(IntEnum):
OK = 0
ERR_AUTH = 1
ERR_NOT_FOUND = 2
ERR_EXPIRED = 3
ERR_INTEGRITY = 4
ERR_INTERNAL = 5
class SSMError(Exception):
def __init__(self, status: int, operation: str = ""):
self.status = SSMStatus(status)
self.operation = operation
msg = ssm_status_to_string(self.status.value)
prefix = f"[{operation}] " if operation else ""
super().__init__(f"{prefix}{msg} (code={status})")
def _load_so(path: Optional[Union[str, Path]] = None) -> ctypes.CDLL:
if path is None:
candidates = [
Path("./build/src/libssm.so"),
Path("./build/libssm.so"),
Path("./libssm.so"),
Path("/usr/local/lib/libssm.so"),
]
for c in candidates:
if c.exists():
path = str(c.resolve())
break
if path is None:
found = ctypes.util.find_library("ssm")
if found:
path = found
else:
raise RuntimeError(
"libssm.so not found. Pass path explicitly or build with: cmake -B build && cmake --build build"
)
lib = ctypes.CDLL(str(path))
lib.ssm_init.argtypes = [
ctypes.POINTER(ctypes.c_void_p),
ctypes.c_char_p,
ctypes.POINTER(ctypes.c_ubyte),
ctypes.c_size_t,
]
lib.ssm_init.restype = ctypes.c_int
lib.ssm_destroy.argtypes = [ctypes.c_void_p]
lib.ssm_destroy.restype = ctypes.c_int
lib.ssm_user_register.argtypes = [
ctypes.c_void_p, ctypes.c_char_p, ctypes.c_char_p,
]
lib.ssm_user_register.restype = ctypes.c_int
lib.ssm_user_authenticate.argtypes = [
ctypes.c_void_p, ctypes.c_char_p, ctypes.c_char_p,
ctypes.POINTER(ctypes.c_int),
]
lib.ssm_user_authenticate.restype = ctypes.c_int
lib.ssm_user_delete.argtypes = [
ctypes.c_void_p, ctypes.c_char_p, ctypes.c_char_p,
]
lib.ssm_user_delete.restype = ctypes.c_int
lib.ssm_user_change_password.argtypes = [
ctypes.c_void_p, ctypes.c_char_p, ctypes.c_char_p, ctypes.c_char_p,
]
lib.ssm_user_change_password.restype = ctypes.c_int
lib.ssm_secret_store.argtypes = [
ctypes.c_void_p, ctypes.c_char_p,
ctypes.POINTER(ctypes.c_ubyte), ctypes.c_size_t,
ctypes.POINTER(ctypes.c_ubyte), ctypes.c_size_t,
ctypes.c_char_p, ctypes.c_char_p,
]
lib.ssm_secret_store.restype = ctypes.c_int
lib.ssm_secret_get.argtypes = [
ctypes.c_void_p, ctypes.c_char_p, ctypes.c_char_p,
ctypes.POINTER(ctypes.c_ubyte), ctypes.POINTER(ctypes.c_size_t),
ctypes.POINTER(ctypes.c_ubyte), ctypes.POINTER(ctypes.c_size_t),
]
lib.ssm_secret_get.restype = ctypes.c_int
lib.ssm_secret_delete.argtypes = [
ctypes.c_void_p, ctypes.c_char_p, ctypes.c_char_p,
]
lib.ssm_secret_delete.restype = ctypes.c_int
lib.ssm_secret_list.argtypes = [
ctypes.c_void_p, ctypes.c_char_p,
ctypes.CFUNCTYPE(None, ctypes.c_char_p, ctypes.c_char_p,
ctypes.c_char_p, ctypes.c_size_t, ctypes.c_void_p),
ctypes.c_void_p,
]
lib.ssm_secret_list.restype = ctypes.c_int
lib.ssm_kek_rotate.argtypes = [ctypes.c_void_p, ctypes.c_char_p]
lib.ssm_kek_rotate.restype = ctypes.c_int
lib.ssm_status_to_string.argtypes = [ctypes.c_int]
lib.ssm_status_to_string.restype = ctypes.c_char_p
return lib
def ssm_status_to_string(status: int) -> str:
return _lib.ssm_status_to_string(status).decode()
_lib = _load_so()
_SecretListCb = ctypes.CFUNCTYPE(
None, ctypes.c_char_p, ctypes.c_char_p, ctypes.c_char_p,
ctypes.c_size_t, ctypes.c_void_p,
)
def _check(status: int, operation: str = "") -> None:
if status != 0:
raise SSMError(status, operation)
class SSMHandle:
def __init__(self, db_path: str = ":memory:",
db_key: Optional[bytes] = None,
lib_path: Optional[Union[str, Path]] = None):
self._path = db_path
self._key = db_key
self._handle: Optional[ctypes.c_void_p] = None
if lib_path is not None:
global _lib
_lib = _load_so(lib_path)
def __enter__(self) -> "SSMHandle":
h = ctypes.c_void_p()
key_ptr = None
key_len = 0
if self._key:
key_ptr = (ctypes.c_ubyte * len(self._key)).from_buffer_copy(self._key)
key_len = len(self._key)
_check(_lib.ssm_init(ctypes.byref(h), self._path.encode(),
key_ptr, key_len), "ssm_init")
self._handle = h
return self
def __exit__(self, *args) -> None:
if self._handle is not None:
_lib.ssm_destroy(self._handle)
self._handle = None
@property
def _h(self) -> ctypes.c_void_p:
if self._handle is None:
raise RuntimeError("SSMHandle not opened (use 'with' statement)")
return self._handle
def user_register(self, username: str, password: str) -> None:
_check(_lib.ssm_user_register(self._h, username.encode(),
password.encode()), "user_register")
def user_authenticate(self, username: str, password: str) -> bool:
out = ctypes.c_int()
_check(_lib.ssm_user_authenticate(self._h, username.encode(),
password.encode(),
ctypes.byref(out)), "user_authenticate")
return bool(out.value)
def user_delete(self, username: str, password: str) -> None:
_check(_lib.ssm_user_delete(self._h, username.encode(),
password.encode()), "user_delete")
def user_change_password(self, username: str, old: str, new: str) -> None:
_check(_lib.ssm_user_change_password(self._h, username.encode(),
old.encode(), new.encode()),
"user_change_password")
def secret_store(self, username: str, private_key: bytes,
public_key: Optional[bytes] = None,
name: str = "", description: Optional[str] = None) -> None:
priv_arr = (ctypes.c_ubyte * len(private_key)).from_buffer_copy(private_key)
pub_ptr = None
pub_len = 0
if public_key:
pub_arr = (ctypes.c_ubyte * len(public_key)).from_buffer_copy(public_key)
pub_ptr = pub_arr
pub_len = len(public_key)
desc_ptr = description.encode() if description else None
_check(_lib.ssm_secret_store(
self._h, username.encode(),
priv_arr, len(private_key),
pub_ptr, pub_len,
name.encode(), desc_ptr,
), "secret_store")
def secret_get(self, username: str, name: str,
max_size: int = 65536) -> Tuple[bytes, Optional[bytes]]:
buf = (ctypes.c_ubyte * max_size)()
buf_len = ctypes.c_size_t(max_size)
pub_buf = (ctypes.c_ubyte * max_size)()
pub_len = ctypes.c_size_t(max_size)
st = _lib.ssm_secret_get(
self._h, username.encode(), name.encode(),
buf, ctypes.byref(buf_len),
pub_buf, ctypes.byref(pub_len),
)
if st == SSMStatus.ERR_INTERNAL and buf_len.value > max_size:
return self.secret_get(username, name, max_size=buf_len.value)
_check(st, "secret_get")
priv = bytes(buf[: buf_len.value])
pub = bytes(pub_buf[: pub_len.value]) if pub_len.value > 0 else None
return priv, pub
def secret_delete(self, username: str, name: str) -> None:
_check(_lib.ssm_secret_delete(self._h, username.encode(),
name.encode()), "secret_delete")
def secret_list(self, username: str) -> Iterator[Tuple[str, Optional[str], str, int]]:
results: List[Tuple[str, Optional[str], str, int]] = []
def cb(name: bytes, desc: Optional[bytes], updated: bytes,
pub_len: int, _: int) -> None:
results.append((
name.decode(),
desc.decode() if desc else None,
updated.decode(),
pub_len,
))
cb_ptr = _SecretListCb(cb)
_check(_lib.ssm_secret_list(self._h, username.encode(),
cb_ptr, None), "secret_list")
yield from results
def kek_rotate(self, username: str) -> None:
_check(_lib.ssm_kek_rotate(self._h, username.encode()), "kek_rotate")
@property
def raw_handle(self) -> Optional[ctypes.c_void_p]:
return self._handle