diff --git a/.chloggen/ottl_sha512_converter.yaml b/.chloggen/ottl_sha512_converter.yaml new file mode 100644 index 000000000000..4a0efa91ff84 --- /dev/null +++ b/.chloggen/ottl_sha512_converter.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: pkg/ottl + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Introduce `sha512` converter to generate SHA-512 hash/digest from given payload. + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [34007] + +# (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 f26168acfa22..058de82da8db 100644 --- a/pkg/ottl/e2e/e2e_test.go +++ b/pkg/ottl/e2e/e2e_test.go @@ -619,6 +619,12 @@ func Test_e2e_converters(t *testing.T) { tCtx.GetLogRecord().Attributes().PutStr("test", "d74ff0ee8da3b9806b18c877dbf29bbde50b5bd8e4dad7a3a725000feb82e8f1") }, }, + { + statement: `set(attributes["test"], SHA512("pass"))`, + want: func(tCtx ottllog.TransformContext) { + tCtx.GetLogRecord().Attributes().PutStr("test", "5b722b307fce6c944905d132691d5e4a2214b7fe92b738920eb3fce3a90420a19511c3010a0e7712b054daef5b57bad59ecbd93b3280f210578f547f4aed4d25") + }, + }, { statement: `set(span_id, SpanID(0x0000000000000000))`, want: func(tCtx ottllog.TransformContext) { diff --git a/pkg/ottl/ottlfuncs/README.md b/pkg/ottl/ottlfuncs/README.md index 19a157d7b024..122626a133a7 100644 --- a/pkg/ottl/ottlfuncs/README.md +++ b/pkg/ottl/ottlfuncs/README.md @@ -446,6 +446,7 @@ Available Converters: - [Seconds](#seconds) - [SHA1](#sha1) - [SHA256](#sha256) +- [SHA512](#sha512) - [SpanID](#spanid) - [Split](#split) - [String](#string) @@ -1206,6 +1207,23 @@ Examples: - `SHA256("name")` +### SHA512 + +`SHA512(input)` + +The `SHA512` converter calculates sha512 hash value/digest of the `input`. + +The returned type is string. + +`input` is either a path expression to a string telemetry field or a literal string. If `input` is another type, converter raises an error. +If an error occurs during hashing, the error will be returned. + +Examples: + +- `SHA512(attributes["device.name"])` + +- `SHA512("name")` + ### SpanID `SpanID(bytes)` diff --git a/pkg/ottl/ottlfuncs/func_sha512.go b/pkg/ottl/ottlfuncs/func_sha512.go new file mode 100644 index 000000000000..370d27767534 --- /dev/null +++ b/pkg/ottl/ottlfuncs/func_sha512.go @@ -0,0 +1,48 @@ +// 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/sha512" + "encoding/hex" + "fmt" + + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" +) + +type SHA512Arguments[K any] struct { + Target ottl.StringGetter[K] +} + +func NewSHA512Factory[K any]() ottl.Factory[K] { + return ottl.NewFactory("SHA512", &SHA512Arguments[K]{}, createSHA512Function[K]) +} + +func createSHA512Function[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) { + args, ok := oArgs.(*SHA512Arguments[K]) + + if !ok { + return nil, fmt.Errorf("SHA512Factory args must be of type *SHA512Arguments[K]") + } + + return SHA512HashString(args.Target) +} + +func SHA512HashString[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 := sha512.New() + _, err = hash.Write([]byte(val)) + if err != nil { + return nil, err + } + hashValue := hex.EncodeToString(hash.Sum(nil)) + return hashValue, nil + }, nil +} diff --git a/pkg/ottl/ottlfuncs/func_sha512_test.go b/pkg/ottl/ottlfuncs/func_sha512_test.go new file mode 100644 index 000000000000..132fc5202173 --- /dev/null +++ b/pkg/ottl/ottlfuncs/func_sha512_test.go @@ -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_SHA512(t *testing.T) { + tests := []struct { + name string + value any + expected any + err bool + }{ + { + name: "empty string", + value: "", + expected: "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", + }, + { + name: "string", + value: "foo bar", + expected: "65019286222ace418f742556366f9b9da5aaf6797527d2f0cba5bfe6b2f8ed24746542a0f2be1da8d63c2477f688b608eb53628993afa624f378b03f10090ce7", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + exprFunc, err := SHA512HashString[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_SHA512Error(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 := SHA512HashString[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 48b3aa330ea4..be49d7f322e1 100644 --- a/pkg/ottl/ottlfuncs/functions.go +++ b/pkg/ottl/ottlfuncs/functions.go @@ -70,6 +70,7 @@ func converters[K any]() []ottl.Factory[K] { NewSecondsFactory[K](), NewSHA1Factory[K](), NewSHA256Factory[K](), + NewSHA512Factory[K](), NewSpanIDFactory[K](), NewSplitFactory[K](), NewFormatFactory[K](),