Skip to content

Enhance the Decode OTTL function to support all flavors of Base64 #38854

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

Merged
merged 4 commits into from
Mar 31, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .chloggen/ottl-decode-base64.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 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. filelogreceiver)
component: ottlfuncs

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Enhance the Decode OTTL function to support all flavors of Base64

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [38854]

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

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# 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: [user]
2 changes: 1 addition & 1 deletion pkg/ottl/ottlfuncs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ Examples:
The `Decode` Converter takes a string or byte array encoded with the specified encoding and returns the decoded string.

`value` is a valid encoded string or byte array.
`encoding` is a valid encoding name included in the [IANA encoding index](https://www.iana.org/assignments/character-sets/character-sets.xhtml).
`encoding` is a valid encoding name included in the [IANA encoding index](https://www.iana.org/assignments/character-sets/character-sets.xhtml) or one of `base64`, `rawbase64`, `urlbase64` or `rawurlbase64`.

Examples:

Expand Down
22 changes: 16 additions & 6 deletions pkg/ottl/ottlfuncs/func_decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,15 @@ func Decode[K any](target ottl.Getter[K], encoding string) (ottl.ExprFunc[K], er
}

switch encoding {
// base64 is not in IANA index, so we have to deal with this encoding separately
case "base64":
// base64 is not in IANA index, so we have to deal with this encoding separately
decodedBytes, err := base64.StdEncoding.DecodeString(stringValue)
if err != nil {
return nil, fmt.Errorf("could not decode: %w", err)
}
return string(decodedBytes), nil
return decodeBase64(base64.StdEncoding, stringValue)
case "rawbase64":
return decodeBase64(base64.RawStdEncoding, stringValue)
case "urlbase64":
return decodeBase64(base64.URLEncoding, stringValue)
case "rawurlbase64":
return decodeBase64(base64.RawURLEncoding, stringValue)
default:
e, err := textutils.LookupEncoding(encoding)
if err != nil {
Expand All @@ -82,3 +84,11 @@ func Decode[K any](target ottl.Getter[K], encoding string) (ottl.ExprFunc[K], er
}
}, nil
}

func decodeBase64(encoding *base64.Encoding, stringValue string) (any, error) {
decodedBytes, err := encoding.DecodeString(stringValue)
if err != nil {
return nil, fmt.Errorf("could not decode: %w", err)
}
return string(decodedBytes), nil
}
24 changes: 24 additions & 0 deletions pkg/ottl/ottlfuncs/func_decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,30 @@ func TestDecode(t *testing.T) {
encoding: "base64",
expectedError: "illegal base64 data at input byte",
},
{
name: "base64 with url-safe sensitive characters",
value: "R28/L1p+eA==",
encoding: "base64",
want: "Go?/Z~x",
},
{
name: "rawbase64 with url-safe sensitive characters",
value: "R28/L1p+eA",
encoding: "rawbase64",
want: "Go?/Z~x",
},
{
name: "urlbase64 with url-safe sensitive characters",
value: "R28_L1p-eA==",
encoding: "urlbase64",
want: "Go?/Z~x",
},
{
name: "rawurlbase64 with url-safe sensitive characters",
value: "R28_L1p-eA",
encoding: "rawurlbase64",
want: "Go?/Z~x",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down