-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use the officially supported apache mina sshd in m2k-func
The various git command we use in m2k func now uses the officially supported ssh implementation for the transport. Added tests to see the transport is working well with ed25519 keys Notice - the StrictHost option is now set using a standard ssh_config (see man ssh_config) file which is assumed to be where the key is located - so now we can just drop the id_rsa file in a well known location along with the rest of the .ssh files: /etc/.ssh/config /etc/.ssh/known_hosts /etc/.ssh/id_rsa Signed-off-by: Roy Golan <[email protected]>
- Loading branch information
Showing
10 changed files
with
197 additions
and
40 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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[allowlist] | ||
description = "Global Allowlist" | ||
|
||
# Ignore based on any subset of the file path | ||
paths = [ | ||
|
||
# Ignore some long path | ||
'''move2kube\/m2k-func\/src\/test\/resources\/m2k-test-ssh-id_ed25519$''', | ||
] | ||
|
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
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
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
123 changes: 123 additions & 0 deletions
123
move2kube/m2k-func/src/test/java/dev/parodos/service/GitServiceImplTest.java
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 |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package dev.parodos.service; | ||
|
||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.nio.file.attribute.PosixFilePermissions; | ||
import java.util.Collections; | ||
import java.util.Comparator; | ||
|
||
import org.apache.sshd.common.keyprovider.ClassLoadableResourceKeyPairProvider; | ||
import org.apache.sshd.git.GitLocationResolver; | ||
import org.apache.sshd.git.pack.GitPackCommandFactory; | ||
import org.apache.sshd.server.SshServer; | ||
import org.apache.sshd.server.auth.password.AcceptAllPasswordAuthenticator; | ||
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider; | ||
import org.apache.sshd.sftp.server.SftpSubsystemFactory; | ||
import org.eclipse.jgit.api.AddCommand; | ||
import org.eclipse.jgit.api.Git; | ||
import org.eclipse.jgit.api.errors.GitAPIException; | ||
import org.eclipse.jgit.lib.Repository; | ||
import org.eclipse.jgit.storage.file.FileRepositoryBuilder; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
|
||
class GitServiceImplTest { | ||
static SshServer sshd; | ||
static Path serverDir; | ||
static Path cloneDir; | ||
static final String testFile = "test-file"; | ||
static final String testFileContent = "test-content"; | ||
GitServiceImpl underTest = new GitServiceImpl(); | ||
|
||
@Test | ||
void cloneRepo() throws IOException, GitAPIException { | ||
try (Git git = underTest.cloneRepo("ssh://%s:%d%s".formatted(sshd.getHost(), sshd.getPort(), serverDir.toString()), "", cloneDir)) { | ||
Path testfile = cloneDir.resolve(testFile); | ||
Assertions.assertTrue(Files.exists(testfile), "cloned file doesn't exists"); | ||
Assertions.assertEquals(Files.readString(testfile), testFileContent); | ||
} | ||
} | ||
|
||
@Test | ||
void push() throws IOException, GitAPIException { | ||
try (Git git = underTest.cloneRepo("ssh://%s:%d%s".formatted(sshd.getHost(), sshd.getPort(), serverDir.toString()), "", cloneDir)) { | ||
Files.write(cloneDir.resolve("push-test-file"), "foo".getBytes()); | ||
underTest.commit(git, "push test commit", "."); | ||
Assertions.assertDoesNotThrow(() -> underTest.push(git)); | ||
} | ||
} | ||
|
||
@BeforeAll | ||
static void init() throws IOException, GitAPIException { | ||
// create an ssh server | ||
sshd = SshServer.setUpDefaultServer(); | ||
sshd.setHost("localhost"); | ||
sshd.setPort(30022); | ||
URL hostkey = GitServiceImplTest.class.getClassLoader().getResource("m2k-test-ssh-id_ed25519.pub"); | ||
SimpleGeneratorHostKeyProvider keyPairProvider = new SimpleGeneratorHostKeyProvider(Paths.get(hostkey.getPath())); | ||
sshd.setKeyPairProvider(keyPairProvider); | ||
keyPairProvider.loadKeys(null); | ||
sshd.setPasswordAuthenticator(AcceptAllPasswordAuthenticator.INSTANCE); | ||
|
||
serverDir = Files.createTempDirectory( | ||
"m2kfunc-test-git-repo-server", | ||
PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rwxrwxrwx"))); | ||
|
||
sshd.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory())); | ||
sshd.setCommandFactory(new GitPackCommandFactory(GitLocationResolver.constantPath(Paths.get("/")))); | ||
sshd.setKeyPairProvider(new ClassLoadableResourceKeyPairProvider("test.key")); | ||
sshd.setPublickeyAuthenticator((username, key, session) -> true); | ||
sshd.start(); | ||
|
||
// crete git repo on the ssh server | ||
Repository repo = new FileRepositoryBuilder().setWorkTree(serverDir.toFile()).build(); | ||
repo.create(true); | ||
Files.write(serverDir.resolve(testFile).toAbsolutePath(), testFileContent.getBytes()); | ||
new AddCommand(repo).addFilepattern(".").call(); | ||
Git git = new Git(repo); | ||
git.add().addFilepattern(".").call(); | ||
git.commit().setAuthor("me", "me@me").setMessage("first commit from test init").call(); | ||
} | ||
|
||
@AfterAll | ||
static void afterAll() throws IOException { | ||
try (var walk = Files.walk(serverDir)) { | ||
walk.sorted(Comparator.reverseOrder()).forEach(path -> { | ||
try { | ||
Files.deleteIfExists(path); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
@BeforeEach | ||
void setup() throws IOException { | ||
cloneDir = Files.createTempDirectory( | ||
"m2kfunc-test-git-repo-clone", | ||
PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rwxrwxrwx"))); | ||
} | ||
|
||
@AfterEach | ||
void teardown() throws IOException { | ||
try (var walk = Files.walk(cloneDir)) { | ||
walk.sorted(Comparator.reverseOrder()).forEach(path -> { | ||
try { | ||
Files.deleteIfExists(path); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
} |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
ssh-priv-key-path=${PWD}/src/test/resources/m2k-test-ssh-id_ed25519 | ||
quarkus.log.level=INFO |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Host * | ||
StrictHostKeyChecking no |
7 changes: 7 additions & 0 deletions
7
move2kube/m2k-func/src/test/resources/m2k-test-ssh-id_ed25519
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
-----BEGIN OPENSSH PRIVATE KEY----- | ||
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW | ||
QyNTUxOQAAACDyPQxDOPtUCJI386z0a2G6b3uWDSTBvB2Q3m4EHY4zZQAAAJAgrbKCIK2y | ||
ggAAAAtzc2gtZWQyNTUxOQAAACDyPQxDOPtUCJI386z0a2G6b3uWDSTBvB2Q3m4EHY4zZQ | ||
AAAECWBqGUjVKfROUwn54KARnsnOX+Y8PAAhHWJG3tYZaoyfI9DEM4+1QIkjfzrPRrYbpv | ||
e5YNJMG8HZDebgQdjjNlAAAADXJnb2xhbkBmZWRvcmE= | ||
-----END OPENSSH PRIVATE KEY----- |
1 change: 1 addition & 0 deletions
1
move2kube/m2k-func/src/test/resources/m2k-test-ssh-id_ed25519.pub
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPI9DEM4+1QIkjfzrPRrYbpve5YNJMG8HZDebgQdjjNl rgolan@fedora |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
-----BEGIN RSA PRIVATE KEY----- | ||
MIIEogIBAAKCAQEAxuhZr4YA7l0S/yij3IXXf9jcLxgHdenjRGrF8tGbYp5n7IQb | ||
msbXcfBeUcsmtrBx02TB45nQ5Nox2Lqch5OyKbVo8FzO5m5RGmmHPUokgJGwBtgH | ||
SwxlX/dJ3X/T0Hk7s9X04t4wfbopWRPFIf+tUMp65XxgAdOpfM/yO4MjoRB3zL3X | ||
wI2KDecpwg10SMLiLXSuHZrMnYDMIie3WSBXur5OHQrNfLDLIvZQxAvgO0O3MaKn | ||
Pcq3XgtXonagYniwtUSi0VqUYUjDUTjwr5pc0v+UIJap610Mx8fiT1B63+YBUbQe | ||
C+Ol82FMUKGKM8igDm9xRBD1LFVYg1YLrdBCGwIDAQABAoIBAB8f1oDXuCeUWteg | ||
dVuZeeogdfvMh8ZUreJTztu7HtRksyBYX7VtbeL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