This is an automated email from the ASF dual-hosted git repository. toulmean pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-tuweni.git
The following commit(s) were added to refs/heads/master by this push: new 687494c Use keyloader for testing. Sneak in the feed test a use of the raw service new 2b57fcc Merge pull request #123 from atoulme/ssb_keyloading 687494c is described below commit 687494ccdc68473b8813d0502f20d01dc28c7a6a Author: Antoine Toulme <anto...@lunar-ocean.com> AuthorDate: Sun Jul 12 16:11:29 2020 -0700 Use keyloader for testing. Sneak in the feed test a use of the raw service --- .../tuweni/scuttlebutt/lib/FeedStreamTest.java | 15 +++++ .../org/apache/tuweni/scuttlebutt/lib/Utils.java | 47 +------------- .../tuweni/scuttlebutt/lib/KeyFileLoader.java | 74 +++++++++++----------- 3 files changed, 53 insertions(+), 83 deletions(-) diff --git a/scuttlebutt-client-lib/src/integrationTest/java/org/apache/tuweni/scuttlebutt/lib/FeedStreamTest.java b/scuttlebutt-client-lib/src/integrationTest/java/org/apache/tuweni/scuttlebutt/lib/FeedStreamTest.java index 757bbcb..a37f37b 100644 --- a/scuttlebutt-client-lib/src/integrationTest/java/org/apache/tuweni/scuttlebutt/lib/FeedStreamTest.java +++ b/scuttlebutt-client-lib/src/integrationTest/java/org/apache/tuweni/scuttlebutt/lib/FeedStreamTest.java @@ -14,6 +14,7 @@ package org.apache.tuweni.scuttlebutt.lib; import static org.apache.tuweni.scuttlebutt.lib.Utils.getMasterClient; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assumptions.assumeTrue; @@ -26,10 +27,15 @@ import org.apache.tuweni.junit.VertxExtension; import org.apache.tuweni.junit.VertxInstance; import org.apache.tuweni.scuttlebutt.lib.model.FeedMessage; import org.apache.tuweni.scuttlebutt.lib.model.StreamHandler; +import org.apache.tuweni.scuttlebutt.rpc.RPCAsyncRequest; +import org.apache.tuweni.scuttlebutt.rpc.RPCFunction; +import org.apache.tuweni.scuttlebutt.rpc.RPCResponse; import java.util.ArrayList; +import java.util.Collections; import java.util.Comparator; import java.util.List; +import java.util.Map; import java.util.Optional; import com.fasterxml.jackson.core.JsonProcessingException; @@ -102,6 +108,14 @@ class FeedStreamTest { lastStreamedMessage.get().getValue().getContentAs(new ObjectMapper(), TestScuttlebuttSerializationModel.class); assertEquals("serialization-test", content.getType()); + + AsyncResult<RPCResponse> result = scuttlebuttClient + .rawRequestService() + .makeAsyncRequest(new RPCAsyncRequest(new RPCFunction("whoami"), Collections.emptyList())); + assertNotNull(result.get()); + @SuppressWarnings("rawtypes") + Map map = result.get().asJSON(new ObjectMapper(), Map.class); + assertTrue(map.containsKey("id")); } private AsyncResult<List<FeedMessage>> publishTestMessages(FeedService feedService) throws JsonProcessingException { @@ -116,4 +130,5 @@ class FeedStreamTest { return AsyncResult.combine(results); } + } diff --git a/scuttlebutt-client-lib/src/integrationTest/java/org/apache/tuweni/scuttlebutt/lib/Utils.java b/scuttlebutt-client-lib/src/integrationTest/java/org/apache/tuweni/scuttlebutt/lib/Utils.java index 703e10b..f9bae77 100644 --- a/scuttlebutt-client-lib/src/integrationTest/java/org/apache/tuweni/scuttlebutt/lib/Utils.java +++ b/scuttlebutt-client-lib/src/integrationTest/java/org/apache/tuweni/scuttlebutt/lib/Utils.java @@ -12,67 +12,24 @@ */ package org.apache.tuweni.scuttlebutt.lib; -import static java.nio.charset.StandardCharsets.UTF_8; import static org.apache.tuweni.scuttlebutt.lib.ScuttlebuttClientFactory.DEFAULT_NETWORK; -import org.apache.tuweni.bytes.Bytes; import org.apache.tuweni.concurrent.AsyncResult; import org.apache.tuweni.crypto.sodium.Signature; -import org.apache.tuweni.io.Base64; import org.apache.tuweni.scuttlebutt.Invite; -import java.io.File; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.HashMap; import java.util.Map; -import java.util.Scanner; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; import io.vertx.core.Vertx; class Utils { private static Signature.KeyPair getLocalKeys() throws Exception { - Map<String, String> env = System.getenv(); - - Path secretPath = Paths.get(env.getOrDefault("ssb_dir", "ssb-keys"), "secret"); - File file = secretPath.toFile(); - - if (!file.exists()) { - throw new Exception("Secret file does not exist " + secretPath.toAbsolutePath()); - } - - Scanner s = new Scanner(file, UTF_8.name()); - s.useDelimiter("\n"); - - ArrayList<String> list = new ArrayList<>(); - while (s.hasNext()) { - String next = s.next(); - - // Filter out the comment lines - if (!next.startsWith("#")) { - list.add(next); - } - } - - String secretJSON = String.join("", list); - - ObjectMapper mapper = new ObjectMapper(); - - HashMap<String, String> values = mapper.readValue(secretJSON, new TypeReference<Map<String, String>>() {}); - String pubKey = values.get("public").replace(".ed25519", ""); - String privateKey = values.get("private").replace(".ed25519", ""); - - Bytes pubKeyBytes = Base64.decode(pubKey); - Bytes privKeyBytes = Base64.decode(privateKey); - - Signature.PublicKey pub = Signature.PublicKey.fromBytes(pubKeyBytes); - Signature.SecretKey secretKey = Signature.SecretKey.fromBytes(privKeyBytes); + Path ssbPath = Paths.get(System.getenv().getOrDefault("ssb_dir", "/tmp/ssb")); - return new Signature.KeyPair(pub, secretKey); + return KeyFileLoader.getLocalKeys(ssbPath); } static ScuttlebuttClient getMasterClient(Vertx vertx) throws Exception { diff --git a/scuttlebutt-client-lib/src/main/java/org/apache/tuweni/scuttlebutt/lib/KeyFileLoader.java b/scuttlebutt-client-lib/src/main/java/org/apache/tuweni/scuttlebutt/lib/KeyFileLoader.java index 9d93bd7..d5266e4 100644 --- a/scuttlebutt-client-lib/src/main/java/org/apache/tuweni/scuttlebutt/lib/KeyFileLoader.java +++ b/scuttlebutt-client-lib/src/main/java/org/apache/tuweni/scuttlebutt/lib/KeyFileLoader.java @@ -18,8 +18,9 @@ import org.apache.tuweni.bytes.Bytes; import org.apache.tuweni.crypto.sodium.Signature; import org.apache.tuweni.io.Base64; -import java.io.File; import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.file.Path; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; @@ -27,33 +28,32 @@ import java.util.Scanner; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import com.google.common.base.Optional; /** * Utility class for loading scuttlebutt keys from the file system. - * */ public class KeyFileLoader { + private KeyFileLoader() {} + + private static final ObjectMapper objectMapper = new ObjectMapper(); + /** * Attempts to load the keys from the default scuttlebutt directory (~/.ssb), and throws an exception if the keys are * not available at the given path * * @return the scuttlebutt key pair - * @throws Exception if no key is found */ - public static Signature.KeyPair getLocalKeys() throws Exception { - Optional<String> path = Optional.fromNullable(System.getenv().get("ssb_dir")); + public static Signature.KeyPair getLocalKeys(Path ssbFolder) { - if (!path.isPresent()) { - throw new Exception("Cannot find ssb directory config value"); - } + Path secretPath = ssbFolder.resolve("secret"); - String secretPath = path.get() + "/secret"; - File file = new File(secretPath); + if (!secretPath.toFile().exists()) { + throw new IllegalArgumentException("Secret file does not exist"); + } - if (!file.exists()) { - throw new Exception("Secret file does not exist"); + if (!secretPath.toFile().canRead()) { + throw new IllegalArgumentException("Secret file cannot be read"); } return loadKeysFromFile(secretPath); @@ -64,40 +64,38 @@ public class KeyFileLoader { * * @param secretPath the filepath to the scuttlebutt secret key to load * @return the scuttlebutt key pair - * @throws IOException */ - public static Signature.KeyPair loadKeysFromFile(String secretPath) throws IOException { + public static Signature.KeyPair loadKeysFromFile(Path secretPath) { + try { + Scanner s = new Scanner(secretPath.toFile(), UTF_8.name()); - File file = new File(secretPath); + s.useDelimiter("\n"); - Scanner s = new Scanner(file, UTF_8.name()); - s.useDelimiter("\n"); + ArrayList<String> list = new ArrayList<String>(); + while (s.hasNext()) { + String next = s.next(); - ArrayList<String> list = new ArrayList<String>(); - while (s.hasNext()) { - String next = s.next(); - - // Filter out the comment lines - if (!next.startsWith("#")) { - list.add(next); + // Filter out the comment lines + if (!next.startsWith("#")) { + list.add(next); + } } - } - - String secretJSON = String.join("", list); + String secretJSON = String.join("", list); - ObjectMapper mapper = new ObjectMapper(); + HashMap<String, String> values = objectMapper.readValue(secretJSON, new TypeReference<Map<String, String>>() {}); + String pubKey = values.get("public").replace(".ed25519", ""); + String privateKey = values.get("private").replace(".ed25519", ""); - HashMap<String, String> values = mapper.readValue(secretJSON, new TypeReference<Map<String, String>>() {}); - String pubKey = values.get("public").replace(".ed25519", ""); - String privateKey = values.get("private").replace(".ed25519", ""); + Bytes pubKeyBytes = Base64.decode(pubKey); + Bytes privKeyBytes = Base64.decode(privateKey); - Bytes pubKeyBytes = Base64.decode(pubKey); - Bytes privKeyBytes = Base64.decode(privateKey); + Signature.PublicKey pub = Signature.PublicKey.fromBytes(pubKeyBytes); + Signature.SecretKey secretKey = Signature.SecretKey.fromBytes(privKeyBytes); - Signature.PublicKey pub = Signature.PublicKey.fromBytes(pubKeyBytes); - Signature.SecretKey secretKey = Signature.SecretKey.fromBytes(privKeyBytes); - - return new Signature.KeyPair(pub, secretKey); + return new Signature.KeyPair(pub, secretKey); + } catch (IOException e) { + throw new UncheckedIOException(e); + } } } --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@tuweni.apache.org For additional commands, e-mail: commits-h...@tuweni.apache.org