jmeter-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pmoua...@apache.org
Subject [jmeter] branch master updated: JOrphanUtils: add random alphanumeric password generator
Date Sat, 21 Sep 2019 16:06:50 GMT
This is an automated email from the ASF dual-hosted git repository.

pmouawad pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jmeter.git


The following commit(s) were added to refs/heads/master by this push:
     new 94248f0  JOrphanUtils: add random alphanumeric password generator
94248f0 is described below

commit 94248f0d998c23ade211561f97154afdcb394772
Author: pmouawad <p.mouawad@ubik-ingenierie.com>
AuthorDate: Sat Sep 21 18:05:27 2019 +0200

    JOrphanUtils: add random alphanumeric password generator
    
    This resolves Bug 63760
    https://bz.apache.org/bugzilla/show_bug.cgi?id=63760
---
 src/jorphan/build.gradle.kts                            |  1 +
 .../main/java/org/apache/jorphan/util/JOrphanUtils.java | 17 +++++++++++++++++
 .../java/org/apache/jorphan/exec/TestKeyToolUtils.java  |  4 ++--
 .../java/org/apache/jorphan/util/TestJorphanUtils.java  |  6 ++++++
 .../apache/jmeter/protocol/http/proxy/ProxyControl.java |  5 ++---
 xdocs/changes.xml                                       |  1 +
 6 files changed, 29 insertions(+), 5 deletions(-)

diff --git a/src/jorphan/build.gradle.kts b/src/jorphan/build.gradle.kts
index e1372d2..6bd5e6a 100644
--- a/src/jorphan/build.gradle.kts
+++ b/src/jorphan/build.gradle.kts
@@ -25,4 +25,5 @@ dependencies {
     implementation("commons-collections:commons-collections")
     implementation("org.apache.commons:commons-lang3")
     implementation("org.apache.commons:commons-math3")
+    implementation("org.apache.commons:commons-text")
 }
diff --git a/src/jorphan/src/main/java/org/apache/jorphan/util/JOrphanUtils.java b/src/jorphan/src/main/java/org/apache/jorphan/util/JOrphanUtils.java
index e700bed..d301a2a 100644
--- a/src/jorphan/src/main/java/org/apache/jorphan/util/JOrphanUtils.java
+++ b/src/jorphan/src/main/java/org/apache/jorphan/util/JOrphanUtils.java
@@ -26,6 +26,7 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.ServerSocket;
 import java.net.Socket;
+import java.security.SecureRandom;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
@@ -35,6 +36,7 @@ import java.util.regex.Matcher;
 
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.text.RandomStringGenerator;
 
 /**
  * This class contains frequently-used static utility methods.
@@ -46,6 +48,8 @@ import org.apache.commons.lang3.StringUtils;
 public final class JOrphanUtils {
 
     private static final int DEFAULT_CHUNK_SIZE = 4096;
+
+    private static final SecureRandom SECURE_RANDOM = new SecureRandom(new SecureRandom().generateSeed(128));
     /**
      * Private constructor to prevent instantiation.
      */
@@ -765,4 +769,17 @@ public final class JOrphanUtils {
         }
         return retVal.toString();
     }
+
+    /**
+     * @param length Max length of password
+     * @return String random password
+     */
+    public static String generateRandomAlphanumericPassword(int length) {
+        char[][] pairs = {{'a','z'}, {'A','Z'}, {'0','9'}};
+        RandomStringGenerator pwdGenerator = new RandomStringGenerator.Builder()
+                .usingRandom(max -> SECURE_RANDOM.nextInt(max))
+                .withinRange(pairs)
+                .build();
+        return pwdGenerator.generate(length);
+    }
 }
