rrcp 0.2.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.
- rrcp/__init__.py +78 -0
- rrcp/auth.py +400 -0
- rrcp/cli.py +627 -0
- rrcp/commands/__init__.py +1 -0
- rrcp/commands/lxm.py +137 -0
- rrcp/commands/rss.py +295 -0
- rrcp/custody.py +171 -0
- rrcp/dispatcher.py +316 -0
- rrcp/enqueue.py +231 -0
- rrcp/envelope.py +229 -0
- rrcp/gateway.py +199 -0
- rrcp/grades.py +47 -0
- rrcp/paper.py +213 -0
- rrcp/provision.py +112 -0
- rrcp/provisioning.py +156 -0
- rrcp/sneakernet.py +522 -0
- rrcp/spool.py +251 -0
- rrcp/sync/__init__.py +1 -0
- rrcp/sync/delta.py +215 -0
- rrcp/sync/engine.py +330 -0
- rrcp/sync/fields.py +76 -0
- rrcp/sync/index.py +264 -0
- rrcp/sync/scanner.py +121 -0
- rrcp/transport.py +356 -0
- rrcp-0.2.0.dist-info/METADATA +21 -0
- rrcp-0.2.0.dist-info/RECORD +29 -0
- rrcp-0.2.0.dist-info/WHEEL +5 -0
- rrcp-0.2.0.dist-info/entry_points.txt +6 -0
- rrcp-0.2.0.dist-info/top_level.txt +1 -0
rrcp/__init__.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"""Python implementation of the Reticulum-to-Reticulum Copy (RRCP) protocol.
|
|
2
|
+
|
|
3
|
+
Reference implementation of ``SPEC.md``. The Python package is canonical;
|
|
4
|
+
the JavaScript and C++ ports must stay interoperable with it.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from .grades import STANDARD_GRADES, grade_key, is_eligible, is_valid_grade
|
|
8
|
+
from .envelope import (
|
|
9
|
+
FIELD_ORDER,
|
|
10
|
+
KNOWN_FLAGS,
|
|
11
|
+
PROTOCOL_VERSION,
|
|
12
|
+
REQUIRED_FIELDS,
|
|
13
|
+
Envelope,
|
|
14
|
+
EnvelopeError,
|
|
15
|
+
parse,
|
|
16
|
+
)
|
|
17
|
+
from .spool import Spool, SpoolError
|
|
18
|
+
from .dispatcher import Dispatcher, Receipt, UnknownCommandError, ArgumentError, parse_args
|
|
19
|
+
from .auth import (
|
|
20
|
+
CMD_OBJECT_PREFIX,
|
|
21
|
+
INFRASTRUCTURE_COMMANDS,
|
|
22
|
+
RELATION_EXECUTE,
|
|
23
|
+
RELATION_READ,
|
|
24
|
+
RELATION_WRITE,
|
|
25
|
+
SYNC_OBJECT_PREFIX,
|
|
26
|
+
AuthorizationError,
|
|
27
|
+
ProvisionedCaps,
|
|
28
|
+
ShareAuthority,
|
|
29
|
+
SharePolicy,
|
|
30
|
+
authorization_target,
|
|
31
|
+
decode_cap,
|
|
32
|
+
encode_cap,
|
|
33
|
+
load_owner,
|
|
34
|
+
owner_hash_from_public_key,
|
|
35
|
+
parse_caps,
|
|
36
|
+
required_relation,
|
|
37
|
+
share_object,
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
__version__ = "0.2.0"
|
|
41
|
+
|
|
42
|
+
__all__ = [
|
|
43
|
+
"STANDARD_GRADES",
|
|
44
|
+
"grade_key",
|
|
45
|
+
"is_eligible",
|
|
46
|
+
"is_valid_grade",
|
|
47
|
+
"FIELD_ORDER",
|
|
48
|
+
"KNOWN_FLAGS",
|
|
49
|
+
"PROTOCOL_VERSION",
|
|
50
|
+
"REQUIRED_FIELDS",
|
|
51
|
+
"Envelope",
|
|
52
|
+
"EnvelopeError",
|
|
53
|
+
"parse",
|
|
54
|
+
"Spool",
|
|
55
|
+
"SpoolError",
|
|
56
|
+
"Dispatcher",
|
|
57
|
+
"Receipt",
|
|
58
|
+
"parse_args",
|
|
59
|
+
"SYNC_OBJECT_PREFIX",
|
|
60
|
+
"CMD_OBJECT_PREFIX",
|
|
61
|
+
"INFRASTRUCTURE_COMMANDS",
|
|
62
|
+
"RELATION_READ",
|
|
63
|
+
"RELATION_WRITE",
|
|
64
|
+
"RELATION_EXECUTE",
|
|
65
|
+
"AuthorizationError",
|
|
66
|
+
"ShareAuthority",
|
|
67
|
+
"SharePolicy",
|
|
68
|
+
"ProvisionedCaps",
|
|
69
|
+
"load_owner",
|
|
70
|
+
"owner_hash_from_public_key",
|
|
71
|
+
"parse_caps",
|
|
72
|
+
"authorization_target",
|
|
73
|
+
"decode_cap",
|
|
74
|
+
"encode_cap",
|
|
75
|
+
"required_relation",
|
|
76
|
+
"share_object",
|
|
77
|
+
"__version__",
|
|
78
|
+
]
|
rrcp/auth.py
ADDED
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
"""Dacar-backed capability enforcement — SPEC.md §3.1 ``cap`` and §5.
|
|
2
|
+
|
|
3
|
+
Every sync share is a Dacar object named ``rrcp:sync:<share>`` with two
|
|
4
|
+
relations: ``read`` (pull files from the share) and ``write`` (push
|
|
5
|
+
manifests and deltas into the share). A share's owner issues signed Dacar
|
|
6
|
+
``GRANT`` operations to peer identity hashes; the msgpack transport
|
|
7
|
+
payload of the signed operation travels in the envelope ``cap`` header as
|
|
8
|
+
a base64url token, exactly as the spec's "base64url-or-hex dacar token"
|
|
9
|
+
allows.
|
|
10
|
+
|
|
11
|
+
Receivers enforce without any bypass: the token must unpack, its
|
|
12
|
+
signature must verify against the node's keyring, and the grant chain
|
|
13
|
+
must evaluate ALLOW on the receiver's replicated Dacar state for the
|
|
14
|
+
(object, relation, sender) triple. Because verified operations are
|
|
15
|
+
applied into the receiver's CRDT state, capabilities keep working across
|
|
16
|
+
nodes that have never seen the grant before — the offline-first property
|
|
17
|
+
the spool protocol depends on.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
import base64
|
|
21
|
+
import binascii
|
|
22
|
+
import hashlib
|
|
23
|
+
|
|
24
|
+
from dacar.config import Config
|
|
25
|
+
from dacar.crdt import StateVector
|
|
26
|
+
from dacar.engine import Engine
|
|
27
|
+
from dacar.hlc import Clock
|
|
28
|
+
from dacar.namespace import NamespaceHasher, SALT_SIZE
|
|
29
|
+
from dacar.operation import Action, Operation
|
|
30
|
+
from dacar.tuple import Tuple
|
|
31
|
+
from dacar.verifier import Keyring, verify_operation
|
|
32
|
+
|
|
33
|
+
#: Prefix of the Dacar object label for every synced share (§3.1 ``args``).
|
|
34
|
+
SYNC_OBJECT_PREFIX = "rrcp:sync"
|
|
35
|
+
|
|
36
|
+
#: Prefix of the Dacar object label for remote-execution commands: a node
|
|
37
|
+
#: must hold an ``execute`` grant on ``rrcp:cmd:<cmd>`` to invoke it.
|
|
38
|
+
CMD_OBJECT_PREFIX = "rrcp:cmd"
|
|
39
|
+
|
|
40
|
+
#: Relation for pulling files from a share (SPEC.md §6.2 ``sync-pull``).
|
|
41
|
+
RELATION_READ = "read"
|
|
42
|
+
|
|
43
|
+
#: Relation for pushing manifests and deltas into a share (§6.2).
|
|
44
|
+
RELATION_WRITE = "write"
|
|
45
|
+
|
|
46
|
+
#: Relation for invoking a remote-execution command.
|
|
47
|
+
RELATION_EXECUTE = "execute"
|
|
48
|
+
|
|
49
|
+
#: Relation each share-scoped ``cmd`` requires (SPEC.md §5 scope examples).
|
|
50
|
+
_RELATION_BY_CMD = {
|
|
51
|
+
"sync-manifest": RELATION_WRITE,
|
|
52
|
+
"sync-delta": RELATION_WRITE,
|
|
53
|
+
"sync-pull": RELATION_READ,
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
#: Administrative records (custody signals, disposition reports) that ride
|
|
57
|
+
#: the protocol itself and are not user-invokable commands.
|
|
58
|
+
INFRASTRUCTURE_COMMANDS = frozenset(("rrcp-ccs", "rrcp-report"))
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
class AuthorizationError(PermissionError):
|
|
62
|
+
"""A job's ``cap`` token is missing, malformed, or does not authorize it."""
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def owner_hash_from_public_key(public_key):
|
|
66
|
+
"""Dacar issuer hash for an RNS identity's public key.
|
|
67
|
+
|
|
68
|
+
SHA-256 over the full 64-byte RNS public key (X25519 ‖ Ed25519),
|
|
69
|
+
truncated to 16 bytes — exactly ``RNS.Identity.hash`` and the issuer
|
|
70
|
+
convention the dacar CLI uses, so grant operations minted by either
|
|
71
|
+
side verify on the other.
|
|
72
|
+
"""
|
|
73
|
+
public_key = bytes(public_key)
|
|
74
|
+
if len(public_key) != 64:
|
|
75
|
+
raise ValueError(
|
|
76
|
+
"an RNS identity public key is 64 bytes (X25519 ‖ Ed25519),"
|
|
77
|
+
f" got {len(public_key)}"
|
|
78
|
+
)
|
|
79
|
+
return hashlib.sha256(public_key).digest()[:16]
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def load_owner(path):
|
|
83
|
+
"""Load the share-owner RNS identity from an RNS identity file.
|
|
84
|
+
|
|
85
|
+
The owner is a full RNS identity (the format ``dacar identity new``
|
|
86
|
+
and ``rrcp-provision owner`` produce): its identity hash is the Dacar
|
|
87
|
+
trust anchor / issuer hash, its Ed25519 half signs grant operations.
|
|
88
|
+
Returns ``(identity, owner_hash)``.
|
|
89
|
+
"""
|
|
90
|
+
import RNS
|
|
91
|
+
|
|
92
|
+
identity = RNS.Identity.from_file(path)
|
|
93
|
+
if identity is None or identity.sig_prv is None:
|
|
94
|
+
raise ValueError(f"not a loadable RNS identity with a signing key: {path}")
|
|
95
|
+
return identity, identity.hash
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def share_object(share):
|
|
99
|
+
"""Return the Dacar object label for *share* (e.g. ``rrcp:sync:notes``)."""
|
|
100
|
+
return f"{SYNC_OBJECT_PREFIX}:{share}"
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def required_relation(cmd):
|
|
104
|
+
"""Return the share relation *cmd* requires, or None if not share-scoped."""
|
|
105
|
+
return _RELATION_BY_CMD.get(cmd)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def authorization_target(cmd, args):
|
|
109
|
+
"""Return ``(object_id, relation)`` *cmd* must be authorized for.
|
|
110
|
+
|
|
111
|
+
Sync verbs authorize against ``rrcp:sync:<share>`` with the relation
|
|
112
|
+
the verb needs; every other user command authorizes against
|
|
113
|
+
``rrcp:cmd:<cmd>`` with ``execute``; infrastructure records (custody
|
|
114
|
+
signals, disposition reports) need no grant. Raises ValueError when a
|
|
115
|
+
sync verb carries no ``share`` arg.
|
|
116
|
+
"""
|
|
117
|
+
if cmd in INFRASTRUCTURE_COMMANDS:
|
|
118
|
+
return None
|
|
119
|
+
relation = _RELATION_BY_CMD.get(cmd)
|
|
120
|
+
if relation is not None:
|
|
121
|
+
share = (args or {}).get("share")
|
|
122
|
+
if not share:
|
|
123
|
+
raise ValueError("missing share arg")
|
|
124
|
+
return share_object(share), relation
|
|
125
|
+
return f"{CMD_OBJECT_PREFIX}:{cmd}", RELATION_EXECUTE
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def encode_cap(operation):
|
|
129
|
+
"""Return the base64url ``cap`` header value for a signed *operation*."""
|
|
130
|
+
return base64.urlsafe_b64encode(operation.to_payload()).decode("ascii")
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def decode_cap(value):
|
|
134
|
+
"""Unpack a ``cap`` header value back into an Operation.
|
|
135
|
+
|
|
136
|
+
SPEC.md §3.1 allows ``base64url-or-hex`` tokens; hex is what the
|
|
137
|
+
dacar CLI's ``grant --no-apply -o`` emits, so its grants can ride
|
|
138
|
+
RRCP jobs unmodified. Whitespace around the token is tolerated.
|
|
139
|
+
"""
|
|
140
|
+
if not value:
|
|
141
|
+
raise AuthorizationError("missing cap token")
|
|
142
|
+
value = value.strip()
|
|
143
|
+
try:
|
|
144
|
+
payload = base64.urlsafe_b64decode(value.encode("ascii"))
|
|
145
|
+
return Operation.from_payload(payload)
|
|
146
|
+
except (binascii.Error, ValueError, UnicodeEncodeError):
|
|
147
|
+
pass # fall through to the hex form
|
|
148
|
+
try:
|
|
149
|
+
payload = bytes.fromhex(value)
|
|
150
|
+
return Operation.from_payload(payload)
|
|
151
|
+
except ValueError as exc:
|
|
152
|
+
raise AuthorizationError("malformed cap token payload") from exc
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
class ShareAuthority:
|
|
156
|
+
"""Issues capability grants for a share on behalf of its owner.
|
|
157
|
+
|
|
158
|
+
The authority holds the owner's RNS identity: its identity hash is
|
|
159
|
+
the Dacar trust anchor / issuer hash and its Ed25519 half signs the
|
|
160
|
+
grant operations. The identity is the same file format the dacar CLI
|
|
161
|
+
manages, so owner key material moves between the tools unchanged.
|
|
162
|
+
Receivers pin the owner's identity hash and Ed25519 public key plus
|
|
163
|
+
the namespace salt out-of-band.
|
|
164
|
+
"""
|
|
165
|
+
|
|
166
|
+
def __init__(self, share, owner_identity, salt=None, clock=None):
|
|
167
|
+
self.share = share
|
|
168
|
+
self.object_id = share_object(share)
|
|
169
|
+
self.owner_identity = owner_identity
|
|
170
|
+
self.owner_key = owner_identity.sig_prv
|
|
171
|
+
self.owner_hash = owner_hash_from_public_key(owner_identity.get_public_key())
|
|
172
|
+
self.salt = bytes(salt) if salt is not None else bytes(SALT_SIZE)
|
|
173
|
+
self.hasher = NamespaceHasher(self.salt)
|
|
174
|
+
self.clock = clock if clock is not None else Clock()
|
|
175
|
+
|
|
176
|
+
def mint(self, relation, grantee_hash):
|
|
177
|
+
"""Mint a signed GRANT operation on this share, base64url-encoded."""
|
|
178
|
+
return self.mint_grant(self.object_id, relation, grantee_hash)
|
|
179
|
+
|
|
180
|
+
def mint_command(self, cmd, grantee_hash):
|
|
181
|
+
"""Mint an ``execute`` grant for a remote-execution command."""
|
|
182
|
+
return self.mint_grant(f"{CMD_OBJECT_PREFIX}:{cmd}", RELATION_EXECUTE, grantee_hash)
|
|
183
|
+
|
|
184
|
+
def mint_grant(self, object_id, relation, grantee_hash):
|
|
185
|
+
"""Mint a signed GRANT operation on an arbitrary object."""
|
|
186
|
+
return self._issue(object_id, relation, grantee_hash, Action.GRANT)
|
|
187
|
+
|
|
188
|
+
def revoke(self, relation, grantee_hash):
|
|
189
|
+
"""Mint a signed REVOKE operation, base64url-encoded."""
|
|
190
|
+
return self._issue(self.object_id, relation, grantee_hash, Action.REVOKE)
|
|
191
|
+
|
|
192
|
+
def _issue(self, object_id, relation, grantee_hash, action):
|
|
193
|
+
tuple_obj = Tuple.from_plaintext(
|
|
194
|
+
object_id=object_id,
|
|
195
|
+
relation=relation,
|
|
196
|
+
grantee=grantee_hash,
|
|
197
|
+
issuer=self.owner_hash,
|
|
198
|
+
hasher=self.hasher,
|
|
199
|
+
)
|
|
200
|
+
operation = Operation(
|
|
201
|
+
tuple=tuple_obj,
|
|
202
|
+
action=action,
|
|
203
|
+
hlc=self.clock.now(),
|
|
204
|
+
)
|
|
205
|
+
signed = operation.sign(self.owner_key)
|
|
206
|
+
return encode_cap(signed)
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
class ProvisionedCaps:
|
|
210
|
+
"""Pre-minted capability tokens for a node that does not hold the
|
|
211
|
+
owner key — the finer-grained distribution model.
|
|
212
|
+
|
|
213
|
+
Tokens are signed GRANT operations minted out-of-band, e.g. by the
|
|
214
|
+
dacar CLI (``dacar grant <peer> write rrcp:sync:notes --no-apply``)
|
|
215
|
+
or an owner's :class:`ShareAuthority`, and labeled by the relation
|
|
216
|
+
they grant. On load, share-relation tokens are verified against the
|
|
217
|
+
pinned owner key and checked to actually grant this node's identity
|
|
218
|
+
on the share, so a mistyped or stale token fails fast at startup
|
|
219
|
+
instead of producing jobs that every receiver rejects. Execute
|
|
220
|
+
tokens are signature-verified only — the receiving side enforces the
|
|
221
|
+
command object.
|
|
222
|
+
"""
|
|
223
|
+
|
|
224
|
+
def __init__(self, owner_hash, salt, owner_public_key, identity_hash, share=None, caps=None):
|
|
225
|
+
self.owner_hash = owner_hash
|
|
226
|
+
self._tokens = dict(caps or {})
|
|
227
|
+
if share is not None:
|
|
228
|
+
policy = SharePolicy(owner_hash, salt=salt, owner_public_key=owner_public_key)
|
|
229
|
+
for relation in (RELATION_READ, RELATION_WRITE):
|
|
230
|
+
token = self._tokens.get(relation)
|
|
231
|
+
if token is None:
|
|
232
|
+
continue
|
|
233
|
+
try:
|
|
234
|
+
policy.authorize_object(share_object(share), relation, token, identity_hash)
|
|
235
|
+
except AuthorizationError as exc:
|
|
236
|
+
raise ValueError(
|
|
237
|
+
f"provisioned {relation} cap does not grant this node on {share}: {exc}"
|
|
238
|
+
) from exc
|
|
239
|
+
|
|
240
|
+
def mint(self, relation, grantee_hash):
|
|
241
|
+
"""Return the provisioned token for *relation* (interface twin of
|
|
242
|
+
:meth:`ShareAuthority.mint`)."""
|
|
243
|
+
return self._grant(relation)
|
|
244
|
+
|
|
245
|
+
def mint_grant(self, object_id, relation, grantee_hash):
|
|
246
|
+
"""Return the provisioned token for *relation* (interface twin of
|
|
247
|
+
:meth:`ShareAuthority.mint_grant`)."""
|
|
248
|
+
return self._grant(relation)
|
|
249
|
+
|
|
250
|
+
def mint_command(self, cmd, grantee_hash):
|
|
251
|
+
"""Return the provisioned ``execute`` token (interface twin of
|
|
252
|
+
:meth:`ShareAuthority.mint_command`)."""
|
|
253
|
+
return self._grant(RELATION_EXECUTE)
|
|
254
|
+
|
|
255
|
+
def _grant(self, relation):
|
|
256
|
+
try:
|
|
257
|
+
return self._tokens[relation]
|
|
258
|
+
except KeyError:
|
|
259
|
+
raise KeyError(f"no provisioned {relation} cap for this node") from None
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
def parse_caps(values):
|
|
263
|
+
"""Parse ``--cap`` provisions into unlabeled and share-labeled maps.
|
|
264
|
+
|
|
265
|
+
``write=TOKEN`` labels only the relation; ``notes/write=TOKEN`` also
|
|
266
|
+
names the share it grants (share names are slugs, so the ``/``
|
|
267
|
+
disambiguates). Unlabeled provisions are only valid when exactly one
|
|
268
|
+
share is mounted. Recognized relations are the share relations
|
|
269
|
+
(``read``, ``write``) and ``execute`` for remote-execution commands.
|
|
270
|
+
|
|
271
|
+
Returns ``(unlabeled, labeled)``: *unlabeled* maps relation→token,
|
|
272
|
+
*labeled* maps share→{relation: token}.
|
|
273
|
+
"""
|
|
274
|
+
unlabeled, labeled = {}, {}
|
|
275
|
+
for value in values or ():
|
|
276
|
+
label, sep, token = value.partition("=")
|
|
277
|
+
if not sep or not label or not token:
|
|
278
|
+
raise ValueError(f"malformed cap provision: {value!r}")
|
|
279
|
+
share = None
|
|
280
|
+
relation = label
|
|
281
|
+
if "/" in label:
|
|
282
|
+
share, _, relation = label.partition("/")
|
|
283
|
+
if not share:
|
|
284
|
+
raise ValueError(f"malformed cap provision: {value!r}")
|
|
285
|
+
if relation not in (RELATION_READ, RELATION_WRITE, RELATION_EXECUTE):
|
|
286
|
+
raise ValueError(f"unknown relation in cap provision: {relation!r}")
|
|
287
|
+
if any(ch.isspace() for ch in token):
|
|
288
|
+
# cap tokens (base64url or hex) never contain whitespace; a
|
|
289
|
+
# spaced value means tokens were pasted together
|
|
290
|
+
raise ValueError(f"cap token for {label!r} contains whitespace")
|
|
291
|
+
target = unlabeled if share is None else labeled.setdefault(share, {})
|
|
292
|
+
if relation in target:
|
|
293
|
+
raise ValueError(f"duplicate cap provision for {label!r}")
|
|
294
|
+
target[relation] = token
|
|
295
|
+
return unlabeled, labeled
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
class SharePolicy:
|
|
299
|
+
"""Receiver-side replicated policy for shares under one namespace salt.
|
|
300
|
+
|
|
301
|
+
``verify_cap`` applies verified capability operations into the local
|
|
302
|
+
CRDT state and then evaluates the request against it, so grants issued
|
|
303
|
+
while this node was offline still authorize — and revocations with a
|
|
304
|
+
newer HLC win over replayed grants (LWW-Element-Set semantics).
|
|
305
|
+
"""
|
|
306
|
+
|
|
307
|
+
def __init__(self, owner_hash, salt=None, owner_public_key=None):
|
|
308
|
+
"""*owner_hash* is one 16-byte anchor or a sequence of them; when a
|
|
309
|
+
sequence, *owner_public_key* is either a parallel sequence of
|
|
310
|
+
32-byte Ed25519 keys or a mapping ``{anchor_hash: key}``. An empty
|
|
311
|
+
anchor set builds a fail-closed policy: every authorize fails
|
|
312
|
+
until the receiver is provisioned (§9.4)."""
|
|
313
|
+
if isinstance(owner_hash, (bytes, bytearray)):
|
|
314
|
+
anchors = [bytes(owner_hash)]
|
|
315
|
+
else:
|
|
316
|
+
anchors = [bytes(anchor) for anchor in (owner_hash or [])]
|
|
317
|
+
self.anchors = tuple(anchors)
|
|
318
|
+
self.owner_hash = anchors[0] if anchors else None
|
|
319
|
+
self.salt = bytes(salt) if salt is not None else bytes(SALT_SIZE)
|
|
320
|
+
self.state = StateVector()
|
|
321
|
+
self.keyring = Keyring()
|
|
322
|
+
if owner_public_key is not None:
|
|
323
|
+
if isinstance(owner_public_key, dict):
|
|
324
|
+
for anchor, key in owner_public_key.items():
|
|
325
|
+
self.keyring.register_single(bytes(anchor), bytes(key))
|
|
326
|
+
elif isinstance(owner_public_key, (bytes, bytearray)):
|
|
327
|
+
if len(anchors) != 1:
|
|
328
|
+
raise ValueError(
|
|
329
|
+
"a single public key anchors exactly one identity"
|
|
330
|
+
)
|
|
331
|
+
self.keyring.register_single(anchors[0], bytes(owner_public_key))
|
|
332
|
+
else:
|
|
333
|
+
if len(owner_public_key) != len(anchors):
|
|
334
|
+
raise ValueError(
|
|
335
|
+
"one public key per anchored identity is required"
|
|
336
|
+
)
|
|
337
|
+
for anchor, key in zip(anchors, owner_public_key):
|
|
338
|
+
self.keyring.register_single(anchor, bytes(key))
|
|
339
|
+
self.engine = None
|
|
340
|
+
if self.anchors:
|
|
341
|
+
self.engine = Engine(
|
|
342
|
+
Config(
|
|
343
|
+
root_trust_anchors=frozenset(self.anchors),
|
|
344
|
+
primary_salt=self.salt,
|
|
345
|
+
),
|
|
346
|
+
self.state,
|
|
347
|
+
)
|
|
348
|
+
|
|
349
|
+
def apply_operation(self, operation):
|
|
350
|
+
"""Verify-on-ingest and apply one Dacar §5.3 operation into state.
|
|
351
|
+
|
|
352
|
+
The ingest path for policy deltas carried by the §9 ``cmd=lxm``
|
|
353
|
+
handler (and any other channel that delivers raw operation
|
|
354
|
+
payloads): the signature must verify against the keyring before
|
|
355
|
+
the CRDT state may mutate. Raises :class:`AuthorizationError` —
|
|
356
|
+
callers drop the delta, never silently accept it.
|
|
357
|
+
"""
|
|
358
|
+
if self.engine is None:
|
|
359
|
+
# §9.4: a receiver with no trust anchors fails closed —
|
|
360
|
+
# every Delta ingest is rejected until provisioned
|
|
361
|
+
raise AuthorizationError("no trust anchors — fail closed")
|
|
362
|
+
if not verify_operation(operation, self.keyring):
|
|
363
|
+
raise AuthorizationError("policy operation signature verification failed")
|
|
364
|
+
self.state.apply(operation)
|
|
365
|
+
|
|
366
|
+
def apply_policy(self, cap_value):
|
|
367
|
+
"""Verify and apply a policy update (e.g. a REVOKE) into local state.
|
|
368
|
+
|
|
369
|
+
Policy updates travel over any available channel — synced Dacar
|
|
370
|
+
state, a grade-0 admin envelope, or sneakernet — never as a job's
|
|
371
|
+
own capability.
|
|
372
|
+
"""
|
|
373
|
+
self.apply_operation(decode_cap(cap_value))
|
|
374
|
+
|
|
375
|
+
def authorize(self, cmd, cap_value, sender_hash, share):
|
|
376
|
+
"""Authorize a share-scoped *cmd* (convenience over authorize_object)."""
|
|
377
|
+
relation = required_relation(cmd)
|
|
378
|
+
if relation is None:
|
|
379
|
+
return
|
|
380
|
+
self.authorize_object(share_object(share), relation, cap_value, sender_hash)
|
|
381
|
+
|
|
382
|
+
def authorize_object(self, object_id, relation, cap_value, sender_hash):
|
|
383
|
+
"""Authorize *relation* on *object_id* for *sender_hash*.
|
|
384
|
+
|
|
385
|
+
Raises :class:`AuthorizationError` when the cap token does not
|
|
386
|
+
prove the grant.
|
|
387
|
+
"""
|
|
388
|
+
if self.engine is None:
|
|
389
|
+
# §9.4: a receiver with no trust anchors fails closed
|
|
390
|
+
raise AuthorizationError("no trust anchors — fail closed")
|
|
391
|
+
operation = decode_cap(cap_value)
|
|
392
|
+
if operation.action is not Action.GRANT:
|
|
393
|
+
raise AuthorizationError("cap token is not a GRANT operation")
|
|
394
|
+
if not verify_operation(operation, self.keyring):
|
|
395
|
+
raise AuthorizationError("cap token signature verification failed")
|
|
396
|
+
self.state.apply(operation)
|
|
397
|
+
if not self.engine.evaluate(object_id, relation, sender_hash):
|
|
398
|
+
raise AuthorizationError(
|
|
399
|
+
f"{sender_hash.hex()} lacks {relation} on {object_id}"
|
|
400
|
+
)
|