cmem-plugin-graphsigning 0.5.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cmem_plugin_graphsigning/__init__.py +1 -0
- cmem_plugin_graphsigning/canonicalization.py +173 -0
- cmem_plugin_graphsigning/create_graph_signature.py +835 -0
- cmem_plugin_graphsigning/create_graph_signature.svg +1 -0
- cmem_plugin_graphsigning/graph_proof_utils.py +341 -0
- cmem_plugin_graphsigning/graph_store.py +95 -0
- cmem_plugin_graphsigning/remove_graph_signature.py +147 -0
- cmem_plugin_graphsigning/remove_graph_signature.svg +1 -0
- cmem_plugin_graphsigning/signing.py +259 -0
- cmem_plugin_graphsigning/verify_graph_signature.py +875 -0
- cmem_plugin_graphsigning/verify_graph_signature.svg +1 -0
- cmem_plugin_graphsigning/vocabulary.py +48 -0
- cmem_plugin_graphsigning-0.5.0.dist-info/METADATA +50 -0
- cmem_plugin_graphsigning-0.5.0.dist-info/RECORD +16 -0
- cmem_plugin_graphsigning-0.5.0.dist-info/WHEEL +4 -0
- cmem_plugin_graphsigning-0.5.0.dist-info/licenses/LICENSE +201 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><mask id="gs-create"><rect width="24" height="24" fill="#fff"/><circle cx="17.75" cy="17.75" r="7.6" fill="#000"/></mask><g mask="url(#gs-create)"><g fill="none" stroke="currentColor" stroke-linecap="round" stroke-width="1.7"><path d="M3.2 12.8a8.8 9.8 0 0 1 17.6 0v2.4"/><path d="M3.2 12.8v4.6"/><path d="M6.3 12.8a5.7 6.6 0 0 1 11.4 0v3"/><path d="M6.3 12.8v6.4"/><path d="M9.4 12.9a2.6 3.4 0 0 1 5.2 0v2.6"/><path d="M9.4 12.9v8"/></g></g><path fill-rule="evenodd" d="M17.75 11.5a6.25 6.25 0 1 0 0 12.5 6.25 6.25 0 0 0 0-12.5ZM16.85 14.65h1.8v2.2h2.2v1.8h-2.2v2.2h-1.8v-2.2h-2.2v-1.8h2.2z"/></svg>
|
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
"""Shared utilities for building W3C Data Integrity proof RDF."""
|
|
2
|
+
|
|
3
|
+
import base64
|
|
4
|
+
import hashlib
|
|
5
|
+
import uuid
|
|
6
|
+
from collections.abc import Callable
|
|
7
|
+
from dataclasses import dataclass
|
|
8
|
+
|
|
9
|
+
import rdflib
|
|
10
|
+
from rdflib import RDF, XSD, Graph, Literal, URIRef
|
|
11
|
+
from rdflib.namespace import DCTERMS
|
|
12
|
+
|
|
13
|
+
from cmem_plugin_graphsigning.canonicalization import canonical_digest_bytes
|
|
14
|
+
from cmem_plugin_graphsigning.vocabulary import ECC, SEC
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@dataclass(frozen=True)
|
|
18
|
+
class ProofMetadata:
|
|
19
|
+
"""Metadata written into a W3C Data Integrity proof, beyond the digest and signature."""
|
|
20
|
+
|
|
21
|
+
created: str
|
|
22
|
+
verification_method_iri: str
|
|
23
|
+
public_key_multibase: str
|
|
24
|
+
cryptosuite: str
|
|
25
|
+
public_key_is_multikey: bool = False
|
|
26
|
+
"""Whether the key may be typed sec:Multikey - true only for key types Multikey covers."""
|
|
27
|
+
expires: str = ""
|
|
28
|
+
"""When the proof expires, formatted like created. Empty writes no sec:expiration at all."""
|
|
29
|
+
controller: str = ""
|
|
30
|
+
"""Who controls the verification method. Empty states no sec:controller at all."""
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
_BASE58BTC_ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def multibase_encode(data: bytes) -> str:
|
|
37
|
+
"""Encode bytes as multibase base64url (``u`` prefix, no padding)."""
|
|
38
|
+
return "u" + base64.urlsafe_b64encode(data).rstrip(b"=").decode("ascii")
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def multibase_encode_base58btc(data: bytes) -> str:
|
|
42
|
+
"""Encode bytes as multibase base58btc (``z`` prefix).
|
|
43
|
+
|
|
44
|
+
This is the encoding the W3C Data Integrity specifications require for proof
|
|
45
|
+
values and Multikey material, which is why proofs use it in preference to
|
|
46
|
+
base64url.
|
|
47
|
+
"""
|
|
48
|
+
number = int.from_bytes(data, "big")
|
|
49
|
+
encoded = ""
|
|
50
|
+
while number > 0:
|
|
51
|
+
number, remainder = divmod(number, 58)
|
|
52
|
+
encoded = _BASE58BTC_ALPHABET[remainder] + encoded
|
|
53
|
+
# every leading zero byte is a leading '1', which the integer has lost
|
|
54
|
+
leading_zeros = len(data) - len(data.lstrip(b"\x00"))
|
|
55
|
+
return "z" + "1" * leading_zeros + encoded
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _base58btc_decode(encoded: str) -> bytes:
|
|
59
|
+
"""Decode a base58btc string without its multibase prefix."""
|
|
60
|
+
number = 0
|
|
61
|
+
for character in encoded:
|
|
62
|
+
digit = _BASE58BTC_ALPHABET.find(character)
|
|
63
|
+
if digit < 0:
|
|
64
|
+
msg = f"Invalid base58btc character {character!r}"
|
|
65
|
+
raise ValueError(msg)
|
|
66
|
+
number = number * 58 + digit
|
|
67
|
+
leading_zeros = len(encoded) - len(encoded.lstrip("1"))
|
|
68
|
+
body = number.to_bytes((number.bit_length() + 7) // 8, "big") if number else b""
|
|
69
|
+
return b"\x00" * leading_zeros + body
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def multibase_decode(encoded: str) -> bytes:
|
|
73
|
+
"""Decode a multibase string, dispatching on its prefix.
|
|
74
|
+
|
|
75
|
+
Multibase is self-describing, so both the base64url values this plugin used to
|
|
76
|
+
write and the base58btc values it writes now decode without being told which
|
|
77
|
+
is which.
|
|
78
|
+
"""
|
|
79
|
+
prefix, body = encoded[:1], encoded[1:]
|
|
80
|
+
if prefix == "z":
|
|
81
|
+
return _base58btc_decode(body)
|
|
82
|
+
if prefix == "u":
|
|
83
|
+
padding = (4 - len(body) % 4) % 4
|
|
84
|
+
# validate=True, so a mangled value is rejected rather than quietly decoding to
|
|
85
|
+
# different bytes: the default discards characters outside the alphabet.
|
|
86
|
+
return base64.b64decode(body + "=" * padding, altchars=b"-_", validate=True)
|
|
87
|
+
msg = f"Unsupported multibase prefix {prefix!r}; expected 'z' (base58btc) or 'u' (base64url)"
|
|
88
|
+
raise ValueError(msg)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def new_rdf_graph() -> Graph:
|
|
92
|
+
"""Return an rdflib Graph pre-bound with the sec and ecc prefixes."""
|
|
93
|
+
g = Graph()
|
|
94
|
+
g.bind("sec", SEC)
|
|
95
|
+
g.bind("ecc", ECC)
|
|
96
|
+
return g
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
PROOF_GRAPH_NAMESPACE = "https://ns.eccenca.com/data/proofs/"
|
|
100
|
+
"""Fixed eccenca-controlled namespace a data graph's companion proof graph is minted under.
|
|
101
|
+
|
|
102
|
+
Follows the same ``https://ns.eccenca.com/data/<x>/`` convention already reserved for other
|
|
103
|
+
system graphs (for example ``AUTHORIZATION_GRAPH_URI`` in ``cmem_client.components.graph_store``),
|
|
104
|
+
rather than the legacy ``example/data/vocabs/`` one.
|
|
105
|
+
"""
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def proof_graph_iri(data_graph_iri: str) -> URIRef:
|
|
109
|
+
"""Return the IRI of *data_graph_iri*'s companion proof graph.
|
|
110
|
+
|
|
111
|
+
Deterministic - the hex-encoded SHA-256 digest of the data graph's own IRI - so every
|
|
112
|
+
caller computes the same key directly, without listing the repository first.
|
|
113
|
+
|
|
114
|
+
**This hash is not a security use.** It derives an identifier, and claims no
|
|
115
|
+
integrity or secrecy property: a collision here would collide two proof graphs, not
|
|
116
|
+
forge a signature, and nothing verifies against it. The security uses in this module
|
|
117
|
+
are ``canonical_digest`` and the digests under ``digest_multibase``.
|
|
118
|
+
"""
|
|
119
|
+
digest = hashlib.sha256(data_graph_iri.encode("utf-8")).hexdigest()
|
|
120
|
+
return URIRef(f"{PROOF_GRAPH_NAMESPACE}{digest}")
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def new_proof_uri() -> URIRef:
|
|
124
|
+
"""Mint the IRI of one proof: ``urn:uuid:`` followed by a fresh UUID.
|
|
125
|
+
|
|
126
|
+
A proof identifies the act of signing, not the graph signed. Two signatures
|
|
127
|
+
over one graph - by two parties, or by one party before and after an edit -
|
|
128
|
+
are two acts and need two identifiers, which a name derived from the graph
|
|
129
|
+
could never give them. The form is the one the Data Integrity specification
|
|
130
|
+
illustrates, and it says nothing about the graph, so a proof carried into a
|
|
131
|
+
document or handed to a third party discloses no IRI of its own accord.
|
|
132
|
+
"""
|
|
133
|
+
return URIRef(f"urn:uuid:{uuid.uuid4()}")
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def canonical_digest(graph: Graph) -> bytes:
|
|
137
|
+
"""Return the SHA-256 digest of *graph* in its RDFC-1.0 canonical form.
|
|
138
|
+
|
|
139
|
+
**This hash is a security use**: it is what a proof's signature covers, so a
|
|
140
|
+
collision here would be a forged signature.
|
|
141
|
+
|
|
142
|
+
Serializes once, directly - no reparse. Reparsing through rdflib's own parser
|
|
143
|
+
would rewrite a recognized datatype's lexical form (an ``xsd:dateTime`` written
|
|
144
|
+
``...Z`` comes back ``...+00:00``), which is exactly what this plugin must not
|
|
145
|
+
do (see ``canonicalization.parse_without_normalizing``). *graph* is trusted to
|
|
146
|
+
already carry the exact lexical form each caller wants signed or checked: a
|
|
147
|
+
graph read from the store via ``parse_without_normalizing`` already does, and
|
|
148
|
+
one built in memory does as long as every literal in it that matters was
|
|
149
|
+
constructed with ``normalize=False``.
|
|
150
|
+
"""
|
|
151
|
+
return canonical_digest_bytes(graph.serialize(format="nt").encode("utf-8"))
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def proof_payload(options_digest: bytes, document_hash_hex: str) -> bytes:
|
|
155
|
+
"""Return the bytes a proof signature covers: the options, then the document.
|
|
156
|
+
|
|
157
|
+
Concatenating the two digests binds the proof's own statements to the data it
|
|
158
|
+
is about, so neither can be swapped without invalidating the signature.
|
|
159
|
+
"""
|
|
160
|
+
return options_digest + bytes.fromhex(document_hash_hex)
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def proof_nodes(
|
|
164
|
+
graph: Graph, graph_uri: URIRef
|
|
165
|
+
) -> list[tuple[rdflib.term.Node, rdflib.term.Node | None]]:
|
|
166
|
+
"""Return every ``sec:proof`` on *graph_uri* that is typed ``sec:DataIntegrityProof``.
|
|
167
|
+
|
|
168
|
+
A resource merely linked via ``sec:proof`` is not itself a proof - it has to say so.
|
|
169
|
+
The type check lives here rather than in each caller, so every one of them -
|
|
170
|
+
signature verification and the signing path that finds a signer's own earlier
|
|
171
|
+
proof to replace - agrees on what counts as a proof to begin with.
|
|
172
|
+
"""
|
|
173
|
+
return [
|
|
174
|
+
(proof_uri, next(graph.objects(proof_uri, SEC.verificationMethod), None))
|
|
175
|
+
for proof_uri in graph.objects(graph_uri, SEC.proof)
|
|
176
|
+
if (proof_uri, RDF.type, SEC.DataIntegrityProof) in graph
|
|
177
|
+
]
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def proofs_by_other_keys(
|
|
181
|
+
graph: Graph,
|
|
182
|
+
graph_uri: URIRef,
|
|
183
|
+
is_own: Callable[[rdflib.term.Node, rdflib.term.Node | None], bool],
|
|
184
|
+
) -> tuple[Graph, list[rdflib.term.Node]]:
|
|
185
|
+
"""Return the signatures on *graph* that *is_own* does not claim.
|
|
186
|
+
|
|
187
|
+
Signing replaces this signer's own earlier signature and leaves everyone
|
|
188
|
+
else's where it is: removing another party's attestation is what **Remove
|
|
189
|
+
Graph Signature** is for, not a side effect of signing. Which of them are
|
|
190
|
+
this signer's own is left to the caller, since deciding it needs the key
|
|
191
|
+
and this module deliberately holds no cryptography.
|
|
192
|
+
"""
|
|
193
|
+
kept, replaced = new_rdf_graph(), []
|
|
194
|
+
for proof_uri, vm_uri in proof_nodes(graph, graph_uri):
|
|
195
|
+
if is_own(proof_uri, vm_uri):
|
|
196
|
+
replaced.append(proof_uri)
|
|
197
|
+
continue
|
|
198
|
+
kept.add((graph_uri, SEC.proof, proof_uri))
|
|
199
|
+
for triple in graph.triples((proof_uri, None, None)):
|
|
200
|
+
kept.add(triple)
|
|
201
|
+
if vm_uri is not None:
|
|
202
|
+
for predicate in (RDF.type, *VERIFICATION_METHOD_PREDICATES):
|
|
203
|
+
for obj in graph.objects(vm_uri, predicate):
|
|
204
|
+
kept.add((vm_uri, predicate, obj))
|
|
205
|
+
return kept, replaced
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
_SHA2_256_MULTIHASH_PREFIX = b"\x12\x20"
|
|
209
|
+
"""Multihash header for a SHA-256 digest: the sha2-256 code, then the 32-byte length."""
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
def digest_multibase(document_hash_hex: str) -> str:
|
|
213
|
+
"""Return *document_hash_hex* as a ``sec:digestMultibase`` value.
|
|
214
|
+
|
|
215
|
+
The Data Integrity specification defines ``digestMultibase`` as a multibase-encoded
|
|
216
|
+
**Multihash**, so the digest is prefixed with its multihash header before being
|
|
217
|
+
base64url-encoded - which is why such values always start ``uEi``.
|
|
218
|
+
"""
|
|
219
|
+
return multibase_encode(_SHA2_256_MULTIHASH_PREFIX + bytes.fromhex(document_hash_hex))
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
def digest_from_multibase(value: str) -> bytes:
|
|
223
|
+
"""Return the SHA-256 digest a ``sec:digestMultibase`` value carries.
|
|
224
|
+
|
|
225
|
+
Raises ValueError for anything that is not a sha2-256 Multihash, since a digest
|
|
226
|
+
made with another hash function could never equal the one recomputed here.
|
|
227
|
+
"""
|
|
228
|
+
decoded = multibase_decode(value)
|
|
229
|
+
header, digest = decoded[:2], decoded[2:]
|
|
230
|
+
if header != _SHA2_256_MULTIHASH_PREFIX or len(digest) != hashlib.sha256().digest_size:
|
|
231
|
+
msg = "sec:digestMultibase is not a sha2-256 Multihash"
|
|
232
|
+
raise ValueError(msg)
|
|
233
|
+
return digest
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
def signature_options(source: Graph, proof_uri: rdflib.term.Node) -> Graph:
|
|
237
|
+
"""Return one proof's configuration: what it asserts about itself, and nothing else.
|
|
238
|
+
|
|
239
|
+
This is exactly the "proof configuration" the eddsa-rdfc-2022 cryptosuite hashes
|
|
240
|
+
and signs - every statement the proof makes about itself except ``proofValue``, the
|
|
241
|
+
value being computed: its type, cryptosuite, timestamps, purpose and the
|
|
242
|
+
verification method it *names*. It excludes the data graph's link to
|
|
243
|
+
the proof (a statement the *document* makes, not the proof) and whatever the named
|
|
244
|
+
verification method states about itself (a statement the *key* makes about itself,
|
|
245
|
+
not the proof). An independent verifier reconstructing proof configuration from the
|
|
246
|
+
same proof would include neither, so this plugin does not either.
|
|
247
|
+
"""
|
|
248
|
+
options = new_rdf_graph()
|
|
249
|
+
for triple in source.triples((proof_uri, None, None)):
|
|
250
|
+
if triple[1] != SEC.proofValue:
|
|
251
|
+
options.add(triple)
|
|
252
|
+
return options
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
def build_digest_record(graph_uri: URIRef, document_hash_hex: str) -> Graph:
|
|
256
|
+
"""Return the record written for a graph that was hashed but not signed.
|
|
257
|
+
|
|
258
|
+
Nothing was signed, so the digest is all there is to record, and the graph resource
|
|
259
|
+
is the only thing left to hang it on.
|
|
260
|
+
"""
|
|
261
|
+
rdf = new_rdf_graph()
|
|
262
|
+
rdf.add(
|
|
263
|
+
(
|
|
264
|
+
graph_uri,
|
|
265
|
+
SEC.digestMultibase,
|
|
266
|
+
Literal(digest_multibase(document_hash_hex), datatype=SEC.multibase),
|
|
267
|
+
)
|
|
268
|
+
)
|
|
269
|
+
return rdf
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
def build_proof_rdf(
|
|
273
|
+
graph_uri: URIRef,
|
|
274
|
+
proof_uri: URIRef,
|
|
275
|
+
signature_b64: str,
|
|
276
|
+
metadata: ProofMetadata,
|
|
277
|
+
) -> Graph:
|
|
278
|
+
"""Build RDF triples for one W3C Data Integrity proof and return them as a Graph.
|
|
279
|
+
|
|
280
|
+
The proof carries exactly the properties the specification defines for it and
|
|
281
|
+
nothing of this plugin's own, so an independent eddsa-rdfc-2022 verifier
|
|
282
|
+
reconstructs the same proof configuration this plugin signed. Passing a placeholder
|
|
283
|
+
signature yields the full proof, which is how the options are built before the
|
|
284
|
+
signature over them exists.
|
|
285
|
+
"""
|
|
286
|
+
rdf = new_rdf_graph()
|
|
287
|
+
multibase_sig = multibase_encode_base58btc(base64.b64decode(signature_b64))
|
|
288
|
+
vm_uri = URIRef(metadata.verification_method_iri)
|
|
289
|
+
|
|
290
|
+
rdf.add((graph_uri, SEC.proof, proof_uri))
|
|
291
|
+
rdf.add((proof_uri, RDF.type, SEC.DataIntegrityProof))
|
|
292
|
+
rdf.add((proof_uri, SEC.proofPurpose, SEC.assertionMethod))
|
|
293
|
+
# created/expires/proofValue use the predicate, and datatype the official
|
|
294
|
+
# https://w3id.org/security/data-integrity/v2 context maps each term to - not this
|
|
295
|
+
# plugin's own choice - so a config hash built from these triples matches one an
|
|
296
|
+
# independent eddsa-rdfc-2022 verifier reconstructs from the same JSON-LD proof.
|
|
297
|
+
rdf.add(
|
|
298
|
+
(
|
|
299
|
+
proof_uri,
|
|
300
|
+
DCTERMS.created,
|
|
301
|
+
Literal(metadata.created, datatype=XSD.dateTime, normalize=False),
|
|
302
|
+
)
|
|
303
|
+
)
|
|
304
|
+
if metadata.expires:
|
|
305
|
+
rdf.add(
|
|
306
|
+
(
|
|
307
|
+
proof_uri,
|
|
308
|
+
SEC.expiration,
|
|
309
|
+
Literal(metadata.expires, datatype=XSD.dateTime, normalize=False),
|
|
310
|
+
)
|
|
311
|
+
)
|
|
312
|
+
rdf.add(
|
|
313
|
+
(proof_uri, SEC.cryptosuite, Literal(metadata.cryptosuite, datatype=SEC.cryptosuiteString))
|
|
314
|
+
)
|
|
315
|
+
rdf.add((proof_uri, SEC.proofValue, Literal(multibase_sig, datatype=SEC.multibase)))
|
|
316
|
+
# The specification requires every proof to name its verification method: it is how
|
|
317
|
+
# a verifier finds the key, so a proof without one is not verifiable by anyone else.
|
|
318
|
+
rdf.add((proof_uri, SEC.verificationMethod, vm_uri))
|
|
319
|
+
|
|
320
|
+
if metadata.public_key_multibase:
|
|
321
|
+
rdf.add((vm_uri, RDF.type, SEC.VerificationMethod))
|
|
322
|
+
if metadata.public_key_is_multikey:
|
|
323
|
+
rdf.add((vm_uri, RDF.type, SEC.Multikey))
|
|
324
|
+
# sec:multibase is the datatype the official Multikey context maps
|
|
325
|
+
# publicKeyMultibase to, so JSON-LD read with that context yields the same triple.
|
|
326
|
+
rdf.add(
|
|
327
|
+
(
|
|
328
|
+
vm_uri,
|
|
329
|
+
SEC.publicKeyMultibase,
|
|
330
|
+
Literal(metadata.public_key_multibase, datatype=SEC.multibase),
|
|
331
|
+
)
|
|
332
|
+
)
|
|
333
|
+
# A verification method has to name its controller (W3C Controlled Identifiers).
|
|
334
|
+
if metadata.controller:
|
|
335
|
+
rdf.add((vm_uri, SEC.controller, URIRef(metadata.controller)))
|
|
336
|
+
|
|
337
|
+
return rdf
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
VERIFICATION_METHOD_PREDICATES = (SEC.publicKeyMultibase, SEC.controller)
|
|
341
|
+
"""What the proof writer states about a verification method, and nothing else."""
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"""Reading graphs from the store, shared by every task in this plugin.
|
|
2
|
+
|
|
3
|
+
Each task identifies the graphs it works on the same way, reads a graph and its
|
|
4
|
+
companion proof graph the same way, and hashes what it read the same way - a
|
|
5
|
+
digest only means something if signing and verifying compute it identically.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
import rdflib
|
|
11
|
+
from cmem_client.client import Client
|
|
12
|
+
from cmem_client.exceptions import RepositoryItemNotFoundError
|
|
13
|
+
from cmem_client.repositories.graphs import GraphExportConfig, GraphsRepository
|
|
14
|
+
from cmem_plugin_base.dataintegration.entity import EntityPath, EntitySchema
|
|
15
|
+
|
|
16
|
+
from cmem_plugin_graphsigning.canonicalization import (
|
|
17
|
+
canonical_digest_bytes,
|
|
18
|
+
parse_without_normalizing,
|
|
19
|
+
)
|
|
20
|
+
from cmem_plugin_graphsigning.graph_proof_utils import proof_graph_iri
|
|
21
|
+
from cmem_plugin_graphsigning.vocabulary import SCHEMA_GRAPH_REFERENCE
|
|
22
|
+
|
|
23
|
+
_NTRIPLES_SERIALIZATION = GraphsRepository.formats["n-triples"]
|
|
24
|
+
"""Requested explicitly on every export, so the bytes parsed are always the format
|
|
25
|
+
``canonicalization.parse_without_normalizing`` expects - never the server's default."""
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def graph_iri_input_schema() -> EntitySchema:
|
|
29
|
+
"""Input schema for port 0: entities carrying graph IRIs."""
|
|
30
|
+
return EntitySchema(
|
|
31
|
+
type_uri=SCHEMA_GRAPH_REFERENCE,
|
|
32
|
+
paths=[
|
|
33
|
+
EntityPath("graph_iri", is_single_value=True),
|
|
34
|
+
],
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def download_and_parse(
|
|
39
|
+
client: Client, graph_iri: str, resolve_owl_imports: bool = False
|
|
40
|
+
) -> rdflib.Graph:
|
|
41
|
+
"""Download Knowledge Graph and parse it to a rdflib graph.
|
|
42
|
+
|
|
43
|
+
Exported as N-Triples explicitly and parsed via ``parse_without_normalizing``
|
|
44
|
+
rather than rdflib's own parser, so a literal's exact lexical form survives -
|
|
45
|
+
an ``xsd:dateTime`` written ``...Z`` does not come back ``...+00:00``.
|
|
46
|
+
|
|
47
|
+
Exporting without a path hands back a file the client created with
|
|
48
|
+
``delete=False``, so deleting it is this caller's job. Left undeleted it is a
|
|
49
|
+
full copy of the graph, kept for as long as the process lives.
|
|
50
|
+
"""
|
|
51
|
+
triple_file = client.graphs.export_item(
|
|
52
|
+
key=graph_iri,
|
|
53
|
+
configuration=GraphExportConfig(
|
|
54
|
+
resolve_owl_imports=resolve_owl_imports,
|
|
55
|
+
serialization=_NTRIPLES_SERIALIZATION,
|
|
56
|
+
),
|
|
57
|
+
)
|
|
58
|
+
try:
|
|
59
|
+
return parse_without_normalizing(Path(triple_file).read_bytes())
|
|
60
|
+
finally:
|
|
61
|
+
Path(triple_file).unlink(missing_ok=True)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def download_and_parse_proof_graph(client: Client, graph_iri: str) -> rdflib.Graph | None:
|
|
65
|
+
"""Download and parse *graph_iri*'s companion proof graph, or None where it has none.
|
|
66
|
+
|
|
67
|
+
Exported and parsed the same lexical-form-preserving way ``download_and_parse``
|
|
68
|
+
is: a proof's own ``dcterms:created``/``sec:expiration`` is exactly what was
|
|
69
|
+
signed, so re-reading it here must not rewrite it out from under the signature
|
|
70
|
+
that covers it.
|
|
71
|
+
"""
|
|
72
|
+
proof_uri = proof_graph_iri(graph_iri)
|
|
73
|
+
try:
|
|
74
|
+
triple_file = client.graphs.export_item(
|
|
75
|
+
key=str(proof_uri),
|
|
76
|
+
configuration=GraphExportConfig(serialization=_NTRIPLES_SERIALIZATION),
|
|
77
|
+
)
|
|
78
|
+
except RepositoryItemNotFoundError:
|
|
79
|
+
return None
|
|
80
|
+
try:
|
|
81
|
+
return parse_without_normalizing(Path(triple_file).read_bytes())
|
|
82
|
+
finally:
|
|
83
|
+
Path(triple_file).unlink(missing_ok=True)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def hash_parsed_graph(parsed_graph: rdflib.Graph) -> str:
|
|
87
|
+
"""Hash a rdflib graph: its RDFC-1.0 canonical form, SHA-256, hex-encoded.
|
|
88
|
+
|
|
89
|
+
**This hash is a security use**: it is the digest a signature is made over and
|
|
90
|
+
checked against, so a collision here would be a forged signature. Not every
|
|
91
|
+
SHA-256 call in this plugin is one - ``graph_proof_utils.proof_graph_iri`` only
|
|
92
|
+
derives an identifier - and the two are indistinguishable in the code.
|
|
93
|
+
"""
|
|
94
|
+
nquads = parsed_graph.serialize(format="nt").encode("utf-8")
|
|
95
|
+
return canonical_digest_bytes(nquads).hex()
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
"""Remove Graph Signature Task"""
|
|
2
|
+
|
|
3
|
+
from collections.abc import Sequence
|
|
4
|
+
from contextlib import suppress
|
|
5
|
+
|
|
6
|
+
from cmem_plugin_base.dataintegration.client import get_client
|
|
7
|
+
from cmem_plugin_base.dataintegration.context import ExecutionContext, ExecutionReport
|
|
8
|
+
from cmem_plugin_base.dataintegration.description import Icon, Plugin, PluginParameter
|
|
9
|
+
from cmem_plugin_base.dataintegration.entity import Entities, Entity
|
|
10
|
+
from cmem_plugin_base.dataintegration.parameter.graph import GraphParameterType
|
|
11
|
+
from cmem_plugin_base.dataintegration.plugins import WorkflowPlugin
|
|
12
|
+
from cmem_plugin_base.dataintegration.ports import (
|
|
13
|
+
FixedNumberOfInputs,
|
|
14
|
+
FixedSchemaPort,
|
|
15
|
+
)
|
|
16
|
+
from rdflib import URIRef
|
|
17
|
+
|
|
18
|
+
from cmem_plugin_graphsigning.graph_proof_utils import proof_graph_iri, proof_nodes
|
|
19
|
+
from cmem_plugin_graphsigning.graph_store import (
|
|
20
|
+
download_and_parse_proof_graph,
|
|
21
|
+
graph_iri_input_schema,
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@Plugin(
|
|
26
|
+
label="Remove Graph Signature",
|
|
27
|
+
plugin_id="cmem_plugin_graphsigning-RemoveGraphSignature",
|
|
28
|
+
description=(
|
|
29
|
+
"Delete a Knowledge Graph's companion proof graph, leaving the data triples "
|
|
30
|
+
"untouched and the graph unsigned."
|
|
31
|
+
),
|
|
32
|
+
documentation="""
|
|
33
|
+
Delete a Knowledge Graph's companion proof graph, leaving the data triples exactly as they
|
|
34
|
+
were and the graph unsigned. Nothing is written in its place, and the data graph itself is
|
|
35
|
+
never read or rewritten.
|
|
36
|
+
|
|
37
|
+
## Ports
|
|
38
|
+
|
|
39
|
+
Graph IRIs arrive on the input port, as `graph_iri` values, and are processed in order. The
|
|
40
|
+
port is replaced by a parameter when a single graph is configured on the task itself.
|
|
41
|
+
|
|
42
|
+
One entity leaves per processed graph, carrying its `graph_iri` alone - the same graph list the
|
|
43
|
+
input port reads, so the graphs can be handed straight on.
|
|
44
|
+
|
|
45
|
+
## Where this task fits
|
|
46
|
+
|
|
47
|
+
It undoes what **Create Graph Signature** writes into a companion proof graph. Its output
|
|
48
|
+
feeds **Create Graph Signature** to sign the graphs afresh, or **Verify Graph Signature**
|
|
49
|
+
reading a graph list, which then reports `signature_source="none"` for every graph.
|
|
50
|
+
|
|
51
|
+
## Caveats
|
|
52
|
+
|
|
53
|
+
- **Every signature goes, not only yours.** A companion proof graph holds the signatures of
|
|
54
|
+
everyone who signed the graph, and this task deletes it whole, whatever it contains. There
|
|
55
|
+
is no way to remove one signature and keep another, so a graph that should carry your
|
|
56
|
+
signature alone has to be stripped and signed again.
|
|
57
|
+
- A graph with no companion proof graph - including one that does not exist, and one carrying
|
|
58
|
+
proof-shaped triples in its own data - is left untouched and still appears in the output.
|
|
59
|
+
- A cancelled run hands on only the graphs processed so far.
|
|
60
|
+
""",
|
|
61
|
+
icon=Icon(file_name="remove_graph_signature.svg", package=__package__),
|
|
62
|
+
parameters=[
|
|
63
|
+
PluginParameter(
|
|
64
|
+
name="input_graph",
|
|
65
|
+
label="Input Graph",
|
|
66
|
+
description=(
|
|
67
|
+
"The Knowledge Graph to remove the signatures from. Leave empty to take "
|
|
68
|
+
"graph IRIs from the input port instead; setting this parameter removes "
|
|
69
|
+
"that port."
|
|
70
|
+
),
|
|
71
|
+
param_type=GraphParameterType(
|
|
72
|
+
show_di_graphs=False,
|
|
73
|
+
show_graphs_without_class=True,
|
|
74
|
+
show_system_graphs=True,
|
|
75
|
+
allow_only_autocompleted_values=False,
|
|
76
|
+
),
|
|
77
|
+
default_value="",
|
|
78
|
+
),
|
|
79
|
+
],
|
|
80
|
+
)
|
|
81
|
+
class RemoveGraphSignature(WorkflowPlugin):
|
|
82
|
+
"""Delete a graph's companion proof graph, leaving the data graph untouched."""
|
|
83
|
+
|
|
84
|
+
def __init__(self, input_graph: str = "") -> None:
|
|
85
|
+
self.input_graph = input_graph
|
|
86
|
+
if input_graph:
|
|
87
|
+
self.input_ports = FixedNumberOfInputs([])
|
|
88
|
+
else:
|
|
89
|
+
self.input_ports = FixedNumberOfInputs(
|
|
90
|
+
[FixedSchemaPort(schema=graph_iri_input_schema())]
|
|
91
|
+
)
|
|
92
|
+
self.output_port = FixedSchemaPort(schema=graph_iri_input_schema())
|
|
93
|
+
|
|
94
|
+
def execute(
|
|
95
|
+
self,
|
|
96
|
+
inputs: Sequence[Entities],
|
|
97
|
+
context: ExecutionContext,
|
|
98
|
+
) -> Entities:
|
|
99
|
+
"""Run the workflow operator."""
|
|
100
|
+
self.log.info("Start removing signatures.")
|
|
101
|
+
client = get_client(context)
|
|
102
|
+
|
|
103
|
+
# Collect graph IRIs from port 0; fall back to the input_graph parameter
|
|
104
|
+
graph_iris: list[str] = []
|
|
105
|
+
if inputs:
|
|
106
|
+
graph_iris.extend(
|
|
107
|
+
entity.values[0][0]
|
|
108
|
+
for entity in inputs[0].entities
|
|
109
|
+
if entity.values and entity.values[0]
|
|
110
|
+
)
|
|
111
|
+
if not graph_iris and self.input_graph:
|
|
112
|
+
graph_iris = [self.input_graph]
|
|
113
|
+
|
|
114
|
+
output_entities: list[Entity] = []
|
|
115
|
+
removed_count = 0
|
|
116
|
+
|
|
117
|
+
for graph_iri in graph_iris:
|
|
118
|
+
with suppress(AttributeError):
|
|
119
|
+
if context.workflow.status() == "Canceling":
|
|
120
|
+
break
|
|
121
|
+
|
|
122
|
+
self.log.info(f"Processing graph: {graph_iri}")
|
|
123
|
+
graph_uri_ref = URIRef(graph_iri)
|
|
124
|
+
proof_graph = download_and_parse_proof_graph(client, graph_iri)
|
|
125
|
+
|
|
126
|
+
if proof_graph is not None:
|
|
127
|
+
signature_count = len(proof_nodes(proof_graph, graph_uri_ref))
|
|
128
|
+
client.graphs.delete_item(key=str(proof_graph_iri(graph_iri)))
|
|
129
|
+
self.log.info(f"{signature_count} signature(s) removed from graph: {graph_iri}")
|
|
130
|
+
removed_count += signature_count
|
|
131
|
+
else:
|
|
132
|
+
self.log.info(f"No proof graph found, graph left untouched: {graph_iri}")
|
|
133
|
+
|
|
134
|
+
output_entities.append(Entity(uri=graph_iri, values=[[graph_iri]]))
|
|
135
|
+
|
|
136
|
+
context.report.update(
|
|
137
|
+
ExecutionReport(
|
|
138
|
+
entity_count=len(output_entities),
|
|
139
|
+
operation="write",
|
|
140
|
+
operation_desc=f"graphs processed ({removed_count} signatures removed)",
|
|
141
|
+
)
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
return Entities(
|
|
145
|
+
schema=graph_iri_input_schema(),
|
|
146
|
+
entities=iter(output_entities),
|
|
147
|
+
)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><mask id="gs-remove"><rect width="24" height="24" fill="#fff"/><circle cx="17.75" cy="17.75" r="7.6" fill="#000"/></mask><g mask="url(#gs-remove)"><g fill="none" stroke="currentColor" stroke-linecap="round" stroke-width="1.7"><path d="M3.2 12.8a8.8 9.8 0 0 1 17.6 0v2.4"/><path d="M3.2 12.8v4.6"/><path d="M6.3 12.8a5.7 6.6 0 0 1 11.4 0v3"/><path d="M6.3 12.8v6.4"/><path d="M9.4 12.9a2.6 3.4 0 0 1 5.2 0v2.6"/><path d="M9.4 12.9v8"/></g></g><path fill-rule="evenodd" d="M17.75 11.5a6.25 6.25 0 1 0 0 12.5 6.25 6.25 0 0 0 0-12.5ZM14.65 16.85h6.2v1.8h-6.2z"/></svg>
|