Home > Software engineering >  Robot Framework: Connect to PostgreSQL Database via SSH Tunnel
Robot Framework: Connect to PostgreSQL Database via SSH Tunnel

Time:10-16

i'm facing the following problem. I've got a PostgreSQL database on a remote server. In order to access it, i need to pass through a SSH Tunnel.

So, the steps of my test are:

  1. create the tunneling
  2. Connect to the db
  3. perform query / queries

I'm able to create the tunnel by using SSHLibrary, and it works:

SSHLibrary.Open Connection    10.xxx.xxx.xx  
SSHLibrary.Login     MySSH_User    MySSH_Password    delay=1      
SSHLibrary.Create Local SSH Tunnel    9191    ${IP_DATABASE}    22

So i go on by using DatabaseLibrary to perform the connection (and i've checked all the fields are right):

DatabaseLibrary.Connect To Database    psycopg2    ${DB_NAME}    ${USER_DB}    ${PASSWORD_DB}    ${IP_DATABASE}    ${PORT_DB}

but i get the following error:

OperationalError: could not connect to server: Connection timed out (0x0000274C/10060)
        Is the server running on host "10.xxx.xxx.xx" and accepting
        TCP/IP connections on port 5432?

How can i solve it? I think i need to specify in some way that the database connection must be done by using the tunnel but i don't know how to do it. Here's the complete test code:

*** Settings ***
Library    DatabaseLibrary
Library    SSHLibrary

*** Variables ***
${IP_DATABASE}    10.xxx.xxx.xx
${PORT_DB}    5432
${DB_NAME}    MyNameDB
${SCHEMA_DATABASE}    MySchemaDB
${USER_DB}    MyUserDB
${PASSWORD_DB}    MyPasswordDB

*** Keywords ***
    
*** Test Cases ***
Connect To DB Via SSH
    SSHLibrary.Open Connection    10.xxx.xxx.xx  
    SSHLibrary.Login     MySSH_User    MySSH_Password    delay=1      
    SSHLibrary.Create Local SSH Tunnel    9191    ${IP_DATABASE}    22
    Sleep    2s
    DatabaseLibrary.Connect To Database    psycopg2    ${DB_NAME}    ${USER_DB}    ${PASSWORD_DB}    ${IP_DATABASE}    ${PORT_DB}

I would like to do it by using RF, Anyone can help? Thank's a lot

CodePudding user response:

Ssh tunnels should work in a way that that you establish SSH connection to another host and expose a PORT in localhost that will then tunnel a traffic from that port in localhost to a IP/PORT in the end point of the tunnel ..

So, lets say you have a host at 192.168.100.10 where your robotframework is running and it needs to connect to a database in 192.168.50.100:

If the postgres is accessible via ssh directly:

Connect To DB Via SSH
    ${IP_DATABASE}=  192.168.50.100
    ${DB_PORT}=  5432
    SSHLibrary.Open Connection    ${IP_DATABASE}  
    SSHLibrary.Login     MySSH_User    MySSH_Password    delay=1      
    SSHLibrary.Create Local SSH Tunnel    ${DB_PORT}    ${IP_DATABASE}    ${DB_PORT}
    DatabaseLibrary.Connect To Database    psycopg2    ${DB_NAME}    ${USER_DB}    ${PASSWORD_DB}    127.0.0.1    ${PORT_DB}

Now, if you have to use ssh jump host, eg, connect to a ssh server that will then connect to another server where postgres is running, all you need to do is change the tunnel ip address where the traffic will be forward to:

Connect To DB Via SSH
    ${IP_DATABASE}=  192.168.50.100
    ${IP_SSH_JUMPHOST}=   192.168.50.1
    ${DB_PORT}=  5432
    SSHLibrary.Open Connection    ${IP_JUMPHOST}  
    SSHLibrary.Login     MySSH_User    MySSH_Password    delay=1      
    SSHLibrary.Create Local SSH Tunnel    ${DB_PORT}    ${IP_DATABASE}    ${DB_PORT}
    DatabaseLibrary.Connect To Database    psycopg2    ${DB_NAME}    ${USER_DB}    ${PASSWORD_DB}    127.0.0.1    ${PORT_DB}
  • Related