Search in sources :

Example 91 with PublicKey

use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.

the class ResidentGroupHandlerImplTest method noGroupConfigButExistedInDb.

@Test
public void noGroupConfigButExistedInDb() {
    PublicKey localKey1 = mock(PublicKey.class);
    PublicKey localKey2 = mock(PublicKey.class);
    when(privacyGroupManager.getManagedKeys()).thenReturn(Set.of(localKey1, localKey2));
    PrivacyGroup rg = mock(PrivacyGroup.class);
    when(rg.getMembers()).thenReturn(List.of(localKey1, localKey2));
    when(rg.getId()).thenReturn(PrivacyGroup.Id.fromBytes("id".getBytes()));
    when(privacyGroupManager.findPrivacyGroupByType(eq(PrivacyGroup.Type.RESIDENT))).thenReturn(List.of(rg));
    residentGroupHandler.onCreate(mock(Config.class));
    verify(privacyGroupManager).getManagedKeys();
    verify(privacyGroupManager).findPrivacyGroupByType(PrivacyGroup.Type.RESIDENT);
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) Config(com.quorum.tessera.config.Config) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup) Test(org.junit.Test)

Example 92 with PublicKey

use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.

the class PrivacyGroupManagerImpl method createLegacyPrivacyGroup.

@Override
public PrivacyGroup createLegacyPrivacyGroup(PublicKey from, List<PublicKey> recipients) {
    final List<PublicKey> members = new ArrayList<>();
    members.add(from);
    members.addAll(recipients);
    final String name = "legacy";
    final String description = "Privacy groups to support the creation of groups by privateFor and privateFrom";
    final byte[] groupIdBytes = privacyGroupUtil.generateId(members);
    final PrivacyGroup created = PrivacyGroup.Builder.create().withPrivacyGroupId(groupIdBytes).withName(name).withDescription(description).withMembers(members).withType(PrivacyGroup.Type.LEGACY).withState(PrivacyGroup.State.ACTIVE).build();
    final byte[] lookupId = privacyGroupUtil.generateLookupId(members);
    final byte[] encodedData = privacyGroupUtil.encode(created);
    privacyGroupDAO.retrieveOrSave(new PrivacyGroupEntity(groupIdBytes, lookupId, encodedData));
    return created;
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) ArrayList(java.util.ArrayList) PrivacyGroupEntity(com.quorum.tessera.data.PrivacyGroupEntity) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup)

Example 93 with PublicKey

use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.

the class PrivacyGroupManagerImpl method deletePrivacyGroup.

@Override
public PrivacyGroup deletePrivacyGroup(PublicKey from, PrivacyGroup.Id privacyGroupId) {
    final PrivacyGroup retrieved = retrievePrivacyGroup(privacyGroupId);
    if (!retrieved.getMembers().contains(from)) {
        throw new PrivacyViolationException("Sender of request does not belong to this privacy group");
    }
    final PrivacyGroup updated = PrivacyGroup.Builder.create().from(retrieved).withState(PrivacyGroup.State.DELETED).build();
    final byte[] updatedData = privacyGroupUtil.encode(updated);
    final byte[] lookupId = privacyGroupUtil.generateLookupId(updated.getMembers());
    final PrivacyGroupEntity updatedEt = new PrivacyGroupEntity(updated.getId().getBytes(), lookupId, updatedData);
    final Set<PublicKey> localKeys = enclave.getPublicKeys();
    final List<PublicKey> forwardingMembers = updated.getMembers().stream().filter(Predicate.not(localKeys::contains)).collect(Collectors.toList());
    privacyGroupDAO.update(updatedEt, () -> {
        publisher.publishPrivacyGroup(updatedData, forwardingMembers);
        return null;
    });
    return updated;
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) PrivacyViolationException(com.quorum.tessera.transaction.exception.PrivacyViolationException) PrivacyGroupEntity(com.quorum.tessera.data.PrivacyGroupEntity) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup)

Example 94 with PublicKey

use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.

the class ResidentGroupHandlerImpl method onCreate.

