Authenticating with Azure MSI for Azure SQL #33678
Replies: 3 comments 1 reply
-
Hi, I'm running into the same problem and was wondering if you figured it out. Currently, I'm digging into the framework's code to see how I can accomplish ActiveDirectoryMsi authentication. At present, I can only trigger ActiveDirectoryIntegrated. |
Beta Was this translation helpful? Give feedback.
-
Hello, not sure if this will help someone, but I was able to figure this out by extending the default database connection classes. I created a new folder under app, and copied the following core files over.
These core files can be found in vendor/laravel/framework/src/Illuminate/. After you have these files copied over, lets edit the files. ConnectionFactory.php Change the namespace.
Import the original class so we can extend it.
Comment out the original class and replace it with your new extended class.
SqlServerConnector.php In this file, we need to add some extra attributes to the getSqlSrvDsn() method. Updated the namespace.
Add required classes.
Update getSqlSrvDsn() by adding the following attributes.
After adding the 'Authentication' to the attributes, update you sqlsrv connection settings array in database.php.
We set the default to be SqlPassword. In order to use Active Directory Managed Identities, we can set it to 'ActiveDirectoryMsi' in our .env file
Next, create a new DatabaseServiceProvider.php to use our custom ConnectionFactory.
Copy over the existing DatabaseServiceProvider code located in:
Update the ConnectionFactory class to grab our custom ConnectionnFactory.
Last, in our app/config/app.php file, comment out the existing DatabaseServiceProvider.
And add our new DatabaseServiceProvider.
This should work. If it doesn't, just try playing around with modifying the Custom folder files. This will give you access to modify the connection string. |
Beta Was this translation helpful? Give feedback.
-
Fix for this was merged yesterday on #43757 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Environment:
Hello,
I am working on a project which requires I use a system assigned managed identify to authenticate with a production database instance. After reviewing the available documentation for Laravel, and reading through Microsoft's documentation on the PDO_SQLSRV driver, I have been unable to identify how to handle this based on the available configuration options. I've even gone as far as falling in the rabbit hole of reviewing how the PDO connections are generated. However, I gained little clarity on the latter.
I am posting here to ask for insight on how to achieve this, if anyone has done this before me. Furthermore, if a PR would be desired to make this easier for future sufferers of PHP-in-a-Microsoft-shop, I'd be happy to author that code.
Available documentation: https://docs.microsoft.com/en-us/sql/connect/php/azure-active-directory?view=sql-server-ver15
In the above linked documentation, Microsoft provides this code snippet:
Translating that to PDO style would look something like:
All that being said, my question (which a code review did not answer) is, would it be as simple as adding a key/value to the sqlsrv array for the authentication parameter? So, something like this in
config/database.php
:Any guidance on this will be greatly appreciated. Thank you!
Beta Was this translation helpful? Give feedback.
All reactions