Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to RServe from JAVA using authentication

  • I am running RServe from Server machine using cmd

    Rserve.exe --RS-conf Rserv.conf --RS-port 12306
    
  • Rserv.conf file has following content:

    pwdfile RserveAuth.txt
    auth required
    remote enable
    plaintext disable

  • RserveAuth.txt has following contents:

    Admin 123456

  • I am connecting to R Server from JAVA

    import org.rosuda.REngine.REXPMismatchException;
    import org.rosuda.REngine.REngineException;
    import org.rosuda.REngine.Rserve.RConnection;
    import org.rosuda.REngine.Rserve.RserveException;
    import org.rosuda.REngine.REXP;
    import org.rosuda.REngine.*;
    public class ConnecttoR
    {
      ...
      ...
     public void connectR()
     {
         try 
        { 
            RConnection connection = new RConnection("172.16.33.242",12306); // Works if authentication is not required in Rserv.conf 
        }
        catch (RserveException e) {
        e.printStackTrace();
        } 
        catch(REXPMismatchException e){
        e.printStackTrace();
        }
        catch(REngineException e){
        e.printStackTrace();            
    } 
     }
    }
    
  • Connection to Rserve is open to all without username & Password. How shall I add security and allow connection only with valid credentials to access Rserve

like image 239
Akki Avatar asked Nov 15 '17 10:11

Akki


1 Answers

As you have enabled the authentification after creating the connection as a first command you need to execute the login command. The Java library has a special wrapper for it.

See code below for example use case.

RConnection connection = new RConnection("127.0.0.1",12306);
connection.login("Admin", "123456");
REXP x = connection.eval("R.version.string");
System.out.println(x.asString());

Also, I would recommend using full path as the pwdfile value.

like image 184
Babl Avatar answered Oct 05 '22 08:10

Babl