Search in sources :

Example 6 with RuntimeContext

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);
}
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) PublicKey(com.quorum.tessera.encryption.PublicKey) 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) URI(java.net.URI) Test(org.junit.Test)

Example 7 with RuntimeContext

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);
}
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 8 with RuntimeContext

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();
}
Also used : Schema(io.swagger.v3.oas.annotations.media.Schema) Consumes(jakarta.ws.rs.Consumes) PublicKey(com.quorum.tessera.encryption.PublicKey) JsonArrayBuilder(jakarta.json.JsonArrayBuilder) Set(java.util.Set) GET(jakarta.ws.rs.GET) Json(jakarta.json.Json) Path(jakarta.ws.rs.Path) Content(io.swagger.v3.oas.annotations.media.Content) Operation(io.swagger.v3.oas.annotations.Operation) Response(jakarta.ws.rs.core.Response) MediaType(jakarta.ws.rs.core.MediaType) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse) Tag(io.swagger.v3.oas.annotations.tags.Tag) RuntimeContext(com.quorum.tessera.context.RuntimeContext) GetPublicKeysResponse(com.quorum.tessera.thirdparty.model.GetPublicKeysResponse) Produces(jakarta.ws.rs.Produces) PublicKey(com.quorum.tessera.encryption.PublicKey) JsonArrayBuilder(jakarta.json.JsonArrayBuilder) RuntimeContext(com.quorum.tessera.context.RuntimeContext) GET(jakarta.ws.rs.GET) Operation(io.swagger.v3.oas.annotations.Operation) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse)

Example 9 with RuntimeContext

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();
    }
}
Also used : Response(jakarta.ws.rs.core.Response) Base64(java.util.Base64) PublicKey(com.quorum.tessera.encryption.PublicKey) StringReader(java.io.StringReader) JsonReader(jakarta.json.JsonReader) RuntimeContext(com.quorum.tessera.context.RuntimeContext) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 10 with RuntimeContext

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);
}
Also used : NodeUri(com.quorum.tessera.discovery.NodeUri) ActiveNode(com.quorum.tessera.discovery.ActiveNode) RuntimeContext(com.quorum.tessera.context.RuntimeContext)

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