Skip to content

Commit

Permalink
refactor: delete duplicated code
Browse files Browse the repository at this point in the history
  • Loading branch information
sparsick committed Oct 12, 2023
1 parent e2cbdaf commit e37faad
Showing 1 changed file with 73 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -36,6 +38,8 @@ public class GitServerContainerTest {
private File tempDir;




@Test
void validDockerImageName() {
assertThatNoException().isThrownBy(() ->
Expand Down Expand Up @@ -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());
Expand All @@ -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()
);

Expand All @@ -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")
Expand All @@ -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()
);

Expand All @@ -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()
);
}
Expand All @@ -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) {
Expand All @@ -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");
}
});
};
}
}

0 comments on commit e37faad

Please sign in to comment.