Securing Model Endpoints with Managed Identity
This lesson explains database scoped credentials for outbound calls, managed identity setup for model endpoints and the permissions that control their use.
Learning outcomes
After completing this lesson, you will be able to:
- Describe the credential identity types for outbound calls.
- Explain the steps to call a model with the server's managed identity.
- Identify the permissions that control model and endpoint use.
Credentials for outbound calls
CREATE EXTERNAL MODEL and sp_invoke_external_rest_endpoint authenticate to endpoints with a database scoped credential whose name is the endpoint's base URL. The credential's IDENTITY decides how authentication is sent: HTTPEndpointHeaders adds headers such as an api-key from the SECRET, HTTPEndpointQueryString adds query string values, Shared Access Signature uses a SAS, and Managed Identity obtains a Microsoft Entra token for the logical server's identity. Credentials with a SECRET need a database master key.
Credential identities
Setting up managed identity
The following steps set up keyless calls from Azure SQL Database to an Azure OpenAI deployment in Microsoft Foundry.
-
Enable the server identity
Turn on the logical server's system-assigned managed identity (or assign a user-assigned identity and make it primary).
-
Grant a role on the AI resource
Assign the identity the Cognitive Services OpenAI User role on the Foundry or Azure OpenAI resource, which allows inference but not management.
-
Create the credential
Create a database scoped credential named after the endpoint URL with IDENTITY = 'Managed Identity' and the resource ID
https://cognitiveservices.azure.com. -
Reference the credential
Use it in CREATE EXTERNAL MODEL or the @credential parameter of sp_invoke_external_rest_endpoint.
CREATE DATABASE SCOPED CREDENTIAL [https://contoso-ai.openai.azure.com]
WITH IDENTITY = 'Managed Identity',
SECRET = '{"resourceid":"https://cognitiveservices.azure.com"}';When a server has both system-assigned and user-assigned identities, the primary user-assigned identity is used. On SQL Server 2025, the managed identity comes from Azure Arc (or the Azure VM), and sp_configure 'allow server scoped db credentials', 1 must be enabled.
Keyless calls to Azure OpenAI
Controlling use inside the database
Database permissions decide who can trigger outbound calls, as the following table lists.
| Permission | Allows |
|---|---|
| EXECUTE ON EXTERNAL MODEL::name | Calling AI functions such as AI_GENERATE_EMBEDDINGS with that model |
| CREATE EXTERNAL MODEL / ALTER ANY EXTERNAL MODEL | Creating or changing model definitions |
| EXECUTE ANY EXTERNAL ENDPOINT | Calling sp_invoke_external_rest_endpoint |
| REFERENCES ON DATABASE SCOPED CREDENTIAL::name | Using a credential in an outbound call |
In Azure SQL Database and Managed Instance, the procedure can call only allow-listed Azure domains, including *.openai.azure.com and *.cognitiveservices.azure.com; other services can be reached through Azure API Management. Outbound firewall rules or a network security perimeter restrict calls further to specific FQDNs, which limits data exfiltration.
Who may call out?
Removing an API key from the database
An Azure SQL database calls an Azure OpenAI embedding deployment with a credential that stores an api-key header. The key was exposed in a script and had to be rotated. The team wants no keys stored in the database. What should the developer do?
Enable the logical server's managed identity, grant it Cognitive Services OpenAI User on the Azure OpenAI resource, and replace the credential with one using IDENTITY = 'Managed Identity' and the cognitiveservices resource ID. The external model keeps referencing the credential by the same URL name. Storing the key in Key Vault still requires the database to hold a secret, and Cognitive Services Contributor would grant management rights the server does not need.
Which permission lets a user call sp_invoke_external_rest_endpoint?
Show answer
EXECUTE ANY EXTERNAL ENDPOINT, plus REFERENCES on any credential the call uses.
Exam Tips
Remember for the exam
- Keyless model calls: credential with IDENTITY = 'Managed Identity'.
- Role for the server identity: Cognitive Services OpenAI User on the AI resource.
- Credential name must match the endpoint URL's scheme and host.
- Limit outbound targets: allow list plus outbound firewall rules.