tomaswolf commented on a change in pull request #194: URL: https://github.com/apache/mina-sshd/pull/194#discussion_r631938991
########## File path: sshd-core/src/test/java/org/apache/sshd/client/opensshcerts/ClientOpenSSHCertificatesTest.java ########## @@ -0,0 +1,152 @@ +package org.apache.sshd.client.opensshcerts; + +import org.apache.sshd.client.SshClient; +import org.apache.sshd.client.session.ClientSession; +import org.apache.sshd.common.config.keys.PublicKeyEntry; +import org.apache.sshd.common.keyprovider.FileKeyPairProvider; +import org.apache.sshd.common.keyprovider.KeyIdentityProvider; +import org.apache.sshd.common.session.SessionContext; +import org.apache.sshd.common.util.GenericUtils; +import org.apache.sshd.common.util.io.IoUtils; +import org.apache.sshd.util.test.BaseTestSupport; +import org.apache.sshd.util.test.CommonTestSupportUtils; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.images.builder.ImageFromDockerfile; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.security.GeneralSecurityException; +import java.security.KeyPair; +import java.security.PrivateKey; +import java.security.PublicKey; +import java.util.ArrayList; +import java.util.Arrays; + +@RunWith(Parameterized.class) // see https://github.com/junit-team/junit/wiki/Parameterized-tests +public class ClientOpenSSHCertificatesTest { + + private static final String USER_KEY_PATH = "org/apache/sshd/client/opensshcerts/user/"; + + @Parameterized.Parameters(name = "key: {0}, cert: {0}-cert.pub") + public static Iterable<? extends String> privateKeyParams() { + return Arrays.asList( + "user01_rsa_sha2_256_2048", + "user01_rsa_sha2_512_2048", + "user01_rsa_sha2_256_4096", + "user01_rsa_sha2_512_4096", + "user01_ed25519", + "user01_ecdsa_256", + "user01_ecdsa_384", + "user01_ecdsa_521" + ); + } + + @Parameterized.Parameter + public String privateKeyName; + + private String getPrivateKeyResource() { + return USER_KEY_PATH + privateKeyName; + } + + private String getCertificateResource() { + return getPrivateKeyResource() + "-cert.pub"; + } + + /** + * This will build a new Docker image once per test class instance + * <br/><br/> + * The {@link ImageFromDockerfile#withFileFromClasspath} calls will build up an in-memory tar filesystem that is sent to the + * docker daemon for image building from assets on the classpath, making this all JVM classpath friendly. + * <br/><br/> + * The Docker image built will run a sshd instance managed by supervisord that has: + * + * <ul> + * <li> + * Two users: user01, user02 with: + * <ul> + * <li>Passwords "password01" and "password02"</li> + * <li>An authorized_keys file with a pub key for the included suite of keypairs (all current variants)</li> + * </ul> + * </li> + * <li>A CA public key configured in sshd_config for the TrustedUserCAKeys option (for client cert publickey auth)</li> + * <li>Two available host keypairs host01 and host02 (selected by env var SSH_HOST_KEY)</li> + * </ul> + **/ + @ClassRule + public static GenericContainer<?> sshdContainer = new GenericContainer<>( + new ImageFromDockerfile("clientopensshcertificatestest", true) + .withFileFromClasspath("entrypoint.sh", "org/apache/sshd/client/opensshcerts/docker/entrypoint.sh") + .withFileFromClasspath("sshd_config", "org/apache/sshd/client/opensshcerts/docker/sshd_config") + .withFileFromClasspath("supervisord.conf", "org/apache/sshd/client/opensshcerts/docker/supervisord.conf") + .withFileFromClasspath("user01_authorized_keys", "org/apache/sshd/client/opensshcerts/user/user01_authorized_keys") + .withFileFromClasspath("user02_authorized_keys", "org/apache/sshd/client/opensshcerts/user/user02_authorized_keys") + .withFileFromClasspath("host01", "org/apache/sshd/client/opensshcerts/host/host01") + .withFileFromClasspath("host01.pub", "org/apache/sshd/client/opensshcerts/host/host01.pub") + .withFileFromClasspath("host02", "org/apache/sshd/client/opensshcerts/host/host02") + .withFileFromClasspath("host02.pub", "org/apache/sshd/client/opensshcerts/host/host02.pub") + .withFileFromClasspath("ca.pub", "org/apache/sshd/client/opensshcerts/ca/ca.pub") + .withFileFromClasspath("Dockerfile", "org/apache/sshd/client/opensshcerts/docker/Dockerfile") + ) + // must be set to "/keys/host/host01" or "/keys/host/host02" + .withEnv("SSH_HOST_KEY", "/keys/host/host01") + .withExposedPorts(22); + + @Test + public void clientCertAuth() throws Exception { + + try (final InputStream certInputStream = + Thread.currentThread().getContextClassLoader().getResourceAsStream(getCertificateResource()) + ) { + + final byte[] certBytes = IoUtils.toByteArray(certInputStream); + final String certLine = GenericUtils.replaceWhitespaceAndTrim(new String(certBytes, StandardCharsets.UTF_8)); + + final PublicKeyEntry certPublicKeyEntry = PublicKeyEntry.parsePublicKeyEntry(certLine); + final PublicKey certPublicKey = certPublicKeyEntry.resolvePublicKey(null, null, null); + + final FileKeyPairProvider keyPairProvider = CommonTestSupportUtils.createTestKeyPairProvider(getPrivateKeyResource()); + + final KeyPair keypair = keyPairProvider.loadKeys(null).iterator().next(); + + final PrivateKey privateKey = keypair.getPrivate(); + + final SshClient client = SshClient.setUpDefaultClient(); + + client.setKeyIdentityProvider(new KeyIdentityProvider() { + @Override + public Iterable<KeyPair> loadKeys(SessionContext session) throws IOException, GeneralSecurityException { + + // build a keypair with the PrivateKey and the certificate as the PublicKey + final KeyPair certKeypair = new KeyPair(certPublicKey, privateKey); + + final ArrayList<KeyPair> list = new ArrayList<>(); + list.add(certKeypair); + + return list; + } + }); + + client.start(); + + final Integer actualPort = sshdContainer.getMappedPort(22); + + try (final ClientSession session = client.connect("user01", "localhost", actualPort).verify().getSession()) { Review comment: I had installed Docker for Mac from [1] a while ago; it's at version 3.3.0. As I discovered today when I tried to run this, that didn't give me docker-machine. So I installed docker-machine from [2]. At that point, running the test tried to use a docker-machine "" and failed. So I created a docker machine (`docker-machine create -d virtualbox default`) and re-ran the test; and then we are at my original comment from above: connection works only with sshdContainer.getHost(). As for maven/JDK versions: ``` $mvn --version Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f) Maven home: /Users/thomas/bin/mvn/apache-maven-3.6.3 Java version: 1.8.0_191, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre Default locale: en_CH, platform encoding: UTF-8 OS name: "mac os x", version: "10.14.6", arch: "x86_64", family: "mac" ``` [1] https://docs.docker.com/docker-for-mac/install/ [2] https://github.com/docker/machine/releases/tag/v0.16.2 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@mina.apache.org For additional commands, e-mail: dev-h...@mina.apache.org