Skip to content
This repository has been archived by the owner on Sep 29, 2023. It is now read-only.

Can't connect to SQL using app registration credentials #206

Closed
michaelcapizzi opened this issue Jun 27, 2019 · 12 comments
Closed

Can't connect to SQL using app registration credentials #206

michaelcapizzi opened this issue Jun 27, 2019 · 12 comments

Comments

@michaelcapizzi
Copy link

michaelcapizzi commented Jun 27, 2019

I am following the steps here, but I'm not able to access my server and database.

import adal
import pyodbc
import struct

# credentials generated by app-registration
USER = "XXXXXXXXXXXXXXXXXX"
PW = "XXXXXXXXXXXXXXXXXX"

database_url = "https://database.windows.net/"

authority_url = "https://login.microsoftonline.com"
tenantId = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
authority_url = authority_url + "/" + tenantId

context = adal.AuthenticationContext(authority_url, api_version=None)

token = context.acquire_token_with_client_credentials(
    database_url,
    USER,
    PW
)
print(token)

tokenb = bytes(token["accessToken"], "UTF-8")

exptoken = b''
for i in tokenb:
    exptoken += bytes({i})
    exptoken += bytes(1)
tokenstruct = struct.pack("=i", len(exptoken)) + exptoken
tokenstruct

SQL_COPT_SS_ACCESS_TOKEN = 1256
CONNSTRING = "DRIVER={};SERVER={};DATABASE={}".format("ODBC Driver 17 for SQL Server", SERVER, DATABASE)

conn = pyodbc.connect(CONNSTRING, attrs_before = { SQL_COPT_SS_ACCESS_TOKEN:tokenstruct })

cursor = conn.cursor()
cursor.execute(QUERY)
row = cursor.fetchone()

The token is generated (accessToken below):

{'tokenType': 'Bearer', 'expiresIn': 3600, 'expiresOn': '2019-06-27 10:36:58.175894', 'resource': 'https://database.windows.net/', 'accessToken': 'XXXXXXXXXXXXXXXXXXXXXXXXXX', 'isMRRT': True, '_clientId': 'XXXXXXXXXXXXXXXXXXXXXX', '_authority': 'https://login.microsoftonline.com/XXXXXXXXXXXXXXX'}

But I get this error when trying to make connection:

    conn = pyodbc.connect(CONNSTRING, attrs_before = { SQL_COPT_SS_ACCESS_TOKEN:tokenstruct })
pyodbc.InterfaceError: ('28000', "[28000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user ''. (18456) (SQLDriverConnect)")

(Note, if from a Windows machine, the error is slightly different....see below):

    conn = pyodbc.connect(CONNSTRING, attrs_before = { SQL_COPT_SS_ACCESS_TOKEN:tokenstruct })
pyodbc.InterfaceError: ('28000', "[28000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user 'NT AUTHORITY\\ANONYMOUS LOGON'. (18456) (SQLDriverConnect); [28000] [Microsoft][ODBC Driver 17 for SQL
Server][SQL Server]Login failed for user 'NT AUTHORITY\\ANONYMOUS LOGON'. (18456)")

My IT guy has confirmed that the app-registration credentials are valid, so the issue is downstream from there, but I don't know where.

@michaelcapizzi
Copy link
Author

The solution was explained here, but basically, my SQL admin had to add permissions for the app-registration to the specific database that I was trying to access.

@rayluo
Copy link
Collaborator

rayluo commented Jun 27, 2019

Hi Michael, thanks for reaching out. That wiki page: (How to) Connect to Azure SQL Database was derived from an upstream pyodbc conversation, which you are now also aware of.

Given that you can successfully obtain an access token, I would also agree with your thought "the issue is downstream from there".

I'm not myself an SQL expert but your CONNSTRING looks good based on this yet another doc "Authenticating with an Access Token". Perhaps you can try to follow its very last See Also link to see if you can find some help.

@rayluo
Copy link
Collaborator

rayluo commented Jun 27, 2019

@michaelcapizzi Glad that you also figured it out! You beat my suggestion (which points to the same solution page) by 4 minutes. LOL

And our wiki page has been updated with your finding. Thank you again Michael!

@imjoseangel
Copy link

imjoseangel commented Nov 4, 2019

The solution was explained here, but basically, my SQL admin had to add permissions for the app-registration to the specific database that I was trying to access.

Hi,

I know this is closed but as far I understand, if you want to automate end to end the creation of a DB, you have the following steps:

  • Server Creation
  • DB Creation
  • DB AD Admin Assignation
  • Manual intervention to DB to give the right access to the automation user
  • DB Automation

Is that right? My next step is trying MSI Authentication, did you try it?

Thanks

@rayluo
Copy link
Collaborator

rayluo commented Nov 4, 2019

@imjoseangel Was that question for @michaelcapizzi ?

There is no extra info from me other than the earlier comment.

Regarding to MSI, that is a feature request currently in the backlog of MSAL Python, which is the successor of ADAL Python. There is no new features planned for ADAL Python.

@imjoseangel
Copy link

Cool, didn't know about MSAL Python. Thanks for answering.

@alpacayao
Copy link

Is app registration credentials supported if client is under MacOS or Linux?

@rayluo
Copy link
Collaborator

rayluo commented Apr 8, 2020 via email

@answerquest
Copy link

My God why am I not finding this sample code anywhere else? I've been looking all over for how to connect to MSSQL from a Python program when my credentials are Active Directory based. All the official documentation does deep into registering your application, getting client / tenant id etc but never mentions how to actually connect to MSSQL after that.

@answerquest
Copy link

Hi, what do I need to tell my DBA to do so I can get this "tenantId" from them?

@answerquest
Copy link

Can I derive this "tenantId" from the servername or something?

@rayluo
Copy link
Collaborator

rayluo commented Jul 21, 2020

@answerquest I thought your earlier comment "All the official documentation does deep into registering your application, getting client / tenant id etc ..." hinted that you was able to find those information?

Regardless, "how to know my tenant id" sounds like a different question in itself. I would suggest you to create a new issue, rather than commenting on a closed issue which may not immediately draws attention.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants