diff --git a/.chloggen/31543-add-base64decode-function-ottl.yaml b/.chloggen/31543-add-base64decode-function-ottl.yaml new file mode 100644 index 0000000000000..79c938797f291 --- /dev/null +++ b/.chloggen/31543-add-base64decode-function-ottl.yaml @@ -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: [] diff --git a/pkg/ottl/e2e/e2e_test.go b/pkg/ottl/e2e/e2e_test.go index 26dbaf85be18d..f850af9b0aa36 100644 --- a/pkg/ottl/e2e/e2e_test.go +++ b/pkg/ottl/e2e/e2e_test.go @@ -249,6 +249,12 @@ func Test_e2e_converters(t *testing.T) { statement string want func(tCtx ottllog.TransformContext) }{ + { + statement: `set(attributes["test"], Base64Decode("cGFzcw=="))`, + want: func(tCtx ottllog.TransformContext) { + tCtx.GetLogRecord().Attributes().PutStr("test", "pass") + }, + }, { statement: `set(attributes["test"], Concat(["A","B"], ":"))`, want: func(tCtx ottllog.TransformContext) { diff --git a/pkg/ottl/ottlfuncs/README.md b/pkg/ottl/ottlfuncs/README.md index 9f4728c8b5c16..a87b6562f57bf 100644 --- a/pkg/ottl/ottlfuncs/README.md +++ b/pkg/ottl/ottlfuncs/README.md @@ -377,6 +377,7 @@ Unlike functions, they do not modify any input telemetry and always return a val Available Converters: +- [Base64Decode](#base64decode) - [Concat](#concat) - [ConvertCase](#convertcase) - [ExtractPatterns](#extractpatterns) @@ -417,6 +418,21 @@ Available Converters: - [UnixSeconds](#unixseconds) - [UUID](#UUID) +### Base64Decode + +`Base64Decode(value)` + +The `Base64Decode` Converter takes a base64 encoded string and returns the decoded string. + +`value` is a valid base64 encoded string. + +Examples: + +- `Base64Decode("aGVsbG8gd29ybGQ=")` + + +- `Base64Decode(attributes["encoded field"])` + ### Concat `Concat(values[], delimiter)` diff --git a/pkg/ottl/ottlfuncs/func_base64decode.go b/pkg/ottl/ottlfuncs/func_base64decode.go new file mode 100644 index 0000000000000..42e401d71505b --- /dev/null +++ b/pkg/ottl/ottlfuncs/func_base64decode.go @@ -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 +} diff --git a/pkg/ottl/ottlfuncs/func_base64decode_test.go b/pkg/ottl/ottlfuncs/func_base64decode_test.go new file mode 100644 index 0000000000000..e6abf925fd983 --- /dev/null +++ b/pkg/ottl/ottlfuncs/func_base64decode_test.go @@ -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) + }) + } +} diff --git a/pkg/ottl/ottlfuncs/functions.go b/pkg/ottl/ottlfuncs/functions.go index a12ba9d147ba4..1f419a746e42a 100644 --- a/pkg/ottl/ottlfuncs/functions.go +++ b/pkg/ottl/ottlfuncs/functions.go @@ -35,6 +35,7 @@ func StandardConverters[K any]() map[string]ottl.Factory[K] { func converters[K any]() []ottl.Factory[K] { return []ottl.Factory[K]{ // Converters + NewBase64DecodeFactory[K](), NewConcatFactory[K](), NewConvertCaseFactory[K](), NewDoubleFactory[K](),