Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Filebeat] Add HMAC template function for httpjson input #27168

Merged
merged 3 commits into from
Aug 4, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Add new template functions and `value_type` parameter to `httpjson` transforms. {pull}26847[26847]
- Add support to merge registry updates in the filestream input across multiple ACKed batches in case of backpressure in the registry or disk. {pull}25976[25976]
- Update Elasticsearch module's ingest pipeline for parsing new deprecation logs {issue}26857[26857] {pull}26880[26880]
- Add new `hmac` template function for httpjson input {pull}27168[27168]

*Heartbeat*

Expand Down
1 change: 1 addition & 0 deletions x-pack/filebeat/docs/inputs/input-httpjson.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ Some built-in helper functions are provided to work with the input state inside
- `add`: adds a list of integers and returns their sum.
- `mul`: multiplies two integers.
- `div`: does the integer division of two integer values.
- `hmac`: calculates the hmac signature of a list of strings concatenated together. Supports sha1 or sha256. Example `[[hmac "sha256" "secret" "string1" "string2" (formatDate (now) "RFC1123")]]`

In addition to the provided functions, any of the native functions for https://golang.org/pkg/time/#Time[`time.Time`], https://golang.org/pkg/net/http/#Header[`http.Header`], and https://golang.org/pkg/net/url/#Values[`url.Values`] types can be used on the corresponding objects. Examples: `[[(now).Day]]`, `[[.last_response.header.Get "key"]]`

Expand Down
29 changes: 29 additions & 0 deletions x-pack/filebeat/input/httpjson/internal/v2/value_tpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ package v2

import (
"bytes"
"crypto/hmac"
"crypto/sha1"
"crypto/sha256"
"encoding/hex"
"errors"
"hash"
"reflect"
"regexp"
"strconv"
Expand Down Expand Up @@ -48,6 +53,7 @@ func (t *valueTpl) Unpack(in string) error {
"add": add,
"mul": mul,
"div": div,
"hmac": hmac_string,
}).
Delims(leftDelim, rightDelim).
Parse(in)
Expand Down Expand Up @@ -236,3 +242,26 @@ func mul(a, b int64) int64 {
func div(a, b int64) int64 {
return a / b
}

func hmac_string(hmac_type string, hmac_key string, values ...string) string {
legoguy1000 marked this conversation as resolved.
Show resolved Hide resolved
data := strings.Join(values[:], "")
if data == "" {
return ""
}
// Create a new HMAC by defining the hash type and the key (as byte array)
var mac hash.Hash
switch hmac_type {
case "sha256":
mac = hmac.New(sha256.New, []byte(hmac_key))
case "sha1":
mac = hmac.New(sha1.New, []byte(hmac_key))
default:
// Upstream config validation prevents this from happening.
return ""
}
// Write Data to it
mac.Write([]byte(data))

// Get result and encode as hexadecimal string
return hex.EncodeToString(mac.Sum(nil))
}
24 changes: 24 additions & 0 deletions x-pack/filebeat/input/httpjson/internal/v2/value_tpl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,30 @@ func TestValueTpl(t *testing.T) {
paramTr: transformable{},
expectedVal: "4",
},
{
name: "func sha1 hmac",
value: `[[hmac "sha1" "secret" "string1" "string2"]]`,
paramCtx: emptyTransformContext(),
paramTr: transformable{},
expectedVal: "87eca1e7cba012b2dd4a907c2ad4345a252a38f4",
},
{
name: "func sha256 hmac",
setup: func() { timeNow = func() time.Time { return time.Unix(1627697597, 0).UTC() } },
teardown: func() { timeNow = time.Now },
value: `[[hmac "sha256" "secret" "string1" "string2" (formatDate (now) "RFC1123")]]`,
paramCtx: emptyTransformContext(),
paramTr: transformable{},
expectedVal: "adc61cd206e146f2d1337504e760ea70f3d2e34bedf28d07802e0e776568a06b",
},
{
name: "func invalid hmac",
value: `[[hmac "md5" "secret" "string1" "string2"]]`,
paramCtx: emptyTransformContext(),
paramTr: transformable{},
expectedVal: "",
expectedError: errEmptyTemplateResult.Error(),
},
}

for _, tc := range cases {
Expand Down