-
Notifications
You must be signed in to change notification settings - Fork 516
/
manager.py
303 lines (258 loc) · 11.6 KB
/
manager.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
"""Classes to manage credential revocation."""
import json
import logging
from typing import Mapping, Sequence, Text
from ..protocols.revocation_notification.v1_0.models.rev_notification_record import (
RevNotificationRecord,
)
from ..core.error import BaseError
from ..core.profile import Profile
from ..indy.issuer import IndyIssuer
from ..storage.error import StorageNotFoundError
from .indy import IndyRevocation
from .models.issuer_cred_rev_record import IssuerCredRevRecord
from .models.issuer_rev_reg_record import IssuerRevRegRecord
from .util import notify_pending_cleared_event, notify_revocation_published_event
from ..protocols.issue_credential.v1_0.models.credential_exchange import (
V10CredentialExchange,
)
from ..protocols.issue_credential.v2_0.models.cred_ex_record import (
V20CredExRecord,
)
class RevocationManagerError(BaseError):
"""Revocation manager error."""
class RevocationManager:
"""Class for managing revocation operations."""
def __init__(self, profile: Profile):
"""
Initialize a RevocationManager.
Args:
context: The context for this revocation manager
"""
self._profile = profile
self._logger = logging.getLogger(__name__)
async def revoke_credential_by_cred_ex_id(
self,
cred_ex_id: str,
publish: bool = False,
notify: bool = False,
thread_id: str = None,
connection_id: str = None,
comment: str = None,
):
"""
Revoke a credential by its credential exchange identifier at issue.
Optionally, publish the corresponding revocation registry delta to the ledger.
Args:
cred_ex_id: credential exchange identifier
publish: whether to publish the resulting revocation registry delta,
along with any revocations pending against it
"""
try:
async with self._profile.session() as session:
rec = await IssuerCredRevRecord.retrieve_by_cred_ex_id(
session,
cred_ex_id,
)
except StorageNotFoundError as err:
raise RevocationManagerError(
"No issuer credential revocation record found for "
f"credential exchange id {cred_ex_id}"
) from err
return await self.revoke_credential(
rev_reg_id=rec.rev_reg_id,
cred_rev_id=rec.cred_rev_id,
publish=publish,
notify=notify,
thread_id=thread_id,
connection_id=connection_id,
comment=comment,
)
async def revoke_credential(
self,
rev_reg_id: str,
cred_rev_id: str,
publish: bool = False,
notify: bool = False,
thread_id: str = None,
connection_id: str = None,
comment: str = None,
):
"""
Revoke a credential.
Optionally, publish the corresponding revocation registry delta to the ledger.
Args:
rev_reg_id: revocation registry id
cred_rev_id: credential revocation id
publish: whether to publish the resulting revocation registry delta,
along with any revocations pending against it
"""
issuer = self._profile.inject(IndyIssuer)
revoc = IndyRevocation(self._profile)
issuer_rr_rec = await revoc.get_issuer_rev_reg_record(rev_reg_id)
if not issuer_rr_rec:
raise RevocationManagerError(
f"No revocation registry record found for id {rev_reg_id}"
)
if notify:
thread_id = thread_id or f"indy::{rev_reg_id}::{cred_rev_id}"
rev_notify_rec = RevNotificationRecord(
rev_reg_id=rev_reg_id,
cred_rev_id=cred_rev_id,
thread_id=thread_id,
connection_id=connection_id,
comment=comment,
)
async with self._profile.session() as session:
await rev_notify_rec.save(session, reason="New revocation notification")
if publish:
rev_reg = await revoc.get_ledger_registry(rev_reg_id)
await rev_reg.get_or_fetch_local_tails_path()
# pick up pending revocations on input revocation registry
crids = list(set(issuer_rr_rec.pending_pub + [cred_rev_id]))
(delta_json, _) = await issuer.revoke_credentials(
issuer_rr_rec.revoc_reg_id, issuer_rr_rec.tails_local_path, crids
)
if delta_json:
issuer_rr_rec.revoc_reg_entry = json.loads(delta_json)
await issuer_rr_rec.send_entry(self._profile)
async with self._profile.session() as session:
await issuer_rr_rec.clear_pending(session)
await self.set_cred_revoked_state(rev_reg_id, [cred_rev_id])
await notify_revocation_published_event(
self._profile, rev_reg_id, [cred_rev_id]
)
else:
async with self._profile.session() as session:
await issuer_rr_rec.mark_pending(session, cred_rev_id)
async def publish_pending_revocations(
self,
rrid2crid: Mapping[Text, Sequence[Text]] = None,
) -> Mapping[Text, Sequence[Text]]:
"""
Publish pending revocations to the ledger.
Args:
rrid2crid: Mapping from revocation registry identifiers to all credential
revocation identifiers within each to publish. Specify null/empty map
for all revocation registries. Specify empty sequence per revocation
registry identifier for all pending within the revocation registry;
e.g.,
{} - publish all pending revocations from all revocation registries
{
"R17v42T4pk...:4:R17v42T4pk...:3:CL:19:tag:CL_ACCUM:0": [],
"R17v42T4pk...:4:R17v42T4pk...:3:CL:19:tag:CL_ACCUM:1": ["1", "2"]
} - publish:
- all pending revocations from all revocation registry tagged 0
- pending ["1", "2"] from revocation registry tagged 1
- no pending revocations from any other revocation registries.
Returns: mapping from each revocation registry id to its cred rev ids published.
"""
result = {}
issuer = self._profile.inject(IndyIssuer)
async with self._profile.transaction() as txn:
issuer_rr_recs = await IssuerRevRegRecord.query_by_pending(txn)
for issuer_rr_rec in issuer_rr_recs:
rrid = issuer_rr_rec.revoc_reg_id
crids = []
if not rrid2crid:
crids = issuer_rr_rec.pending_pub
elif rrid in rrid2crid:
crids = [
crid
for crid in issuer_rr_rec.pending_pub
if crid in (rrid2crid[rrid] or []) or not rrid2crid[rrid]
]
if crids:
# FIXME - must use the same transaction
(delta_json, failed_crids) = await issuer.revoke_credentials(
issuer_rr_rec.revoc_reg_id,
issuer_rr_rec.tails_local_path,
crids,
transaction=txn,
)
issuer_rr_rec.revoc_reg_entry = json.loads(delta_json)
await issuer_rr_rec.send_entry(self._profile)
published = [crid for crid in crids if crid not in failed_crids]
result[issuer_rr_rec.revoc_reg_id] = published
await issuer_rr_rec.clear_pending(txn, published)
await txn.commit()
await self.set_cred_revoked_state(issuer_rr_rec.revoc_reg_id, crids)
await notify_revocation_published_event(
self._profile, issuer_rr_rec.revoc_reg_id, crids
)
return result
async def clear_pending_revocations(
self, purge: Mapping[Text, Sequence[Text]] = None
) -> Mapping[Text, Sequence[Text]]:
"""
Clear pending revocation publications.
Args:
purge: Mapping from revocation registry identifiers to all credential
revocation identifiers within each to clear. Specify null/empty map
for all revocation registries. Specify empty sequence per revocation
registry identifier for all pending within the revocation registry;
e.g.,
{} - clear all pending revocations from all revocation registries
{
"R17v42T4pk...:4:R17v42T4pk...:3:CL:19:tag:CL_ACCUM:0": [],
"R17v42T4pk...:4:R17v42T4pk...:3:CL:19:tag:CL_ACCUM:1": ["1", "2"]
} - clear
- all pending revocations from all revocation registry tagged 0
- pending ["1", "2"] from revocation registry tagged 1
- no pending revocations from any other revocation registries.
Returns:
mapping from revocation registry id to its remaining
cred rev ids still marked pending, omitting revocation registries
with no remaining pending publications.
"""
result = {}
async with self._profile.transaction() as txn:
issuer_rr_recs = await IssuerRevRegRecord.query_by_pending(txn)
for issuer_rr_rec in issuer_rr_recs:
rrid = issuer_rr_rec.revoc_reg_id
await issuer_rr_rec.clear_pending(txn, (purge or {}).get(rrid))
if issuer_rr_rec.pending_pub:
result[rrid] = issuer_rr_rec.pending_pub
await notify_pending_cleared_event(self._profile, rrid)
await txn.commit()
return result
async def set_cred_revoked_state(
self, rev_reg_id: str, cred_rev_ids: Sequence[str]
) -> None:
"""
Update credentials state to credential_revoked.
Args:
rev_reg_id: revocation registry ID
cred_rev_ids: list of credential revocation IDs
Returns:
None
"""
for cred_rev_id in cred_rev_ids:
async with self._profile.session() as session:
try:
rev_rec = await IssuerCredRevRecord.retrieve_by_ids(
session, rev_reg_id, cred_rev_id
)
try:
cred_ex_record = await V10CredentialExchange.retrieve_by_id(
session, rev_rec.cred_ex_id
)
cred_ex_record.state = (
V10CredentialExchange.STATE_CREDENTIAL_REVOKED
)
await cred_ex_record.save(session, reason="revoke credential")
except StorageNotFoundError:
try:
cred_ex_record = await V20CredExRecord.retrieve_by_id(
session, rev_rec.cred_ex_id
)
cred_ex_record.state = (
V20CredExRecord.STATE_CREDENTIAL_REVOKED
)
await cred_ex_record.save(
session, reason="revoke credential"
)
except StorageNotFoundError:
pass
except StorageNotFoundError:
pass