Search in sources :

Example 16 with RuntimeContext

use of com.quorum.tessera.context.RuntimeContext in project tessera by ConsenSys.

the class RuntimeContextHolderTest method setContextCanOnlyBeStoredOnce.

@Test
public void setContextCanOnlyBeStoredOnce() {
    RuntimeContextHolder contextHolder = RuntimeContextHolder.INSTANCE;
    RuntimeContext runtimeContext = mock(RuntimeContext.class);
    contextHolder.setContext(runtimeContext);
    assertThat(contextHolder.getContext().get()).isSameAs(runtimeContext);
    try {
        contextHolder.setContext(mock(RuntimeContext.class));
        failBecauseExceptionWasNotThrown(IllegalStateException.class);
    } catch (IllegalStateException ex) {
        assertThat(ex).hasMessage("RuntimeContext has already been stored");
    }
}
Also used : RuntimeContext(com.quorum.tessera.context.RuntimeContext) Test(org.junit.Test)

Example 17 with RuntimeContext

use of com.quorum.tessera.context.RuntimeContext in project tessera by ConsenSys.

the class RuntimeContextProviderTest method provides.

@Test
public void provides() {
    Config confg = createMockConfig();
    try (var mockedStaticConfigFactory = mockStatic(ConfigFactory.class);
        var mockStaticRestClientFactory = mockStatic(RestClientFactory.class);
        var mockStaticKeyDataUtil = mockStatic(KeyDataUtil.class);
        var mockStaticEnclave = mockStatic(Enclave.class)) {
        Enclave enclave = mock(Enclave.class);
        mockStaticEnclave.when(Enclave::create).thenReturn(enclave);
        ConfigKeyPair configKeyPair = mock(ConfigKeyPair.class);
        when(configKeyPair.getPublicKey()).thenReturn(Base64.getEncoder().encodeToString("PublicKey".getBytes()));
        when(configKeyPair.getPrivateKey()).thenReturn(Base64.getEncoder().encodeToString("PrivateKey".getBytes()));
        mockStaticKeyDataUtil.when(() -> KeyDataUtil.unmarshal(any(KeyData.class), any(KeyEncryptor.class))).thenReturn(configKeyPair);
        RestClientFactory restClientFactory = mock(RestClientFactory.class);
        when(restClientFactory.buildFrom(any(ServerConfig.class))).thenReturn(mock(Client.class));
        mockStaticRestClientFactory.when(RestClientFactory::create).thenReturn(restClientFactory);
        ConfigFactory configFactory = mock(ConfigFactory.class);
        when(configFactory.getConfig()).thenReturn(confg);
        mockedStaticConfigFactory.when(ConfigFactory::create).thenReturn(configFactory);
        RuntimeContext runtimeContext = RuntimeContextProvider.provider();
        assertThat(runtimeContext).isNotNull().isSameAs(RuntimeContextProvider.provider());
        mockedStaticConfigFactory.verify(ConfigFactory::create);
        mockedStaticConfigFactory.verifyNoMoreInteractions();
        mockStaticRestClientFactory.verify(RestClientFactory::create);
        mockedStaticConfigFactory.verifyNoMoreInteractions();
        mockStaticKeyDataUtil.verify(() -> KeyDataUtil.unmarshal(any(KeyData.class), any(KeyEncryptor.class)));
        mockStaticKeyDataUtil.verifyNoMoreInteractions();
        mockStaticEnclave.verify(Enclave::create);
        mockStaticEnclave.verifyNoMoreInteractions();
        verify(enclave).getPublicKeys();
        verifyNoMoreInteractions(enclave);
    }
}
Also used : Enclave(com.quorum.tessera.enclave.Enclave) KeyEncryptor(com.quorum.tessera.config.keys.KeyEncryptor) RestClientFactory(com.quorum.tessera.context.RestClientFactory) ConfigKeyPair(com.quorum.tessera.config.keypairs.ConfigKeyPair) Client(jakarta.ws.rs.client.Client) RuntimeContext(com.quorum.tessera.context.RuntimeContext) Test(org.junit.Test)

Example 18 with RuntimeContext

use of com.quorum.tessera.context.RuntimeContext in project tessera by ConsenSys.

the class RuntimeContextProvider method provider.

