WIP: ssh-agent support using jsch-agent-proxy

This commit is contained in:
Adrian Kuschelyagi Malacoda 2020-11-05 03:35:31 -06:00
parent 35d3de2d89
commit a84a307eb5
10 changed files with 73 additions and 12 deletions

View File

@ -6,7 +6,6 @@
<factorypathentry kind="VARJAR" id="M2_REPO/com/google/code/findbugs/jsr305/3.0.2/jsr305-3.0.2.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/checkerframework/checker-compat-qual/2.5.5/checker-compat-qual-2.5.5.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/com/google/j2objc/j2objc-annotations/1.3/j2objc-annotations-1.3.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/com/google/code/gson/gson/2.8.6/gson-2.8.6.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/com/jcraft/jsch/0.1.55/jsch-0.1.55.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/jodd/jodd-util/6.0.0/jodd-util-6.0.0.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/com/google/android/android/4.1.1.4/android-4.1.1.4.jar" enabled="true" runInBatchMode="false"/>

View File

@ -1,6 +1,7 @@
<factorypath>
<factorypathentry kind="VARJAR" id="M2_REPO/net/monarchpass/libpiecannon/0.0.1-SNAPSHOT/libpiecannon-0.0.1-SNAPSHOT.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/com/github/mwiede/jsch/0.1.60/jsch-0.1.60.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/com/google/code/gson/gson/2.8.6/gson-2.8.6.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/com/google/guava/guava/30.0-jre/guava-30.0-jre.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/com/google/guava/failureaccess/1.0.1/failureaccess-1.0.1.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/com/google/guava/listenablefuture/9999.0-empty-to-avoid-conflict-with-guava/listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar" enabled="true" runInBatchMode="false"/>
@ -9,6 +10,7 @@
<factorypathentry kind="VARJAR" id="M2_REPO/com/google/errorprone/error_prone_annotations/2.3.4/error_prone_annotations-2.3.4.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/com/google/j2objc/j2objc-annotations/1.3/j2objc-annotations-1.3.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/net/kothar/xdg-java/0.1.1/xdg-java-0.1.1.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/com/google/code/gson/gson/2.8.6/gson-2.8.6.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/com/jcraft/jsch.agentproxy.jsch/0.0.9/jsch.agentproxy.jsch-0.0.9.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/com/jcraft/jsch.agentproxy.core/0.0.9/jsch.agentproxy.core-0.0.9.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/projectlombok/lombok/1.18.16/lombok-1.18.16.jar" enabled="true" runInBatchMode="false"/>
</factorypath>

View File

@ -8,5 +8,13 @@ STUB_POM="<project xmlns='http://maven.apache.org/POM/4.0.0' xmlns:xsi='http://w
STUB_POM_FILE=/tmp/pc.xml
echo $STUB_POM > $STUB_POM_FILE
RESULT=$(mvn -f $STUB_POM_FILE -B exec:java -Dexec.mainClass=$PC_CLASS -Dexec.arguments=$1 -Dorg.slf4j.simpleLogger.defaultLogLevel=WARN | tail -n 1)
OUTPUT=$(mvn -f $STUB_POM_FILE -B exec:java -Dexec.mainClass=$PC_CLASS -Dexec.arguments=$1 -Dorg.slf4j.simpleLogger.defaultLogLevel=WARN)
EXEC_RESULT=$?
echo $OUTPUT
if [ $EXEC_RESULT ]; then
exit $EXEC_RESULT;
fi
RESULT=$(echo $OUTPUT | tail -n 1)
xdg-open $RESULT

View File

@ -21,12 +21,28 @@
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.0-jre</version>
</dependency>
</dependency>
<dependency>
<groupId>net.kothar</groupId>
<artifactId>xdg-java</artifactId>
<version>0.1.1</version>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch.agentproxy.jsch</artifactId>
<version>0.0.9</version>
<exclusions>
<exclusion>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch.agentproxy.connector-factory</artifactId>
<version>0.0.9</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>

View File

@ -13,11 +13,19 @@ import java.util.logging.Level;
import net.monarchpass.piecannon.util.ServerFactory;
import com.jcraft.jsch.IdentityRepository;
import com.jcraft.jsch.agentproxy.Connector;
import com.jcraft.jsch.agentproxy.AgentProxyException;
import com.jcraft.jsch.agentproxy.RemoteIdentityRepository;
import com.jcraft.jsch.agentproxy.ConnectorFactory;
@Log
public class App {
public static void main (final String... args) throws Exception {
final File serversJson = getServersJson();
final PieCannon cannon = new PieCannon();
//cannon.getServerFactory().setIdentityRepository(createIdentityRepository());
final List<Server> servers = cannon.loadServersFrom(serversJson);
log.log(Level.INFO, "{0} servers loaded from {1}", new Object[] {
servers.size(), serversJson
@ -53,4 +61,20 @@ public class App {
public static File getDataDirectory () {
return new File(BaseDirectory.get(BaseDirectory.XDG_DATA_HOME), "piecannon");
}
private static IdentityRepository createIdentityRepository () {
try {
final ConnectorFactory connectorFactory = ConnectorFactory.getDefault();
final Connector connector = connectorFactory.createConnector();
if (connector != null) {
return new RemoteIdentityRepository(connector);
}
}
catch (final AgentProxyException exception) {
log.log(Level.WARNING, "Failed to create agent identity repository", exception);
}
return null;
}
}

View File

@ -7,5 +7,6 @@
<factorypathentry kind="VARJAR" id="M2_REPO/org/checkerframework/checker-compat-qual/2.5.5/checker-compat-qual-2.5.5.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/com/google/errorprone/error_prone_annotations/2.3.4/error_prone_annotations-2.3.4.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/com/google/j2objc/j2objc-annotations/1.3/j2objc-annotations-1.3.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/com/google/code/gson/gson/2.8.6/gson-2.8.6.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/projectlombok/lombok/1.18.16/lombok-1.18.16.jar" enabled="true" runInBatchMode="false"/>
</factorypath>

View File

@ -10,24 +10,24 @@ import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import com.google.common.base.Charsets;
import com.google.common.io.CharSource;
import com.google.common.io.ByteStreams;
import java.util.stream.Collectors;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import net.monarchpass.piecannon.util.ServerFactory;
import lombok.Setter;
import lombok.Getter;
public class PieCannon {
public List<Server> servers;
public @Getter @Setter ServerFactory serverFactory = new ServerFactory();
public List<Server> loadServersFrom (final File serversJson) throws IOException {
try (final InputStream in = new FileInputStream(serversJson)) {
@ -36,7 +36,6 @@ public class PieCannon {
}
public List<Server> loadServersFrom (final InputStream in) throws IOException {
final ServerFactory factory = new ServerFactory();
final JsonParser parser = new JsonParser();
final List<Server> loadedServers = new ArrayList<>();
for (final JsonElement element : parser.parse(new InputStreamReader(in)).getAsJsonArray()) {
@ -44,7 +43,7 @@ public class PieCannon {
continue;
}
loadedServers.add(factory.apply((JsonObject)element));
loadedServers.add(serverFactory.apply((JsonObject)element));
}
servers = loadedServers;
return loadedServers;

View File

@ -13,6 +13,7 @@ import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.SftpException;
import com.jcraft.jsch.IdentityRepository;
import lombok.Data;
@ -25,12 +26,17 @@ public class SftpServer implements Server {
private final String password;
private final String path;
private final URI uri;
private final IdentityRepository identityRepository;
public URI upload (String name, ByteSource source) {
try {
final URI target = URI.create(uri.toString() + "/" + name.replace(" ", "%20"));
final JSch jsch = new JSch();
if (identityRepository != null) {
jsch.setIdentityRepository(identityRepository);
}
final Session session = jsch.getSession(username, host, port);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");

View File

@ -5,10 +5,16 @@ import java.net.URI;
import java.util.function.Function;
import com.google.gson.JsonObject;
import com.jcraft.jsch.IdentityRepository;
import net.monarchpass.piecannon.Server;
import net.monarchpass.piecannon.impl.SftpServer;
import lombok.Setter;
public class ServerFactory implements Function<JsonObject, Server> {
private @Setter IdentityRepository identityRepository;
public Server apply (final JsonObject object) {
return makeSftpServer(object);
}
@ -24,7 +30,7 @@ public class ServerFactory implements Function<JsonObject, Server> {
return new SftpServer(
label, host, port, username, password,
path, URI.create(url)
path, URI.create(url), identityRepository
);
}
}

View File

@ -22,7 +22,7 @@ public class SftpTest {
final Server server = new SftpServer(
"Test Server Instance", testServerHost, 22,
testServerUser, testServerPassword, testServerPath,
URI.create(testServerURL)
URI.create(testServerURL), null
);
assertThat(PieCannon.testServer(server)).isTrue();