Skip to content

Commit dac0662

Browse files
committed
Update examples/sftp: Dependencies, container, host key
Update all dependencies to latest versions. Update container from single arch atmoz/sftp:alpine-3.7 to multi arch jmcombs/sftp:alpine, see https://github.com/jmcombs/sftp Replace insecure `"StrictHostKeyChecking", "no"` with a sample host key to demonstrate that the test should also cover the host key checking.
1 parent ad2e8b1 commit dac0662

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

examples/sftp/build.gradle

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ repositories {
77
}
88

99
dependencies {
10-
testImplementation 'com.jcraft:jsch:0.1.55'
10+
testImplementation 'com.github.mwiede:jsch:0.2.24'
1111
testImplementation 'org.testcontainers:testcontainers'
12-
testImplementation 'org.assertj:assertj-core:3.26.3'
13-
testImplementation 'ch.qos.logback:logback-classic:1.3.14'
14-
testImplementation 'org.junit.jupiter:junit-jupiter:5.11.0'
12+
testImplementation 'org.assertj:assertj-core:3.27.3'
13+
testImplementation 'ch.qos.logback:logback-classic:1.5.17'
14+
testImplementation 'org.junit.jupiter:junit-jupiter:5.12.1'
15+
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
1516
}
1617

1718
test {

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
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;
7+
import com.jcraft.jsch.UserInfo;
8+
69
import org.junit.jupiter.api.Test;
710
import org.testcontainers.containers.GenericContainer;
811
import org.testcontainers.utility.MountableFile;
912

1013
import java.io.BufferedReader;
1114
import java.io.InputStreamReader;
1215
import java.nio.charset.StandardCharsets;
16+
import java.util.Base64;
1317
import java.util.stream.Collectors;
1418

1519
import static org.assertj.core.api.Assertions.assertThat;
@@ -19,19 +23,26 @@ class SftpContainerTest {
1923
@Test
2024
void test() throws Exception {
2125
try (
22-
GenericContainer<?> sftp = new GenericContainer<>("atmoz/sftp:alpine-3.7")
26+
GenericContainer<?> sftp = new GenericContainer<>("jmcombs/sftp:alpine")
2327
.withCopyFileToContainer(
2428
MountableFile.forClasspathResource("testcontainers/", 0777),
2529
"/home/foo/upload/testcontainers"
2630
)
31+
.withCopyFileToContainer(
32+
MountableFile.forClasspathResource("./ssh_host_ed25519_key", 0400),
33+
"/etc/ssh/ssh_host_ed25519_key"
34+
)
2735
.withExposedPorts(22)
2836
.withCommand("foo:pass:::upload")
2937
) {
3038
sftp.start();
3139
JSch jsch = new JSch();
3240
Session jschSession = jsch.getSession("foo", sftp.getHost(), sftp.getMappedPort(22));
3341
jschSession.setPassword("pass");
34-
jschSession.setConfig("StrictHostKeyChecking", "no");
42+
// hostKeyString is string starting with AAAA from file known_hosts or ssh_host_*_key.pub
43+
String hostKeyString = "AAAAC3NzaC1lZDI1NTE5AAAAINaBuegbLGHOgpXCePq80uY79Xw716jWXAwWjRdFYi53";
44+
HostKey hostKey = new HostKey(sftp.getHost(), Base64.getDecoder().decode(hostKeyString));
45+
jschSession.getHostKeyRepository().add(hostKey, null);
3546
jschSession.connect();
3647
ChannelSftp channel = (ChannelSftp) jschSession.openChannel("sftp");
3748
channel.connect();
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)