public static RuntimeContext provider() {
    LOGGER.debug("Providing RuntimeContext");
    RuntimeContextHolder contextHolder = RuntimeContextHolder.INSTANCE;
    if (contextHolder.getContext().isPresent()) {
        LOGGER.debug("Found stored RuntimeContext instance");
        return contextHolder.getContext().get();
    }
    Config config = ConfigFactory.create().getConfig();
    EncryptorConfig encryptorConfig = Optional.ofNullable(config.getEncryptor()).orElse(new EncryptorConfig() {

        {
            setType(EncryptorType.NACL);
        }
    });
    KeyEncryptor keyEncryptor = KeyEncryptorFactory.newFactory().create(encryptorConfig);
    final KeyVaultConfigValidations vaultConfigValidation = KeyVaultConfigValidations.create();
    final RuntimeContextBuilder runtimeContextBuilder = RuntimeContextBuilder.create();
    if (Objects.nonNull(config.getKeys())) {
        List<ConfigKeyPair> configKeyPairs = config.getKeys().getKeyData().stream().map(o -> KeyDataUtil.unmarshal(o, keyEncryptor)).collect(Collectors.toList());
        Set<ConstraintViolation<?>> violations = vaultConfigValidation.validate(config.getKeys(), configKeyPairs);
        if (!violations.isEmpty()) {
            LOGGER.debug("Constraint violations {}", violations);
            throw new ConstraintViolationException(violations);
        }
        final Enclave enclave = Enclave.create();
        runtimeContextBuilder.withKeys(enclave.getPublicKeys());
    }
    List<ServerConfig> servers = config.getServerConfigs();
    ServerConfig p2pServerContext = servers.stream().filter(s -> s.getApp() == AppType.P2P).findFirst().orElseThrow(() -> new IllegalStateException("No P2P server configured"));
    Client p2pClient = RestClientFactory.create().buildFrom(p2pServerContext);
    List<PublicKey> alwaysSendTo = Stream.of(config).map(Config::getAlwaysSendTo).filter(Objects::nonNull).flatMap(List::stream).map(Base64.getDecoder()::decode).map(PublicKey::from).collect(Collectors.toList());
    RuntimeContext context = runtimeContextBuilder.withP2pServerUri(config.getP2PServerConfig().getServerUri()).withP2pClient(p2pClient).withKeyEncryptor(keyEncryptor).withDisablePeerDiscovery(config.isDisablePeerDiscovery()).withRemoteKeyValidation(config.getFeatures().isEnableRemoteKeyValidation()).withEnhancedPrivacy(config.getFeatures().isEnablePrivacyEnhancements()).withPeers(config.getPeers().stream().map(Peer::getUrl).map(URI::create).collect(Collectors.toList())).withAlwaysSendTo(alwaysSendTo).withUseWhiteList(config.isUseWhiteList()).withRecoveryMode(config.isRecoveryMode()).withMultiplePrivateStates(config.getFeatures().isEnableMultiplePrivateStates()).withClientMode(config.getClientMode()).build();
    contextHolder.setContext(context);
    return context;
}
Also used : ConstraintViolation(jakarta.validation.ConstraintViolation) ConfigKeyPair(com.quorum.tessera.config.keypairs.ConfigKeyPair) PublicKey(com.quorum.tessera.encryption.PublicKey) Client(jakarta.ws.rs.client.Client) java.util(java.util) Logger(org.slf4j.Logger) KeyEncryptorFactory(com.quorum.tessera.config.keys.KeyEncryptorFactory) LoggerFactory(org.slf4j.LoggerFactory) KeyEncryptor(com.quorum.tessera.config.keys.KeyEncryptor) ConstraintViolationException(jakarta.validation.ConstraintViolationException) Collectors(java.util.stream.Collectors) RestClientFactory(com.quorum.tessera.context.RestClientFactory) KeyDataUtil(com.quorum.tessera.config.util.KeyDataUtil) Stream(java.util.stream.Stream) com.quorum.tessera.config(com.quorum.tessera.config) RuntimeContext(com.quorum.tessera.context.RuntimeContext) Enclave(com.quorum.tessera.enclave.Enclave) URI(java.net.URI) KeyVaultConfigValidations(com.quorum.tessera.context.KeyVaultConfigValidations) KeyVaultConfigValidations(com.quorum.tessera.context.KeyVaultConfigValidations) PublicKey(com.quorum.tessera.encryption.PublicKey) ConfigKeyPair(com.quorum.tessera.config.keypairs.ConfigKeyPair) URI(java.net.URI) Enclave(com.quorum.tessera.enclave.Enclave) KeyEncryptor(com.quorum.tessera.config.keys.KeyEncryptor) ConstraintViolation(jakarta.validation.ConstraintViolation) ConstraintViolationException(jakarta.validation.ConstraintViolationException) Client(jakarta.ws.rs.client.Client) RuntimeContext(com.quorum.tessera.context.RuntimeContext)

Example 19 with RuntimeContext

use of com.quorum.tessera.context.RuntimeContext in project tessera by ConsenSys.

the class DiscoveryHelperTest method buildAllNodeInfosFilteredOutOwn.

