I am trying to run sftp using atmoz/sftp
image in GenericContainer
for an integration test.
I want to run it using ssh keys without password.
I am taking help of this solution here.
First I am creating ssh keys to test foo
user:
ssh-keygen -f foo
and saving it in srctestresourceskeys
directory.
This is my test:
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.utility.MountableFile;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
class SftpContainerTest {
@Test
void test() throws Exception {
try (
GenericContainer<?> sftp = new GenericContainer<>("atmoz/sftp:alpine-3.7")
.withCopyFileToContainer(
MountableFile.forClasspathResource("testcontainers/", 0777),
"/home/foo/upload/testcontainers"
)
.withCopyFileToContainer(MountableFile.forClasspathResource("keys/", 400),
"/home/foo/.ssh/keys/")
.withExposedPorts(22)
.withCommand("foo::1001")
) {
sftp.start();
JSch jsch = new JSch();
Session jschSession = jsch.getSession("foo", sftp.getHost(), sftp.getMappedPort(22));
jschSession.setConfig("StrictHostKeyChecking", "no");
jschSession.connect();
ChannelSftp channel = (ChannelSftp) jschSession.openChannel("sftp");
channel.connect();
assertThat(channel.ls("/upload/testcontainers")).anyMatch(item -> item.toString().contains("file.txt"));
assertThat(
new BufferedReader(
new InputStreamReader(channel.get("/upload/testcontainers/file.txt"), StandardCharsets.UTF_8)
)
.lines()
.collect(Collectors.joining("n"))
)
.contains("Testcontainers");
channel.rm("/upload/testcontainers/file.txt");
assertThat(channel.ls("/upload/testcontainers/"))
.noneMatch(item -> item.toString().contains("testcontainers/file.txt"));
}
}
}
But I am getting com.jcraft.jsch.JSchException: Auth fail
in jschSession.connect();
.
Container logs:
2024-12-07 13:49:27 [entrypoint] Parsing user data: "foo::1001"
2024-12-07 13:49:27 Generating public/private ed25519 key pair.
2024-12-07 13:49:27 Your identification has been saved in /etc/ssh/ssh_host_ed25519_key.
2024-12-07 13:49:27 Your public key has been saved in /etc/ssh/ssh_host_ed25519_key.pub.
2024-12-07 13:49:27 The key fingerprint is:
2024-12-07 13:49:27 SHA256:KyH9k12zLVwFW3/U4Ah7pelIklcONc2ZnAIgYggEoTT root@9f0d8d3d6851
2024-12-07 13:49:27 The key's randomart image is:
2024-12-07 13:49:27 +--[ED25519 256]--+
2024-12-07 13:49:27 |o+E. .o . +o==+0=|
2024-12-07 13:49:27 |.. ... . o B BO++|
2024-12-07 13:49:27 |. o = O.o==|
2024-12-07 13:49:27 | + = + +*|
2024-12-07 13:49:27 | S . o *.o|
2024-12-07 13:49:27 | . . o |
2024-12-07 13:49:27 | . |
2024-12-07 13:49:27 | |
2024-12-07 13:49:27 | |
2024-12-07 13:49:27 +----[SHA256]-----+
2024-12-07 13:49:27 Generating public/private rsa key pair.
2024-12-07 13:49:30 Your identification has been saved in /etc/ssh/ssh_host_rsa_key.
2024-12-07 13:49:30 Your public key has been saved in /etc/ssh/ssh_host_rsa_key.pub.
2024-12-07 13:49:30 The key fingerprint is:
2024-12-07 13:49:30 SHA256:j+pEXDjExttPFAafCEURFnfvoUBevcFz/67uibICsKU root@9f0d8d3d6851
2024-12-07 13:49:30 The key's randomart image is:
2024-12-07 13:49:30 +---[RSA 4096]----+
2024-12-07 13:49:30 | oooO==.oo |
2024-12-07 13:49:30 | A++ C.* .= .|
2024-12-07 13:49:30 | .+oo.= o=.|
2024-12-07 13:49:30 | o.+. .. o...|
2024-12-07 13:49:27 Creating mailbox file: No such file or directory
2024-12-07 13:49:30 Server listening on 0.0.0.0 port 22.
2024-12-07 13:49:30 Server listening on :: port 22.
2024-12-07 13:49:30 | + So . . .|
2024-12-07 13:49:30 | o . o. .|
2024-12-07 13:49:30 | . . . |
2024-12-07 13:49:30 | . . . . . +|
2024-12-07 13:49:30 | .o ..o..+o|
2024-12-07 13:49:30 +----[SHA256]-----+
2024-12-07 13:49:30 [entrypoint] Executing sshd
2024-12-07 13:49:44 Received disconnect from 172.17.0.1 port 59336:3: com.jcraft.jsch.JSchException: Auth fail [preauth]
2024-12-07 13:49:44 Disconnected from authenticating user foo 172.17.0.1 port 59336 [preauth]
What am I doing wrong?