Connecting to Snowflake via SSO for RStudio - r

I am currently trying to connect to Snowflake via Azure AD SSO. What I would like is for the browser window to pop up so that I can put in my credentials to log me into Snowflake.
This is the code that I am currently using:
connection <- DBI::dbConnect(
drv = odbc::odbc(),
dsn = "snowflake",
server = "XXXXXXXXXXX.snowflakecomputing.com",
Trusted_Connection = "True",
authenticator = "externalbrowser"
)
A browser window does pop up, but it only has this message:
Your identity was confirmed and propagated to Snowflake ODBC driver. You can close this window now and go back where you started from.
And in my R console it says this:
Error: nanodbc/nanodbc.cpp:1021: 28000: The user you were trying to authenticate as differs from the user currently logged in at the IDP.
Where am I going wrong and how do I do this? :(

ODBC required connection parameters include the user id for the connection. As #Sergiu suggested, add the user information into the connection parameters. The user would be either yourself or whatever service account you may be using in RStudio.

Related

Problem while trying to connect from R to SQL server using a service account not the default Windows authentication

I am in a corporate environment. Using packages DBI and odbc I can easily establish a connection between R and a SQL database like that:
con <- DBI::dbConnect(
odbc::odbc(),
driver = SQL.driver,
server = SQL.server,
database = SQL.database,
encoding = SQL.encoding
)
I understood that, without specific UID/PWD, the Windows authentication is used by DBI.
Since this R code will have to run on an external server (in fact, it is a shiny application behind), I need to specify a UID/PWD, and for that I requested and get "service account" from my IT department.
However, as soon as I specify the service account UID/PWD to DBI::dbConnect() I get a login failure:
con <- DBI::dbConnect(
odbc::odbc()
,driver = SQL.driver
,server = SQL.server
,database = SQL.database
,encoding = SQL.encoding
,UID = SQL.UID
,PWD = SQL.PWD
)
# Error: nanodbc/nanodbc.cpp:1021: 28000: [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'xxxxxxxx'.
If I manually open MS SQL server and try to connect with this service account using the "SQL server authentication method" instead of "Windows authentication" I also have the same error.
According to the IT department, this is "normal" and I think I am supposed to launch MS SQL server using this service account as user, then to use the Windows authentication to connect. Funny thing is that we are not allowed to "run as" another user.
Therefore, assuming this mechanic would work, how one can mimic this behavior in R please?
I have a similar set up at work with personal creds and service acct creds to a MS SQLServer db. I use the following to connect with the service account...
Note that you will have to download the mssql jdbc jar file.
username <= "myusername"
password <- "mypassword"
drv <- RJDBC::JDBC("com.microsoft.sqlserver.jdbc.SQLServerDriver",
'mssql-jdbc-7.0.0.jre8.jar')
connection_string <- paste0("jdbc:sqlserver://10.10.10.01\\srv1;databaseName=dbname;user=",
username, ";password=", password, ";")
conn <- RJDBC::dbConnect(drv, connection_string)

Login Failed for ' '. when using DSN with Rstudio connecting to MS SQL 2016

Running into a odd error when trying to move a SQL connection details from R code to the DSN and using the DSN as the connection string.
Using the ODBC package, I can build a connection through RStudio using:
con <- DBI::dbConnect(odbc::odbc(),
Driver = "SQL Server Native Client 11.0",
Server = "XXX",
Database = "YYY",
uid = "username",
pwd = "password",
port = 1443)
This code above works correctly and allows the SQL connection. When moving this to the DSN (have attempted both user and system DSN) an error is generated. Both user and system DSN's have been tried and when connections are tested from the ODBC application in Windows (Win 10), the test is successful.
When using the code below in R, the connection fails.
con <- DBI::dbConnect(odbc::odbc(), "ZZZ")
Error: nanodbc/nanodbc.cpp:950: 28000: [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user ''
This would seem to indicate that the username is unknown but my information is that all of the connection parameters are stored in the DSN entry. My initial setup was generated from:
https://support.rstudio.com/hc/en-us/articles/214510788-Setting-up-R-to-connect-to-SQL-Server-
Following the further discussion at the bottom of the page, only appears to discuss the code version of the connection.
https://db.rstudio.com/databases/microsoft-sql-server/
This link discusses the DSN component as well as ensuring the driver is available.
https://db.rstudio.com/getting-started/connect-to-database
This confirms the code structure when trying to use the DSN.
I also verified this code snippet via:
https://www.r-bloggers.com/setting-up-an-odbc-connection-with-ms-sql-server-on-windows/
I attempted to use the SQL Server Driver versus the Native Client version and received the same error except the ODBC SQL Server Driver identifier instead of the Native Client 11.0. This seems to indicate that the dbconnect call is at least identifying the DSN entry.
Using the code below I confirmed that the Driver for both SQL Server and native Client 11.0 were available.
sort(unique(odbcListDrivers()[[1]]))
Add trusted connection attribute
con <- DBI::dbConnect(odbc::odbc(),
Driver = "SQL Server Native Client 11.0",
Server = "XXX",
Database = "YYY",
Trusted_Connection = "True",
uid = "username",
pwd = "password",
port = 1443)
Not sure if you're encountering the same issue, but the credentials used during DSN creation (Windows account) were different from those used in the dbConnect function call (somehow, my Microsoft account).
The user in the error message did not exist in the Security roles within SQL Server, thus not granting access to connect to the database.
Granting the user in the error message access to the required SQL server/database fixed the issue*.
CREATE LOGIN [MicrosoftAccount\xyz#outlook.com] FROM WINDOWS WITH DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[us_english]
ALTER SERVER ROLE [sysadmin] ADD MEMBER [MicrosoftAccount\xyz#outlook.com]
* though additional security measures are advised in role creation, off-topic here.

Cannot connect to Azure SQL DataWarehouse from RStudio using Active Directory Authentication

I am trying to connect to Azure SQL Datawarehouse using RStudio. The only Authentication that is setup on the warehouse is Active Directory Password Authentication. I tried using the below connection string.
connectionString="Data Source = abc.database.secure.windows.net; Authentication=Active Directory Password;
Initial Catalog=dbo; UID='UserName';
PWD= ;
It fails with "neither DSN nor SERVER keywork supplied" . And I dont want to set up a DSN as I am building a front end app and it must be portable.
I tried RODBC too ..but couldn't get much help with Active Dir Authentication.
I finally got to the bottom of this after a few deadends. A key step is updating your ODBC driver to ODBC Driver 17. The following works using Azure Active Directory authentication, which is slightly different to a consumer key/consumer secret but may achieve the same outcome (i.e. authentication without a SQL password).
library(DBI)
server <- "yourserver.database.windows.net"
database = "database-name"
con <- DBI::dbConnect(odbc::odbc(),
UID = rstudioapi::askForPassword("username"),
Driver="ODBC Driver 17 for SQL Server",
Server = server, Database = database,
Authentication = "ActiveDirectoryInteractive")
For integrated AD authentication the connection string should look like:
Driver={ODBC Driver 13 for SQL Server};Server=tcp:{full qualified server name},1433;Database={dbname};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;Authentication=ActiveDirectoryIntegrated
For AD passoword authentication:
Driver={ODBC Driver 13 for SQL Server};Server=tcp:{full qualified server name},1433;Database={dbname};Uid={your_user_name};Pwd={your_password_here};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;Authentication=ActiveDirectoryPassword
Did you download the ADALSQL.DLL library https://www.microsoft.com/en-us/download/details.aspx?id=48742? required to connect to Azure AD. Also to double check the ODBC driver you use is 13.1 - correct?
If the recommendation indicated above (the ADALSQL.dll download) will not solve your problem, please create a customer case for "Azure AD authentication with SQL DB" and report it to SQL customer service.
This code works but a window appears:
library(DBI)
server <- "xxxxxxxx.database.windows.net"
database = "myDB"
con <- DBI::dbConnect(odbc::odbc(),
UID = "name#mycompany.com",
Driver="ODBC Driver 17 for SQL Server",
Server = server, Database = database,
Authentication = "ActiveDirectoryInteractive")

DSN tests ok, but login failure trying to use the DSN to SQL Server 2005

I'm writing a small ASP.NET application in a hosted environment (meaning I don't own the server).
Using the hosting provider's webtools, I created a DSN that specifies the Driver, the Server, the UID, the PWD, and the Database. When I test the connection, it tests out fine.
However, when I load my web page with the code:
OdbcConnection DB = new OdbcConnection("DSN=MyDSNName");
DB.Open();
I get the error: ERROR [28000] [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user ''.
I know I'm using the correct DSN name because when I change to "DSN=NonExistentDSN" I get a different error.
I don't understand why the logon works when I test it but not when I use it in code. Since I don't own the server, some of the usual troubleshooting tools aren't available to me, but I'd appreciate any feedback the community has.
Perhaps the DSN does not retain the password. Have you tried to supply login credentials?
OdbcConnection DB = new OdbcConnection("DSN=MyDSNName;UID=login;PWD=password;");
DB.Open();

Connect to iSeries with OdbcConnection: password prompting

I want to connect to DB using the iSeries Client Access driver. I use the following connection string:
DRIVER=Client Access ODBC Driver (32-bit);QUERYTIMEOUT=0;PKG=QGPL/DEFAULT(IBM),2,0,1,0,512;LANGUAGEID=ENU;DFTPKGLIB=QGPL;DBQ=QGPL
XXXXXXXX;SYSTEM=XXX.XXXXXXX.XXX;Signon=2
I get an exception when connecting:
ERROR [28000] [IBM][iSeries Access ODBC Driver]Communication link failure. comm rc=8015 - CWBSY1006 - User ID is invalid, Password length = 0, Prompt Mode = Never
How can I make the application prompt the user for his credentials to the DB?
I presume that this is a desktop application and not a server application based on the wording of the application.
If you could make use of the ODBC setup within the ODBC Data Source Administrator, that can be set to use the default setup of iSeries access.
Otherwise, you might have to prompt for the username and password within the application then pass that into the connection string.
Here's a quick example of how to make an ODBC connection to an iSeries from Excel. It'll prompt for a username and password if it doesn't already have a connection. You'll need the iSeries Navigator (aka client access) ODBC driver installed on the client.
Dim DB2Con As New ADODB.Connection
DB2Con.Properties("Prompt") = adPromptComplete
DB2Con.Open "DRIVER=Client Access ODBC Driver (32-bit);SIGNON=1;SYSTEM = YOURSYSTEMNAME"

Resources