Search in sources :

Example 26 with PrivacyGroup

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

the class PrivacyGroupManagerImplTest method testStoreUpdatedPrivacyGroup.

@Test
public void testStoreUpdatedPrivacyGroup() {
    final PrivacyGroup mockPrivacyGroup = mock(PrivacyGroup.class);
    when(mockPrivacyGroup.getId()).thenReturn(PrivacyGroup.Id.fromBytes("id".getBytes()));
    when(mockPrivacyGroup.getState()).thenReturn(PrivacyGroup.State.DELETED);
    final byte[] encoded = "encoded".getBytes();
    when(privacyGroupUtil.decode(encoded)).thenReturn(mockPrivacyGroup);
    when(privacyGroupUtil.generateLookupId(anyList())).thenReturn("lookup".getBytes());
    PrivacyGroupEntity existing = new PrivacyGroupEntity("id".getBytes(), "lookup".getBytes(), "old".getBytes());
    when(privacyGroupDAO.retrieve("id".getBytes())).thenReturn(Optional.of(existing));
    privacyGroupManager.storePrivacyGroup(encoded);
    ArgumentCaptor<PrivacyGroupEntity> argCaptor = ArgumentCaptor.forClass(PrivacyGroupEntity.class);
    verify(privacyGroupDAO).retrieve("id".getBytes());
    verify(privacyGroupDAO).update(argCaptor.capture());
    final PrivacyGroupEntity saved = argCaptor.getValue();
    assertThat(saved).isNotNull();
    assertThat(saved.getId()).isEqualTo("id".getBytes());
    assertThat(saved.getLookupId()).isEqualTo("lookup".getBytes());
    assertThat(saved.getData()).isEqualTo("encoded".getBytes());
}
Also used : PrivacyGroupEntity(com.quorum.tessera.data.PrivacyGroupEntity) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup) Test(org.junit.Test)

Example 27 with PrivacyGroup

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

the class ResidentGroupHandlerImplTest method homelessKeysNotAllowedConsideringBothConfigAndDb.

@Test
public void homelessKeysNotAllowedConsideringBothConfigAndDb() {
    ResidentGroup rg1 = new ResidentGroup();
    rg1.setMembers(List.of(PublicKey.from("m1".getBytes()).encodeToBase64()));
    rg1.setName("rg1");
    when(privacyGroupManager.getManagedKeys()).thenReturn(Set.of(PublicKey.from("m1".getBytes()), PublicKey.from("m2".getBytes()), PublicKey.from("m3".getBytes())));
    Config config = mock(Config.class);
    when(config.getResidentGroups()).thenReturn(List.of(rg1));
    final PrivacyGroup pg = PrivacyGroup.Builder.buildResidentGroup("private", "", List.of(PublicKey.from("m2".getBytes())));
    when(privacyGroupManager.findPrivacyGroupByType(eq(PrivacyGroup.Type.RESIDENT))).thenReturn(List.of(pg));
    assertThatThrownBy(() -> residentGroupHandler.onCreate(config)).isInstanceOf(PrivacyViolationException.class).hasMessageContaining("PublicKey[bTM=] must belong to a resident group");
    verify(privacyGroupManager).findPrivacyGroupByType(eq(PrivacyGroup.Type.RESIDENT));
    verify(privacyGroupManager).getManagedKeys();
}
Also used : ResidentGroup(com.quorum.tessera.config.ResidentGroup) Config(com.quorum.tessera.config.Config) PrivacyViolationException(com.quorum.tessera.transaction.exception.PrivacyViolationException) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup) Test(org.junit.Test)

Example 28 with PrivacyGroup

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

the class ResidentGroupHandlerImplTest method keyGetsAddedToAnExistingGroup.

