Skip to content

Commit dce0082

Browse files
nahguamedmocosta
andauthored
Enhance the Decode OTTL function to support all flavors of Base64 (open-telemetry#38854)
#### Description Adding a feature - Enhance the Decode OTTL function to support all flavors of Base64 #### Testing Added tests and ran all tests for the extension. #### Documentation Updated the OTTL readme --------- Co-authored-by: Edmo Vamerlatti Costa <[email protected]>
1 parent 91eb4f5 commit dce0082

File tree

4 files changed

+68
-7
lines changed

4 files changed

+68
-7
lines changed

.chloggen/ottl-decode-base64.yaml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: pkg/ottl
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Enhance the Decode OTTL function to support all flavors of Base64
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [38854]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: [user]

pkg/ottl/ottlfuncs/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ Examples:
554554
The `Decode` Converter takes a string or byte array encoded with the specified encoding and returns the decoded string.
555555

556556
`value` is a valid encoded string or byte array.
557-
`encoding` is a valid encoding name included in the [IANA encoding index](https://www.iana.org/assignments/character-sets/character-sets.xhtml).
557+
`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`, `base64-raw`, `base64-url` or `base64-raw-url`.
558558

559559
Examples:
560560

pkg/ottl/ottlfuncs/func_decode.go

+16-6
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,15 @@ func Decode[K any](target ottl.Getter[K], encoding string) (ottl.ExprFunc[K], er
6060
}
6161

6262
switch encoding {
63+
// base64 is not in IANA index, so we have to deal with this encoding separately
6364
case "base64":
64-
// base64 is not in IANA index, so we have to deal with this encoding separately
65-
decodedBytes, err := base64.StdEncoding.DecodeString(stringValue)
66-
if err != nil {
67-
return nil, fmt.Errorf("could not decode: %w", err)
68-
}
69-
return string(decodedBytes), nil
65+
return decodeBase64(base64.StdEncoding, stringValue)
66+
case "base64-raw":
67+
return decodeBase64(base64.RawStdEncoding, stringValue)
68+
case "base64-url":
69+
return decodeBase64(base64.URLEncoding, stringValue)
70+
case "base64-raw-url":
71+
return decodeBase64(base64.RawURLEncoding, stringValue)
7072
default:
7173
e, err := textutils.LookupEncoding(encoding)
7274
if err != nil {
@@ -82,3 +84,11 @@ func Decode[K any](target ottl.Getter[K], encoding string) (ottl.ExprFunc[K], er
8284
}
8385
}, nil
8486
}
87+
88+
func decodeBase64(encoding *base64.Encoding, stringValue string) (any, error) {
89+
decodedBytes, err := encoding.DecodeString(stringValue)
90+
if err != nil {
91+
return nil, fmt.Errorf("could not decode: %w", err)
92+
}
93+
return string(decodedBytes), nil
94+
}

pkg/ottl/ottlfuncs/func_decode_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,30 @@ func TestDecode(t *testing.T) {
171171
encoding: "base64",
172172
expectedError: "illegal base64 data at input byte",
173173
},
174+
{
175+
name: "base64 with url-safe sensitive characters",
176+
value: "R28/L1p+eA==",
177+
encoding: "base64",
178+
want: "Go?/Z~x",
179+
},
180+
{
181+
name: "base64-raw with url-safe sensitive characters",
182+
value: "R28/L1p+eA",
183+
encoding: "base64-raw",
184+
want: "Go?/Z~x",
185+
},
186+
{
187+
name: "base64-url with url-safe sensitive characters",
188+
value: "R28_L1p-eA==",
189+
encoding: "base64-url",
190+
want: "Go?/Z~x",
191+
},
192+
{
193+
name: "base64-raw-url with url-safe sensitive characters",
194+
value: "R28_L1p-eA",
195+
encoding: "base64-raw-url",
196+
want: "Go?/Z~x",
197+
},
174198
}
175199
for _, tt := range tests {
176200
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)