Skip to content

Commit 47398c4

Browse files
Allow escape of '$' character in the config file. (#305)
* Fix expandenv --------- Signed-off-by: Anders Swanson <anders.swanson@oracle.com>
1 parent 8763c5a commit 47398c4

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,9 @@ If you prefer to provide configuration via a [config file](./example-config.yaml
676676
```yaml
677677
# Example Oracle Database Metrics Exporter Configuration file.
678678
# Environment variables of the form ${VAR_NAME} will be expanded.
679+
# If you include a config value that contains a '$' character, escape that '$' with another '$', e.g.,
680+
# "$test$pwd" => "$$test$$pwd"
681+
# Otherwise, the value will be expanded as an environment variable.
679682
680683
# Example Oracle Database Metrics Exporter Configuration file.
681684
# Environment variables of the form ${VAR_NAME} will be expanded.

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Our current priorities are support for Exadata metrics. We expect to address th
66

77
This release includes the following changes:
88
- Fixed a bug where database type (CDB, PDB, etc.) was not reported in certain situations.
9+
- Fixed a bug where literal passwords containing the '$' character (in the config file) would be evaluated as environment variables. To use literal passwords with the '$' character, escape the '$' character with a second '$': `$test$pwd` becomes `$$test$$pwd`.
910

1011
### Version 2.0.2, June 24, 2025
1112

collector/config.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,35 +136,47 @@ func (c ConnectConfig) GetQueryTimeout() int {
136136
}
137137

138138
func (d DatabaseConfig) GetUsername() string {
139-
140-
if d.Vault.OCI.UsernameSecret != "" {
139+
if d.isOCIVault() && d.Vault.OCI.UsernameSecret != "" {
141140
return ocivault.GetVaultSecret(d.Vault.OCI.ID, d.Vault.OCI.UsernameSecret)
142141
}
143-
if d.Vault.Azure.UsernameSecret != "" {
142+
if d.isAzureVault() && d.Vault.Azure.UsernameSecret != "" {
144143
return azvault.GetVaultSecret(d.Vault.Azure.ID, d.Vault.Azure.UsernameSecret)
145144
}
146145
return d.Username
147146
}
148147

149148
func (d DatabaseConfig) GetPassword() string {
150-
151-
if d.Vault.OCI.PasswordSecret != "" {
149+
if d.isOCIVault() && d.Vault.OCI.PasswordSecret != "" {
152150
return ocivault.GetVaultSecret(d.Vault.OCI.ID, d.Vault.OCI.PasswordSecret)
153151
}
154-
if d.Vault.Azure.PasswordSecret != "" {
152+
if d.isAzureVault() && d.Vault.Azure.PasswordSecret != "" {
155153
return azvault.GetVaultSecret(d.Vault.Azure.ID, d.Vault.Azure.PasswordSecret)
156154
}
157155
return d.Password
158156
}
159157

158+
func (d DatabaseConfig) isOCIVault() bool {
159+
return d.Vault != nil && d.Vault.OCI != nil
160+
}
161+
162+
func (d DatabaseConfig) isAzureVault() bool {
163+
return d.Vault != nil && d.Vault.Azure != nil
164+
}
165+
160166
func LoadMetricsConfiguration(logger *slog.Logger, cfg *Config, path string) (*MetricsConfiguration, error) {
161167
m := &MetricsConfiguration{}
162168
if len(cfg.ConfigFile) > 0 {
163169
content, err := os.ReadFile(cfg.ConfigFile)
164170
if err != nil {
165171
return m, err
166172
}
167-
expanded := os.ExpandEnv(string(content))
173+
expanded := os.Expand(string(content), func(s string) string {
174+
// allows escaping literal $ characters
175+
if s == "$" {
176+
return "$"
177+
}
178+
return os.Getenv(s)
179+
})
168180
if yerr := yaml.UnmarshalStrict([]byte(expanded), m); yerr != nil {
169181
return m, yerr
170182
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ require (
1212
github.com/prometheus/client_golang v1.22.0
1313
github.com/prometheus/common v0.65.0
1414
github.com/prometheus/exporter-toolkit v0.14.0
15+
gopkg.in/yaml.v2 v2.4.0
1516
)
1617

1718
require (
@@ -50,5 +51,4 @@ require (
5051
golang.org/x/sys v0.33.0 // indirect
5152
golang.org/x/text v0.25.0 // indirect
5253
google.golang.org/protobuf v1.36.6 // indirect
53-
gopkg.in/yaml.v2 v2.4.0 // indirect
5454
)

0 commit comments

Comments
 (0)