@Test
public void keyGetsAddedToAnExistingGroup() {
    ResidentGroup rg1 = new ResidentGroup();
    rg1.setMembers(List.of(PublicKey.from("m1".getBytes()).encodeToBase64(), PublicKey.from("m2".getBytes()).encodeToBase64()));
    rg1.setName("rg1");
    Config config = mock(Config.class);
    when(config.getResidentGroups()).thenReturn(List.of(rg1));
    when(privacyGroupManager.getManagedKeys()).thenReturn(Set.of(PublicKey.from("m1".getBytes()), PublicKey.from("m2".getBytes())));
    final PrivacyGroup existedGroup = mock(PrivacyGroup.class);
    when(existedGroup.getMembers()).thenReturn(List.of(PublicKey.from("m1".getBytes())));
    when(existedGroup.getId()).thenReturn(PrivacyGroup.Id.fromBytes("rg1".getBytes()));
    when(privacyGroupManager.findPrivacyGroupByType(eq(PrivacyGroup.Type.RESIDENT))).thenReturn(List.of(existedGroup));
    residentGroupHandler.onCreate(config);
    verify(privacyGroupManager).findPrivacyGroupByType(eq(PrivacyGroup.Type.RESIDENT));
    verify(privacyGroupManager).getManagedKeys();
    verify(privacyGroupManager).saveResidentGroup(eq("rg1"), any(), memberCaptor.capture());
    assertThat(memberCaptor.getValue()).containsExactlyInAnyOrder(PublicKey.from("m1".getBytes()), PublicKey.from("m2".getBytes()));
}
Also used : ResidentGroup(com.quorum.tessera.config.ResidentGroup) Config(com.quorum.tessera.config.Config) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup) Test(org.junit.Test)

Example 29 with PrivacyGroup

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

the class PrivacyGroupManagerImpl method storePrivacyGroup.

@Override
public void storePrivacyGroup(byte[] encodedData) {
    final PrivacyGroup privacyGroup = privacyGroupUtil.decode(encodedData);
    if (privacyGroup.getState() == PrivacyGroup.State.DELETED) {
        privacyGroupDAO.retrieve(privacyGroup.getId().getBytes()).ifPresent(et -> {
            et.setData(encodedData);
            privacyGroupDAO.update(et);
        });
        return;
    }
    final byte[] id = privacyGroup.getId().getBytes();
    final byte[] lookupId = privacyGroupUtil.generateLookupId(privacyGroup.getMembers());
    final PrivacyGroupEntity newEntity = new PrivacyGroupEntity(id, lookupId, encodedData);
    privacyGroupDAO.save(newEntity);
}
Also used : PrivacyGroupEntity(com.quorum.tessera.data.PrivacyGroupEntity) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup)

Example 30 with PrivacyGroup

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

the class PrivacyGroupManagerImpl method createPrivacyGroup.

@Override
public PrivacyGroup createPrivacyGroup(String name, String description, PublicKey from, List<PublicKey> members, byte[] seed) {
    final Set<PublicKey> localKeys = enclave.getPublicKeys();
    if (!members.contains(from)) {
        throw new PrivacyViolationException("The list of members in a privacy group should include self");
    }
    final byte[] groupIdBytes = privacyGroupUtil.generateId(members, seed);
    final PrivacyGroup created = PrivacyGroup.Builder.create().withPrivacyGroupId(groupIdBytes).withName(name).withDescription(description).withMembers(members).withSeed(seed).withType(PrivacyGroup.Type.PANTHEON).withState(PrivacyGroup.State.ACTIVE).build();
    final byte[] lookupId = privacyGroupUtil.generateLookupId(members);
    final byte[] encodedData = privacyGroupUtil.encode(created);
    final List<PublicKey> forwardingMembers = members.stream().filter(Predicate.not(localKeys::contains)).collect(Collectors.toList());
    privacyGroupDAO.save(new PrivacyGroupEntity(groupIdBytes, lookupId, encodedData), () -> {
        publisher.publishPrivacyGroup(encodedData, forwardingMembers);
        return null;
    });
    return created;
}
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)

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