-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
**Description:** Adds a new Base64Decode function to facilitate ingest of base64 encoded data **Link to tracking Issue:** #31543 **Testing:** Added unit and e2e tests **Documentation:** Updated the func readme.
- Loading branch information
1 parent
9102c75
commit c485615
Showing
6 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: ottl | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: "Add new function to decode a base64 encoded string and output the original string" | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [31543] | ||
|
||
# (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: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package ottlfuncs // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs" | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"fmt" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" | ||
) | ||
|
||
type Base64DecodeArguments[K any] struct { | ||
Target ottl.StringGetter[K] | ||
} | ||
|
||
func NewBase64DecodeFactory[K any]() ottl.Factory[K] { | ||
return ottl.NewFactory("Base64Decode", &Base64DecodeArguments[K]{}, createBase64DecodeFunction[K]) | ||
} | ||
|
||
func createBase64DecodeFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) { | ||
args, ok := oArgs.(*Base64DecodeArguments[K]) | ||
|
||
if !ok { | ||
return nil, fmt.Errorf("Base64DecodeFactory args must be of type *Base64DecodeArguments[K]") | ||
} | ||
|
||
return Base64Decode(args.Target) | ||
} | ||
|
||
func Base64Decode[K any](target ottl.StringGetter[K]) (ottl.ExprFunc[K], error) { | ||
|
||
return func(ctx context.Context, tCtx K) (any, error) { | ||
val, err := target.Get(ctx, tCtx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
base64string, err := base64.StdEncoding.DecodeString(val) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return string(base64string), nil | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package ottlfuncs | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" | ||
) | ||
|
||
func Test_Base64Decode(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
value any | ||
expected any | ||
err bool | ||
}{ | ||
{ | ||
name: "base64-string", | ||
value: "aGVsbG8gd29ybGQ=", | ||
expected: "hello world", | ||
}, | ||
{ | ||
name: "empty string", | ||
value: "", | ||
expected: "", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
exprFunc, err := Base64Decode[any](&ottl.StandardStringGetter[any]{ | ||
Getter: func(context.Context, any) (any, error) { | ||
return tt.value, nil | ||
}, | ||
}) | ||
assert.NoError(t, err) | ||
result, err := exprFunc(nil, nil) | ||
if tt.err { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
assert.Equal(t, tt.expected, result) | ||
}) | ||
} | ||
} | ||
|
||
func Test_Base64DecodeError(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
value any | ||
err bool | ||
expectedError string | ||
}{ | ||
{ | ||
name: "non-string", | ||
value: 10, | ||
expectedError: "expected string but got int", | ||
}, | ||
{ | ||
name: "nil", | ||
value: nil, | ||
expectedError: "expected string but got nil", | ||
}, | ||
{ | ||
name: "not-base64-string", | ||
value: "!@#$%^&*()_+", | ||
expectedError: "illegal base64 data at input byte", | ||
}, | ||
{ | ||
name: "missing-base64-padding", | ||
value: "cmVtb3ZlZCBwYWRkaW5nCg", | ||
expectedError: "illegal base64 data at input byte", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
exprFunc, err := Base64Decode[any](&ottl.StandardStringGetter[any]{ | ||
Getter: func(context.Context, any) (any, error) { | ||
return tt.value, nil | ||
}, | ||
}) | ||
assert.NoError(t, err) | ||
_, err = exprFunc(nil, nil) | ||
assert.ErrorContains(t, err, tt.expectedError) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters