|
| 1 | +package org.example; |
| 2 | + |
| 3 | +import com.jcraft.jsch.ChannelSftp; |
| 4 | +import com.jcraft.jsch.HostKey; |
| 5 | +import com.jcraft.jsch.JSch; |
| 6 | +import com.jcraft.jsch.Session; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | +import org.testcontainers.containers.GenericContainer; |
| 9 | +import org.testcontainers.utility.MountableFile; |
| 10 | + |
| 11 | +import java.io.BufferedReader; |
| 12 | +import java.io.InputStreamReader; |
| 13 | +import java.nio.charset.StandardCharsets; |
| 14 | +import java.util.Base64; |
| 15 | +import java.util.stream.Collectors; |
| 16 | + |
| 17 | +import static org.assertj.core.api.Assertions.assertThat; |
| 18 | + |
| 19 | +class SftpContainerHostKeyTest { |
| 20 | + |
| 21 | + @Test |
| 22 | + void test() throws Exception { |
| 23 | + try ( |
| 24 | + GenericContainer<?> sftp = new GenericContainer<>("atmoz/sftp:alpine-3.7") |
| 25 | + .withCopyFileToContainer( |
| 26 | + MountableFile.forClasspathResource("testcontainers/", 0777), |
| 27 | + "/home/foo/upload/testcontainers" |
| 28 | + ) |
| 29 | + .withCopyFileToContainer( |
| 30 | + MountableFile.forClasspathResource("./ssh_host_rsa_key", 0400), |
| 31 | + "/etc/ssh/ssh_host_rsa_key" |
| 32 | + ) |
| 33 | + .withExposedPorts(22) |
| 34 | + .withCommand("foo:pass:::upload") |
| 35 | + ) { |
| 36 | + sftp.start(); |
| 37 | + JSch jsch = new JSch(); |
| 38 | + Session jschSession = jsch.getSession("foo", sftp.getHost(), sftp.getMappedPort(22)); |
| 39 | + jschSession.setPassword("pass"); |
| 40 | + // hostKeyString is string starting with AAAA from file known_hosts or ssh_host_*_key.pub |
| 41 | + // generate the files with: |
| 42 | + // ssh-keygen -t rsa -b 3072 -f ssh_host_rsa_key < /dev/null |
| 43 | + String hostKeyString = |
| 44 | + "AAAAB3NzaC1yc2EAAAADAQABAAABgQCXMxVRzmFWxfrRB9XiZ/3HNM+xkYYE+IMGuOZD" + |
| 45 | + "04M2ezU25XjT6cPajzpFmzTxR2qEpRCKHeVnSG5nT6UXQp7760brTN7m5sDasbMnHgYh" + |
| 46 | + "fC/3of2k6qTR9X/JHRpgwzq5+6FtEe41w1H1dXoNIr4YTKnLijSp8MKqBtPPNUpzEVb9" + |
| 47 | + "5YKZGdCDoCbbYOyS/Dc8azUDo0mqM542J3nA2Sq9HCP0BAv43hrTAtCZodkB5wo18exb" + |
| 48 | + "fPKsjGtA3de2npybFoSRbavZmT8L/b2iHZX6FRaqLsbYGKtszCWu5OU7WBX5g5QVlLfO" + |
| 49 | + "nGQ+LsF6d6pX5LlMwEU14uu4gNPvZFOaZXtHNHZqnBcjd/sMaw5N/atFsPgtQ0vYnrEA" + |
| 50 | + "D6oDjj0uXMsnmgUWTZBi3q2GBWWPqhE+0ASb2xBQGa+tWWTVYbuuYlA7hUX0URK8FcLw" + |
| 51 | + "4UOYJjscDjnjlvQkghd2esP5NxV1NXkG2XYNHnf1E/tH4+AHJzy+qOQom7ehda96FZ8="; |
| 52 | + HostKey hostKey = new HostKey(sftp.getHost(), Base64.getDecoder().decode(hostKeyString)); |
| 53 | + jschSession.getHostKeyRepository().add(hostKey, null); |
| 54 | + jschSession.connect(); |
| 55 | + ChannelSftp channel = (ChannelSftp) jschSession.openChannel("sftp"); |
| 56 | + channel.connect(); |
| 57 | + assertThat(channel.ls("/upload/testcontainers")).anyMatch(item -> item.toString().contains("file.txt")); |
| 58 | + assertThat( |
| 59 | + new BufferedReader( |
| 60 | + new InputStreamReader(channel.get("/upload/testcontainers/file.txt"), StandardCharsets.UTF_8) |
| 61 | + ) |
| 62 | + .lines() |
| 63 | + .collect(Collectors.joining("\n")) |
| 64 | + ) |
| 65 | + .contains("Testcontainers"); |
| 66 | + channel.rm("/upload/testcontainers/file.txt"); |
| 67 | + assertThat(channel.ls("/upload/testcontainers/")) |
| 68 | + .noneMatch(item -> item.toString().contains("testcontainers/file.txt")); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments