-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #112 from gdgib/G2-1476-SSHFileSystem
G2-1476 SSH FileSystem, multi-session client, & key support
- Loading branch information
Showing
8 changed files
with
168 additions
and
48 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
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
53 changes: 53 additions & 0 deletions
53
gb-ssh/src/main/java/com/g2forge/gearbox/ssh/SSHConfig.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,53 @@ | ||
package com.g2forge.gearbox.ssh; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.nio.file.FileSystem; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
|
||
import org.apache.sshd.client.session.ClientSession; | ||
import org.apache.sshd.sftp.client.fs.SftpFileSystemClientSessionInitializer; | ||
import org.apache.sshd.sftp.client.fs.SftpFileSystemInitializationContext; | ||
import org.apache.sshd.sftp.client.fs.SftpFileSystemProvider; | ||
|
||
import com.g2forge.alexandria.java.core.helpers.HCollection; | ||
import com.g2forge.alexandria.java.io.RuntimeIOException; | ||
|
||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Data | ||
@Builder(toBuilder = true) | ||
@RequiredArgsConstructor | ||
public class SSHConfig { | ||
protected final SSHRemote remote; | ||
|
||
protected final SSHCredentials credentials; | ||
|
||
public FileSystem createFileSystem() { | ||
final SftpFileSystemProvider provider = new SftpFileSystemProvider(); | ||
provider.setSftpFileSystemClientSessionInitializer(new SftpFileSystemClientSessionInitializer() { | ||
@Override | ||
public void authenticateClientSession(SftpFileSystemProvider provider, SftpFileSystemInitializationContext context, ClientSession session) throws IOException { | ||
final String password = context.getCredentials().getPassword(); | ||
// If no password provided perhaps the client is set-up to use registered public keys | ||
if (password != null) session.addPasswordIdentity(password); | ||
getCredentials().configure(session); | ||
session.auth().verify(context.getMaxAuthTime()); | ||
} | ||
}); | ||
|
||
final SSHRemote remote = getRemote(); | ||
final Collection<? extends String> passwords = getCredentials().getPasswords(); | ||
final String password = passwords.isEmpty() ? null : HCollection.getFirst(passwords); | ||
final URI uri = SftpFileSystemProvider.createFileSystemURI(remote.getHost(), remote.getPort(), remote.getUsername(), password); | ||
|
||
try { | ||
return provider.newFileSystem(uri, Collections.<String, Object>emptyMap()); | ||
} catch (IOException e) { | ||
throw new RuntimeIOException("Failed to create SFTP filesystem for " + this, e); | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
gb-ssh/src/main/java/com/g2forge/gearbox/ssh/SSHCredentials.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,33 @@ | ||
package com.g2forge.gearbox.ssh; | ||
|
||
import java.security.KeyPair; | ||
import java.util.Collection; | ||
|
||
import org.apache.sshd.client.ClientAuthenticationManager; | ||
|
||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.Singular; | ||
import lombok.ToString; | ||
|
||
@Data | ||
@Builder(toBuilder = true) | ||
@RequiredArgsConstructor | ||
public class SSHCredentials { | ||
@ToString.Exclude | ||
@Singular | ||
protected final Collection<? extends String> passwords; | ||
|
||
@Singular | ||
protected final Collection<? extends KeyPair> keys; | ||
|
||
public void configure(ClientAuthenticationManager clientAuthenticationManager) { | ||
if (getPasswords() != null) for (String password : getPasswords()) { | ||
clientAuthenticationManager.addPasswordIdentity(password); | ||
} | ||
if (getKeys() != null) for (KeyPair key : getKeys()) { | ||
clientAuthenticationManager.addPublicKeyIdentity(key); | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
gb-ssh/src/main/java/com/g2forge/gearbox/ssh/SSHRemote.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,34 @@ | ||
package com.g2forge.gearbox.ssh; | ||
|
||
import java.io.IOException; | ||
|
||
import org.apache.sshd.client.SshClient; | ||
import org.apache.sshd.client.future.ConnectFuture; | ||
import org.apache.sshd.client.session.ClientSession; | ||
|
||
import com.g2forge.alexandria.java.io.RuntimeIOException; | ||
|
||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Data | ||
@Builder(toBuilder = true) | ||
@RequiredArgsConstructor | ||
public class SSHRemote { | ||
protected final String username; | ||
|
||
protected final String host; | ||
|
||
protected final int port; | ||
|
||
public ClientSession connect(SshClient client) { | ||
try { | ||
final ConnectFuture future = client.connect(getUsername(), getHost(), getPort()); | ||
if (!future.await()) throw new RuntimeIOException("Failed to connect to " + this); | ||
return future.getSession(); | ||
} catch (IOException exception) { | ||
throw new RuntimeIOException("Failed to connect to " + this, exception); | ||
} | ||
} | ||
} |
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
18 changes: 0 additions & 18 deletions
18
gb-ssh/src/main/java/com/g2forge/gearbox/ssh/SSHServer.java
This file was deleted.
Oops, something went wrong.
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