简体   繁体   中英

How to add keystore file by using jmeter library in Java code

Like I have tried adding keystore.jks file in jmeter application by heading over to Options > SSL manager > there choose. jks file and at run time we need to give password/passphrase for that file and it will work, like same way I'm trying to mimic similar thing using jmeter library in Java, but couldn't find any alternative in doing so.

like is there any way to add.jks file through Java code using jmeter library so that to achieve similar thing which I did in jmeter application?

this is what i tried using jmeter library for java, ALIAS_VALUE, ENDPOINT_VALUE, CONTENT_PATH_VALUE, API_KEY_VALUE are used to mask the original value for security concerns .

StandardJMeterEngine jmeterEngine = new StandardJMeterEngine();
        JMeterUtils.setJMeterHome("C:/Users/pmaiya/Downloads/apache-jmeter-5.2.1/apache-jmeter-5.2.1");
        JMeterUtils.loadJMeterProperties("C:/Users/pmaiya/Downloads/apache-jmeter-5.2.1/apache-jmeter-5.2.1/bin/jmeter.properties");
        JMeterUtils.initLocale();
        System.setProperty("javax.net.ssl.keyStore", "C:/Users/pmaiya/Documents/Zoom/identity.jks");
        System.setProperty("javax.net.ssl.keyStorePassword", "123456");

        HashTree testPlanHashTree = new HashTree();




        JmeterKeyStore.getInstance("JKS").load(new FileInputStream("C:/Users/pmaiya/Documents/Zoom/identity.jks"), "123456");
        JmeterKeyStore.DEFAULT_ALIAS_VAR_NAME.contentEquals("ALIAS_VALUE");

        JmeterKeyStore.getInstance("JKS", 0, -1, "test").load(new FileInputStream("C:/Users/pmaiya/Documents/Zoom/identity.jks"), "123456");


       SSLManager.getInstance().configureKeystore(true, 0, -1, "ALIAS_VALUE");
       SSLManager.isSSLSupported();





        // create http sampler
        HTTPSamplerProxy httpSampler = new HTTPSamplerProxy();
        httpSampler.setProtocol("HTTPS");
        httpSampler.setDomain("ENDPOINT_VALUE");
        httpSampler.setPath("CONTEXT_PATH_VALUE");
        httpSampler.setMethod("GET");
        httpSampler.setName("get call");
        httpSampler.setFollowRedirects(true);
        httpSampler.setAutoRedirects(false);
        httpSampler.setUseKeepAlive(true);
        httpSampler.setEnabled(true);
        httpSampler.setProperty(TestElement.TEST_CLASS, HTTPSampler.class.getName());
        httpSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());


        HeaderManager manager = new HeaderManager();
        manager.setEnabled(true);
        manager.add(new Header("apikey", "API_KEY_VALUE"));
        manager.add(new Header("Content-Type", "application/json"));
        manager.setName("headermanager"); 
        manager.setProperty(TestElement.TEST_CLASS, HeaderManager.class.getName());
        manager.setProperty(TestElement.GUI_CLASS, HeaderPanel.class.getName());





        LoopController loopController = new LoopController();
        loopController.setLoops(1);
        loopController.setFirst(true);
        loopController.setProperty(TestElement.TEST_CLASS, LoopController.class.getName());
        loopController.setProperty(TestElement.GUI_CLASS, LoopControlPanel.class.getName());
        loopController.initialize();

        //create thread group
        ThreadGroup threadGroup = new ThreadGroup();
        threadGroup.setNumThreads(1);
        threadGroup.setRampUp(1);
        threadGroup.setSamplerController(loopController);
        threadGroup.setName("get");
        threadGroup.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName());
        threadGroup.setProperty(TestElement.GUI_CLASS, ThreadGroupGui.class.getName());

        //create test plan
        TestPlan testPlan = new TestPlan();
        testPlan.setEnabled(true);
        testPlan.setName("Test Plan");
        testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
        testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
        testPlan.setUserDefinedVariables((Arguments) new ArgumentsPanel().createTestElement());



        //Add sampler to the thread group


        HashTree threadGroupHashTree = testPlanHashTree.add(testPlan, threadGroup);
        threadGroupHashTree.add(httpSampler);
       threadGroupHashTree.add(manager);



        // Generating the JMX file
        SaveService.saveTree(testPlanHashTree, new FileOutputStream("C:/Users/pmaiya/Desktop/TestSSL.jmx"));
        jmeterEngine.configure(testPlanHashTree);
        jmeterEngine.run();            
         System.out.println("executed successfully");

If your keystore contains only one certificate which you need to use for client-side encryption - setting javax.net.ssl.keyStore and javax.net.ssl.keyStorePassword , just make sure to call these functions:

System.setProperty("javax.net.ssl.keyStore", "C:/Users/pmaiya/Documents/Zoom/identity.jks");
System.setProperty("javax.net.ssl.keyStorePassword", "123456");

before this line

JMeterUtils.loadJMeterProperties("C:/Users/pmaiya/Downloads/apache-jmeter-5.2.1/apache-jmeter-5.2.1/bin/jmeter.properties");

If your keystore contains >1 certificate and you would like each JMeter virtual user to use its own certificate - go for Keystore Configuration (the relevant JMeter API class name is org.apache.jmeter.config.KeystoreConfig ) like it's described in How to Use Multiple Certificates When Load Testing Secure Websites article

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM