Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use Federated credentials with service principal to auth the Azure resources #1362

Closed
kachely26 opened this issue Jun 13, 2024 · 7 comments

Comments

@kachely26
Copy link

Please first make sure you have looked at:

Environment

To diagnose, we usually need to know the following, including version numbers. On Windows, be
sure to specify 32-bit Python or 64-bit:

  • Python: 3.10
  • pyodbc: 5.0.1
  • OS: linux ubuntu 18.04
  • DB: Azure Synapse serverless DB
  • driver: pyodbc

Issue

Due to security issue, I cannot use clientid + secret when I use service principal as the authentication method.
Then how can I use federated credentials to do the authentication?
Thank you!

Often it is easiest to describe your issue as "expected behavior" and "observed behavior".

@v-chojas
Copy link
Contributor

If you're using the msodbcsql (17 or 18) driver you can give it the access token directly: https://learn.microsoft.com/en-us/sql/connect/odbc/using-azure-active-directory#authenticating-with-an-access-token

@gordthompson
Copy link
Collaborator

Hey @v-chojas - Would this be the same issue as documented in the wiki here

https://github.com/mkleehammer/pyodbc/wiki/Tips-and-Tricks-by-Database-Platform#authenticate-using-an-access-token-from-azure-active-directory

making this a duplicate of #228 ?

@v-chojas
Copy link
Contributor

@gordthompson I wouldn't call this a duplicate without knowing more details about what the OP is trying to accomplish ("federated credentials" is vague and can refer to various things) or indeed if this is the msodbcsql driver being used.

@kachely26
Copy link
Author

Thank you so much @v-chojas and @gordthompson!
I think this is what I want https://github.com/mkleehammer/pyodbc/wiki/Tips-and-Tricks-by-Database-Platform#authenticate-using-an-access-token-from-azure-active-directory
Besides, when I use access token, should I set the connection as something like below?
Driver={ODBC Driver 18 for SQL Server};Server={server};Authentication=ActiveDirectoryIntegrated;Encrypt=yes;?
Which Authentication type should I use for access token authentication?
Should I set the token when calling connect function? Is it possible to set it in the connection string?
conn = pyodbc.connect(connstr, attrs_before = { 1256:bytearray(tokenstruct) })

@v-chojas
Copy link
Contributor

@kachely26
Copy link
Author

I got the access token from

https://review.learn.microsoft.com/en-us/identity/microsoft-identity-platform/federated-identity-credentials?branch=main&tabs=python#msal-python

Does the access token a correct one?

I see it returns me Login failed for user \'<token-identified principal>\'. The server is not currently configured to accept this token. (18456) (SQLDriverConnect)")\n(Background on this error at: http://sqlalche.me/e/13/rvf5)

Could you provide some insights here?

@v-chojas
Copy link
Contributor

You need to add the corresponding account on the server with CREATE USER ... FROM EXTERNAL PROVIDER

Repository owner locked and limited conversation to collaborators Jun 20, 2024
@gordthompson gordthompson converted this issue into discussion #1364 Jun 20, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants