From af6a444a0036955c68badfbedaf89c995ccc0de1 Mon Sep 17 00:00:00 2001 From: Abdelhamid Bakhta Date: Wed, 24 Apr 2019 11:25:50 +0200 Subject: [PATCH 1/2] [PAN-1878] Provide error message when invalid key specified in key file - display explicit message when invalid key file is specified --- .../pantheon/controller/KeyPairUtil.java | 32 +++++++++------- .../pantheon/util/KeyPairUtilTest.java | 37 +++++++++++++++++++ .../src/test/resources/invalidPrivateKey.txt | 1 + .../src/test/resources/validPrivateKey.txt | 1 + 4 files changed, 58 insertions(+), 13 deletions(-) create mode 100644 pantheon/src/test/java/tech/pegasys/pantheon/util/KeyPairUtilTest.java create mode 100644 pantheon/src/test/resources/invalidPrivateKey.txt create mode 100644 pantheon/src/test/resources/validPrivateKey.txt diff --git a/pantheon/src/main/java/tech/pegasys/pantheon/controller/KeyPairUtil.java b/pantheon/src/main/java/tech/pegasys/pantheon/controller/KeyPairUtil.java index 60c62f572b..8e4a8572b6 100644 --- a/pantheon/src/main/java/tech/pegasys/pantheon/controller/KeyPairUtil.java +++ b/pantheon/src/main/java/tech/pegasys/pantheon/controller/KeyPairUtil.java @@ -12,6 +12,7 @@ */ package tech.pegasys.pantheon.controller; +import tech.pegasys.pantheon.crypto.InvalidSEC256K1PrivateKeyStoreException; import tech.pegasys.pantheon.crypto.SECP256K1; import java.io.File; @@ -24,20 +25,25 @@ public class KeyPairUtil { private static final Logger LOG = LogManager.getLogger(); - public static SECP256K1.KeyPair loadKeyPair(final File keyFile) throws IOException { - final SECP256K1.KeyPair key; - if (keyFile.exists()) { - key = SECP256K1.KeyPair.load(keyFile); - LOG.info("Loaded key {} from {}", key.getPublicKey().toString(), keyFile.getAbsolutePath()); - } else { - key = SECP256K1.KeyPair.generate(); - key.getPrivateKey().store(keyFile); - LOG.info( - "Generated new key {} and stored it to {}", - key.getPublicKey().toString(), - keyFile.getAbsolutePath()); + public static SECP256K1.KeyPair loadKeyPair(final File keyFile) + throws IOException, IllegalArgumentException { + try { + final SECP256K1.KeyPair key; + if (keyFile.exists()) { + key = SECP256K1.KeyPair.load(keyFile); + LOG.info("Loaded key {} from {}", key.getPublicKey().toString(), keyFile.getAbsolutePath()); + } else { + key = SECP256K1.KeyPair.generate(); + key.getPrivateKey().store(keyFile); + LOG.info( + "Generated new key {} and stored it to {}", + key.getPublicKey().toString(), + keyFile.getAbsolutePath()); + } + return key; + } catch (InvalidSEC256K1PrivateKeyStoreException e) { + throw new IllegalArgumentException("Supplied file does not contain valid key pair."); } - return key; } public static SECP256K1.KeyPair loadKeyPair(final Path homeDirectory) throws IOException { diff --git a/pantheon/src/test/java/tech/pegasys/pantheon/util/KeyPairUtilTest.java b/pantheon/src/test/java/tech/pegasys/pantheon/util/KeyPairUtilTest.java new file mode 100644 index 0000000000..d61bd538cf --- /dev/null +++ b/pantheon/src/test/java/tech/pegasys/pantheon/util/KeyPairUtilTest.java @@ -0,0 +1,37 @@ +/* + * Copyright 2019 ConsenSys AG. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package tech.pegasys.pantheon.util; + +import static org.junit.Assert.*; + +import tech.pegasys.pantheon.controller.KeyPairUtil; + +import java.io.File; + +import org.junit.Test; + +public class KeyPairUtilTest { + + @Test + public void shouldLoadValidKeyPair() throws Exception { + assertNotNull( + KeyPairUtil.loadKeyPair( + new File(this.getClass().getResource("/validPrivateKey.txt").toURI()))); + } + + @Test(expected = IllegalArgumentException.class) + public void shouldNotLoadInvalidKeyPair() throws Exception { + KeyPairUtil.loadKeyPair( + new File(this.getClass().getResource("/invalidPrivateKey.txt").toURI())); + } +} diff --git a/pantheon/src/test/resources/invalidPrivateKey.txt b/pantheon/src/test/resources/invalidPrivateKey.txt new file mode 100644 index 0000000000..25073600d8 --- /dev/null +++ b/pantheon/src/test/resources/invalidPrivateKey.txt @@ -0,0 +1 @@ +this is not a valid private key \ No newline at end of file diff --git a/pantheon/src/test/resources/validPrivateKey.txt b/pantheon/src/test/resources/validPrivateKey.txt new file mode 100644 index 0000000000..c498f25bf9 --- /dev/null +++ b/pantheon/src/test/resources/validPrivateKey.txt @@ -0,0 +1 @@ +000000000000000000000000000000000000000000000000000000000000000A \ No newline at end of file From 2e15b2c2f167411c3dc2a32ab228f2e0110ca2c5 Mon Sep 17 00:00:00 2001 From: Abdelhamid Bakhta Date: Wed, 24 Apr 2019 11:31:28 +0200 Subject: [PATCH 2/2] Update KeyPairUtilTest.java --- .../test/java/tech/pegasys/pantheon/util/KeyPairUtilTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pantheon/src/test/java/tech/pegasys/pantheon/util/KeyPairUtilTest.java b/pantheon/src/test/java/tech/pegasys/pantheon/util/KeyPairUtilTest.java index d61bd538cf..dcc839029b 100644 --- a/pantheon/src/test/java/tech/pegasys/pantheon/util/KeyPairUtilTest.java +++ b/pantheon/src/test/java/tech/pegasys/pantheon/util/KeyPairUtilTest.java @@ -12,7 +12,7 @@ */ package tech.pegasys.pantheon.util; -import static org.junit.Assert.*; +import static org.junit.Assert.assertNotNull; import tech.pegasys.pantheon.controller.KeyPairUtil;