-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
73 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,11 +5,14 @@ | |
import com.jcraft.jsch.JSchException; | ||
import com.jcraft.jsch.Session; | ||
import org.eclipse.jgit.api.Git; | ||
import org.eclipse.jgit.api.TransportConfigCallback; | ||
import org.eclipse.jgit.api.errors.GitAPIException; | ||
import org.eclipse.jgit.transport.SshTransport; | ||
import org.eclipse.jgit.transport.Transport; | ||
import org.eclipse.jgit.transport.ssh.jsch.JschConfigSessionFactory; | ||
import org.eclipse.jgit.transport.ssh.jsch.OpenSshConfig; | ||
import org.eclipse.jgit.util.FS; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.CleanupMode; | ||
import org.junit.jupiter.api.io.TempDir; | ||
|
@@ -21,7 +24,6 @@ | |
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
|
@@ -36,6 +38,8 @@ public class GitServerContainerTest { | |
private File tempDir; | ||
|
||
|
||
|
||
|
||
@Test | ||
void validDockerImageName() { | ||
assertThatNoException().isThrownBy(() -> | ||
|
@@ -99,11 +103,7 @@ void gitRepoURI() { | |
|
||
@Test | ||
void copyExistingGitRepo(@TempDir File sampleRepo) throws GitAPIException, IOException { | ||
FileUtils.copyFileToDirectory(new File("src/test/resources/sampleRepo/testFile"), sampleRepo); | ||
|
||
Git repo = Git.init().setDirectory(sampleRepo).setInitialBranch("main").call(); | ||
repo.add().addFilepattern("testFile").call(); | ||
repo.commit().setAuthor("Sandra Parsick", "[email protected]").setMessage("init").call(); | ||
initSampleRepo(sampleRepo, "src/test/resources/sampleRepo/testFile"); | ||
|
||
var containerUnderTest = new GitServerContainer(LATEST_GIT_SERVER_VERSION) | ||
.withCopyExistingGitRepoToContainer(sampleRepo.getAbsolutePath()); | ||
|
@@ -117,16 +117,7 @@ void copyExistingGitRepo(@TempDir File sampleRepo) throws GitAPIException, IOExc | |
.setURI(gitRepoURI.toString()) | ||
.setDirectory(tempDir) | ||
.setBranch("main") | ||
.setTransportConfigCallback(transport -> { | ||
var sshTransport = (SshTransport) transport; | ||
sshTransport.setSshSessionFactory(new JschConfigSessionFactory() { | ||
@Override | ||
protected void configure(OpenSshConfig.Host hc, Session session) { | ||
session.setPassword("12345"); | ||
session.setConfig("StrictHostKeyChecking", "no"); | ||
} | ||
}); | ||
}) | ||
.setTransportConfigCallback(GitServerContainerTest::configureWithPasswordAndNoHostKeyChecking) | ||
.call() | ||
); | ||
|
||
|
@@ -135,11 +126,7 @@ protected void configure(OpenSshConfig.Host hc, Session session) { | |
|
||
@Test | ||
void copyExistingGitRepoWithCustomRepoName(@TempDir File sampleRepo) throws IOException, GitAPIException { | ||
FileUtils.copyFileToDirectory(new File("src/test/resources/sampleRepo/testFile"), sampleRepo); | ||
|
||
Git repo = Git.init().setDirectory(sampleRepo).setInitialBranch("main").call(); | ||
repo.add().addFilepattern("testFile").call(); | ||
repo.commit().setAuthor("Sandra Parsick", "[email protected]").setMessage("init").call(); | ||
initSampleRepo(sampleRepo, "src/test/resources/sampleRepo/testFile"); | ||
|
||
var containerUnderTest = new GitServerContainer(LATEST_GIT_SERVER_VERSION) | ||
.withGitRepo("customRepoName") | ||
|
@@ -153,16 +140,7 @@ void copyExistingGitRepoWithCustomRepoName(@TempDir File sampleRepo) throws IOEx | |
.setURI(gitRepoURI.toString()) | ||
.setDirectory(tempDir) | ||
.setBranch("main") | ||
.setTransportConfigCallback(transport -> { | ||
var sshTransport = (SshTransport) transport; | ||
sshTransport.setSshSessionFactory(new JschConfigSessionFactory() { | ||
@Override | ||
protected void configure(OpenSshConfig.Host hc, Session session) { | ||
session.setPassword("12345"); | ||
session.setConfig("StrictHostKeyChecking", "no"); | ||
} | ||
}); | ||
}) | ||
.setTransportConfigCallback(GitServerContainerTest::configureWithPasswordAndNoHostKeyChecking) | ||
.call() | ||
); | ||
|
||
|
@@ -183,16 +161,7 @@ void setupGitRepo(GitServerVersions gitServer) { | |
.setURI(gitRepoURI.toString()) | ||
.setDirectory(tempDir) | ||
.setBranch("main") | ||
.setTransportConfigCallback(transport -> { | ||
var sshTransport = (SshTransport) transport; | ||
sshTransport.setSshSessionFactory(new JschConfigSessionFactory() { | ||
@Override | ||
protected void configure(OpenSshConfig.Host hc, Session session) { | ||
session.setPassword("12345"); | ||
session.setConfig("StrictHostKeyChecking", "no"); | ||
} | ||
}); | ||
}) | ||
.setTransportConfigCallback(GitServerContainerTest::configureWithPasswordAndNoHostKeyChecking) | ||
.call() | ||
); | ||
} | ||
|
@@ -211,26 +180,13 @@ void pubKeyAuth(GitServerVersions gitServer) { | |
.setURI(gitRepoURI.toString()) | ||
.setDirectory(tempDir) | ||
.setBranch("main") | ||
.setTransportConfigCallback(transport -> { | ||
var sshTransport = (SshTransport) transport; | ||
sshTransport.setSshSessionFactory(new JschConfigSessionFactory() { | ||
|
||
@Override | ||
protected JSch createDefaultJSch(FS fs ) throws JSchException { | ||
JSch defaultJSch = super.createDefaultJSch( fs ); | ||
configureSshIdentity(defaultJSch, containerUnderTest); | ||
return defaultJSch; | ||
} | ||
@Override | ||
protected void configure(OpenSshConfig.Host hc, Session session) { | ||
session.setConfig("StrictHostKeyChecking", "no"); | ||
} | ||
}); | ||
}) | ||
.setTransportConfigCallback(configureWithSshIdentityAndNoHostVerification(containerUnderTest.getSshClientIdentity())) | ||
.call() | ||
); | ||
} | ||
|
||
|
||
|
||
@ParameterizedTest | ||
@EnumSource(GitServerVersions.class) | ||
void strictHostKeyVerifivation(GitServerVersions gitServer) { | ||
|
@@ -245,35 +201,78 @@ void strictHostKeyVerifivation(GitServerVersions gitServer) { | |
.setURI(gitRepoURI.toString()) | ||
.setDirectory(tempDir) | ||
.setBranch("main") | ||
.setTransportConfigCallback(transport -> { | ||
var sshTransport = (SshTransport) transport; | ||
sshTransport.setSshSessionFactory(new JschConfigSessionFactory() { | ||
|
||
@Override | ||
protected JSch createDefaultJSch(FS fs ) throws JSchException { | ||
JSch defaultJSch = super.createDefaultJSch( fs ); | ||
configureSshIdentity(defaultJSch, containerUnderTest); | ||
configureHostKeyRepository(defaultJSch, containerUnderTest); | ||
return defaultJSch; | ||
} | ||
}); | ||
}) | ||
.setTransportConfigCallback(configureWithSshIdentityAndHostKey(containerUnderTest.getSshClientIdentity(), containerUnderTest.getHostKey())) | ||
.call() | ||
); | ||
} | ||
|
||
private void configureSshIdentity(JSch defaultJSch, GitServerContainer containerUnderTest) throws JSchException { | ||
SshIdentity sshIdentity = containerUnderTest.getSshClientIdentity(); | ||
@NotNull | ||
private TransportConfigCallback configureWithSshIdentityAndHostKey(SshIdentity sshIdentity, SshHostKey hostKey) { | ||
return transport -> { | ||
var sshTransport = (SshTransport) transport; | ||
sshTransport.setSshSessionFactory(new JschConfigSessionFactory() { | ||
|
||
@Override | ||
protected JSch createDefaultJSch(FS fs) throws JSchException { | ||
JSch defaultJSch = super.createDefaultJSch(fs); | ||
configureSshIdentity(defaultJSch, sshIdentity); | ||
configureHostKeyRepository(defaultJSch, hostKey); | ||
return defaultJSch; | ||
} | ||
}); | ||
}; | ||
} | ||
|
||
private void configureSshIdentity(JSch defaultJSch, SshIdentity sshIdentity) throws JSchException { | ||
byte[] privateKey = sshIdentity.getPrivateKey(); | ||
byte[] publicKey = sshIdentity.getPublicKey(); | ||
byte[] passphrase = sshIdentity.getPassphrase(); | ||
defaultJSch.addIdentity("git-server", privateKey, publicKey, passphrase); | ||
} | ||
|
||
private void configureHostKeyRepository(JSch defaultJSch, GitServerContainer containerUnderTest) throws JSchException { | ||
SshHostKey hostKey = containerUnderTest.getHostKey(); | ||
private void configureHostKeyRepository(JSch defaultJSch, SshHostKey hostKey) throws JSchException { | ||
String host = hostKey.getHostname(); | ||
byte[] key = hostKey.getKey(); | ||
defaultJSch.getHostKeyRepository().add(new HostKey(host, key), null); | ||
} | ||
|
||
private void initSampleRepo(File sampleRepo, String repoContent) throws IOException, GitAPIException { | ||
FileUtils.copyFileToDirectory(new File(repoContent), sampleRepo); | ||
|
||
Git repo = Git.init().setDirectory(sampleRepo).setInitialBranch("main").call(); | ||
repo.add().addFilepattern("testFile").call(); | ||
repo.commit().setAuthor("Sandra Parsick", "[email protected]").setMessage("init").call(); | ||
} | ||
|
||
private static void configureWithPasswordAndNoHostKeyChecking(Transport transport) { | ||
var sshTransport = (SshTransport) transport; | ||
sshTransport.setSshSessionFactory(new JschConfigSessionFactory() { | ||
@Override | ||
protected void configure(OpenSshConfig.Host hc, Session session) { | ||
session.setPassword("12345"); | ||
session.setConfig("StrictHostKeyChecking", "no"); | ||
} | ||
}); | ||
} | ||
|
||
@NotNull | ||
private TransportConfigCallback configureWithSshIdentityAndNoHostVerification(SshIdentity sshIdentity) { | ||
return transport -> { | ||
var sshTransport = (SshTransport) transport; | ||
sshTransport.setSshSessionFactory(new JschConfigSessionFactory() { | ||
|
||
@Override | ||
protected JSch createDefaultJSch(FS fs) throws JSchException { | ||
JSch defaultJSch = super.createDefaultJSch(fs); | ||
configureSshIdentity(defaultJSch, sshIdentity); | ||
return defaultJSch; | ||
} | ||
|
||
@Override | ||
protected void configure(OpenSshConfig.Host hc, Session session) { | ||
session.setConfig("StrictHostKeyChecking", "no"); | ||
} | ||
}); | ||
}; | ||
} | ||
} |