jmeter-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pmoua...@apache.org
Subject svn commit: r1646084 - in /jmeter/trunk: src/protocol/http/org/apache/jmeter/protocol/http/control/ src/protocol/http/org/apache/jmeter/protocol/http/proxy/ xdocs/ xdocs/usermanual/
Date Tue, 16 Dec 2014 21:23:27 GMT
Author: pmouawad
Date: Tue Dec 16 21:23:26 2014
New Revision: 1646084

URL: http://svn.apache.org/r1646084
Log:
Bug 25430 - HTTP(S) Test Script Recorder : Make it populate HTTP Authorisation Manager 
Bugzilla Id: 25430

Modified:
    jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/control/AuthManager.java
    jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/proxy/Proxy.java
    jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/proxy/ProxyControl.java
    jmeter/trunk/xdocs/changes.xml
    jmeter/trunk/xdocs/usermanual/component_reference.xml

Modified: jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/control/AuthManager.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/control/AuthManager.java?rev=1646084&r1=1646083&r2=1646084&view=diff
==============================================================================
--- jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/control/AuthManager.java
(original)
+++ jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/control/AuthManager.java
Tue Dec 16 21:23:26 2014
@@ -256,8 +256,32 @@ public class AuthManager extends ConfigT
     public void addConfigElement(ConfigElement config) {
     }
 
