Skip to content

Commit

Permalink
[pkg/ottl]: Add MD5 function (#33793)
Browse files Browse the repository at this point in the history
**Description:** <Describe what has changed.>
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->

Introduced the `MD5` converter function which returns the MD5
hash/digest of the given `value` (path expression to a string telemetry
field or a literal string).

**Link to tracking Issue:**
#33792

**Testing:** <Describe what testing was performed and which tests were
added.>
- Unit tests
- E2E tests

**Documentation:** 
A new README entry was added for the `MD5` function.

---------

Co-authored-by: Evan Bradley <11745660+evan-bradley@users.noreply.github.com>
  • Loading branch information
edmocosta and evan-bradley authored Aug 9, 2024
1 parent 9727f23 commit 46a9f94
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .chloggen/ottl_md5_function.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: pkg/ottl

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Add the `MD5` function to convert the `value` into a MD5 hash/digest"

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

# (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]
6 changes: 6 additions & 0 deletions pkg/ottl/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,12 @@ func Test_e2e_converters(t *testing.T) {
tCtx.GetLogRecord().Attributes().PutDouble("test", 0)
},
},
{
statement: `set(attributes["test"], MD5("pass"))`,
want: func(tCtx ottllog.TransformContext) {
tCtx.GetLogRecord().Attributes().PutStr("test", "1a1dc91c907325c69271ddf0c944bc72")
},
},
{
statement: `set(attributes["test"], Microseconds(Duration("1ms")))`,
want: func(tCtx ottllog.TransformContext) {
Expand Down
21 changes: 21 additions & 0 deletions pkg/ottl/ottlfuncs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ Available Converters:
- [IsString](#isstring)
- [Len](#len)
- [Log](#log)
- [MD5](#md5)
- [Microseconds](#microseconds)
- [Milliseconds](#milliseconds)
- [Minute](#minute)
Expand Down Expand Up @@ -990,6 +991,26 @@ Examples:

- `Int(Log(attributes["duration_ms"])`

### MD5

`MD5(value)`

The `MD5` Converter converts the `value` to a md5 hash/digest.

The returned type is string.

`value` is either a path expression to a string telemetry field or a literal string. If `value` is another type an error is returned.

If an error occurs during hashing it will be returned.

Examples:

- `MD5(attributes["device.name"])`

- `MD5("name")`

**Note:** According to the National Institute of Standards and Technology (NIST), MD5 is no longer a recommended hash function. It should be avoided except when required for compatibility. New uses should prefer a SHA-2 family function (e.g. SHA-256, SHA-512) whenever possible.

### Microseconds

`Microseconds(value)`
Expand Down
47 changes: 47 additions & 0 deletions pkg/ottl/ottlfuncs/func_md5.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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"
"crypto/md5" // #nosec
"encoding/hex"
"fmt"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
)

type MD5Arguments[K any] struct {
Target ottl.StringGetter[K]
}

func NewMD5Factory[K any]() ottl.Factory[K] {
return ottl.NewFactory("MD5", &MD5Arguments[K]{}, createMD5Function[K])
}

func createMD5Function[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) {
args, ok := oArgs.(*MD5Arguments[K])

if !ok {
return nil, fmt.Errorf("MD5Factory args must be of type *MD5Arguments[K]")
}

return MD5HashString(args.Target)
}

func MD5HashString[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
}
hash := md5.New() // #nosec
_, err = hash.Write([]byte(val))
if err != nil {
return nil, err
}
return hex.EncodeToString(hash.Sum(nil)), nil
}, nil
}
82 changes: 82 additions & 0 deletions pkg/ottl/ottlfuncs/func_md5_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// 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_MD5(t *testing.T) {
tests := []struct {
name string
value any
expected any
err bool
}{
{
name: "string",
value: "hello world",
expected: "5eb63bbbe01eeed093cb22bb8f5acdc3",
},
{
name: "empty string",
value: "",
expected: "d41d8cd98f00b204e9800998ecf8427e",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
exprFunc, err := MD5HashString[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_MD5Error(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",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
exprFunc, err := MD5HashString[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 @@ -57,6 +57,7 @@ func converters[K any]() []ottl.Factory[K] {
NewIsStringFactory[K](),
NewLenFactory[K](),
NewLogFactory[K](),
NewMD5Factory[K](),
NewMicrosecondsFactory[K](),
NewMillisecondsFactory[K](),
NewMinuteFactory[K](),
Expand Down

0 comments on commit 46a9f94

Please sign in to comment.