{"id":4277,"date":"2025-02-18T11:07:29","date_gmt":"2025-02-18T19:07:29","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/azure-sql\/?p=4277"},"modified":"2025-02-20T09:47:29","modified_gmt":"2025-02-20T17:47:29","slug":"go-passwordless-when-calling-azure-openai-from-azure-sql-using-managed-identities","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/azure-sql\/go-passwordless-when-calling-azure-openai-from-azure-sql-using-managed-identities\/","title":{"rendered":"Go passwordless when calling Azure OpenAI from Azure SQL using Managed Identities"},"content":{"rendered":"<p>Security is a significant topic today, and the ability to access a service requiring authentication without using an API key, password, or secret is a common request from those concerned about the security of a solution, which includes all of us.<\/p>\n<p>In today&#8217;s digital landscape, cybersecurity threats are increasingly sophisticated and frequent, making it imperative to protect sensitive information. Traditional methods of authentication, such as passwords and API keys, are often susceptible to breaches and misuse. As a result, there is growing interest in alternative, more secure authentication methods.<\/p>\n<p>Azure SQL DB, as you have learned in the past months, is well integrated with Azure OpenAI, and thanks to such integration calling a model exposed by Azure OpenAI without the need to resort to an API-key is very easy.<\/p>\n<h2>Assign a Managed Identity to your Azure SQL<\/h2>\n<p>To use a Managed Identity from Azure SQL, it must be assigned to the Azure SQL logical server: \u201cA System Managed Identity (SMI) is automatically assigned to Azure SQL Managed Instance when it&#8217;s created. When you&#8217;re using Microsoft Entra authentication with Azure SQL Database, you must assign an SMI when Azure service principals are used to create Microsoft Entra users in SQL Database.\u201d<\/p>\n<p>The SMI has the same name as the service. If your database server is named <code>mydbserver<\/code> the assigned SMI will also be named <code>mydbserver<\/code>.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2025\/02\/azure-sql-smi.png\"><img decoding=\"async\" class=\"aligncenter wp-image-4279 size-full\" src=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2025\/02\/azure-sql-smi.png\" alt=\"Image azure sql smi\" width=\"624\" height=\"334\" srcset=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2025\/02\/azure-sql-smi.png 624w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2025\/02\/azure-sql-smi-300x161.png 300w\" sizes=\"(max-width: 624px) 100vw, 624px\" \/><\/a><\/p>\n<p>You can also assign as User Managed Identity (UMI), as explained here: as explained in <a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/azure-sql\/database\/authentication-azure-ad-user-assigned-managed-identity?view=azuresql\">Managed identities in Microsoft Entra for Azure SQL<\/a>. To better understand the difference between System and User assigned managed identities, take a look at the \u201c<a href=\"https:\/\/learn.microsoft.com\/en-us\/entra\/identity\/managed-identities-azure-resources\/overview#managed-identity-types\">Managed identity types<\/a>\u201d article.<\/p>\n<h2>Create a Scoped Credential pointing to the Managed Identity<\/h2>\n<p>Once the Managed Identity is set, a <em>Scoped Database Credential<\/em> must be created so that it can be used to connect to a specific URL. As explained in <code>sp_invoke_external_rest_endpoint<\/code> <a href=\"https:\/\/learn.microsoft.com\/en-us\/sql\/relational-databases\/system-stored-procedures\/sp-invoke-external-rest-endpoint-transact-sql\">documentation<\/a>, the secret must be set to the correct resource for OAuth2 authentication which, for Azure OpenAI, is <code>cognitiveservices.azure.com<\/code> (as per <a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/ai-services\/openai\/how-to\/managed-identity\">documentation<\/a>):<\/p>\n<pre class=\"prettyprint language-sql\"><code class=\"language-sql\">create database scoped credential [https:\/\/&lt;my-azure-openai-endpoint&gt;.openai.azure.com]\r\nwith identity = 'Managed Identity', secret = '{\"resourceid\":\"https:\/\/cognitiveservices.azure.com\"}';\r\ngo<\/code><\/pre>\n<h2>Give the Managed Identity access to Azure OpenAI<\/h2>\n<p>The Managed Identity must possess appropriate permission to access the Azure OpenAI service. It is necessary to assign the &#8220;<a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/ai\/azure-ai-services-authentication#assign-roles-to-your-identity\">Cognitive Services OpenAI User<\/a>&#8221; role to the Managed Identity to ensure it has the required permissions.<\/p>\n<h2>Call Azure OpenAI service from Azure SQL<\/h2>\n<p>Now it is possible to use <code>sp_invoke_external_rest_endpoint<\/code> in the usual way. Behind the scenes Azure SQL and Entra ID will do\u00a0 the \u201cauthentication dance\u201d automatically so that you don\u2019t have to worry about using API key anymore:<\/p>\n<pre class=\"prettyprint language-sql\"><code class=\"language-sql\">declare @response nvarchar(max)\r\ndeclare @payload nvarchar(max) = json_object('input': 'hello world');\r\n\r\nexec sp_invoke_external_rest_endpoint\r\n\u00a0\u00a0\u00a0 @url = 'https:\/\/&lt;my-azure-openai-endpoint&gt;.openai.azure.com\/openai\/deployments\/&lt;model-deployment-name&gt;\/embeddings?api-version=2024-08-01-preview',\r\n\u00a0\u00a0\u00a0 @method = 'POST',\r\n\u00a0\u00a0\u00a0 @credential = [https:\/\/&lt;my-azure-openai-endpoint&gt;.openai.azure.com],\r\n\u00a0\u00a0\u00a0 @payload = @payload,\r\n\u00a0\u00a0\u00a0 @response = @response output;<\/code><\/pre>\n<h2>Conclusion<\/h2>\n<p>Adopting a passwordless approach enhances security while simplifying the process, as it eliminates the need to manage security keys and rotate them periodically. This makes the overall solution easier to maintain and more secure.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Security is a significant topic today, and the ability to access a service requiring authentication without using an API key, password, or secret is a common request from those concerned about the security of a solution, which includes all of us. In today&#8217;s digital landscape, cybersecurity threats are increasingly sophisticated and frequent, making it imperative [&hellip;]<\/p>\n","protected":false},"author":24720,"featured_media":4283,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[601,1,648],"tags":[649],"class_list":["post-4277","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ai","category-azure-sql","category-security","tag-passwordless"],"acf":[],"blog_post_summary":"<p>Security is a significant topic today, and the ability to access a service requiring authentication without using an API key, password, or secret is a common request from those concerned about the security of a solution, which includes all of us. In today&#8217;s digital landscape, cybersecurity threats are increasingly sophisticated and frequent, making it imperative [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/posts\/4277","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/users\/24720"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/comments?post=4277"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/posts\/4277\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/media\/4283"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/media?parent=4277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/categories?post=4277"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/tags?post=4277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}