use of com.quorum.tessera.context.RuntimeContext in project tessera by ConsenSys.
the class DiscoveryHelperTest method buildCurrent.
@Test
public void buildCurrent() {
final URI uri = URI.create("http://somedomain.com");
when(runtimeContext.getP2pServerUri()).thenReturn(uri);
final List<PublicKey> keys = IntStream.range(0, 5).mapToObj(i -> mock(PublicKey.class)).collect(Collectors.toList());
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();
assertThat(result.getUrl()).isEqualTo("http://somedomain.com/");
assertThat(result.getRecipients()).hasSize(5);
List<Recipient> recipients = List.copyOf(result.getRecipients());
assertThat(recipients.stream().map(Recipient::getKey).collect(Collectors.toList())).containsExactlyInAnyOrderElementsOf(keys);
verify(networkStore).getActiveNodes();
verify(runtimeContext).getP2pServerUri();
mockedRuntimeContext.verify(RuntimeContext::getInstance);
}
use of com.quorum.tessera.context.RuntimeContext in project tessera by ConsenSys.
the class DiscoveryHelperTest method buildAllNodeInfos.
@Test
public void buildAllNodeInfos() {
when(runtimeContext.getP2pServerUri()).thenReturn(URI.create("http://own.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(2);
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(node1, node2);
verify(networkStore).getActiveNodes();
verify(runtimeContext).getP2pServerUri();
mockedRuntimeContext.verify(RuntimeContext::getInstance);
}
use of com.quorum.tessera.context.RuntimeContext in project tessera by ConsenSys.
the class KeyResource method getPublicKeys.
@GET
@Operation(summary = "/keys", description = "get all public keys managed by the server's enclave")
@ApiResponse(responseCode = "200", description = "server's public keys", content = @Content(schema = @Schema(implementation = GetPublicKeysResponse.class)))
public Response getPublicKeys() {
RuntimeContext runtimeContext = RuntimeContext.getInstance();
Set<PublicKey> publicKeys = runtimeContext.getPublicKeys();
final JsonArrayBuilder keyBuilder = Json.createArrayBuilder();
publicKeys.stream().map(key -> Json.createObjectBuilder().add("key", key.encodeToBase64()).build()).forEach(keyBuilder::add);
final String output = Json.createObjectBuilder().add("keys", keyBuilder.build()).build().toString();
return Response.status(Response.Status.OK).entity(output).build();
}
use of com.quorum.tessera.context.RuntimeContext in project tessera by ConsenSys.
the class KeyResourceTest method testGetPublicKeys.
@Test
public void testGetPublicKeys() {
try (var mockedStaticRuntimeContext = mockStatic(RuntimeContext.class)) {
mockedStaticRuntimeContext.when(RuntimeContext::getInstance).thenReturn(runtimeContext);
Base64.Decoder base64Decoder = Base64.getDecoder();
final String keyJsonString = "{\"keys\": [{\"key\": \"QfeDAys9MPDs2XHExtc84jKGHxZg/aj52DTh0vtA3Xc=\"}]}";
String key = "QfeDAys9MPDs2XHExtc84jKGHxZg/aj52DTh0vtA3Xc=";
Set<PublicKey> publicKeys = new HashSet<>();
publicKeys.add(PublicKey.from(base64Decoder.decode(key)));
when(runtimeContext.getPublicKeys()).thenReturn(publicKeys);
Response response = keyResource.getPublicKeys();
assertThat(response).isNotNull();
assertThat(response.getStatus()).isEqualTo(200);
final String output = response.getEntity().toString();
final JsonReader expected = Json.createReader(new StringReader(keyJsonString));
final JsonReader actual = Json.createReader(new StringReader(output));
assertThat(expected.readObject()).isEqualTo(actual.readObject());
verify(runtimeContext).getPublicKeys();
mockedStaticRuntimeContext.verify(RuntimeContext::getInstance);
mockedStaticRuntimeContext.verifyNoMoreInteractions();
}
}
use of com.quorum.tessera.context.RuntimeContext in project tessera by ConsenSys.
the class DiscoveryHelperImpl method onCreate.
@Override
public void onCreate() {
RuntimeContext runtimeContext = RuntimeContext.getInstance();
final NodeUri nodeUri = Optional.of(runtimeContext).map(RuntimeContext::getP2pServerUri).map(NodeUri::create).get();
ActiveNode thisNode = ActiveNode.Builder.create().withUri(nodeUri).withKeys(enclave.getPublicKeys()).withSupportedVersions(ApiVersion.versions()).build();
networkStore.store(thisNode);
}
Aggregations