Search in sources :

Example 1 with RuntimeContext

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

the class IPWhitelistFilter method filter.

/**
 * If the filter is disabled, return immediately Otherwise, extract the callers hostname and
 * address, and check it against the whitelist
 *
 * <p>If a problem occurs, then disable the filter
 *
 * <p>If the host is not whitelisted, finish the filter chain here and return an Unauthorized
 * response
 *
 * @param requestContext the context of the current request
 */
@Override
public void filter(final ContainerRequestContext requestContext) {
    RuntimeContext runtimeContext = RuntimeContext.getInstance();
    if (!runtimeContext.isUseWhiteList()) {
        return;
    }
    final Set<String> whitelisted = runtimeContext.getPeers().stream().map(URI::getHost).collect(Collectors.toSet());
    // as sent by curl
    if (whitelisted.contains("localhost") || whitelisted.contains("127.0.0.1")) {
        whitelisted.add("localhost");
        whitelisted.add("127.0.0.1");
        whitelisted.add("0:0:0:0:0:0:0:1");
    }
    final String remoteAddress = httpServletRequest.getRemoteAddr();
    final String remoteHost = httpServletRequest.getRemoteHost();
    final boolean allowed = whitelisted.stream().anyMatch(v -> Arrays.asList(remoteAddress, remoteHost).contains(v));
    if (!allowed) {
        LOGGER.warn("Remote host {} with IP {} failed whitelist validation", remoteHost, remoteAddress);
        requestContext.abortWith(Response.status(Response.Status.UNAUTHORIZED).build());
    }
}
Also used : RuntimeContext(com.quorum.tessera.context.RuntimeContext)

Example 2 with RuntimeContext

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

the class PrivacyGroupResourceTest method testGetGroupsMPSDisabled.

@Test
public void testGetGroupsMPSDisabled() {
    RuntimeContext runtimeContext = mock(RuntimeContext.class);
    when(runtimeContext.isMultiplePrivateStates()).thenReturn(false);
    try (var runtimeContextMockedStatic = mockStatic(RuntimeContext.class)) {
        runtimeContextMockedStatic.when(RuntimeContext::getInstance).thenReturn(runtimeContext);
        final Response response = privacyGroupResource.getPrivacyGroups("resident");
        assertThat(response).isNotNull();
        assertThat(response.getStatus()).isEqualTo(503);
        assertThat(response.getEntity()).isEqualTo("Multiple private state feature is not available on this privacy manager");
        runtimeContextMockedStatic.verify(RuntimeContext::getInstance);
        runtimeContextMockedStatic.verifyNoMoreInteractions();
    }
    verify(runtimeContext).isMultiplePrivateStates();
    verifyNoMoreInteractions(runtimeContext);
}
Also used : Response(jakarta.ws.rs.core.Response) RuntimeContext(com.quorum.tessera.context.RuntimeContext) Test(org.junit.Test)

Example 3 with RuntimeContext

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

the class PrivacyGroupResourceTest method testGetGroups.

@Test
public void testGetGroups() {
    final RuntimeContext runtimeContext = mock(RuntimeContext.class);
    when(runtimeContext.isMultiplePrivateStates()).thenReturn(true);
    when(privacyGroupManager.findPrivacyGroupByType(PrivacyGroup.Type.RESIDENT)).thenReturn(List.of(mockResult));
    try (var runtimeContextMockedStatic = mockStatic(RuntimeContext.class)) {
        runtimeContextMockedStatic.when(RuntimeContext::getInstance).thenReturn(runtimeContext);
        final Response response = privacyGroupResource.getPrivacyGroups("resident");
        assertThat(response).isNotNull();
        assertThat(response.getStatus()).isEqualTo(200);
        PrivacyGroupResponse result = ((PrivacyGroupResponse[]) response.getEntity())[0];
        assertThat(result.getName()).isEqualTo(mockResult.getName());
        assertThat(result.getDescription()).isEqualTo(mockResult.getDescription());
        assertThat(result.getPrivacyGroupId()).isEqualTo(mockResult.getId().getBase64());
        assertThat(result.getType()).isEqualTo(mockResult.getType().name());
        runtimeContextMockedStatic.verify(RuntimeContext::getInstance);
        runtimeContextMockedStatic.verifyNoMoreInteractions();
    }
    verify(runtimeContext).isMultiplePrivateStates();
    verifyNoMoreInteractions(runtimeContext);
    verify(privacyGroupManager).findPrivacyGroupByType(eq(PrivacyGroup.Type.RESIDENT));
}
Also used : Response(jakarta.ws.rs.core.Response) RuntimeContext(com.quorum.tessera.context.RuntimeContext) Test(org.junit.Test)

Example 4 with RuntimeContext

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

the class DiscoveryHelperTest method onCreate.

@Test
public void onCreate() {
    URI uri = URI.create("http://somedomain.com/");
    when(runtimeContext.getPeers()).thenReturn(List.of(uri));
    when(runtimeContext.getP2pServerUri()).thenReturn(uri);
    PublicKey publicKey = mock(PublicKey.class);
    when(enclave.getPublicKeys()).thenReturn(Set.of(publicKey));
    discoveryHelper.onCreate();
    verify(networkStore).store(any(ActiveNode.class));
    verify(runtimeContext).getP2pServerUri();
    verify(enclave).getPublicKeys();
    mockedRuntimeContext.verify(RuntimeContext::getInstance);
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) ActiveNode(com.quorum.tessera.discovery.ActiveNode) RuntimeContext(com.quorum.tessera.context.RuntimeContext) URI(java.net.URI) Test(org.junit.Test)

Example 5 with RuntimeContext

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

the class DiscoveryHelperTest method getCurrentWithUriOnly.

@Test
public void getCurrentWithUriOnly() {
    final URI uri = URI.create("http://somedomain.com");
    when(runtimeContext.getP2pServerUri()).thenReturn(uri);
    NodeInfo result = discoveryHelper.buildCurrent();
    assertThat(result).isNotNull();
    verify(runtimeContext).getP2pServerUri();
    assertThat(result.getUrl()).isEqualTo("http://somedomain.com/");
    assertThat(result.getRecipients()).isEmpty();
    verify(networkStore).getActiveNodes();
    mockedRuntimeContext.verify(RuntimeContext::getInstance);
}
Also used : NodeInfo(com.quorum.tessera.partyinfo.node.NodeInfo) 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