@Override
public void onCreate(Config config) {
    final Set<PublicKey> managedKeys = privacyGroupManager.getManagedKeys();
    final List<PrivacyGroup> configuredResidentGroups = Stream.ofNullable(config.getResidentGroups()).flatMap(Collection::stream).map(convertToPrivacyGroup).collect(Collectors.toUnmodifiableList());
    configuredResidentGroups.stream().map(PrivacyGroup::getMembers).flatMap(List::stream).filter(Predicate.not(managedKeys::contains)).findFirst().ifPresent(key -> {
        throw new PrivacyViolationException("Key " + key + " configured in resident groups must be locally managed");
    });
    final List<PrivacyGroup> existing = privacyGroupManager.findPrivacyGroupByType(PrivacyGroup.Type.RESIDENT);
    final List<PrivacyGroup> allResidentGroups = new ArrayList<>(configuredResidentGroups);
    allResidentGroups.addAll(existing);
    final List<PrivacyGroup> merged = allResidentGroups.stream().collect(Collectors.collectingAndThen(Collectors.toMap(PrivacyGroup::getId, Function.identity(), (left, right) -> {
        final List<PublicKey> mergedMembers = Stream.concat(left.getMembers().stream(), right.getMembers().stream()).distinct().collect(Collectors.toUnmodifiableList());
        return PrivacyGroup.Builder.create().from(left).withMembers(mergedMembers).build();
    }), m -> new ArrayList<>(m.values())));
    try {
        merged.stream().flatMap(p -> p.getMembers().stream().distinct().map(m -> Map.entry(m, p.getId()))).distinct().collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    } catch (IllegalStateException ex) {
        throw new PrivacyViolationException("Key cannot belong to more than one resident group." + "Cause: " + ex.getMessage());
    }
    final Set<PublicKey> mergedResidentKeys = merged.stream().map(PrivacyGroup::getMembers).flatMap(List::stream).collect(Collectors.toUnmodifiableSet());
    managedKeys.stream().filter(Predicate.not(mergedResidentKeys::contains)).findAny().ifPresent(key -> {
        throw new PrivacyViolationException(key + " must belong to a resident group");
    });
    final List<PrivacyGroup.Id> configuredGroupId = configuredResidentGroups.stream().map(PrivacyGroup::getId).collect(Collectors.toList());
    merged.stream().filter(pg -> configuredGroupId.contains(pg.getId())).collect(Collectors.toList()).forEach(toPersist -> privacyGroupManager.saveResidentGroup(toPersist.getName(), toPersist.getDescription(), toPersist.getMembers()));
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) java.util(java.util) ResidentGroup(com.quorum.tessera.config.ResidentGroup) Stream(java.util.stream.Stream) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup) Predicate(java.util.function.Predicate) PrivacyViolationException(com.quorum.tessera.transaction.exception.PrivacyViolationException) Config(com.quorum.tessera.config.Config) PrivacyGroupManager(com.quorum.tessera.privacygroup.PrivacyGroupManager) ResidentGroupHandler(com.quorum.tessera.privacygroup.ResidentGroupHandler) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) PublicKey(com.quorum.tessera.encryption.PublicKey) PrivacyViolationException(com.quorum.tessera.transaction.exception.PrivacyViolationException) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup)

Example 95 with PublicKey

use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.

the class ReceiveRequestTest method buildWithTransactionHashAndRecipient.

@Test
public void buildWithTransactionHashAndRecipient() {
    MessageHash messageHash = mock(MessageHash.class);
    PublicKey recipient = mock(PublicKey.class);
    ReceiveRequest result = ReceiveRequest.Builder.create().withTransactionHash(messageHash).withRecipient(recipient).build();
    assertThat(result).isNotNull();
    assertThat(result.getTransactionHash()).isNotNull().isSameAs(messageHash);
    assertThat(result.getRecipient()).containsSame(recipient);
    assertThat(result.isRaw()).isFalse();
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) MessageHash(com.quorum.tessera.data.MessageHash) Test(org.junit.Test)

Aggregations

PublicKey (com.quorum.tessera.encryption.PublicKey)281 Test (org.junit.Test)213 Response (jakarta.ws.rs.core.Response)59 MessageHash (com.quorum.tessera.data.MessageHash)57 EncodedPayload (com.quorum.tessera.enclave.EncodedPayload)48 Collectors (java.util.stream.Collectors)32 PrivacyGroup (com.quorum.tessera.enclave.PrivacyGroup)28 NodeInfo (com.quorum.tessera.partyinfo.node.NodeInfo)25 java.util (java.util)23 SendResponse (com.quorum.tessera.api.SendResponse)21 Nonce (com.quorum.tessera.encryption.Nonce)20 Recipient (com.quorum.tessera.partyinfo.node.Recipient)20 Operation (io.swagger.v3.oas.annotations.Operation)20 ApiResponse (io.swagger.v3.oas.annotations.responses.ApiResponse)20 Stream (java.util.stream.Stream)19 ReceiveResponse (com.quorum.tessera.transaction.ReceiveResponse)18 EncryptedTransaction (com.quorum.tessera.data.EncryptedTransaction)17 PrivacyMode (com.quorum.tessera.enclave.PrivacyMode)17 URI (java.net.URI)17 SendRequest (com.quorum.tessera.api.SendRequest)15