0

From groovy script in SoapUI I need to connect to a mysql database to perform some queries. The problem is that due to security reasons no external access is possible. Therefore it is required to get an ssh access (like a tunnel) and invoke mysql locally.

Initially I was reading the below project properties and then connect to mysql:

ServerUrl=jdbc:mysql://10.255.255.122:3306/db
ServerDbUser=user
ServerDbPwd=password
ServerDriver=com.mysql.jdbc.Driver

def url=testRunner.testCase.testSuite.project.getPropertyValue("ServerUrl")
def usr=testRunner.testCase.testSuite.project.getPropertyValue("ServerDbUser")
def pwd=testRunner.testCase.testSuite.project.getPropertyValue("ServerDbPwd")
def driver=testRunner.testCase.testSuite.project.getPropertyValue("ServerDriver")

com.eviware.soapui.support.GroovyUtils.registerJdbcDriver(driver)
sqlServer = Sql.newInstance(url, usr, pwd, driver)`

But this didn't work so now it is required to establish first a ssh connection to the server with the IP 10.255.255.122 and then open the mysql connection locally. So I guess the Server Url will change to:

ServerUrl=jdbc:mysql://127.0.0.1:3306/db

But I don't know how to set first the ssh connection to the server.

Can someone help me with this?

Thanks.

user3510661
  • 15
  • 2
  • 5

1 Answers1

0

Have a look at http://forum.soapui.org/viewtopic.php?t=15400 and connect to remote mysql database through ssh using java

It will give you an idea about implementing it in soapUI.

Below is the code by Ripon Al Wasim which is available as an answer at the stackoverflow link mentioned above

package mypackage;
import java.sql.*;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class UpdateMySqlDatabase {
    static int lport;
    static String rhost;
    static int rport;
    public static void go(){
        String user = "ripon";
        String password = "wasim";
        String host = "myhost.ripon.wasim";
        int port=22;
        try
            {
            JSch jsch = new JSch();
            Session session = jsch.getSession(user, host, port);
            lport = 4321;
            rhost = "localhost";
            rport = 3306;
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
            System.out.println("Establishing Connection...");
            session.connect();
            int assinged_port=session.setPortForwardingL(lport, rhost, rport);
            System.out.println("localhost:"+assinged_port+" -> "+rhost+":"+rport);
            }
        catch(Exception e){System.err.print(e);}
    }
    public static void main(String[] args) {
        try{
            go();
        } catch(Exception ex){
            ex.printStackTrace();
        }
          System.out.println("An example for updating a Row from Mysql Database!");
          Connection con = null;
          String driver = "com.mysql.jdbc.Driver";
          String url = "jdbc:mysql://" + rhost +":" + lport + "/";
          String db = "testDB";
          String dbUser = "wasim";
          String dbPasswd = "riponalwasim123";
          try{
          Class.forName(driver);
          con = DriverManager.getConnection(url+db, dbUser, dbPasswd);
          try{
          Statement st = con.createStatement();
          String sql = "UPDATE MyTableName " +
                  "SET email = 'ripon.wasim@smile.com' WHERE email='peace@happy.com'";

          int update = st.executeUpdate(sql);
          if(update >= 1){
          System.out.println("Row is updated.");
          }
          else{
          System.out.println("Row is not updated.");
          }
          }
          catch (SQLException s){
          System.out.println("SQL statement is not executed!");
          }
          }
          catch (Exception e){
          e.printStackTrace();
          }
          }
        }
Community
  • 1
  • 1
Abhishek Asthana
  • 1,857
  • 1
  • 31
  • 51
  • Useful links... I'll see if I can translate this to SoapUI groovy script. Thanks – user3510661 Apr 09 '14 at 12:26
  • you probably wouldn't have to change a lot and just be able to reuse most of this code. Let me know if this works for you. – Abhishek Asthana Apr 09 '14 at 13:28
  • I thought this can be done easier, without importing java and java secure channel... – user3510661 Apr 09 '14 at 16:07
  • Well you can create an SSh using groovy too...check out this post http://www.randomactsofsentience.com/2012/02/using-groovy-script-to-ssh-tunnel-to.html – Abhishek Asthana Apr 09 '14 at 16:11
  • I tried the code from your solution and I get this error: `Thu Apr 10 12:03:38 EEST 2014:INFO:com.jcraft.jsch.JSchException: PortForwardingL: local port 127.0.0.1:4321 cannot be bound.` – user3510661 Apr 10 '14 at 09:04
  • unfortunately i cannot debug this problem at my end...however google gave me this http://www.linuxquestions.org/questions/linux-networking-3/unable-to-allocate-port-with-port-forwarding-software-616616/ – Abhishek Asthana Apr 10 '14 at 11:03
  • What is the error that you are getting? Is it still the `cannot be bound` error? – Abhishek Asthana Apr 11 '14 at 19:27