Skip to content

Commit

Permalink
[configopaque] Implement encoding.BinaryMarshaler interface for String (
Browse files Browse the repository at this point in the history
open-telemetry#9279)

**Description:** <Describe what has changed.>
Implements the `encoding.BinaryMarshaler` interface for `String`. This
prevents the situation `configopaque` -> `[]byte` -> `string` from
leaking the `String` value.

**Link to tracking Issue:** <Issue number if applicable>
Closes
open-telemetry#9272

**Testing:** 
Added unit test

---------

Co-authored-by: Antoine Toulme <antoine@toulme.name>
  • Loading branch information
TylerHelmuth and atoulme committed Jan 23, 2024
1 parent 899c439 commit d08b9b9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .chloggen/configopaque-implement-BinaryMarshaler.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: breaking

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

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Implement `encoding.BinaryMarshaler` interface to prevent `configopaque` -> `[]byte` -> `string` conversions from leaking the value

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

# (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:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [api]
7 changes: 7 additions & 0 deletions config/configopaque/opaque.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,10 @@ var _ fmt.GoStringer = String("")
func (s String) GoString() string {
return fmt.Sprintf("%#v", maskedString)
}

var _ encoding.BinaryMarshaler = String("")

// MarshalBinary marshals the string `[REDACTED]` as []byte.
func (s String) MarshalBinary() (text []byte, err error) {
return []byte(maskedString), nil
}
9 changes: 9 additions & 0 deletions config/configopaque/opaque_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,12 @@ func TestStringFmt(t *testing.T) {
}
}
}

func TestStringMarshalBinary(t *testing.T) {
examples := []String{"opaque", "s", "veryveryveryveryveryveryveryveryveryverylong"}
for _, example := range examples {
opaque, err := example.MarshalBinary()
require.NoError(t, err)
assert.Equal(t, []byte("[REDACTED]"), opaque)
}
}

0 comments on commit d08b9b9

Please sign in to comment.