-
Notifications
You must be signed in to change notification settings - Fork 793
feat: Add Azure CLI authentication support for Azure OpenAI #1426
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
Conversation
4013886 to
7f45c3c
Compare
narengogi
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @shubhamdeodia thank you for the PR, but I'm having a tough time understanding the need for this implementation.
What are you trying to achieve here.
From what I understand, is this so that people can deploy the gateway on azure and use configured environment variables to generate a temporary token and make the inference request with it?
I believe what you are looking for is the azureManagedIdentity implementation, which serves the same puprose
https://github.com/Portkey-AI/gateway-enterprise-node/blob/66ea88c8c00d5b177e4940e4f79b1bd0e2369998/src/providers/azure-openai/utils.ts#L54
spawning a child process for every request and expecting azure cli (which is an external dependency on the system the gateway is deployed on) is a red flag and anti pattern.
can you please detail what exactly is your requirement here?
|
This implementation is not intended for production deployments, Azure-hosted gateways, or situations where Managed Identity / Entra ID already exists. The primary purpose is to support local development workflows where developers are:
In our company, we never stored secrets of any sorts (not even for Testing) |
|
got it @shubhamdeodia |
|
@narengogi thanks, I think that make sense. Seems like I missed that it supports the Auth Headers. |
Azure CLI Authentication Implementation
Pull Request Documentation
Summary
This PR adds support for Azure CLI-based authentication (
azure_auth_mode: "azure_cli") to the Portkey AI Gateway for both Azure OpenAI and Azure AI Inference services. This enhancement enables developers to authenticate using their existing Azure CLI credentials, simplifying local development and testing workflows.Changes Overview
Files Modified
src/providers/azure-openai/utils.tsgetAzureCliToken()function to obtain access tokens via Azure CLIsrc/providers/azure-openai/api.tsgetAzureCliTokenfunctionazure_cliauthentication mode handler in headers functionsrc/providers/azure-ai-inference/api.tsgetAzureCliTokenfunctionazure_cliauthentication mode handler in headers functionplugins/azure/utils.tsgetAzureCliToken()function for Azure plugin supportgetAccessToken()to handleazure_climodeplugins/azure/types.tsAzureCredentialsinterface with strict union type'azure_cli'to valid authentication modessrc/types/requestBody.tsazureAuthModein 3 interfaces (Options,Targets,ShortConfig)azure_cliAuthentication Flow Sequence Diagram
sequenceDiagram participant Client participant Gateway as Portkey Gateway<br/>(Node.js) participant Utils as getAzureCliToken()<br/>Function participant CLI as Azure CLI<br/>(Local Process) participant Azure as Azure OpenAI API Client->>Gateway: Request with config<br/>{azure_auth_mode: "azure_cli"} Gateway->>Gateway: Detect azure_auth_mode === "azure_cli"<br/>& runtime === "node" Gateway->>Utils: Call getAzureCliToken(scope) Utils->>CLI: execSync('az account get-access-token<br/>--resource https://cognitiveservices.azure.com/') alt Azure CLI Success CLI->>Utils: Return JSON<br/>{accessToken: "eyJ0...", expiresOn: "..."} Utils->>Utils: Parse JSON and extract accessToken Utils->>Gateway: Return access token Gateway->>Gateway: Add Authorization header<br/>"Bearer eyJ0..." Gateway->>Azure: Forward request with Bearer token Azure->>Gateway: Response Gateway->>Client: Response else Azure CLI Error CLI->>Utils: Error: "az not found" or<br/>"Not logged in" Utils->>Utils: Log error message Utils->>Gateway: Return undefined Gateway->>Gateway: Fall back to API key authentication Gateway->>Azure: Forward request with api-key header Azure->>Gateway: Response (or error if no API key) Gateway->>Client: Response endTechnical Implementation Details
1. Token Acquisition Function
Location:
src/providers/azure-openai/utils.tsKey Design Decisions:
execSyncto block until token is retrieved (simplifies async flow)/.defaultsuffix as Azure CLI expects raw resource URLundefinedon failure, allowing fallback to API key authentication2. Azure OpenAI Integration
Location:
src/providers/azure-openai/api.tsIntegration Points:
workloadauth mode and before API key fallback3. Azure AI Inference Integration
Location:
src/providers/azure-ai-inference/api.tsDifferences from OpenAI Implementation:
headersobject rather than returning new object4. Azure Plugin Support
Location:
plugins/azure/utils.tsIntegration in
getAccessToken():Key Features:
tokenanderrorproperties for consistent error handling5. Type Definitions
Location:
plugins/azure/types.tsLocation:
src/types/requestBody.tsType Safety Features:
AzureCredentialsfor compile-time checkingOptions,Targets, andShortConfigAuthentication Mode Precedence
The authentication logic follows this order:
azureAdToken(if provided directly)entramode (client credentials flow)managedmode (managed identity)workloadmode (workload identity)azure_climode (Azure CLI tokens) ← NewRuntime Requirements
The
azure_climode is only available in Node.js runtime because:child_process.execSyncto execute shell commandsRuntime Detection:
Security Considerations
Token Security
Credential Management
Audit Trail
Error Scenarios and Handling
execSyncthrows errorundefined, fall back to API keyundefined, fall back to API keyConfiguration Examples
Minimal Configuration
{ "provider": "azure-openai", "azure_auth_mode": "azure_cli", "resource_name": "my-openai", "deployment_id": "gpt-4", "api_version": "2024-02-15-preview" }With Fallback to API Key
{ "provider": "azure-openai", "azure_auth_mode": "azure_cli", "api_key": "${AZURE_OPENAI_API_KEY}", "resource_name": "my-openai", "deployment_id": "gpt-4", "api_version": "2024-02-15-preview" }Multiple Providers with Loadbalancing
{ "strategy": { "mode": "loadbalance" }, "targets": [ { "provider": "azure-openai", "azure_auth_mode": "azure_cli", "resource_name": "openai-dev", "deployment_id": "gpt-4", "api_version": "2024-02-15-preview", "weight": 1 }, { "provider": "azure-openai", "azure_auth_mode": "entra", "azure_entra_client_id": "${AZURE_CLIENT_ID}", "azure_entra_client_secret": "${AZURE_CLIENT_SECRET}", "azure_entra_tenant_id": "${AZURE_TENANT_ID}", "resource_name": "openai-prod", "deployment_id": "gpt-4", "api_version": "2024-02-15-preview", "weight": 2 } ] }Testing
Manual Testing Steps
Prerequisites:
Test Basic Authentication:
Test Error Handling:
Performance Considerations
Token Acquisition Time
Future Optimization Opportunities
Backwards Compatibility
This change is 100% backwards compatible:
azureAuthMode?: string)azure_auth_mode: "azure_cli"Migration Guide
For users wanting to switch from API key to Azure CLI authentication:
Step 1: Install and configure Azure CLI
Step 2: Update configuration
{ "provider": "azure-openai", - "api_key": "sk-***", + "azure_auth_mode": "azure_cli", "resource_name": "my-openai-resource", "deployment_id": "gpt-4", "api_version": "2024-02-15-preview" }Step 3: Test the connection
Deployment Considerations
Local Development
✅ Recommended Use Case
CI/CD Pipelines
✅ Supported
Production Serverless
❌ Not Supported
entra,managed, orworkloadmodes insteadProduction VMs/Containers
managedorentramodes for production workloadsBreaking Changes
None - This is a purely additive change with no breaking changes to existing functionality.
Summary of Changes
This PR adds comprehensive Azure CLI authentication support across the entire gateway:
Core Provider Support:
src/providers/azure-openai/*)src/providers/azure-ai-inference/*)Plugin Support:
plugins/azure/*)Type Safety:
AzureCredentialsDocumentation:
Total Lines Changed: ~150+ lines added across 6 code files + 4 documentation files
Related Issues: N/A (Feature Request)
Breaking Changes: None
Deployment Notes: Requires Node.js runtime for functionality