@@ -300,7 +300,7 @@ func renderSharedMCPConfig(yaml *strings.Builder, toolName string, toolConfig ma
300300 // Extract secrets from headers for HTTP MCP tools (copilot engine only)
301301 var headerSecrets map [string ]string
302302 if mcpConfig .Type == "http" && renderer .RequiresCopilotFields {
303- headerSecrets = extractSecretsFromHeaders (mcpConfig .Headers )
303+ headerSecrets = ExtractSecretsFromMap (mcpConfig .Headers )
304304 }
305305
306306 // Determine properties based on type
@@ -545,7 +545,7 @@ func renderSharedMCPConfig(yaml *strings.Builder, toolName string, toolConfig ma
545545 // Replace secret expressions with env var references for copilot
546546 headerValue := mcpConfig .Headers [headerKey ]
547547 if renderer .RequiresCopilotFields && len (headerSecrets ) > 0 {
548- headerValue = replaceSecretsWithEnvVars (headerValue , headerSecrets )
548+ headerValue = ReplaceSecretsWithEnvVars (headerValue , headerSecrets )
549549 }
550550
551551 fmt .Fprintf (yaml , "%s \" %s\" : \" %s\" %s\n " , renderer .IndentLevel , headerKey , headerValue , headerComma )
@@ -650,82 +650,6 @@ func (m MapToolConfig) GetAny(key string) (any, bool) {
650650 return value , exists
651651}
652652
653- // extractSecretsFromValue extracts GitHub Actions secret expressions from a string value
654- // Returns a map of environment variable names to their secret expressions
655- // Example: "${{ secrets.DD_API_KEY }}" -> {"DD_API_KEY": "${{ secrets.DD_API_KEY }}"}
656- // Example: "${{ secrets.DD_SITE || 'datadoghq.com' }}" -> {"DD_SITE": "${{ secrets.DD_SITE || 'datadoghq.com' }}"}
657- func extractSecretsFromValue (value string ) map [string ]string {
658- secrets := make (map [string ]string )
659-
660- // Pattern to match ${{ secrets.VARIABLE_NAME }} or ${{ secrets.VARIABLE_NAME || 'default' }}
661- // We need to extract the variable name and the full expression
662- start := 0
663- for {
664- // Find the start of an expression
665- startIdx := strings .Index (value [start :], "${{ secrets." )
666- if startIdx == - 1 {
667- break
668- }
669- startIdx += start
670-
671- // Find the end of the expression
672- endIdx := strings .Index (value [startIdx :], "}}" )
673- if endIdx == - 1 {
674- break
675- }
676- endIdx += startIdx + 2 // Include the closing }}
677-
678- // Extract the full expression
679- fullExpr := value [startIdx :endIdx ]
680-
681- // Extract the variable name from "secrets.VARIABLE_NAME" or "secrets.VARIABLE_NAME ||"
682- secretsPart := strings .TrimPrefix (fullExpr , "${{ secrets." )
683- secretsPart = strings .TrimSuffix (secretsPart , "}}" )
684- secretsPart = strings .TrimSpace (secretsPart )
685-
686- // Find the variable name (everything before space, ||, or end)
687- varName := secretsPart
688- if spaceIdx := strings .IndexAny (varName , " |" ); spaceIdx != - 1 {
689- varName = varName [:spaceIdx ]
690- }
691-
692- // Store the variable name and full expression
693- if varName != "" {
694- secrets [varName ] = fullExpr
695- }
696-
697- start = endIdx
698- }
699-
700- return secrets
701- }
702-
703- // extractSecretsFromHeaders extracts all secrets from HTTP MCP headers
704- // Returns a map of environment variable names to their secret expressions
705- func extractSecretsFromHeaders (headers map [string ]string ) map [string ]string {
706- allSecrets := make (map [string ]string )
707-
708- for _ , headerValue := range headers {
709- secrets := extractSecretsFromValue (headerValue )
710- for varName , expr := range secrets {
711- allSecrets [varName ] = expr
712- }
713- }
714-
715- return allSecrets
716- }
717-
718- // replaceSecretsWithEnvVars replaces secret expressions in a value with environment variable references
719- // Example: "${{ secrets.DD_API_KEY }}" -> "\${DD_API_KEY}"
720- func replaceSecretsWithEnvVars (value string , secrets map [string ]string ) string {
721- result := value
722- for varName , secretExpr := range secrets {
723- // Replace ${{ secrets.VAR }} with \${VAR} (backslash-escaped for copilot JSON config)
724- result = strings .ReplaceAll (result , secretExpr , "\\ ${" + varName + "}" )
725- }
726- return result
727- }
728-
729653// collectHTTPMCPHeaderSecrets collects all secrets from HTTP MCP tool headers
730654// Returns a map of environment variable names to their secret expressions
731655func collectHTTPMCPHeaderSecrets (tools map [string ]any ) map [string ]string {
@@ -737,7 +661,7 @@ func collectHTTPMCPHeaderSecrets(tools map[string]any) map[string]string {
737661 if hasMcp , mcpType := hasMCPConfig (toolConfig ); hasMcp && mcpType == "http" {
738662 // Extract MCP config to get headers
739663 if mcpConfig , err := getMCPConfig (toolConfig , toolName ); err == nil {
740- secrets := extractSecretsFromHeaders (mcpConfig .Headers )
664+ secrets := ExtractSecretsFromMap (mcpConfig .Headers )
741665 for varName , expr := range secrets {
742666 allSecrets [varName ] = expr
743667 }
0 commit comments