Search in sources :

Example 16 with PrivacyGroup

use of com.quorum.tessera.enclave.PrivacyGroup in project tessera by ConsenSys.

the class PrivacyGroupManagerImpl method saveResidentGroup.

@Override
public PrivacyGroup saveResidentGroup(String name, String description, List<PublicKey> members) {
    final PrivacyGroup privacyGroup = PrivacyGroup.Builder.buildResidentGroup(name, description, members);
    final byte[] lookupId = privacyGroupUtil.generateLookupId(members);
    final byte[] encodedData = privacyGroupUtil.encode(privacyGroup);
    privacyGroupDAO.update(new PrivacyGroupEntity(name.getBytes(), lookupId, encodedData));
    return privacyGroup;
}
Also used : PrivacyGroupEntity(com.quorum.tessera.data.PrivacyGroupEntity) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup)

Example 17 with PrivacyGroup

use of com.quorum.tessera.enclave.PrivacyGroup 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 18 with PrivacyGroup

use of com.quorum.tessera.enclave.PrivacyGroup 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 19 with PrivacyGroup

use of com.quorum.tessera.enclave.PrivacyGroup in project tessera by ConsenSys.

the class PrivacyGroupManagerImplTest method testCreatePrivacyGroup.

@Test
public void testCreatePrivacyGroup() {
    when(privacyGroupUtil.generateId(anyList(), any(byte[].class))).thenReturn("generatedId".getBytes());
    when(privacyGroupUtil.generateLookupId(anyList())).thenReturn("lookup".getBytes());
    when(privacyGroupUtil.encode(any())).thenReturn("encoded".getBytes());
    PublicKey recipient1 = mock(PublicKey.class);
    PublicKey recipient2 = mock(PublicKey.class);
    final List<PublicKey> members = List.of(localKey, recipient1, recipient2);
    doAnswer(invocation -> {
        Callable callable = invocation.getArgument(1);
        callable.call();
        return mock(PrivacyGroupEntity.class);
    }).when(privacyGroupDAO).save(any(), any());
    final PrivacyGroup privacyGroup = privacyGroupManager.createPrivacyGroup("name", "description", localKey, members, new byte[1]);
    // Verify entity being saved has the correct values
    ArgumentCaptor<PrivacyGroupEntity> argCaptor = ArgumentCaptor.forClass(PrivacyGroupEntity.class);
    verify(privacyGroupDAO).save(argCaptor.capture(), any());
    PrivacyGroupEntity savedEntity = argCaptor.getValue();
    assertThat(savedEntity).isNotNull();
    assertThat(savedEntity.getId()).isEqualTo("generatedId".getBytes());
    assertThat(savedEntity.getLookupId()).isEqualTo("lookup".getBytes());
    assertThat(savedEntity.getData()).isEqualTo("encoded".getBytes());
    // Verify payload being distributed has the correct values
    ArgumentCaptor<byte[]> payloadCaptor = ArgumentCaptor.forClass(byte[].class);
    ArgumentCaptor<List<PublicKey>> recipientsCaptor = ArgumentCaptor.forClass(List.class);
    verify(publisher).publishPrivacyGroup(payloadCaptor.capture(), recipientsCaptor.capture());
    assertThat(payloadCaptor.getValue()).isEqualTo("encoded".getBytes());
    assertThat(recipientsCaptor.getValue()).containsExactlyInAnyOrder(recipient1, recipient2);
    // Verify generated privacy group has the correct values
    assertThat(privacyGroup).isNotNull();
    assertThat(privacyGroup.getId().getBytes()).isEqualTo("generatedId".getBytes());
    assertThat(privacyGroup.getName()).isEqualTo("name");
    assertThat(privacyGroup.getDescription()).isEqualTo("description");
    assertThat(privacyGroup.getMembers()).containsAll(members);
    assertThat(privacyGroup.getType()).isEqualTo(PrivacyGroup.Type.PANTHEON);
    assertThat(privacyGroup.getState()).isEqualTo(PrivacyGroup.State.ACTIVE);
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) PrivacyGroupEntity(com.quorum.tessera.data.PrivacyGroupEntity) List(java.util.List) Callable(java.util.concurrent.Callable) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup) Test(org.junit.Test)

Example 20 with PrivacyGroup

use of com.quorum.tessera.enclave.PrivacyGroup in project tessera by ConsenSys.

the class PrivacyGroupManagerImplTest method testDeletePrivacyGroupFromKeyNotBelong.

@Test
public void testDeletePrivacyGroupFromKeyNotBelong() {
    PublicKey from = PublicKey.from("local".getBytes());
    PrivacyGroupEntity retrievedEt = mock(PrivacyGroupEntity.class);
    when(retrievedEt.getData()).thenReturn("data".getBytes());
    PrivacyGroup mockPG = mock(PrivacyGroup.class);
    when(mockPG.getId()).thenReturn(PrivacyGroup.Id.fromBytes("id".getBytes()));
    when(mockPG.getMembers()).thenReturn(List.of(PublicKey.from("r1".getBytes()), PublicKey.from("r2".getBytes())));
    when(mockPG.getState()).thenReturn(PrivacyGroup.State.ACTIVE);
    when(mockPG.getType()).thenReturn(PrivacyGroup.Type.PANTHEON);
    when(privacyGroupDAO.retrieve("id".getBytes())).thenReturn(Optional.of(retrievedEt));
    when(privacyGroupUtil.decode("data".getBytes())).thenReturn(mockPG);
    assertThatThrownBy(() -> privacyGroupManager.deletePrivacyGroup(from, PrivacyGroup.Id.fromBytes("id".getBytes()))).isInstanceOf(PrivacyViolationException.class);
    verify(privacyGroupDAO).retrieve("id".getBytes());
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) PrivacyGroupEntity(com.quorum.tessera.data.PrivacyGroupEntity) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup) Test(org.junit.Test)

Aggregations

PrivacyGroup (com.quorum.tessera.enclave.PrivacyGroup)36 Test (org.junit.Test)22 PublicKey (com.quorum.tessera.encryption.PublicKey)21 PrivacyGroupEntity (com.quorum.tessera.data.PrivacyGroupEntity)17 MessageHash (com.quorum.tessera.data.MessageHash)8 Response (jakarta.ws.rs.core.Response)8 PrivacyViolationException (com.quorum.tessera.transaction.exception.PrivacyViolationException)7 Operation (io.swagger.v3.oas.annotations.Operation)7 ApiResponse (io.swagger.v3.oas.annotations.responses.ApiResponse)7 SendResponse (com.quorum.tessera.api.SendResponse)6 Config (com.quorum.tessera.config.Config)6 SendRequest (com.quorum.tessera.api.SendRequest)5 ResidentGroup (com.quorum.tessera.config.ResidentGroup)5 PrivacyGroupManager (com.quorum.tessera.privacygroup.PrivacyGroupManager)5 java.util (java.util)5 Collectors (java.util.stream.Collectors)5 Stream (java.util.stream.Stream)5 PrivacyValid (com.quorum.tessera.api.constraint.PrivacyValid)4 PrivacyMode (com.quorum.tessera.enclave.PrivacyMode)4 TransactionManager (com.quorum.tessera.transaction.TransactionManager)4