Skip to content

Commit

Permalink
Fix: Force usage of case-sensitive keys in configurations
Browse files Browse the repository at this point in the history
Signed-off-by: Raphael Silva <[email protected]>
  • Loading branch information
rapphil committed Jan 3, 2023
1 parent e4e76de commit 923fd8e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .chloggen/case-sensitive-configuration.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: 'bug_fix'

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: confmap

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Fix bug in confmap validation that allowed the usage of case-insensitive keys in the configurations, leading to unexpected behavior.

# One or more tracking issues or pull requests related to the change
issues: [6876]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
8 changes: 8 additions & 0 deletions confmap/confmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ func decodeConfig(m *Conf, result interface{}, errorUnused bool) error {
Result: result,
TagName: "mapstructure",
WeaklyTypedInput: true,
MatchName: caseSensitiveMatchName(),
DecodeHook: mapstructure.ComposeDecodeHookFunc(
expandNilStructPointersHookFunc(),
mapstructure.StringToSliceHookFunc(","),
Expand Down Expand Up @@ -189,6 +190,13 @@ func encoderConfig(rawVal interface{}) *encoder.EncoderConfig {
}
}

// case-sensitive version of the callback to be used in the MatchName property
// of the DecoderConfig. The default for MatchEqual is to use strings.EqualFold,
// which is case-insensitive.
func caseSensitiveMatchName() func(mapKey, keyName string) bool {
return func(mapKey, keyName string) bool { return mapKey == keyName }
}

// In cases where a config has a mapping of something to a struct pointers
// we want nil values to resolve to a pointer to the zero value of the
// underlying struct just as we want nil values of a mapping of something
Expand Down
4 changes: 4 additions & 0 deletions exporter/loggingexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ func TestUnmarshalConfig(t *testing.T) {
filename: "invalid_verbosity_loglevel.yaml",
expectedErr: "'loglevel' and 'verbosity' are incompatible. Use only 'verbosity' instead",
},
{
filename: "config_loglevel_typo.yaml",
expectedErr: "1 error(s) decoding:\n\n* '' has invalid keys: logLevel",
},
}

for _, tt := range tests {
Expand Down
2 changes: 2 additions & 0 deletions exporter/loggingexporter/testdata/config_loglevel_typo.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Typo in the configuration that assumes that this property is camelcase
logLevel: debug

0 comments on commit 923fd8e

Please sign in to comment.