Search in sources :

Example 11 with PrivacyGroup

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

the class PrivacyGroupManagerImplTest method testCreateLegacyPrivacyGroup.

@Test
public void testCreateLegacyPrivacyGroup() {
    final List<PublicKey> members = List.of(mock(PublicKey.class), mock(PublicKey.class));
    when(privacyGroupUtil.generateId(anyList())).thenReturn("generatedId".getBytes());
    when(privacyGroupUtil.generateLookupId(anyList())).thenReturn("lookup".getBytes());
    when(privacyGroupUtil.encode(any())).thenReturn("encoded".getBytes());
    when(privacyGroupDAO.retrieve("generatedId".getBytes())).thenReturn(Optional.empty());
    final PrivacyGroup privacyGroup = privacyGroupManager.createLegacyPrivacyGroup(localKey, members);
    // Verify entity being saved has the correct values
    ArgumentCaptor<PrivacyGroupEntity> argCaptor = ArgumentCaptor.forClass(PrivacyGroupEntity.class);
    verify(privacyGroupDAO).retrieveOrSave(argCaptor.capture());
    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 generated privacy group has the correct values
    assertThat(privacyGroup).isNotNull();
    assertThat(privacyGroup.getId().getBytes()).isEqualTo("generatedId".getBytes());
    assertThat(privacyGroup.getName()).isEqualTo("legacy");
    assertThat(privacyGroup.getDescription()).isEqualTo("Privacy groups to support the creation of groups by privateFor and privateFrom");
    assertThat(privacyGroup.getMembers()).containsAll(members).contains(localKey);
    assertThat(privacyGroup.getType()).isEqualTo(PrivacyGroup.Type.LEGACY);
    assertThat(privacyGroup.getState()).isEqualTo(PrivacyGroup.State.ACTIVE);
    verify(privacyGroupDAO).retrieveOrSave(any());
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) PrivacyGroupEntity(com.quorum.tessera.data.PrivacyGroupEntity) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup) Test(org.junit.Test)

Example 12 with PrivacyGroup

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

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

the class ResidentGroupHandlerImplTest method keyExistedInADifferentGroupInDb.

@Test
public void keyExistedInADifferentGroupInDb() {
    ResidentGroup rg1 = new ResidentGroup();
    rg1.setMembers(List.of(PublicKey.from("m1".getBytes()).encodeToBase64()));
    rg1.setName("rg1");
    ResidentGroup rg2 = new ResidentGroup();
    rg2.setMembers(List.of(PublicKey.from("m2".getBytes()).encodeToBase64()));
    rg2.setName("rg2");
    when(privacyGroupManager.getManagedKeys()).thenReturn(Set.of(PublicKey.from("m1".getBytes()), PublicKey.from("m2".getBytes())));
    Config config = mock(Config.class);
    when(config.getResidentGroups()).thenReturn(List.of(rg1, rg2));
    final PrivacyGroup existedGroup = mock(PrivacyGroup.class);
    when(existedGroup.getMembers()).thenReturn(List.of(PublicKey.from("m2".getBytes())));
    when(existedGroup.getId()).thenReturn(PrivacyGroup.Id.fromBytes("otherGroup".getBytes()));
    when(privacyGroupManager.findPrivacyGroupByType(eq(PrivacyGroup.Type.RESIDENT))).thenReturn(List.of(existedGroup));
    assertThatThrownBy(() -> residentGroupHandler.onCreate(config)).isInstanceOf(PrivacyViolationException.class).hasMessageContaining("Key cannot belong to more than one 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 14 with PrivacyGroup

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

the class ResidentGroupHandlerImplTest method keysCanNotBeMovedOutOfAGroup.

@Test
public void keysCanNotBeMovedOutOfAGroup() {
    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));
    ResidentGroup rg2 = new ResidentGroup();
    rg2.setMembers(List.of(PublicKey.from("m1".getBytes()).encodeToBase64(), PublicKey.from("m2".getBytes()).encodeToBase64()));
    rg2.setName("rg2");
    Config config = mock(Config.class);
    when(config.getResidentGroups()).thenReturn(List.of(rg2));
    when(privacyGroupManager.getManagedKeys()).thenReturn(Set.of(PublicKey.from("m1".getBytes()), PublicKey.from("m2".getBytes())));
    assertThatThrownBy(() -> residentGroupHandler.onCreate(config)).isInstanceOf(PrivacyViolationException.class).hasMessageContaining("Key cannot belong to more than one 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 15 with PrivacyGroup

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

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