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());
}
}
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);
}
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));
}
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);
}
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);
}
Aggregations