@Test
public void buildAllNodeInfosFilteredOutOwn() {
    when(runtimeContext.getP2pServerUri()).thenReturn(URI.create("http://node1.com"));
    final ActiveNode node1 = ActiveNode.Builder.create().withUri(NodeUri.create("http://node1.com")).withKeys(List.of(PublicKey.from("key1".getBytes()))).withSupportedVersions(List.of("v1")).build();
    final ActiveNode node2 = ActiveNode.Builder.create().withUri(NodeUri.create("http://node2.com")).withKeys(List.of(PublicKey.from("key2".getBytes()))).withSupportedVersions(List.of("v2")).build();
    when(networkStore.getActiveNodes()).thenReturn(Stream.of(node1, node2));
    final Set<NodeInfo> nodeInfos = discoveryHelper.buildRemoteNodeInfos();
    assertThat(nodeInfos).hasSize(1);
    Set<ActiveNode> activeNodes = nodeInfos.stream().map(nodeInfo -> ActiveNode.Builder.create().withUri(NodeUri.create(nodeInfo.getUrl())).withKeys(nodeInfo.getRecipients().stream().map(Recipient::getKey).collect(Collectors.toSet())).withSupportedVersions(nodeInfo.supportedApiVersions()).build()).collect(Collectors.toSet());
    assertThat(activeNodes).containsExactlyInAnyOrder(node2);
    verify(networkStore).getActiveNodes();
    verify(runtimeContext).getP2pServerUri();
    mockedRuntimeContext.verify(RuntimeContext::getInstance);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) KeyNotFoundException(com.quorum.tessera.encryption.KeyNotFoundException) PublicKey(com.quorum.tessera.encryption.PublicKey) DiscoveryHelper(com.quorum.tessera.discovery.DiscoveryHelper) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Set(java.util.Set) Test(org.junit.Test) NodeUri(com.quorum.tessera.discovery.NodeUri) Collectors(java.util.stream.Collectors) Recipient(com.quorum.tessera.partyinfo.node.Recipient) Mockito(org.mockito.Mockito) ActiveNode(com.quorum.tessera.discovery.ActiveNode) NetworkStore(com.quorum.tessera.discovery.NetworkStore) List(java.util.List) MockedStatic(org.mockito.MockedStatic) Stream(java.util.stream.Stream) NodeInfo(com.quorum.tessera.partyinfo.node.NodeInfo) After(org.junit.After) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) RuntimeContext(com.quorum.tessera.context.RuntimeContext) Enclave(com.quorum.tessera.enclave.Enclave) URI(java.net.URI) Before(org.junit.Before) NodeInfo(com.quorum.tessera.partyinfo.node.NodeInfo) Recipient(com.quorum.tessera.partyinfo.node.Recipient) ActiveNode(com.quorum.tessera.discovery.ActiveNode) RuntimeContext(com.quorum.tessera.context.RuntimeContext) Test(org.junit.Test)

Example 20 with RuntimeContext

use of com.quorum.tessera.context.RuntimeContext in project tessera by ConsenSys.

the class DiscoveryHelperTest method getCurrentWithNoKeys.

@Test
public void getCurrentWithNoKeys() {
    final URI uri = URI.create("http://somedomain.com");
    when(runtimeContext.getP2pServerUri()).thenReturn(uri);
    final List<PublicKey> keys = List.of();
    final ActiveNode activeNode = ActiveNode.Builder.create().withUri(NodeUri.create(uri)).withKeys(keys).build();
    when(networkStore.getActiveNodes()).thenReturn(Stream.of(activeNode));
    NodeInfo result = discoveryHelper.buildCurrent();
    assertThat(result).isNotNull();
    verify(runtimeContext).getP2pServerUri();
    assertThat(result.getUrl()).isEqualTo("http://somedomain.com/");
    verify(networkStore).getActiveNodes();
    assertThat(result.getRecipients()).isEmpty();
    mockedRuntimeContext.verify(RuntimeContext::getInstance);
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) NodeInfo(com.quorum.tessera.partyinfo.node.NodeInfo) ActiveNode(com.quorum.tessera.discovery.ActiveNode) RuntimeContext(com.quorum.tessera.context.RuntimeContext) URI(java.net.URI) Test(org.junit.Test)

Aggregations

RuntimeContext (com.quorum.tessera.context.RuntimeContext)33 Test (org.junit.Test)20 URI (java.net.URI)14 PublicKey (com.quorum.tessera.encryption.PublicKey)11 ActiveNode (com.quorum.tessera.discovery.ActiveNode)9 NodeUri (com.quorum.tessera.discovery.NodeUri)9 Enclave (com.quorum.tessera.enclave.Enclave)9 Discovery (com.quorum.tessera.discovery.Discovery)6 Set (java.util.Set)6 Stream (java.util.stream.Stream)6 NetworkStore (com.quorum.tessera.discovery.NetworkStore)5 NodeInfo (com.quorum.tessera.partyinfo.node.NodeInfo)5 List (java.util.List)5 Collectors (java.util.stream.Collectors)5 Before (org.junit.Before)5 Response (jakarta.ws.rs.core.Response)4 TransactionManager (com.quorum.tessera.transaction.TransactionManager)3 Client (jakarta.ws.rs.client.Client)3 ConfigKeyPair (com.quorum.tessera.config.keypairs.ConfigKeyPair)2 KeyEncryptor (com.quorum.tessera.config.keys.KeyEncryptor)2