Skip to content

Commit

Permalink
[pkg/ottl] Add Base64Decode function (#31543) (#31730)
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
DougManton authored Mar 14, 2024
1 parent 9102c75 commit c485615
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .chloggen/31543-add-base64decode-function-ottl.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: 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: []
6 changes: 6 additions & 0 deletions pkg/ottl/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 16 additions & 0 deletions pkg/ottl/ottlfuncs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)`
Expand Down
45 changes: 45 additions & 0 deletions pkg/ottl/ottlfuncs/func_base64decode.go
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
}
92 changes: 92 additions & 0 deletions pkg/ottl/ottlfuncs/func_base64decode_test.go
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)
})
}
}
1 change: 1 addition & 0 deletions pkg/ottl/ottlfuncs/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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](),
Expand Down

0 comments on commit c485615

Please sign in to comment.