Skip to content

Commit ebc1c18

Browse files
committed
Polish
1 parent b093b64 commit ebc1c18

File tree

2 files changed

+53
-71
lines changed

2 files changed

+53
-71
lines changed

examples/sftp/src/test/java/org/example/SftpContainerHostKeyTest.java

Lines changed: 0 additions & 71 deletions
This file was deleted.

examples/sftp/src/test/java/org/example/SftpContainerTest.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.example;
22

33
import com.jcraft.jsch.ChannelSftp;
4+
import com.jcraft.jsch.HostKey;
45
import com.jcraft.jsch.JSch;
56
import com.jcraft.jsch.Session;
67
import org.junit.jupiter.api.Test;
@@ -10,6 +11,7 @@
1011
import java.io.BufferedReader;
1112
import java.io.InputStreamReader;
1213
import java.nio.charset.StandardCharsets;
14+
import java.util.Base64;
1315
import java.util.stream.Collectors;
1416

1517
import static org.assertj.core.api.Assertions.assertThat;
@@ -49,4 +51,55 @@ void test() throws Exception {
4951
.noneMatch(item -> item.toString().contains("testcontainers/file.txt"));
5052
}
5153
}
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+
}
52105
}

0 commit comments

Comments
 (0)