Unable to make JDBC connection when running as rstudio-connect user

This is a weird one...

I have a shiny app that calls out to a database using a JDBC connection with a .jar file that is supplied with the app bundle. The code runs fine on other machines and looks like:

driver <- RJDBC::JDBC(driverClass = "net.snowflake.client.jdbc.SnowflakeDriver", 
        classPath = "localfile.jar")
conn <- RJDBC::dbConnect(driver, url, username, password)
RJDBC::dbGetQuery(conn, statement = sql)

However on Connect, this code throws an error when run as the default rstudio-connect user:

Error in .jcall(drv@jdrv, "Ljava/sql/Connection;", "connect", as.character(url)[1],  : 
  java.lang.NoClassDefFoundError: Could not initialize class 
  net.snowflake.client.core.SFTrustManager

On a hunch, I added a super user on the RStudio Connect host machine to the rstudio-connect group and ran the app under that user and it works!

I'm at a loss here... what possible permissions could the default rstudio-connect user be lacking such that this file (or something Java related) is not accessible? I feel like there are so many possibilities I'm hoping someone knowledgeable could help narrow it down.

Very strange!! Sorry for the late reply here.

I'm at a loss here, too. Does changing the user back to rstudio-connect cause the app to start failing again? Do you know what the permissions on the file are?

You could also try running R as the rstudio-connect user (manually, outside of Connect) and see whether accessing rJava / java / etc. works. Usually this requires something like su -s /bin/bash rstudio-connect to assume the rstudio-connect user.

The other possibility is that R needs to be configured for non-privileged users to use Java. rJava is one of the few packages that we often end up recommending that users install as an "external package," because it does sometimes require some more system-level configuration than Connect knows how to do on installation (sudo /path/to/R CMD javareconf and the like).

https://docs.rstudio.com/connect/admin/package-management.html#external-package-installation

Please do let us know if you try some of these ideas and whether they turn out to be helpful at all! Fixing Java problems always feels a lot like flailing :sweat_smile:

Changing the user back to rstudio-connect in the app Access panel results in the same failure. I threw a file.info call into the app logs, which returned about what I expected (see below). It's worth noting that in this case, I am referencing the driver file from inside a custom package called CARS via system.file, but I get the exact same behavior if I add the driver file directly to the Connect content bundle.

Running the code manually as the rstudio-connect user as you described results in the same failure, even after running R CMD javareconf on the associated R binary.

Could it possibly have something to do with the rstudio-connect user not having a home folder?

file.info output

 size
10/07 13:57:28.772
/opt/rstudio-connect/mnt/packrat/3.6.0/v2/library/CARS/47752effed34bed64299a7e3b42427e6/CARS/java/snowflake-jdbc-3.9.1.jar 30611433
10/07 13:57:28.772
isdir
10/07 13:57:28.772
/opt/rstudio-connect/mnt/packrat/3.6.0/v2/library/CARS/47752effed34bed64299a7e3b42427e6/CARS/java/snowflake-jdbc-3.9.1.jar FALSE
10/07 13:57:28.772
mode
10/07 13:57:28.772
/opt/rstudio-connect/mnt/packrat/3.6.0/v2/library/CARS/47752effed34bed64299a7e3b42427e6/CARS/java/snowflake-jdbc-3.9.1.jar 640
10/07 13:57:28.772
mtime
10/07 13:57:28.772
/opt/rstudio-connect/mnt/packrat/3.6.0/v2/library/CARS/47752effed34bed64299a7e3b42427e6/CARS/java/snowflake-jdbc-3.9.1.jar 2019-09-19 21:00:52
10/07 13:57:28.772
ctime
10/07 13:57:28.772
/opt/rstudio-connect/mnt/packrat/3.6.0/v2/library/CARS/47752effed34bed64299a7e3b42427e6/CARS/java/snowflake-jdbc-3.9.1.jar 2019-09-19 21:00:52
10/07 13:57:28.772
atime
10/07 13:57:28.772
/opt/rstudio-connect/mnt/packrat/3.6.0/v2/library/CARS/47752effed34bed64299a7e3b42427e6/CARS/java/snowflake-jdbc-3.9.1.jar 2019-09-19 21:00:52
10/07 13:57:28.772
uid
10/07 13:57:28.772
/opt/rstudio-connect/mnt/packrat/3.6.0/v2/library/CARS/47752effed34bed64299a7e3b42427e6/CARS/java/snowflake-jdbc-3.9.1.jar 998
10/07 13:57:28.773
gid
10/07 13:57:28.773
/opt/rstudio-connect/mnt/packrat/3.6.0/v2/library/CARS/47752effed34bed64299a7e3b42427e6/CARS/java/snowflake-jdbc-3.9.1.jar 998
10/07 13:57:28.773
uname
10/07 13:57:28.773
/opt/rstudio-connect/mnt/packrat/3.6.0/v2/library/CARS/47752effed34bed64299a7e3b42427e6/CARS/java/snowflake-jdbc-3.9.1.jar rstudio-connect
10/07 13:57:28.773
grname
10/07 13:57:28.773
/opt/rstudio-connect/mnt/packrat/3.6.0/v2/library/CARS/47752effed34bed64299a7e3b42427e6/CARS/java/snowflake-jdbc-3.9.1.jar rstudio-connect

It very well might! I have no idea what dark magic happens inside of Java :stuck_out_tongue:

It could be worth creating other non-sudo users with / without home directories and see if the behavior is reproducible for them?