Search in sources :

Example 31 with Config

use of com.quorum.tessera.config.Config 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 32 with Config

use of com.quorum.tessera.config.Config 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 33 with Config

use of com.quorum.tessera.config.Config in project tessera by ConsenSys.

the class PeerToPeerIT method benevolentNodeBecomesPosessedAndSendsInvalidUrlInRecipientList.

/*
  A good node with valid key has a bad recipient in its party info.
  The key is valid (node C's key) but there is a validation failure as
  the url cannot be called.
   */
@Test
public void benevolentNodeBecomesPosessedAndSendsInvalidUrlInRecipientList() throws Exception {
    Party partyB = partyHelper.findByAlias(NodeAlias.B);
    ServerConfig serverConfig = Optional.of(partyB.getConfig()).map(Config::getP2PServerConfig).get();
    PublicKey publicKey = Optional.of(partyB).map(Party::getPublicKey).map(Base64.getDecoder()::decode).map(PublicKey::from).get();
    Recipient itself = Recipient.of(publicKey, serverConfig.getServerUri().toString());
    String validKeyFromOtherNode = partyHelper.findByAlias(NodeAlias.C).getPublicKey();
    PublicKey validButIncorrectKey = Optional.of(validKeyFromOtherNode).map(Base64.getDecoder()::decode).map(PublicKey::from).get();
    Recipient badRecipient = Recipient.of(validButIncorrectKey, "http://bogus.supersnide.com:8829");
    Set<Recipient> recipients = Stream.of(itself, badRecipient).collect(Collectors.toSet());
    assertThat(recipients).containsExactlyInAnyOrder(itself, badRecipient);
    PartyInfo partyInfo = new PartyInfo(serverConfig.getServerUri().toString(), recipients, Collections.emptySet());
    Client client = new ClientFactory().buildFrom(serverConfig);
    PartyInfoParser partyInfoParser = PartyInfoParser.create();
    byte[] data = partyInfoParser.to(partyInfo);
    StreamingOutput output = out -> out.write(data);
    Response response = client.target(partyA.getP2PUri()).path("partyinfo").request().post(Entity.entity(output, MediaType.APPLICATION_OCTET_STREAM));
    assertThat(response.getStatus()).isEqualTo(200);
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) java.util(java.util) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ServerConfig(com.quorum.tessera.config.ServerConfig) Response(jakarta.ws.rs.core.Response) After(org.junit.After) JsonObject(jakarta.json.JsonObject) StreamingOutput(jakarta.ws.rs.core.StreamingOutput) Before(org.junit.Before) PartyInfoParser(com.quorum.tessera.p2p.partyinfo.PartyInfoParser) ConfigKeyPair(com.quorum.tessera.config.keypairs.ConfigKeyPair) Client(jakarta.ws.rs.client.Client) EncryptorConfig(com.quorum.tessera.config.EncryptorConfig) KeyEncryptorFactory(com.quorum.tessera.config.keys.KeyEncryptorFactory) KeyEncryptor(com.quorum.tessera.config.keys.KeyEncryptor) ClientFactory(com.quorum.tessera.jaxrs.client.ClientFactory) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Entity(jakarta.ws.rs.client.Entity) KeyDataUtil(com.quorum.tessera.config.util.KeyDataUtil) PartyInfo(com.quorum.tessera.partyinfo.model.PartyInfo) Stream(java.util.stream.Stream) MediaType(jakarta.ws.rs.core.MediaType) Recipient(com.quorum.tessera.partyinfo.model.Recipient) Config(com.quorum.tessera.config.Config) NodeAlias(suite.NodeAlias) PublicKey(com.quorum.tessera.encryption.PublicKey) ClientFactory(com.quorum.tessera.jaxrs.client.ClientFactory) Recipient(com.quorum.tessera.partyinfo.model.Recipient) StreamingOutput(jakarta.ws.rs.core.StreamingOutput) PartyInfoParser(com.quorum.tessera.p2p.partyinfo.PartyInfoParser) PartyInfo(com.quorum.tessera.partyinfo.model.PartyInfo) Response(jakarta.ws.rs.core.Response) ServerConfig(com.quorum.tessera.config.ServerConfig) Client(jakarta.ws.rs.client.Client) Test(org.junit.Test)