diff --git a/src/jorphan/src/test/java/org/apache/jorphan/exec/TestKeyToolUtils.java b/src/jorphan/src/test/java/org/apache/jorphan/exec/TestKeyToolUtils.java
index ef4dd96..0533b63 100644
--- a/src/jorphan/src/test/java/org/apache/jorphan/exec/TestKeyToolUtils.java
+++ b/src/jorphan/src/test/java/org/apache/jorphan/exec/TestKeyToolUtils.java
@@ -28,7 +28,7 @@ import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 
-import org.apache.commons.lang3.RandomStringUtils;
+import org.apache.jorphan.util.JOrphanUtils;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -36,7 +36,7 @@ import org.junit.Test;
 public class TestKeyToolUtils {
 
     private File keystore;
-    private String password = RandomStringUtils.randomAlphabetic(32);
+    private String password = JOrphanUtils.generateRandomAlphanumericPassword(32);
     private int validity = 1;
 
     @Before
diff --git a/src/jorphan/src/test/java/org/apache/jorphan/util/TestJorphanUtils.java b/src/jorphan/src/test/java/org/apache/jorphan/util/TestJorphanUtils.java
index 3d9189f..3b4f3f1 100644
--- a/src/jorphan/src/test/java/org/apache/jorphan/util/TestJorphanUtils.java
+++ b/src/jorphan/src/test/java/org/apache/jorphan/util/TestJorphanUtils.java
@@ -402,4 +402,10 @@ public class TestJorphanUtils {
         assertEquals("11", JOrphanUtils.unsplit(new Object[] {null, 1}, 1));
         assertEquals("-26738698", JOrphanUtils.unsplit(new Object[] {-26_738_698}, 1));
     }
+
+    @Test
+    public void testGenerateRandomAlphanumericPassword() {
+        assertTrue("Password should be alphanumeric", JOrphanUtils.generateRandomAlphanumericPassword(20).matches("[A-Za-z0-9]+"));
+        assertEquals("Password should be alphanumeric", 20, JOrphanUtils.generateRandomAlphanumericPassword(20).length());
+    }
 }
diff --git a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/proxy/ProxyControl.java
b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/proxy/ProxyControl.java
index 634a58d..aaec3e2 100644
--- a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/proxy/ProxyControl.java
+++ b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/proxy/ProxyControl.java
@@ -46,7 +46,6 @@ import java.util.prefs.Preferences;
 
 import org.apache.commons.codec.binary.Base64;
 import org.apache.commons.codec.digest.DigestUtils;
-import org.apache.commons.lang3.RandomStringUtils;
 import org.apache.commons.lang3.time.DateUtils;
 import org.apache.http.conn.ssl.AbstractVerifier;
 import org.apache.jmeter.assertions.Assertion;
@@ -1498,7 +1497,7 @@ public class ProxyControl extends GenericController implements NonTestElement
{
             }
         }
         if (keyStore == null) { // no existing file or not valid
-            storePassword = RandomStringUtils.randomAlphanumeric(20); // Alphanum to avoid
issues with command-line quoting
+            storePassword = JOrphanUtils.generateRandomAlphanumericPassword(20); // Alphanum
to avoid issues with command-line quoting
             keyPassword = storePassword; // we use same password for both
             setPassword(storePassword);
             log.info(
@@ -1563,7 +1562,7 @@ public class ProxyControl extends GenericController implements NonTestElement
{
             }
         }
         if (keyStore == null) { // no existing file or not valid
-            storePassword = RandomStringUtils.randomAlphanumeric(20); // Alphanum to avoid
issues with command-line quoting
+            storePassword = JOrphanUtils.generateRandomAlphanumericPassword(20); // Alphanum
to avoid issues with command-line quoting
             keyPassword = storePassword; // we use same password for both
             setPassword(storePassword);
             log.info("Generating standard keypair in {}", CERT_PATH_ABS);
diff --git a/xdocs/changes.xml b/xdocs/changes.xml
index ceeb021..e425518 100644
--- a/xdocs/changes.xml
+++ b/xdocs/changes.xml
@@ -134,6 +134,7 @@ to view the last release notes of version 5.1.1.
     <li><bug>63480</bug>XPathAssertion and XPathAssertion2: Improve test
coverage for input coming from variable. Contributed by Ubik Load Pack (support at ubikloadpack.com)</li>
     <li><bug>63452</bug>Tools / Import from cURL: Complete coverage of
all command line options that are valid in JMeter use case. Contributed by Ubik Load Pack
(support at ubikloadpack.com)</li>
     <li><bug>63419</bug>Tools / Import from cURL: Add ability to import
a set of cURL commands from a file. Contributed by Ubik Load Pack (support at ubikloadpack.com)</li>
+    <li><bug>63760</bug>JOrphanUtils: add random alphanumeric password
generator</li>
 </ul>
 
 <ch_section>Non-functional changes</ch_section>


Mime
View raw message