-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use the officially supported apache mina sshd in m2k-func #254
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
apiVersion: v1 | ||
kind: ConfigMap | ||
metadata: | ||
name: m2k-ssh-config | ||
data: | ||
config: | | ||
Host * | ||
StrictHostKeyChecking no |
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/dJ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zBFdA | ||
PTsLjXU8FGVdwteX9Perqt2qyXt0urtaJX2L37VPmSgkp172tcHxuvv1hJWNEIEQ | ||
yVn7fEHkeEPaMG6pQCnCowKBgGJIh0TfE9Wu79wd7+les1GGblciRTc/AET1uoK+ | ||
KH7dyzYAVg5Vty+mMM6Ejze65g2Qux+IgHvbmwKcRzXoQU471vgyucbS8TFb3zA9 | ||
VYT+Y1urcpI0KqxDqMwWDLcbyJpgdcrUsNSlXzZxx+GKhAmM1exMouxpm+1hWw3L | ||
7bjJAoGAAep1Yg8/b0NRJtYQK6duFccrVzeXXSJ6nYcHgquVmg+VEQKwdaZBVDwv | ||
u5QZXEhcC7dziq07Sh4FziGJXKwNzV86860w+MxkJcqrWSuQkprw/3UcODtE+RyN | ||
j4b8grxg4ejd0KtCYzLy+ZpTqZZuotENpXhhcOH9VQRhWYXSSJg= | ||
-----END RSA PRIVATE KEY----- |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that there's a good chance we don't need a custom session factory now that we can you proper .ssh config folder. If we mount the key in the supported defalt name , say id_rsa or id_ed25519 it should work out of the box. Alternatively we could say in the ssh config
IdentityFile /path/to/key
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and of course make publickey the preferred auth