-    public void addAuth(Authorization auth) {
-        getAuthObjects().addItem(auth);
+    /**
+     * Add newAuthorization if it does not already exist
+     * @param newAuthorization
+     */
+    public void addAuth(Authorization newAuthorization) {
+        boolean alreadyExists=false;
+        PropertyIterator iter = getAuthObjects().iterator();
+        //iterate over authentication objects in manager
+        while (iter.hasNext()) {
+            Authorization authorization = (Authorization) iter.next().getObjectValue();
+            if (authorization == null) {
+                continue;
+            }
+            if (match(authorization,newAuthorization)) {
+                if (log.isDebugEnabled()) {
+                    log.debug("Found the same Authorization object:" + newAuthorization.toString());
+                }
+                //set true, if found the same one
+                alreadyExists=true;
+                break;
+            }
+        }
+        if(!alreadyExists){
+            // if there was no such auth object, add.
+            getAuthObjects().addItem(newAuthorization);
+        }
     }
 
     public void addAuth() {
@@ -419,10 +443,26 @@ public class AuthManager extends ConfigT
         if (STRIP_PORT) {
             return true;
         }
-        return (url.getPort() == HTTPConstants.DEFAULT_HTTP_PORT || 
+        return (url.getPort() == HTTPConstants.DEFAULT_HTTP_PORT ||
                 url.getPort() == HTTPConstants.DEFAULT_HTTPS_PORT);
     }
 
+    /**
+     * Check if two authorization objects are equal
+     * @param a {@link Authorization}
+     * @param b {@link Authorization}
+     * @return true if a and b match
+     */
+    private boolean match(Authorization a, Authorization b){
+        return
+                a.getURL().equals(b.getURL())&&
+                a.getName().equals(b.getName())&&
+                a.getPass().equals(b.getPass())&&
+                a.getDomain().equals(b.getDomain())&&
+                a.getRealm().equals(b.getRealm())&&
+                a.getMechanism().equals(b.getMechanism());
+    }
+
     /** {@inheritDoc} */
     @Override
     public void testStarted() {

Modified: jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/proxy/Proxy.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/proxy/Proxy.java?rev=1646084&r1=1646083&r2=1646084&view=diff
==============================================================================
--- jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/proxy/Proxy.java (original)
+++ jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/proxy/Proxy.java Tue Dec
16 21:23:26 2014
@@ -267,7 +267,9 @@ public class Proxy extends Thread {
              */
             if (headers != null) {
                 headers.removeHeaderNamed(HTTPConstants.HEADER_COOKIE);// Always remove cookies
-                headers.removeHeaderNamed(HTTPConstants.HEADER_AUTHORIZATION);// Always remove
authorization
+                // See https://issues.apache.org/bugzilla/show_bug.cgi?id=25430
+                // HEADER_AUTHORIZATION won't be removed, it will be used
+                // for creating Authorization Manager
                 // Remove additional headers
                 for(String hdr : HEADERS_TO_REMOVE){
                     headers.removeHeaderNamed(hdr);

Modified: jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/proxy/ProxyControl.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/proxy/ProxyControl.java?rev=1646084&r1=1646083&r2=1646084&view=diff
==============================================================================
--- jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/proxy/ProxyControl.java
(original)
+++ jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/proxy/ProxyControl.java
Tue Dec 16 21:23:26 2014
@@ -24,10 +24,12 @@ import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.lang.reflect.InvocationTargetException;
+import java.net.MalformedURLException;
 import java.security.GeneralSecurityException;
 import java.security.KeyStore;
 import java.security.UnrecoverableKeyException;
 import java.security.cert.X509Certificate;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Date;
 import java.util.Enumeration;
@@ -38,6 +40,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.prefs.Preferences;
 
+import org.apache.commons.codec.binary.Base64;
 import org.apache.commons.codec.digest.DigestUtils;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang3.RandomStringUtils;
@@ -58,12 +61,17 @@ import org.apache.jmeter.functions.Inval
 import org.apache.jmeter.gui.GuiPackage;
 import org.apache.jmeter.gui.tree.JMeterTreeModel;
 import org.apache.jmeter.gui.tree.JMeterTreeNode;
+import org.apache.jmeter.protocol.http.control.AuthManager;
+import org.apache.jmeter.protocol.http.control.Authorization;
+import org.apache.jmeter.protocol.http.control.Header;
 import org.apache.jmeter.protocol.http.control.HeaderManager;
 import org.apache.jmeter.protocol.http.control.RecordingController;
+import org.apache.jmeter.protocol.http.gui.AuthPanel;
 import org.apache.jmeter.protocol.http.gui.HeaderPanel;
 import org.apache.jmeter.protocol.http.sampler.HTTPSampleResult;
 import org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase;
 import org.apache.jmeter.protocol.http.sampler.HTTPSamplerFactory;
+import org.apache.jmeter.protocol.http.util.HTTPConstants;
 import org.apache.jmeter.samplers.SampleEvent;
 import org.apache.jmeter.samplers.SampleListener;
 import org.apache.jmeter.samplers.SampleResult;
@@ -77,6 +85,7 @@ import org.apache.jmeter.testelement.pro
 import org.apache.jmeter.testelement.property.JMeterProperty;
 import org.apache.jmeter.testelement.property.PropertyIterator;
 import org.apache.jmeter.testelement.property.StringProperty;
+import org.apache.jmeter.testelement.property.TestElementProperty;
 import org.apache.jmeter.threads.AbstractThreadGroup;
 import org.apache.jmeter.timers.Timer;
 import org.apache.jmeter.util.JMeterUtils;
@@ -108,6 +117,10 @@ public class ProxyControl extends Generi
 
     private static final String HEADER_PANEL = HeaderPanel.class.getName();
 
+    private static final String AUTH_PANEL = AuthPanel.class.getName();
+
+    private static final String AUTH_MANAGER = AuthManager.class.getName();
+
     public static final int DEFAULT_PORT = 8080;
 
     // and as a string
@@ -147,6 +160,10 @@ public class ProxyControl extends Generi
 
     private static final String NOTIFY_CHILD_SAMPLER_LISTENERS_FILTERED = "ProxyControlGui.notify_child_sl_filtered";
// $NON-NLS-1$
 
+    private static final String BASIC_AUTH = "Basic"; // $NON-NLS-1$
+
+    private static final String DIGEST_AUTH = "Digest"; // $NON-NLS-1$
+
     //- JMX file attributes
 
     // Must agree with the order of entries in the drop-down
@@ -340,7 +357,7 @@ public class ProxyControl extends Generi
         samplerDownloadImages = b;
         setProperty(new BooleanProperty(SAMPLER_DOWNLOAD_IMAGES, b));
     }
-    
+
     public void setNotifyChildSamplerListenerOfFilteredSamplers(boolean b) {
         notifyChildSamplerListenersOfFilteredSamples = b;
         setProperty(new BooleanProperty(NOTIFY_CHILD_SAMPLER_LISTENERS_FILTERED, b));
@@ -426,7 +443,7 @@ public class ProxyControl extends Generi
     public boolean getNotifyChildSamplerListenerOfFilteredSamplers() {
         return getPropertyAsBoolean(NOTIFY_CHILD_SAMPLER_LISTENERS_FILTERED, true);
     }
-    
+
     public boolean getRegexMatch() {
         return getPropertyAsBoolean(REGEX_MATCH, false);
     }
@@ -551,6 +568,10 @@ public class ProxyControl extends Generi
                 sampler.setUseKeepAlive(useKeepAlive);
                 sampler.setImageParser(samplerDownloadImages);
 
+                Authorization authorization = createAuthorization(subConfigs, sampler);
+                if (authorization != null) {
+                    setAuthorization(authorization, myTarget);
+                }
                 placeSampler(sampler, subConfigs, myTarget);
             } else {
                 if(log.isDebugEnabled()) {
@@ -559,7 +580,7 @@ public class ProxyControl extends Generi
                 notifySampleListeners = notifyChildSamplerListenersOfFilteredSamples;
                 result.setSampleLabel("["+result.getSampleLabel()+"]");
             }
-        } 
+        }
         if(notifySampleListeners) {
             // SampleEvent is not passed JMeterVariables, because they don't make sense for
Proxy Recording
             notifySampleListeners(new SampleEvent(result, "WorkBench")); // TODO - is this
the correct threadgroup name?
@@ -568,6 +589,70 @@ public class ProxyControl extends Generi
         }
     }
 
+    /**
+     * Detect Header manager in subConfigs,
+     * Find(if any) Authorization header
+     * Construct Authentication object
+     * Removes Authorization if present 
+     *
+     * @param subConfigs {@link TestElement}[]
+     * @param sampler {@link HTTPSamplerBase}
+     * @return {@link Authorization}
+     */
+    private Authorization createAuthorization(final TestElement[] subConfigs, HTTPSamplerBase
sampler) {
+        Header authHeader = null;
+        Authorization authorization = null;
+        // Iterate over subconfig elements searching for HeaderManager
+        for (TestElement te : subConfigs) {
+            if (te instanceof HeaderManager) {
+                List<TestElementProperty> headers = (ArrayList<TestElementProperty>)
((HeaderManager) te).getHeaders().getObjectValue();
+                for (Iterator<?> iterator = headers.iterator(); iterator.hasNext();)
{
+                    TestElementProperty tep = (TestElementProperty) iterator
+                            .next();
+                    if (tep.getName().equals(HTTPConstants.HEADER_AUTHORIZATION)) {
+                        //Construct Authorization object from HEADER_AUTHORIZATION
+                        authHeader = (Header) tep.getObjectValue();
+                        String[] authHeaderContent = authHeader.getValue().split(" ");//$NON-NLS-1$
+                        String authType = null;
+                        String authCredentialsBase64 = null;
+                        if(authHeaderContent.length>=2) {
+                            authType = authHeaderContent[0];
+                            authCredentialsBase64 = authHeaderContent[1];
+                            authorization=new Authorization();
+                            try {
+                                authorization.setURL(sampler.getUrl().toExternalForm());
+                            } catch (MalformedURLException e) {
+                                log.error("Error filling url on authorization, message:"+e.getMessage(),
e);
+                                authorization.setURL("${AUTH_BASE_URL}");//$NON-NLS-1$
+                            }
+                            // if HEADER_AUTHORIZATION contains "Basic"
+                            // then set Mechanism.BASIC_DIGEST, otherwise Mechanism.KERBEROS
+                            authorization.setMechanism(
+                                    authType.equals(BASIC_AUTH)||authType.equals(DIGEST_AUTH)?
+                                    AuthManager.Mechanism.BASIC_DIGEST:
+                                    AuthManager.Mechanism.KERBEROS);
+                            if(BASIC_AUTH.equals(authType)) {
+                                String authCred= new String(Base64.decodeBase64(authCredentialsBase64));
+                                String[] loginPassword = authCred.split(":"); //$NON-NLS-1$
+                                authorization.setUser(loginPassword[0]);
+                                authorization.setPass(loginPassword[1]);
+                            } else {
+                                // Digest or Kerberos
+                                authorization.setUser("${AUTH_LOGIN}");//$NON-NLS-1$
+                                authorization.setPass("${AUTH_PASSWORD}");//$NON-NLS-1$
+                                
+                            }
+                        }
+                        // remove HEADER_AUTHORIZATION from HeaderManager 
+                        // because it's useless after creating Authorization object
+                        iterator.remove();
+                    }
+                }
+            }
+        }
+        return authorization;
+    }
+
     public void stopProxy() {
         if (server != null) {
             server.stopServer();
@@ -698,6 +783,31 @@ public class ProxyControl extends Generi
         }
         return true;
     }
+
+    /**
+     * Find if there is any AuthManager in JMeterTreeModel
+     * If there is no one, create and add it to tree
+     * Add authorization object to AuthManager
+     * @param authorization {@link Authorization}
+     * @param target {@link JMeterTreeNode}
+     */
+    private void setAuthorization(Authorization authorization, JMeterTreeNode target) {
+        JMeterTreeModel jmeterTreeModel = GuiPackage.getInstance().getTreeModel();
+        List<JMeterTreeNode> authManagerNodes = jmeterTreeModel.getNodesOfType(AuthManager.class);
+        if (authManagerNodes.size() == 0) {
+            try {
+                log.debug("Creating HTTP Authentication manager for authorization:"+authorization);
+                AuthManager authManager = newAuthorizationManager(authorization);
+                jmeterTreeModel.addComponent(authManager, target);
+            } catch (IllegalUserActionException e) {
+                log.error("Failed to add Authorization Manager to target node:" + target.getName(),
e);
+            }
+        } else{
+            AuthManager authManager=(AuthManager)authManagerNodes.get(0).getTestElement();
+            authManager.addAuth(authorization);
+        }
+    }
+
     /**
      * Helper method to add a Response Assertion
      * Called from AWT Event thread
@@ -711,6 +821,21 @@ public class ProxyControl extends Generi
     }
 
     /**
+     * Construct AuthManager
+     * @param authorization
+     * @return
+     * @throws IllegalUserActionException
+     */
+    private AuthManager newAuthorizationManager(Authorization authorization) throws IllegalUserActionException
{
+        AuthManager authManager = new AuthManager();
+        authManager.setProperty(TestElement.GUI_CLASS, AUTH_PANEL);
+        authManager.setProperty(TestElement.TEST_CLASS, AUTH_MANAGER);
+        authManager.setName("HTTP Authorization Manager");
+        authManager.addAuth(authorization);
+        return authManager;
+    }
+
+    /**
      * Helper method to add a Divider
      * Called from Application Thread that needs to update GUI (JMeterTreeModel)
      */

Modified: jmeter/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/jmeter/trunk/xdocs/changes.xml?rev=1646084&r1=1646083&r2=1646084&view=diff
==============================================================================
--- jmeter/trunk/xdocs/changes.xml (original)
+++ jmeter/trunk/xdocs/changes.xml Tue Dec 16 21:23:26 2014
@@ -182,6 +182,7 @@ See  <bugzilla>56357</bugzilla> for deta
 
 <h3>HTTP Samplers and Test Script Recorder</h3>
 <ul>
+<li><bug>25430</bug>HTTP(S) Test Script Recorder : Make it populate HTTP
Authorisation Manager. Partly based on a patch from Dzmitry Kashlach (dzmitrykashlach at gmail.com)</li>
 </ul>
 
 <h3>Other samplers</h3>
@@ -228,6 +229,7 @@ See  <bugzilla>56357</bugzilla> for deta
 <ul>
 <li><a href="http://ubikloadpack.com">Ubik Load Pack</a></li>
 <li>Yngvi &amp;THORN;&amp;oacute;r Sigurj&amp;oacute;nsson (blitzkopf at
gmail.com)</li>
+<li>Dzmitry Kashlach (dzmitrykashlach at gmail.com)</li>
 </ul>
 
 <br/>

Modified: jmeter/trunk/xdocs/usermanual/component_reference.xml
URL: http://svn.apache.org/viewvc/jmeter/trunk/xdocs/usermanual/component_reference.xml?rev=1646084&r1=1646083&r2=1646084&view=diff
==============================================================================
--- jmeter/trunk/xdocs/usermanual/component_reference.xml (original)
+++ jmeter/trunk/xdocs/usermanual/component_reference.xml Tue Dec 16 21:23:26 2014
@@ -6303,8 +6303,11 @@ plan because they are likely to change b
 </p>
 <h4>Authorization Manager</h4>
 <p>
-The Proxy server passes on any Authorization headers sent by the browser, but does not save
them in the test plan.
-If the site requires Authorization, you will need to add an Authorization Manager and fill
it in with the necessary entries.
+The HTTP(S) Test Script Recorder grabs "Authentication" header, tries to compute the Auth
Policy. If Authorization Manager was added to target
+controller manually, HTTP(S) Test Script Recorder will find it and add authorization(matching
ones will be removed). Otherwise
+Authorization Manager will be added to target controller with authorization object.
+You may have to fix automatically computed values after recording.
+
 </p>
 <h4>Uploading files</h4>
 <p>



Mime
View raw message