-
Notifications
You must be signed in to change notification settings - Fork 0
/
mini_kms.py
639 lines (511 loc) · 18.5 KB
/
mini_kms.py
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
"""A Mini KMS server using Askar for key management."""
from contextlib import asynccontextmanager
import json
import logging
from typing import Any, List, Mapping, Optional, Sequence, Set, Tuple, cast
from uuid import uuid4
from aries_askar import AskarError, AskarErrorCode, Entry, Key, KeyAlg, Store
import base58
from fastapi import Depends, FastAPI, Header, Request, status
from fastapi.responses import JSONResponse
from fastapi.exceptions import RequestValidationError
from pydantic import Base64UrlBytes, BaseModel, Field
from pydantic.types import Base64UrlEncoder
LOGGER = logging.getLogger("uvicorn.error." + __name__)
DEFAULT_PROFILE = "default"
PROFILE_HEADER = "X-Profile"
@asynccontextmanager
async def setup_store(app: FastAPI):
"""Setup the Askar store."""
key = Store.generate_raw_key()
store = await Store.provision(
"sqlite://:memory:", "raw", key, profile=DEFAULT_PROFILE
)
app.state.store = store
try:
yield
finally:
await store.close()
app = FastAPI(
lifespan=setup_store,
title="Mini KMS",
summary="A lightweight service for secure key management using Aries Askar",
version="0.1.1",
)
async def store():
"""Get the store from the app state."""
yield app.state.store
class ProblemDetails(BaseModel):
"""RFC 9457 Problem Details."""
type: Optional[str] = None
status: int
title: str
detail: Optional[str] = None
def __str__(self) -> str:
"""Return short string representation of problem details object."""
return f"{self.status}: {self.title}"
@classmethod
def NotFound(cls, detail: Optional[str] = None) -> "ProblemDetails":
"""Not Found problem details."""
return cls(status=404, title="Not Found", detail=detail)
@classmethod
def InternalServerError(cls, detail: Optional[str] = None) -> "ProblemDetails":
"""Internal Error problem details."""
return cls(
status=500,
title="Internal Server Error",
detail=detail,
)
@classmethod
def BadRequest(cls, detail: Optional[str] = None) -> "ProblemDetails":
"""Bad Request problem details."""
return cls(
status=400,
title="Bad Request",
detail=detail,
)
class ProblemDetailsException(Exception):
"""Exception wrapper for problem details."""
def __init__(self, details: ProblemDetails):
"""Initiliaze exception."""
self.details = details
@classmethod
def NotFound(cls, detail: Optional[str] = None) -> "ProblemDetailsException":
"""Not Found problem details."""
return cls(ProblemDetails(status=404, title="Not Found", detail=detail))
@classmethod
def InternalServerError(
cls, detail: Optional[str] = None
) -> "ProblemDetailsException":
"""Internal Error problem details."""
return cls(
ProblemDetails(
status=500,
title="Internal Server Error",
detail=detail,
)
)
@classmethod
def BadRequest(cls, detail: Optional[str] = None) -> "ProblemDetailsException":
"""Bad Request problem details."""
return cls(
ProblemDetails(
status=400,
title="Bad Request",
detail=detail,
)
)
@app.exception_handler(ProblemDetailsException)
async def problem_details_exception_handler(_: Request, exc: ProblemDetailsException):
"""Handle ProblemDetails exceptions."""
details = exc.details
return JSONResponse(
status_code=details.status,
content=details.model_dump(exclude_none=True),
headers={"Content-Type": "application/problem+json"},
)
class ValidationErrorInfo(BaseModel):
"""Validation error info."""
loc: Tuple[str, Any]
msg: str
type: str
class ValidationProblemDetails(ProblemDetails):
"""Problem details for validation errors."""
errors: List[ValidationErrorInfo]
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
"""Handle request validation errors."""
details = ValidationProblemDetails(
status=status.HTTP_422_UNPROCESSABLE_ENTITY,
title="Validation Error",
detail="Failed to validate request body",
errors=[ValidationErrorInfo.model_validate(error) for error in exc.errors()],
)
return JSONResponse(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
content=details.model_dump(exclude_none=True),
)
class ProfileReq(BaseModel):
"""Profile create request."""
name: str
class ProfileResp(BaseModel):
"""Profile create response."""
name: str
@app.post("/profile", tags=["profiles"], response_description="Profile create response")
async def create_profile(req: ProfileReq, store: Store = Depends(store)) -> ProfileResp:
"""Create a new Profile."""
try:
name = await store.create_profile(req.name)
except AskarError as error:
if error.code == AskarErrorCode.DUPLICATE:
raise ProblemDetailsException.BadRequest(
f"Profile with name '{req.name}' already exists"
)
else:
raise ProblemDetailsException.InternalServerError(
"Could not create profile"
) from error
return ProfileResp(name=name)
class ProfileList(BaseModel):
"""List of Profiles."""
profiles: List[str]
@app.get("/profiles", tags=["profiles"], response_description="Profile list")
async def get_profiles(store: Store = Depends(store)) -> ProfileList:
"""Get available profiles."""
profiles = list(await store.list_profiles())
return ProfileList(profiles=profiles)
@app.delete("/profile/{name}", tags=["profiles"], response_description="Success bool")
async def delete_profile(name: str, store: Store = Depends(store)):
"""Delete a profile."""
ok = await store.remove_profile(name)
return {"success": ok}
class GenerateKeyReq(BaseModel):
"""Generate key request."""
alg: KeyAlg
class GenerateKeyResp(BaseModel):
"""Generated key response."""
kid: str
jwk: dict
b58: str
@classmethod
def from_key(cls, kid: str, key: Key) -> "GenerateKeyResp":
"""Create a response from a key."""
b58 = base58.b58encode(key.get_public_bytes()).decode("utf-8")
jwk = json.loads(key.get_jwk_public())
return cls(kid=kid, jwk=jwk, b58=b58)
def derive_kid(key: Key) -> str:
"""Derive a kid from a key."""
return key.get_jwk_thumbprint()
@app.post("/key/generate", tags=["keys"], response_description="The generated key")
async def generate_key(
req: GenerateKeyReq,
profile: str = Header(default=DEFAULT_PROFILE, alias=PROFILE_HEADER),
store: Store = Depends(store),
) -> GenerateKeyResp:
"""Generate a key and store it."""
key = Key.generate(req.alg)
kid = derive_kid(key)
async with store.session(profile=profile) as txn:
await txn.insert_key(kid, key)
return GenerateKeyResp.from_key(kid, key)
class AssociateKeyReq(BaseModel):
"""Associate Key Request body."""
alias: str = Field(
examples=["did:example:1234#key-1"],
)
class AssociateKeyResp(BaseModel):
"""Associate Key Response body."""
alias: str
kid: str
@app.post(
"/key/{kid}/associate",
tags=["keys"],
response_description="Summary of associated identifiers",
)
async def associate_key(
kid: str,
req: AssociateKeyReq,
profile: str = Header(default=DEFAULT_PROFILE, alias=PROFILE_HEADER),
store: Store = Depends(store),
) -> AssociateKeyResp:
"""Associate a key with identifiers."""
LOGGER.debug(
"Associating key %s with: profile: %s, alias: %s",
kid,
profile,
req.alias,
)
async with store.transaction(profile=profile) as txn:
entry = await txn.fetch_key(kid, for_update=True)
if not entry:
raise ProblemDetailsException.NotFound("Key not found")
did = req.alias.split("#", 1)[0]
await txn.update_key(
kid,
tags={
"alias": req.alias,
"did": did,
},
)
await txn.commit()
return AssociateKeyResp(alias=req.alias, kid=kid)
@app.get("/key", tags=["keys"], response_description="Retrieved key")
async def get_key_by_alias(
alias: str,
profile: str = Header(default=DEFAULT_PROFILE, alias=PROFILE_HEADER),
store: Store = Depends(store),
) -> GenerateKeyResp:
"""Retrieve a key by identifier alias."""
LOGGER.debug("Retrieving key with: profile: %s, alias: %s", profile, alias)
async with store.session(profile=profile) as txn:
entries = await txn.fetch_all_keys(tag_filter={"alias": alias}, limit=1)
if not entries:
raise ProblemDetailsException.NotFound("Key not found")
kid = cast(str, entries[0].name)
key = cast(Key, entries[0].key)
return GenerateKeyResp.from_key(kid, key)
@app.get("/keys", tags=["keys"], response_description="List of keys")
async def get_all_keys(
profile: str = Header(default=DEFAULT_PROFILE, alias=PROFILE_HEADER),
store: Store = Depends(store),
) -> List[GenerateKeyResp]:
"""Return list of all keys.
WARNING: This will return at most 100 keys. Do not rely on this in production.
"""
async with store.session(profile=profile) as txn:
entries = await txn.fetch_all_keys(limit=100)
return [
GenerateKeyResp.from_key(cast(str, entry.name), cast(Key, entry.key))
for entry in entries
]
@app.get("/key/{kid}", tags=["keys"], response_description="Retrieved key")
async def get_key(
kid: str,
profile: str = Header(default=DEFAULT_PROFILE, alias=PROFILE_HEADER),
store: Store = Depends(store),
) -> GenerateKeyResp:
"""Get a key by its kid."""
async with store.session(profile=profile) as txn:
key_entry = await txn.fetch_key(kid)
if key_entry:
key = cast(Key, key_entry.key)
else:
raise ProblemDetailsException.NotFound("Key not found")
return GenerateKeyResp.from_key(kid, key)
@app.delete("/key/{kid}", tags=["keys"], response_description="Deleted kid")
async def delete_key(
kid: str,
profile: str = Header(default=DEFAULT_PROFILE, alias=PROFILE_HEADER),
store: Store = Depends(store),
):
"""Delete a key by its kid."""
async with store.session(profile=profile) as txn:
await txn.remove_key(kid)
return {"kid": kid}
class SigReq(BaseModel):
"""KID and Message to be signed in base64url encoding."""
kid: str
data: Base64UrlBytes
class SigResp(BaseModel):
"""Signed message in base64url encoding."""
sig: Base64UrlBytes
@app.post(
"/sign", tags=["ops"], response_description="Signed message in base64url encoding"
)
async def sign(
req: SigReq,
profile: str = Header(default=DEFAULT_PROFILE, alias=PROFILE_HEADER),
store: Store = Depends(store),
) -> SigResp:
"""Sign a message with a key."""
async with store.session(profile=profile) as txn:
key_entry = await txn.fetch_key(req.kid)
if key_entry:
key = cast(Key, key_entry.key)
else:
raise ProblemDetailsException.NotFound("Key not found")
sig = key.sign_message(req.data)
return SigResp(sig=Base64UrlEncoder.encode(sig))
class VCRecord(BaseModel):
"""Credential storage request."""
contexts: Set[str]
expanded_types: Set[str]
issuer_id: str
subject_ids: Set[str]
schema_ids: Set[str]
proof_types: Set[str]
cred_value: Mapping
given_id: Optional[str] = None
cred_tags: Optional[Mapping] = None
record_id: Optional[str] = None
class CredStoreResult(BaseModel):
"""Result of credential storage."""
record_id: str
VC_HOLDER_CAT = "vc-holder"
@app.post(
"/vc-holder/store", tags=["vc-holder"], response_description="Stored credential id"
)
async def store_credential(
cred: VCRecord,
profile: str = Header(default=DEFAULT_PROFILE, alias=PROFILE_HEADER),
store: Store = Depends(store),
):
"""Store a credential."""
tags = {
attr: value
for attr in (
"contexts",
"expanded_types",
"schema_ids",
"subject_ids",
"proof_types",
"issuer_id",
"given_id",
)
if (value := getattr(cred, attr))
}
for tagname, tagval in (cred.cred_tags or {}).items():
tags[f"cstm:{tagname}"] = tagval
record_id = cred.record_id or str(uuid4())
async with store.session(profile=profile) as txn:
await txn.insert(
category=VC_HOLDER_CAT, name=record_id, tags=tags, value_json=cred.cred_value
)
return CredStoreResult(record_id=record_id)
def entry_to_vc_record(entry: Entry) -> VCRecord:
"""Convert an Askar stored entry into a VC record."""
tags = cast(dict, entry.tags)
cred_tags = {name[5:]: value for name, value in tags if name.startswith("cstm:")}
contexts = tags.get("contexts", set())
types = tags.get("expanded_types", set())
schema_ids = tags.get("schema_ids", set())
subject_ids = tags.get("subject_ids", set())
proof_types = tags.get("proof_types", set())
issuer_id = tags.get("issuer_id")
if not isinstance(issuer_id, str):
raise ValueError("issuer_id must be str")
given_id = tags.get("given_id")
return VCRecord(
contexts=contexts,
expanded_types=types,
schema_ids=schema_ids,
issuer_id=issuer_id,
subject_ids=subject_ids,
proof_types=proof_types,
cred_value=json.loads(entry.value),
given_id=given_id,
cred_tags=cred_tags,
record_id=cast(str, entry.name),
)
@app.get(
"/vc-holder/credential/record/{record_id}",
tags=["vc-holder"],
response_description="Retrieved credential",
)
async def retrieve_credential_by_id(
record_id: str,
profile: str = Header(default=DEFAULT_PROFILE, alias=PROFILE_HEADER),
store: Store = Depends(store),
) -> VCRecord:
"""Retrieve a credential by id."""
async with store.session(profile=profile) as txn:
entry = await txn.fetch(VC_HOLDER_CAT, record_id)
if not entry:
raise ProblemDetailsException.NotFound(
f"No credential record found for id {record_id}"
)
return entry_to_vc_record(entry)
@app.get(
"/vc-holder/credential/given/{record_id}",
tags=["vc-holder"],
response_description="Retrieved credential",
)
async def retrieve_credential_by_given_id(
given_id: str,
profile: str = Header(default=DEFAULT_PROFILE, alias=PROFILE_HEADER),
store: Store = Depends(store),
) -> VCRecord:
"""Retrieve a credential by id."""
async with store.session(profile=profile) as txn:
entries = await txn.fetch_all(VC_HOLDER_CAT, {"given_id": given_id}, limit=2)
if not entries:
raise ProblemDetailsException.NotFound(
f"No credential record found for given id {given_id}"
)
if len(entries) > 1:
raise ProblemDetailsException.BadRequest(
f"Duplicate record found for given id {given_id}"
)
return entry_to_vc_record(entries[0])
@app.delete(
"/vc-holder/credential/record/{record_id}",
tags=["vc-holder"],
response_description="Retrieved credential",
)
async def delete_credential(
record_id: str,
profile: str = Header(default=DEFAULT_PROFILE, alias=PROFILE_HEADER),
store: Store = Depends(store),
) -> None:
"""Delete a credential."""
async with store.session(profile=profile) as txn:
# TODO error handling
await txn.remove(VC_HOLDER_CAT, record_id)
class VCRecords(BaseModel):
"""Records from a search."""
records: List[VCRecord]
def build_type_or_schema_query(uri_list: Sequence[str]) -> dict:
"""Build and return indy-specific type_or_schema_query."""
type_or_schema_query: dict[str, Any] = {}
for uri in uri_list:
q = {"$or": [{"type": uri}, {"schema": uri}]}
if type_or_schema_query:
if "$and" not in type_or_schema_query:
type_or_schema_query = {"$and": [type_or_schema_query]}
type_or_schema_query["$and"].append(q)
else:
type_or_schema_query = q
return type_or_schema_query
class CredSearchReq(BaseModel):
"""Credential search request body."""
contexts: Optional[List[str]] = None
types: Optional[List[str]] = None
schema_ids: Optional[List[str]] = None
issuer_id: Optional[str] = None
subject_ids: Optional[str] = None
proof_types: Optional[List[str]] = None
given_id: Optional[str] = None
tag_query: Optional[Mapping] = None
pd_uri_list: Optional[List[str]] = None
offset: int = 0
limit: int = 10
@app.post(
"/vc-holder/credentials",
tags=["vc-holder"],
response_description="Retrieved credentials",
)
async def search_credentials( # noqa: C901
req: CredSearchReq,
profile: str = Header(default=DEFAULT_PROFILE, alias=PROFILE_HEADER),
store: Store = Depends(store),
) -> VCRecords:
"""Search for credentials."""
offset = req.offset or 0
offset = 0 if offset < 0 else offset
limit = req.limit or 10
limit = 50 if limit > 50 else limit
def _match_any(query: list, k, vals):
if vals is None:
pass
elif len(vals) > 1:
query.append({"$or": [{k: v for v in vals}]})
else:
query.append({k: vals[0]})
def _make_custom_query(query):
result = {}
for k, v in query.items():
if isinstance(v, (list, set)) and k != "$exist":
result[k] = [_make_custom_query(cl) for cl in v]
elif k.startswith("$"):
result[k] = v
else:
result[f"cstm:{k}"] = v
return result
query = []
_match_any(query, "contexts", req.contexts)
_match_any(query, "expanded_types", req.types)
_match_any(query, "schema_ids", req.schema_ids)
_match_any(query, "subject_ids", req.subject_ids)
_match_any(query, "proof_types", req.proof_types)
if req.issuer_id:
query.append({"issuer_id": req.issuer_id})
if req.given_id:
query.append({"given_id": req.given_id})
if req.tag_query:
query.append(_make_custom_query(req.tag_query))
if req.pd_uri_list:
query.append(build_type_or_schema_query(req.pd_uri_list))
query = {"$and": query} if query else {}
scan = store.scan(VC_HOLDER_CAT, query, offset=offset, limit=limit, profile=profile)
entries = await scan.fetch_all()
return VCRecords(records=[entry_to_vc_record(entry) for entry in entries])