Skip to content

Commit 35ab020

Browse files
committed
examples/sftp: SftpContainerHostKeyTest with host key checking
The new SftpContainerHostKeyTest.java demonstrates how to upload a private host key into the SFTP server allowing host key checking. It avoids the unsecure ("StrictHostKeyChecking", "no") of SftpContainerTest.
1 parent ad2e8b1 commit 35ab020

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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_ed25519_key", 0400),
31+
"/etc/ssh/ssh_host_ed25519_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+
String hostKeyString = "AAAAC3NzaC1lZDI1NTE5AAAAINaBuegbLGHOgpXCePq80uY79Xw716jWXAwWjRdFYi53";
42+
HostKey hostKey = new HostKey(sftp.getHost(), Base64.getDecoder().decode(hostKeyString));
43+
jschSession.getHostKeyRepository().add(hostKey, null);
44+
jschSession.connect();
45+
ChannelSftp channel = (ChannelSftp) jschSession.openChannel("sftp");
46+
channel.connect();
47+
assertThat(channel.ls("/upload/testcontainers")).anyMatch(item -> item.toString().contains("file.txt"));
48+
assertThat(
49+
new BufferedReader(
50+
new InputStreamReader(channel.get("/upload/testcontainers/file.txt"), StandardCharsets.UTF_8)
51+
)
52+
.lines()
53+
.collect(Collectors.joining("\n"))
54+
)
55+
.contains("Testcontainers");
56+
channel.rm("/upload/testcontainers/file.txt");
57+
assertThat(channel.ls("/upload/testcontainers/"))
58+
.noneMatch(item -> item.toString().contains("testcontainers/file.txt"));
59+
}
60+
}
61+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-----BEGIN OPENSSH PRIVATE KEY-----
2+
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
3+
QyNTUxOQAAACDWgbnoGyxhzoKVwnj6vNLmO/V8O9eo1lwMFo0XRWIudwAAAJixm9bFsZvW
4+
xQAAAAtzc2gtZWQyNTUxOQAAACDWgbnoGyxhzoKVwnj6vNLmO/V8O9eo1lwMFo0XRWIudw
5+
AAAEDUMj/yjokN6yVDNM85skqB2LrPXgyH4FyztT3r3uKBDNaBuegbLGHOgpXCePq80uY7
6+
9Xw716jWXAwWjRdFYi53AAAAD2FhQDIzLTA3MTUzLTAwOQECAwQFBg==
7+
-----END OPENSSH PRIVATE KEY-----
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINaBuegbLGHOgpXCePq80uY79Xw716jWXAwWjRdFYi53 someone@localhost

0 commit comments

Comments
 (0)