|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "regexp" |
| 7 | + |
| 8 | + "github.com/github/gh-aw-mcpg/internal/config/rules" |
| 9 | +) |
| 10 | + |
| 11 | +// Variable expression pattern: ${VARIABLE_NAME} |
| 12 | +var varExprPattern = regexp.MustCompile(`\$\{([A-Za-z_][A-Za-z0-9_]*)\}`) |
| 13 | + |
| 14 | +// expandVariablesCore is the shared implementation for variable expansion. |
| 15 | +// It works with byte slices and handles the core expansion logic, tracking undefined variables. |
| 16 | +// This eliminates code duplication between expandVariables and ExpandRawJSONVariables. |
| 17 | +// It returns the expanded bytes, a slice of undefined variable names, and an error |
| 18 | +// (currently always nil). |
| 19 | +func expandVariablesCore(data []byte, contextDesc string) ([]byte, []string, error) { |
| 20 | + logValidation.Printf("Expanding variables: context=%s", contextDesc) |
| 21 | + var undefinedVars []string |
| 22 | + |
| 23 | + result := varExprPattern.ReplaceAllFunc(data, func(match []byte) []byte { |
| 24 | + // Extract variable name (remove ${ and }) |
| 25 | + varName := string(match[2 : len(match)-1]) |
| 26 | + |
| 27 | + if envValue, exists := os.LookupEnv(varName); exists { |
| 28 | + logValidation.Printf("Expanded variable: %s (found in environment)", varName) |
| 29 | + return []byte(envValue) |
| 30 | + } |
| 31 | + |
| 32 | + // Track undefined variable |
| 33 | + undefinedVars = append(undefinedVars, varName) |
| 34 | + logValidation.Printf("Undefined variable: %s", varName) |
| 35 | + return match // Keep original if undefined |
| 36 | + }) |
| 37 | + |
| 38 | + logValidation.Printf("Variable expansion completed: context=%s, undefined_count=%d", contextDesc, len(undefinedVars)) |
| 39 | + return result, undefinedVars, nil |
| 40 | +} |
| 41 | + |
| 42 | +// expandVariables expands variable expressions in a string. |
| 43 | +// value is the source string and jsonPath identifies the config location for errors. |
| 44 | +// It returns the expanded string and an error if any variable is undefined. |
| 45 | +func expandVariables(value, jsonPath string) (string, error) { |
| 46 | + result, undefinedVars, _ := expandVariablesCore([]byte(value), fmt.Sprintf("jsonPath=%s", jsonPath)) |
| 47 | + |
| 48 | + if len(undefinedVars) > 0 { |
| 49 | + logValidation.Printf("Variable expansion failed: undefined variables=%v", undefinedVars) |
| 50 | + return "", rules.UndefinedVariable(undefinedVars[0], jsonPath) |
| 51 | + } |
| 52 | + |
| 53 | + return string(result), nil |
| 54 | +} |
| 55 | + |
| 56 | +// ExpandRawJSONVariables expands all ${VAR} expressions in JSON data before schema validation. |
| 57 | +// This ensures the schema validates the expanded values, not the variable syntax. |
| 58 | +// It collects undefined variables and reports the first undefined variable as an error. |
| 59 | +func ExpandRawJSONVariables(data []byte) ([]byte, error) { |
| 60 | + result, undefinedVars, _ := expandVariablesCore(data, "raw JSON data") |
| 61 | + |
| 62 | + if len(undefinedVars) > 0 { |
| 63 | + logValidation.Printf("Variable expansion failed: undefined variables=%v", undefinedVars) |
| 64 | + return nil, rules.UndefinedVariable(undefinedVars[0], "configuration") |
| 65 | + } |
| 66 | + |
| 67 | + return result, nil |
| 68 | +} |
| 69 | + |
| 70 | +// expandEnvVariables expands all variable expressions in an env map. |
| 71 | +// env is the map to expand and serverName is used for config-path error context. |
| 72 | +// It returns a new map with expanded values or an error if any variable is undefined. |
| 73 | +func expandEnvVariables(env map[string]string, serverName string) (map[string]string, error) { |
| 74 | + logValidation.Printf("Expanding env variables for server: %s, count=%d", serverName, len(env)) |
| 75 | + result := make(map[string]string, len(env)) |
| 76 | + |
| 77 | + for key, value := range env { |
| 78 | + jsonPath := fmt.Sprintf("mcpServers.%s.env.%s", serverName, key) |
| 79 | + |
| 80 | + expanded, err := expandVariables(value, jsonPath) |
| 81 | + if err != nil { |
| 82 | + return nil, err |
| 83 | + } |
| 84 | + |
| 85 | + result[key] = expanded |
| 86 | + } |
| 87 | + |
| 88 | + logValidation.Printf("Env variable expansion completed for server: %s", serverName) |
| 89 | + return result, nil |
| 90 | +} |
| 91 | + |
| 92 | +// expandTracingVariables expands ${VAR} expressions in TracingConfig fields. |
| 93 | +// This is called for TOML-loaded configs before validation, mirroring the |
| 94 | +// stdin JSON path where ExpandRawJSONVariables handles expansion. |
| 95 | +func expandTracingVariables(cfg *TracingConfig) error { |
| 96 | + if cfg == nil { |
| 97 | + return nil |
| 98 | + } |
| 99 | + |
| 100 | + if cfg.Endpoint != "" { |
| 101 | + expanded, err := expandVariables(cfg.Endpoint, "gateway.opentelemetry.endpoint") |
| 102 | + if err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + cfg.Endpoint = expanded |
| 106 | + } |
| 107 | + |
| 108 | + if cfg.TraceID != "" { |
| 109 | + expanded, err := expandVariables(cfg.TraceID, "gateway.opentelemetry.traceId") |
| 110 | + if err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + cfg.TraceID = expanded |
| 114 | + } |
| 115 | + |
| 116 | + if cfg.SpanID != "" { |
| 117 | + expanded, err := expandVariables(cfg.SpanID, "gateway.opentelemetry.spanId") |
| 118 | + if err != nil { |
| 119 | + return err |
| 120 | + } |
| 121 | + cfg.SpanID = expanded |
| 122 | + } |
| 123 | + |
| 124 | + if cfg.Headers != "" { |
| 125 | + expanded, err := expandVariables(cfg.Headers, "gateway.opentelemetry.headers") |
| 126 | + if err != nil { |
| 127 | + return err |
| 128 | + } |
| 129 | + cfg.Headers = expanded |
| 130 | + } |
| 131 | + |
| 132 | + return nil |
| 133 | +} |
0 commit comments