class TestRecord(DatastoreRecord): __test__ = False # For pytest _test = RecordField(bytes) _test_date = RecordField(datetime, encode=lambda val: datetime.isoformat(val).encode(), decode=lambda val: datetime.fromisoformat(val.decode()))
class Workorder(DatastoreRecord): _arrangement_id = RecordField(bytes) _bob_verifying_key = RecordField(UmbralPublicKey, encode=bytes, decode=UmbralPublicKey.from_bytes) _bob_signature = RecordField(Signature, encode=bytes, decode=Signature.from_bytes)
class TreasureMap(DatastoreRecord): # Ideally this is a `policy.collections.TreasureMap`, but it causes a huge # circular import due to `Bob` and `Character` in `policy.collections`. # TODO #2126 _treasure_map = RecordField(bytes) _expiration = RecordField( MayaDT, encode=lambda maya_date: maya_date.iso8601().encode(), decode=lambda maya_bytes: MayaDT.from_iso8601(maya_bytes.decode()))
class PolicyArrangement(DatastoreRecord): _arrangement_id = RecordField(bytes) _expiration = RecordField(MayaDT, encode=lambda maya_date: maya_date.iso8601().encode(), decode=lambda maya_bytes: MayaDT.from_iso8601(maya_bytes.decode())) _kfrag = RecordField(KFrag, encode=lambda kfrag: kfrag.to_bytes(), decode=KFrag.from_bytes) _alice_verifying_key = RecordField(UmbralPublicKey, encode=bytes, decode=UmbralPublicKey.from_bytes)
def test_alice_verifies_ursula_just_in_time(fleet_of_highperf_mocked_ursulas, highperf_mocked_alice, highperf_mocked_bob): # Patch the Datastore PolicyArrangement model with the highperf # NotAPublicKey not_public_key_record_field = RecordField(NotAPublicKey, encode=bytes, decode=NotAPublicKey.from_bytes) _umbral_pubkey_from_bytes = UmbralPublicKey.from_bytes def actual_random_key_instead(*args, **kwargs): _previous_bytes = args[0] serial = _previous_bytes[-5:] pubkey = NotAPublicKey(serial=serial) return pubkey def mock_set_policy(id_as_hex): return "" def mock_receive_treasure_map(): return Response(bytes(), status=201) with NotARestApp.replace_route("receive_treasure_map", mock_receive_treasure_map): with NotARestApp.replace_route("set_policy", mock_set_policy): with patch('umbral.keys.UmbralPublicKey.__eq__', lambda *args, **kwargs: True): with patch('umbral.keys.UmbralPublicKey.from_bytes', new=actual_random_key_instead): with patch( "nucypher.datastore.models.PolicyArrangement._alice_verifying_key", new=not_public_key_record_field): with mock_cert_loading, mock_metadata_validation, mock_message_verification: with mock_secret_source(): policy = highperf_mocked_alice.grant( highperf_mocked_bob, b"any label", m=20, n=30, expiration=maya.when('next week'), publish_treasure_map=False) # TODO: Make some assertions about policy. total_verified = sum(node.verified_node for node in highperf_mocked_alice.known_nodes) # Alice may be able to verify more than `n`, but certainly not less, # otherwise `grant()` would fail. assert total_verified >= 30 _POLICY_PRESERVER.append(policy)
class NoRecord(DatastoreRecord): _nothing = RecordField(bytes)
class BarRecord(DatastoreRecord): _foo = RecordField(bytes) _bar = RecordField(bytes)
class FooRecord(DatastoreRecord): _foo = RecordField(bytes)
class ReencryptionRequest(DatastoreRecord): _bob_verifying_key = RecordField(PublicKey, encode=bytes, decode=PublicKey.from_bytes)