|
1 | 1 | package org.example;
|
2 | 2 |
|
3 | 3 | import com.jcraft.jsch.ChannelSftp;
|
| 4 | +import com.jcraft.jsch.HostKey; |
4 | 5 | import com.jcraft.jsch.JSch;
|
5 | 6 | import com.jcraft.jsch.Session;
|
6 | 7 | import org.junit.jupiter.api.Test;
|
|
10 | 11 | import java.io.BufferedReader;
|
11 | 12 | import java.io.InputStreamReader;
|
12 | 13 | import java.nio.charset.StandardCharsets;
|
| 14 | +import java.util.Base64; |
13 | 15 | import java.util.stream.Collectors;
|
14 | 16 |
|
15 | 17 | import static org.assertj.core.api.Assertions.assertThat;
|
@@ -49,4 +51,55 @@ void test() throws Exception {
|
49 | 51 | .noneMatch(item -> item.toString().contains("testcontainers/file.txt"));
|
50 | 52 | }
|
51 | 53 | }
|
| 54 | + |
| 55 | + @Test |
| 56 | + void testHostKeyCheck() throws Exception { |
| 57 | + try ( |
| 58 | + GenericContainer<?> sftp = new GenericContainer<>("atmoz/sftp:alpine-3.7") |
| 59 | + .withCopyFileToContainer( |
| 60 | + MountableFile.forClasspathResource("testcontainers/", 0777), |
| 61 | + "/home/foo/upload/testcontainers" |
| 62 | + ) |
| 63 | + .withCopyFileToContainer( |
| 64 | + MountableFile.forClasspathResource("./ssh_host_rsa_key", 0400), |
| 65 | + "/etc/ssh/ssh_host_rsa_key" |
| 66 | + ) |
| 67 | + .withExposedPorts(22) |
| 68 | + .withCommand("foo:pass:::upload") |
| 69 | + ) { |
| 70 | + sftp.start(); |
| 71 | + JSch jsch = new JSch(); |
| 72 | + Session jschSession = jsch.getSession("foo", sftp.getHost(), sftp.getMappedPort(22)); |
| 73 | + jschSession.setPassword("pass"); |
| 74 | + // hostKeyString is string starting with AAAA from file known_hosts or ssh_host_*_key.pub |
| 75 | + // generate the files with: |
| 76 | + // ssh-keygen -t rsa -b 3072 -f ssh_host_rsa_key < /dev/null |
| 77 | + String hostKeyString = |
| 78 | + "AAAAB3NzaC1yc2EAAAADAQABAAABgQCXMxVRzmFWxfrRB9XiZ/3HNM+xkYYE+IMGuOZD" + |
| 79 | + "04M2ezU25XjT6cPajzpFmzTxR2qEpRCKHeVnSG5nT6UXQp7760brTN7m5sDasbMnHgYh" + |
| 80 | + "fC/3of2k6qTR9X/JHRpgwzq5+6FtEe41w1H1dXoNIr4YTKnLijSp8MKqBtPPNUpzEVb9" + |
| 81 | + "5YKZGdCDoCbbYOyS/Dc8azUDo0mqM542J3nA2Sq9HCP0BAv43hrTAtCZodkB5wo18exb" + |
| 82 | + "fPKsjGtA3de2npybFoSRbavZmT8L/b2iHZX6FRaqLsbYGKtszCWu5OU7WBX5g5QVlLfO" + |
| 83 | + "nGQ+LsF6d6pX5LlMwEU14uu4gNPvZFOaZXtHNHZqnBcjd/sMaw5N/atFsPgtQ0vYnrEA" + |
| 84 | + "D6oDjj0uXMsnmgUWTZBi3q2GBWWPqhE+0ASb2xBQGa+tWWTVYbuuYlA7hUX0URK8FcLw" + |
| 85 | + "4UOYJjscDjnjlvQkghd2esP5NxV1NXkG2XYNHnf1E/tH4+AHJzy+qOQom7ehda96FZ8="; |
| 86 | + HostKey hostKey = new HostKey(sftp.getHost(), Base64.getDecoder().decode(hostKeyString)); |
| 87 | + jschSession.getHostKeyRepository().add(hostKey, null); |
| 88 | + jschSession.connect(); |
| 89 | + ChannelSftp channel = (ChannelSftp) jschSession.openChannel("sftp"); |
| 90 | + channel.connect(); |
| 91 | + assertThat(channel.ls("/upload/testcontainers")).anyMatch(item -> item.toString().contains("file.txt")); |
| 92 | + assertThat( |
| 93 | + new BufferedReader( |
| 94 | + new InputStreamReader(channel.get("/upload/testcontainers/file.txt"), StandardCharsets.UTF_8) |
| 95 | + ) |
| 96 | + .lines() |
| 97 | + .collect(Collectors.joining("\n")) |
| 98 | + ) |
| 99 | + .contains("Testcontainers"); |
| 100 | + channel.rm("/upload/testcontainers/file.txt"); |
| 101 | + assertThat(channel.ls("/upload/testcontainers/")) |
| 102 | + .noneMatch(item -> item.toString().contains("testcontainers/file.txt")); |
| 103 | + } |
| 104 | + } |
52 | 105 | }
|
0 commit comments