From cac34ea5cc61d485f56e3dfdee02287db0be3c8d Mon Sep 17 00:00:00 2001 From: Claus Ibsen Date: Fri, 24 Jan 2025 09:56:15 +0100 Subject: [PATCH 1/4] #752: Add getKeys to JSch which makes access to all the config values possible for troubleshooting. --- src/main/java/com/jcraft/jsch/JSch.java | 16 ++++++++++++++ src/test/java/com/jcraft/jsch/JSchTest.java | 24 +++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/main/java/com/jcraft/jsch/JSch.java b/src/main/java/com/jcraft/jsch/JSch.java index c09cff5d..4b6bae2d 100644 --- a/src/main/java/com/jcraft/jsch/JSch.java +++ b/src/main/java/com/jcraft/jsch/JSch.java @@ -27,8 +27,10 @@ package com.jcraft.jsch; import java.io.InputStream; +import java.util.Collections; import java.util.Enumeration; import java.util.Hashtable; +import java.util.Set; import java.util.Vector; public class JSch { @@ -639,6 +641,20 @@ public static void setConfig(String key, String value) { } } + /** + * Returns the config keys for all the stored configurations. + * + * For example this can be used for troubleshooting to enumerate all the configuration values to + * be logged. + * + * @return config keys + */ + public static Set getConfigKeys() { + synchronized (config) { + return Collections.unmodifiableSet(config.keySet()); + } + } + /** * Sets the logger * diff --git a/src/test/java/com/jcraft/jsch/JSchTest.java b/src/test/java/com/jcraft/jsch/JSchTest.java index e2735a7a..80b7aab4 100644 --- a/src/test/java/com/jcraft/jsch/JSchTest.java +++ b/src/test/java/com/jcraft/jsch/JSchTest.java @@ -2,8 +2,11 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Hashtable; +import java.util.Set; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -64,6 +67,27 @@ void checkLoggerBehavior() throws Exception { assertSame(JSch.DEVNULL, jsch.getInstanceLogger(), "instance logger should be DEVNULL"); } + @Test + void getConfigKeys() throws Exception { + Set keys = JSch.getConfigKeys(); + // there are many keys so just assert a high number in case new keys + // are added so this test still passes + + int before = keys.size(); + + assertTrue(before > 150); + assertTrue(keys.contains("diffie-hellman-group14-sha256")); + assertTrue(keys.contains("HashKnownHosts")); + + // add new key + JSch.setConfig("mySpecialKey", "mySpecialValue"); + + // add 1 new key + keys = JSch.getConfigKeys(); + int after = keys.size(); + assertEquals(before + 1, after); + } + static final class TestLogger implements Logger { @Override public boolean isEnabled(int level) { From 78aae2c0f15f82cd2f733341f9f347ed7494729c Mon Sep 17 00:00:00 2001 From: Claus Ibsen Date: Fri, 24 Jan 2025 13:42:38 +0100 Subject: [PATCH 2/4] #752: Add getConfig() to JSch which makes access to all the config values possible for troubleshooting. --- src/main/java/com/jcraft/jsch/JSch.java | 21 +++++++++++++-------- src/test/java/com/jcraft/jsch/JSchTest.java | 16 ++++++++-------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/jcraft/jsch/JSch.java b/src/main/java/com/jcraft/jsch/JSch.java index 4b6bae2d..3a3ba337 100644 --- a/src/main/java/com/jcraft/jsch/JSch.java +++ b/src/main/java/com/jcraft/jsch/JSch.java @@ -29,7 +29,9 @@ import java.io.InputStream; import java.util.Collections; import java.util.Enumeration; +import java.util.HashMap; import java.util.Hashtable; +import java.util.Map; import java.util.Set; import java.util.Vector; @@ -642,17 +644,20 @@ public static void setConfig(String key, String value) { } /** - * Returns the config keys for all the stored configurations. - * - * For example this can be used for troubleshooting to enumerate all the configuration values to - * be logged. - * - * @return config keys + * Gets the configuration */ - public static Set getConfigKeys() { + public static Map getConfig() { + Map ret = new HashMap<>(); synchronized (config) { - return Collections.unmodifiableSet(config.keySet()); + for (Map.Entry entry : config.entrySet()) { + String key = entry.getKey(); + if (key.equals("PubkeyAcceptedKeyTypes")) { + key = "PubkeyAcceptedAlgorithms"; + } + ret.put(key, entry.getValue()); + } } + return Collections.unmodifiableMap(ret); } /** diff --git a/src/test/java/com/jcraft/jsch/JSchTest.java b/src/test/java/com/jcraft/jsch/JSchTest.java index 80b7aab4..830b6996 100644 --- a/src/test/java/com/jcraft/jsch/JSchTest.java +++ b/src/test/java/com/jcraft/jsch/JSchTest.java @@ -5,6 +5,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Hashtable; +import java.util.Map; import java.util.Set; import org.junit.jupiter.api.BeforeEach; @@ -69,22 +70,21 @@ void checkLoggerBehavior() throws Exception { @Test void getConfigKeys() throws Exception { - Set keys = JSch.getConfigKeys(); + Map map = JSch.getConfig(); + // there are many keys so just assert a high number in case new keys // are added so this test still passes - - int before = keys.size(); - + int before = map.size(); assertTrue(before > 150); - assertTrue(keys.contains("diffie-hellman-group14-sha256")); - assertTrue(keys.contains("HashKnownHosts")); + assertTrue(map.containsKey("diffie-hellman-group14-sha256")); + assertTrue(map.containsKey("HashKnownHosts")); // add new key JSch.setConfig("mySpecialKey", "mySpecialValue"); // add 1 new key - keys = JSch.getConfigKeys(); - int after = keys.size(); + map = JSch.getConfig(); + int after = map.size(); assertEquals(before + 1, after); } From 880cae166f6d64736c7ccf486e8c345b74d91d3f Mon Sep 17 00:00:00 2001 From: Claus Ibsen Date: Fri, 24 Jan 2025 14:23:17 +0100 Subject: [PATCH 3/4] #752: Add getConfig() to JSch which makes access to all the config values possible for troubleshooting. --- src/test/java/com/jcraft/jsch/JSchTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/test/java/com/jcraft/jsch/JSchTest.java b/src/test/java/com/jcraft/jsch/JSchTest.java index 830b6996..1ea2f5d2 100644 --- a/src/test/java/com/jcraft/jsch/JSchTest.java +++ b/src/test/java/com/jcraft/jsch/JSchTest.java @@ -6,7 +6,6 @@ import java.util.Hashtable; import java.util.Map; -import java.util.Set; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -69,7 +68,7 @@ void checkLoggerBehavior() throws Exception { } @Test - void getConfigKeys() throws Exception { + void getConfig() throws Exception { Map map = JSch.getConfig(); // there are many keys so just assert a high number in case new keys From 8a592d803c410ff814eda620ea782d66fbc6d878 Mon Sep 17 00:00:00 2001 From: Claus Ibsen Date: Fri, 24 Jan 2025 15:18:32 +0100 Subject: [PATCH 4/4] #752: Add getConfig() to JSch which makes access to all the config values possible for troubleshooting. --- src/main/java/com/jcraft/jsch/JSch.java | 1 - src/test/java/com/jcraft/jsch/JSchTest.java | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/com/jcraft/jsch/JSch.java b/src/main/java/com/jcraft/jsch/JSch.java index 3a3ba337..21eea0dc 100644 --- a/src/main/java/com/jcraft/jsch/JSch.java +++ b/src/main/java/com/jcraft/jsch/JSch.java @@ -32,7 +32,6 @@ import java.util.HashMap; import java.util.Hashtable; import java.util.Map; -import java.util.Set; import java.util.Vector; public class JSch { diff --git a/src/test/java/com/jcraft/jsch/JSchTest.java b/src/test/java/com/jcraft/jsch/JSchTest.java index 1ea2f5d2..a2d2d2b6 100644 --- a/src/test/java/com/jcraft/jsch/JSchTest.java +++ b/src/test/java/com/jcraft/jsch/JSchTest.java @@ -68,7 +68,7 @@ void checkLoggerBehavior() throws Exception { } @Test - void getConfig() throws Exception { + void getConfig() { Map map = JSch.getConfig(); // there are many keys so just assert a high number in case new keys