Skip to content

Commit

Permalink
[configopaque] configopaque.String implements fmt.Stringer (#9214)
Browse files Browse the repository at this point in the history
**Description:**
configopaque.String implements `fmt.Stringer`, outputting [REDACTED]
when used as `fmt.Sprintf("%s", opaquestring)`

**Link to tracking Issue:**
Fixes #9213

---------

Co-authored-by: Pablo Baeyens <pablo.baeyens@datadoghq.com>
  • Loading branch information
atoulme and mx-psi authored Jan 10, 2024
1 parent 9b5ef90 commit bf804d6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .chloggen/configopaque_stringer.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: enhancement

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

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: configopaque.String implements `fmt.Stringer`, outputting [REDACTED] when used as `fmt.Sprintf("%s", opaquestring)`

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

# (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 @@ -5,11 +5,14 @@ package configopaque // import "go.opentelemetry.io/collector/config/configopaqu

import (
"encoding"
"fmt"
)

// String alias that is marshaled in an opaque way.
type String string

var _ fmt.Stringer = String("")

const maskedString = "[REDACTED]"

var _ encoding.TextMarshaler = String("")
Expand All @@ -18,3 +21,7 @@ var _ encoding.TextMarshaler = String("")
func (s String) MarshalText() ([]byte, error) {
return []byte(maskedString), nil
}

func (s String) String() string {
return maskedString
}
12 changes: 12 additions & 0 deletions config/configopaque/opaque_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package configopaque // import "go.opentelemetry.io/collector/config/configopaque"

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -18,3 +19,14 @@ func TestStringMarshalText(t *testing.T) {
assert.Equal(t, "[REDACTED]", string(opaque))
}
}

func TestStringFmt(t *testing.T) {
examples := []String{"opaque", "s", "veryveryveryveryveryveryveryveryveryverylong"}
for _, example := range examples {
// nolint S1025
assert.Equal(t, "[REDACTED]", fmt.Sprintf("%s", example))
// converting to a string allows to output as an unredacted string still:
// nolint S1025
assert.Equal(t, string(example), fmt.Sprintf("%s", string(example)))
}
}

0 comments on commit bf804d6

Please sign in to comment.