One solution is to use the keyringr
package and use it as following. I adapted the Howto on the CRAN page.
This is the solution for a Windows machine. First of all one has to create a small powershell script PasswordEncryption.ps1:
# Create directory user profile if it doesn't already exist.
$passwordDir = "DPAPI\passwords\$($env:computername)"
New-Item -ItemType Directory -Force -Path $passwordDir
# Prompt for password to encrypt
$account = Read-Host "Please enter a label for the text to encrypt. This will be how you refer to the password in R. eg. MYDB_MYUSER"
$SecurePassword = Read-Host -AsSecureString "Enter password" | convertfrom-securestring | out-file "$($passwordDir)\$($account).txt"
# Check output and press any key to exit
Write-Host "Press any key to continue..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
After the execution of this script and the entering of the label and the password one now can use the encrypted password in R.
library(keyringr)
credential_label <- "MYDB_MYUSER"
credential_path <- paste( getwd(),'\\DPAPI\\passwords\\', Sys.info()["nodename"], '\\', credential_label, '.txt', sep="")
my_pwd <- decrypt_dpapi_pw(credential_path)
print(my_pwd)
Or better just add the call to decrypt the password directly to the ODBC command and don't store it in the R environment.
library(DBI)
rodbc <- DBI::dbConnect(RODBCDBI::ODBC()
, dsn = "DSN0123"
, user = "user"
, password = decrypt_dpapi_pw(credential_path))
Edit: An alternative, when using rstudio is to use the rstudioapi, like this:
rodbc <- DBI::dbConnect(RODBCDBI::ODBC()
, dsn = "DSN0123"
, user = rstudioapi::askForPassword("Database username")
, password = rstudioapi::askForPassword("Database password")