Example 34 with Config

use of com.quorum.tessera.config.Config in project tessera by ConsenSys.

the class Main method main.

public static void main(String... args) throws Exception {
    Security.addProvider(new BouncyCastleProvider());
    final CommandLine commandLine = new CommandLine(new EnclaveCliAdapter());
    commandLine.registerConverter(Config.class, new ConfigConverter()).setSeparator(" ").setCaseInsensitiveEnumValuesAllowed(true);
    commandLine.execute(args);
    final CliResult cliResult = commandLine.getExecutionResult();
    if (cliResult == null) {
        System.exit(1);
    }
    if (!cliResult.getConfig().isPresent()) {
        System.exit(cliResult.getStatus());
    }
    final TesseraServerFactory restServerFactory = TesseraServerFactory.create(CommunicationType.REST);
    final Config config = cliResult.getConfig().get();
    ConfigFactory.create().store(config);
    final ServerConfig serverConfig = config.getServerConfigs().stream().findFirst().get();
    Enclave enclave = EnclaveServer.create();
    LOGGER.debug("Created enclave {}", enclave);
    final TesseraServer server = restServerFactory.createServer(serverConfig, Set.of(new EnclaveApplication(enclave)));
    server.start();
    CountDownLatch latch = new CountDownLatch(1);
    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        try {
            server.stop();
        } catch (Exception ex) {
            LOGGER.error(null, ex);
        } finally {
        }
    }));
    latch.await();
}
Also used : ConfigConverter(com.quorum.tessera.cli.parsers.ConfigConverter) TesseraServerFactory(com.quorum.tessera.server.TesseraServerFactory) ServerConfig(com.quorum.tessera.config.ServerConfig) Config(com.quorum.tessera.config.Config) CountDownLatch(java.util.concurrent.CountDownLatch) ServerConfig(com.quorum.tessera.config.ServerConfig) TesseraServer(com.quorum.tessera.server.TesseraServer) CommandLine(picocli.CommandLine) CliResult(com.quorum.tessera.cli.CliResult) EnclaveCliAdapter(com.quorum.tessera.enclave.server.EnclaveCliAdapter) Enclave(com.quorum.tessera.enclave.Enclave) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider)

Example 35 with Config

use of com.quorum.tessera.config.Config in project tessera by ConsenSys.

the class EnclaveClientProviderTest method beforeTest.

@Before
public void beforeTest() {
    configFactory = mock(ConfigFactory.class);
    Config config = mock(Config.class);
    ServerConfig serverConfig = mock(ServerConfig.class);
    when(serverConfig.getApp()).thenReturn(appType);
    when(serverConfig.getServerUri()).thenReturn(URI.create("someEnclaveServerUri"));
    when(config.getServerConfigs()).thenReturn(List.of(serverConfig));
    when(configFactory.getConfig()).thenReturn(config);
}
Also used : ServerConfig(com.quorum.tessera.config.ServerConfig) ServerConfig(com.quorum.tessera.config.ServerConfig) Config(com.quorum.tessera.config.Config) ConfigFactory(com.quorum.tessera.config.ConfigFactory) Before(org.junit.Before)

Aggregations

Config (com.quorum.tessera.config.Config)78 Test (org.junit.Test)54 ServerConfig (com.quorum.tessera.config.ServerConfig)20 ConfigFactory (com.quorum.tessera.config.ConfigFactory)18 Path (java.nio.file.Path)11 Before (org.junit.Before)11 ResidentGroup (com.quorum.tessera.config.ResidentGroup)9 ClientFactory (com.quorum.tessera.jaxrs.client.ClientFactory)9 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)9 Client (jakarta.ws.rs.client.Client)9 JdbcConfig (com.quorum.tessera.config.JdbcConfig)7 Map (java.util.Map)7 Collectors (java.util.stream.Collectors)7 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)7 After (org.junit.After)7 CommandLine (picocli.CommandLine)7 PrivacyViolationException (com.quorum.tessera.transaction.exception.PrivacyViolationException)6 CliResult (com.quorum.tessera.cli.CliResult)5 EncryptorConfig (com.quorum.tessera.config.EncryptorConfig)5 PublicKey (com.quorum.tessera.encryption.PublicKey)5