-
Notifications
You must be signed in to change notification settings - Fork 298
feat: support custom OpenAI and Anthropic API targets in AWF sandbox #20631
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -190,6 +190,21 @@ func BuildAWFArgs(config AWFCommandConfig) []string { | |||||||||||||||||||||||||||||||||||
| awfArgs = append(awfArgs, "--enable-api-proxy") | ||||||||||||||||||||||||||||||||||||
| awfHelpersLog.Print("Added --enable-api-proxy for LLM API proxying") | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| // Add custom API targets if configured in engine.env | ||||||||||||||||||||||||||||||||||||
| // This allows AWF's credential isolation and firewall to work with custom endpoints | ||||||||||||||||||||||||||||||||||||
| // (e.g., corporate LLM routers, Azure OpenAI, self-hosted APIs) | ||||||||||||||||||||||||||||||||||||
| openaiTarget := extractAPITargetHost(config.WorkflowData, "OPENAI_BASE_URL") | ||||||||||||||||||||||||||||||||||||
| if openaiTarget != "" { | ||||||||||||||||||||||||||||||||||||
| awfArgs = append(awfArgs, "--openai-api-target", openaiTarget) | ||||||||||||||||||||||||||||||||||||
| awfHelpersLog.Printf("Added --openai-api-target=%s", openaiTarget) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| anthropicTarget := extractAPITargetHost(config.WorkflowData, "ANTHROPIC_BASE_URL") | ||||||||||||||||||||||||||||||||||||
| if anthropicTarget != "" { | ||||||||||||||||||||||||||||||||||||
| awfArgs = append(awfArgs, "--anthropic-api-target", anthropicTarget) | ||||||||||||||||||||||||||||||||||||
| awfHelpersLog.Printf("Added --anthropic-api-target=%s", anthropicTarget) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| // Add SSL Bump support for HTTPS content inspection (v0.9.0+) | ||||||||||||||||||||||||||||||||||||
| sslBumpArgs := getSSLBumpArgs(firewallConfig) | ||||||||||||||||||||||||||||||||||||
| awfArgs = append(awfArgs, sslBumpArgs...) | ||||||||||||||||||||||||||||||||||||
|
|
@@ -245,3 +260,68 @@ func WrapCommandInShell(command string) string { | |||||||||||||||||||||||||||||||||||
| // Wrap in shell invocation | ||||||||||||||||||||||||||||||||||||
| return fmt.Sprintf("/bin/bash -c '%s'", escapedCommand) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| // extractAPITargetHost extracts the hostname from a custom API base URL in engine.env. | ||||||||||||||||||||||||||||||||||||
| // This supports custom OpenAI-compatible or Anthropic-compatible endpoints (e.g., internal | ||||||||||||||||||||||||||||||||||||
| // LLM routers, Azure OpenAI) while preserving AWF's credential isolation and firewall features. | ||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||
| // The function: | ||||||||||||||||||||||||||||||||||||
| // 1. Checks if the specified env var (e.g., "OPENAI_BASE_URL") exists in engine.env | ||||||||||||||||||||||||||||||||||||
| // 2. Extracts the hostname from the URL (e.g., "https://llm-router.internal.example.com/v1" → "llm-router.internal.example.com") | ||||||||||||||||||||||||||||||||||||
| // 3. Returns empty string if no custom URL is configured or if the URL is invalid | ||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||
| // Parameters: | ||||||||||||||||||||||||||||||||||||
| // - workflowData: The workflow data containing engine configuration | ||||||||||||||||||||||||||||||||||||
| // - envVar: The environment variable name (e.g., "OPENAI_BASE_URL", "ANTHROPIC_BASE_URL") | ||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||
| // Returns: | ||||||||||||||||||||||||||||||||||||
| // - string: The hostname to use as --openai-api-target or --anthropic-api-target, or empty string if not configured | ||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||
| // Example: | ||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||
| // engine: | ||||||||||||||||||||||||||||||||||||
| // id: codex | ||||||||||||||||||||||||||||||||||||
| // env: | ||||||||||||||||||||||||||||||||||||
| // OPENAI_BASE_URL: "https://llm-router.internal.example.com/v1" | ||||||||||||||||||||||||||||||||||||
| // OPENAI_API_KEY: ${{ secrets.LLM_ROUTER_KEY }} | ||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||
| // extractAPITargetHost(workflowData, "OPENAI_BASE_URL") | ||||||||||||||||||||||||||||||||||||
| // // Returns: "llm-router.internal.example.com" | ||||||||||||||||||||||||||||||||||||
| func extractAPITargetHost(workflowData *WorkflowData, envVar string) string { | ||||||||||||||||||||||||||||||||||||
| // Check if engine config and env are available | ||||||||||||||||||||||||||||||||||||
| if workflowData == nil || workflowData.EngineConfig == nil || workflowData.EngineConfig.Env == nil { | ||||||||||||||||||||||||||||||||||||
| return "" | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| // Get the custom base URL from engine.env | ||||||||||||||||||||||||||||||||||||
| baseURL, exists := workflowData.EngineConfig.Env[envVar] | ||||||||||||||||||||||||||||||||||||
| if !exists || baseURL == "" { | ||||||||||||||||||||||||||||||||||||
| return "" | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| // Extract hostname from URL | ||||||||||||||||||||||||||||||||||||
| // URLs can be: | ||||||||||||||||||||||||||||||||||||
| // - "https://llm-router.internal.example.com/v1" → "llm-router.internal.example.com" | ||||||||||||||||||||||||||||||||||||
| // - "http://localhost:8080/v1" → "localhost:8080" | ||||||||||||||||||||||||||||||||||||
| // - "api.openai.com" → "api.openai.com" (treated as hostname) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| // Remove protocol prefix if present | ||||||||||||||||||||||||||||||||||||
| host := baseURL | ||||||||||||||||||||||||||||||||||||
| if idx := strings.Index(host, "://"); idx != -1 { | ||||||||||||||||||||||||||||||||||||
| host = host[idx+3:] | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| // Remove path suffix if present (everything after first /) | ||||||||||||||||||||||||||||||||||||
| if idx := strings.Index(host, "/"); idx != -1 { | ||||||||||||||||||||||||||||||||||||
| host = host[:idx] | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| // Validate that we have a non-empty hostname | ||||||||||||||||||||||||||||||||||||
| if host == "" { | ||||||||||||||||||||||||||||||||||||
| awfHelpersLog.Printf("Invalid %s URL (no hostname): %s", envVar, baseURL) | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+319
to
+321
|
||||||||||||||||||||||||||||||||||||
| // Validate that we have a non-empty hostname | |
| if host == "" { | |
| awfHelpersLog.Printf("Invalid %s URL (no hostname): %s", envVar, baseURL) | |
| // Remove userinfo (credentials) if present (e.g., "user:pass@host") | |
| // Keep only the part after '@' so that credentials are never logged. | |
| if idx := strings.LastIndex(host, "@"); idx != -1 { | |
| host = host[idx+1:] | |
| } | |
| // Remove query or fragment if present (e.g., "host:8080?api_key=..." or "host#section") | |
| if idx := strings.IndexAny(host, "?#"); idx != -1 { | |
| host = host[:idx] | |
| } | |
| // Validate that we have a non-empty hostname | |
| if host == "" { | |
| awfHelpersLog.Printf("Invalid %s URL (no hostname)", envVar) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
extractAPITargetHostdoes manual string slicing and fails to strip URL query/fragment/userinfo. For examplehttps://example.com?x=1would returnexample.com?x=1, andhttps://user:pass@host/v1would returnuser:pass@host, which is not a valid--*-api-targethost and can break AWF routing. Consider parsing withnet/url(optionally prefixing a scheme when missing), then returningu.Host(oru.Hostname()+ optionalu.Port()), and trimming whitespace.