From 7faa419d0ed07830c2ee15f4f2647f17d90f516f Mon Sep 17 00:00:00 2001 From: Erik Wramner Date: Mon, 31 Jul 2023 15:41:22 +0200 Subject: [PATCH] #105 added integration test using KeyPair. --- .../io/quarkus/it/jsch/KeyPairResource.java | 42 +++++++++++++++++++ .../src/main/resources/id_rsa.pem | 39 +++++++++++++++++ .../java/io/quarkus/it/jsch/KeyPairTest.java | 17 ++++++++ .../io/quarkus/it/jsch/NativeKeyPairIT.java | 7 ++++ 4 files changed, 105 insertions(+) create mode 100644 integration-tests/src/main/java/io/quarkus/it/jsch/KeyPairResource.java create mode 100644 integration-tests/src/main/resources/id_rsa.pem create mode 100644 integration-tests/src/test/java/io/quarkus/it/jsch/KeyPairTest.java create mode 100644 integration-tests/src/test/java/io/quarkus/it/jsch/NativeKeyPairIT.java diff --git a/integration-tests/src/main/java/io/quarkus/it/jsch/KeyPairResource.java b/integration-tests/src/main/java/io/quarkus/it/jsch/KeyPairResource.java new file mode 100644 index 0000000..58456da --- /dev/null +++ b/integration-tests/src/main/java/io/quarkus/it/jsch/KeyPairResource.java @@ -0,0 +1,42 @@ +package io.quarkus.it.jsch; + +import java.nio.charset.StandardCharsets; + +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.core.Response; + +import com.jcraft.jsch.JSch; +import com.jcraft.jsch.JSchException; +import com.jcraft.jsch.KeyPair; + +@Path("/key") +public class KeyPairResource { + + @GET + public Response checkKey() throws Exception { + try (var is = getClass().getResourceAsStream(("/id_rsa.pem"))) { + return Response.ok(verifyPemEncodedPrivateKey(is.readAllBytes(), "PrettyPlease")).build(); + } + } + + /** + * Verify that the private key is valid and can be decrypted with the given passphrase. + * + * @param privateKeyBytes The private key file (PEM encoded) as raw bytes. + * @param passphrase The passphrase, may be null for plain text keys. + * @return finger print. + */ + private String verifyPemEncodedPrivateKey(byte[] privateKeyBytes, String passphrase) { + KeyPair keyPair; + try { + keyPair = KeyPair.load(new JSch(), privateKeyBytes, null); + } catch (JSchException e) { + throw new RuntimeException("Failed to parse SSH private key", e); + } + if (keyPair.isEncrypted()) { + keyPair.decrypt(passphrase.getBytes(StandardCharsets.UTF_8)); + } + return keyPair.getFingerPrint(); + } +} diff --git a/integration-tests/src/main/resources/id_rsa.pem b/integration-tests/src/main/resources/id_rsa.pem new file mode 100644 index 0000000..4b4683f --- /dev/null +++ b/integration-tests/src/main/resources/id_rsa.pem @@ -0,0 +1,39 @@ +-----BEGIN OPENSSH PRIVATE KEY----- +b3BlbnNzaC1rZXktdjEAAAAACmFlczI1Ni1jdHIAAAAGYmNyeXB0AAAAGAAAABB8KarbgH +gP3XEK+fsZmlP5AAAAEAAAAAEAAAGXAAAAB3NzaC1yc2EAAAADAQABAAABgQDWavxs+pOy +y/66A+id4U94005jFHk4+mtqsZcPY3x5xuXviqWjzhyEa1vL+gKe9DywinxBWVl51R8OSE +BZ1H/mz77u4qaVkUlu0a/r3eYFlELIjDFGzODkEGdvptYt2FPPAdsoCCpB8pqNky8R873A +SvA316fhY3Y801x30OGNcjNhYmrEk+vAV37VnJJKJ1IgGWAFg7ZAoGDaund/2Liif9yC3c +UPSPZolkduDyCqBW4YlIe+FYCeBV8F8dBQCip4jjQIEqwSrJbpCqI5txORtfJhwHPaXan/ +JhjH5vIZ5zjhSPOcpVQxFJbc1eTAPhoSE/za6mLgA4t+IkSg06BRDpEpVIbxr3eVtIMELG +4pZmZsi6X8LcA2DI/8vdpcwVTDzpdZ1LMuEzfZFFxBk4L1U996U/FVzEhyE1U8t5ZLButT +pC8OXuEe58IyT/aLtksWKUN9g5VjVl/Bm0R5HviyUvvClkWM6nN5hWtI0AzQ7Q+VBlRF+3 +38cXR9tOmPFZMAAAWQDMCOotnrWCkAHoRWqMMEfjlMLn+laGd5xkxP9lkRcBSjIdqWQ7N5 +CXeM1QX/0soKzWKS5raMx0AcwkdkTGjLKV3LcjAX7xmJ6KQxZWBL+PPStIvz4UhvfVHXy2 +b9ZI8cWAaIuTldNgUVRqFixtaaWy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ih6E4ymsEJmaJPalj5f7jft+WRFX8AjFqTlHOz61iXa18xe +ucGI+X/NjP80Ym+FWwtnLzMYK4zLIX+/OJAXN8SfccgorLBq/t75uKFWJlgbsK2KG1cvJf +9EJnpxTedhLYPSY9EI6+xs3cT0FGxD2u57+iD2ejl0DuQU1VqANMxGM8pfYxTYXlurJRbw +Xedychxls++wgIlA40l8FWfaDHBtYeAVKuGbAx+osyWB/gvgo48NXqvhz6KPl95wvDbqUf +L8R23rLTt6QZkuE5L20kMhVARJLgS0g38IaVeoev8Nl723oxRb6ZDFamkME4mUnpmkKYUo +KTACH0+CijxbzROl41b9AsKswJxZHzGqecfUXQS+jFJO6DNdM/P3m7/DJorsr71WvWGQFO +mzNgZkFv/DUgR4FNChTi4jdOY+DRdkJbt4hTjT2dcyiurZDXUkgPJiQd7scMZfIQYVIzAm +z6/hXwOXxSUowDfl3hMQoJRr8rjV+ZpS1OFfC/G4PkWi5zyOaRvvuCzzMglTvgVvleCbPN +NtpQV8SqvveYJJf2hKOx1lN+yxqVTgppB1dU3btzwJegWTXRZbRgyTAFeE1ZMiE1cSf8nW +Vs3LYvEo2D+CoHD4dMnd6YpyqV+lagK+4dx8RSwivOpB/D/aUBcwGF6XugvVGdfdh/2Dnw +MsrQcO3ZafSW9TJbAo/JOneZccY= +-----END OPENSSH PRIVATE KEY----- diff --git a/integration-tests/src/test/java/io/quarkus/it/jsch/KeyPairTest.java b/integration-tests/src/test/java/io/quarkus/it/jsch/KeyPairTest.java new file mode 100644 index 0000000..39e3563 --- /dev/null +++ b/integration-tests/src/test/java/io/quarkus/it/jsch/KeyPairTest.java @@ -0,0 +1,17 @@ +package io.quarkus.it.jsch; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.core.Is.is; + +import org.junit.jupiter.api.Test; + +import io.quarkus.test.junit.QuarkusTest; + +@QuarkusTest +public class KeyPairTest { + + @Test + void shouldBeAbleToDecryptKey() { + given().get("/key").then().statusCode(is(200)); + } +} diff --git a/integration-tests/src/test/java/io/quarkus/it/jsch/NativeKeyPairIT.java b/integration-tests/src/test/java/io/quarkus/it/jsch/NativeKeyPairIT.java new file mode 100644 index 0000000..d91d201 --- /dev/null +++ b/integration-tests/src/test/java/io/quarkus/it/jsch/NativeKeyPairIT.java @@ -0,0 +1,7 @@ +package io.quarkus.it.jsch; + +import io.quarkus.test.junit.QuarkusIntegrationTest; + +@QuarkusIntegrationTest +public class NativeKeyPairIT extends KeyPairTest { +}