-
Notifications
You must be signed in to change notification settings - Fork 1
Config: Add support for environment variable expansion. #111
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,9 @@ | |
| "errors" | ||
| "fmt" | ||
| "os" | ||
| "regexp" | ||
| "slices" | ||
| "strings" | ||
|
|
||
| "google.golang.org/grpc/codes" | ||
| "google.golang.org/grpc/status" | ||
|
|
@@ -66,7 +69,7 @@ | |
| User string `yaml:"user" json:"user"` | ||
|
|
||
| // Password is the database password used for authentication | ||
| Password string `yaml:"password" json:"password"` //nolint:gosec // We need a password to connect to the database. | ||
|
|
||
| // Params contains additional connection parameters (e.g., {"sslmode": "disable", "timeout": "30s"}) | ||
| Params map[string]string `yaml:"params" json:"params"` | ||
|
|
@@ -500,15 +503,52 @@ | |
| return config, nil | ||
| } | ||
|
|
||
| type LookupFunc func(key string) (string, bool) | ||
|
|
||
| func expandEnvironmentVariables(_ context.Context, data []byte, lookup LookupFunc) ([]byte, error) { | ||
| var missingVars []string | ||
| envVarRegex := regexp.MustCompile(`\${([^}]+)}`) | ||
|
|
||
| dataStr := envVarRegex.ReplaceAllStringFunc(string(data), func(match string) string { | ||
| envVar := match[2 : len(match)-1] | ||
| if envVal, exists := lookup(envVar); exists { | ||
| if strings.ContainsAny(envVal, "\n\r") { | ||
| // Quote multi-line values so they remain a single YAML scalar. | ||
| escaped := strings.ReplaceAll(envVal, `\`, `\\`) | ||
| escaped = strings.ReplaceAll(escaped, `"`, `\"`) | ||
| escaped = strings.ReplaceAll(escaped, "\n", `\n`) | ||
| escaped = strings.ReplaceAll(escaped, "\r", `\r`) | ||
| return `"` + escaped + `"` | ||
| } | ||
| return envVal | ||
| } | ||
| if !slices.Contains(missingVars, envVar) { | ||
| missingVars = append(missingVars, envVar) | ||
| } | ||
| return match | ||
| }) | ||
|
|
||
| if len(missingVars) > 0 { | ||
| return nil, fmt.Errorf("missing environment variables: %s", strings.Join(missingVars, ", ")) | ||
| } | ||
|
Comment on lines
+531
to
+533
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefix the new errors with Both new error messages omit the required connector prefix. Please make these Suggested diff- return nil, fmt.Errorf("missing environment variables: %s", strings.Join(missingVars, ", "))
+ return nil, fmt.Errorf("baton-sql: missing environment variables: %s", strings.Join(missingVars, ", "))
- return nil, fmt.Errorf("failed to expand environment variables: %w", err)
+ return nil, fmt.Errorf("baton-sql: failed to expand environment variables: %w", err)As per coding guidelines, Also applies to: 547-548 🤖 Prompt for AI Agents |
||
|
|
||
| return []byte(dataStr), nil | ||
| } | ||
|
|
||
| // LoadConfigFromFile reads a YAML configuration file from the given path and parses its content into a Config struct. | ||
| func LoadConfigFromFile(path string) (*Config, error) { | ||
| func LoadConfigFromFile(ctx context.Context, path string) (*Config, error) { | ||
| data, err := os.ReadFile(path) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // First pass: expand environment variables in string nodes | ||
| replacedData, err := expandEnvironmentVariables(ctx, data, os.LookupEnv) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to expand environment variables: %w", err) | ||
| } | ||
| config := &Config{} | ||
| err = yaml.Unmarshal(data, config) | ||
| err = yaml.Unmarshal(replacedData, config) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
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.
Expand parsed YAML scalars, not raw file text.
This replacement runs before YAML parsing, so it also rewrites
${...}inside comments, keys, and already-quoted values. That breaks valid configs in common cases—for example, a commented example like# ${DB_PASSWORD}becomes a hard startup error, and a quoted placeholder with a multiline secret produces invalid YAML. Please expand onlyyaml.Nodestring scalar values after parsing.Possible direction
🤖 Prompt for AI Agents