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 Base64 encoded HMAC & UUID template functions to httpjson input #27873

Merged
merged 7 commits into from
Sep 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -756,6 +756,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Add `ignore_empty_value` flag to `httpjson` `split` processor. {pull}27880[27880]
- Update Cisco ASA/FTD ingest pipeline grok/dissect patterns for multiple message IDs. {issue}26869[26869] {pull}26879[26879]
- Add write access to `url.value` from `request.transforms` in `httpjson` input. {pull}27937[27937]
- Add Base64 encoded HMAC and UUID template functions to `httpjson` input {pull}27873[27873]

*Heartbeat*

Expand Down
4 changes: 3 additions & 1 deletion x-pack/filebeat/docs/inputs/input-httpjson.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,13 @@ 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")]]`
- `hmac`: calculates the hmac signature of a list of strings concatenated together. Returns a hex encoded signature. Supports sha1 or sha256. Example `[[hmac "sha256" "secret" "string1" "string2" (formatDate (now) "RFC1123")]]`
- `base64Encode`: Joins and base64 encodes all supplied strings. Example `[[base64Encode "string1" "string2"]]`
- `base64EncodeNoPad`: Joins and base64 encodes all supplied strings without padding. Example `[[base64EncodeNoPad "string1" "string2"]]`
- `join`: joins a list of strings using the specified separator. Example: `[[join .body.arr ","]]`
- `sprintf`: formats according to a format specifier and returns the resulting string. Refer to https://pkg.go.dev/fmt#Sprintf[the Go docs] for usage. Example: `[[sprintf "%d:%q" 34 "quote this"]]`
- `hmacBase64`: calculates the hmac signature of a list of strings concatenated together. Returns a base64 encoded signature. Supports sha1 or sha256. Example `[[hmac "sha256" "secret" "string1" "string2" (formatDate (now) "RFC1123")]]`
- `uuid`: returns a random UUID such as `a11e8780-e3e7-46d0-8e76-f66e75acf019` Example: `[[ uuid ]]`

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
44 changes: 38 additions & 6 deletions x-pack/filebeat/input/httpjson/internal/v2/value_tpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"text/template"
"time"

"github.com/google/uuid"

"github.com/elastic/beats/v7/libbeat/logp"
)

Expand Down Expand Up @@ -55,11 +57,13 @@ func (t *valueTpl) Unpack(in string) error {
"add": add,
"mul": mul,
"div": div,
"hmac": hmacString,
"hmac": hmacStringHex,
"base64Encode": base64Encode,
"base64EncodeNoPad": base64EncodeNoPad,
"join": strings.Join,
"sprintf": fmt.Sprintf,
"hmacBase64": hmacStringBase64,
"uuid": uuidString,
}).
Delims(leftDelim, rightDelim).
Parse(in)
Expand Down Expand Up @@ -267,10 +271,9 @@ func base64EncodeNoPad(values ...string) string {
return base64.RawStdEncoding.EncodeToString([]byte(data))
}

func hmacString(hmacType string, hmacKey string, values ...string) string {
data := strings.Join(values[:], "")
func hmacString(hmacType string, hmacKey string, data string) []byte {
if data == "" {
return ""
return nil
}
// Create a new HMAC by defining the hash type and the key (as byte array)
var mac hash.Hash
Expand All @@ -281,11 +284,40 @@ func hmacString(hmacType string, hmacKey string, values ...string) string {
mac = hmac.New(sha1.New, []byte(hmacKey))
default:
// Upstream config validation prevents this from happening.
return ""
return nil
}
// Write Data to it
mac.Write([]byte(data))

// Get result and encode as bytes
return mac.Sum(nil)
}

func hmacStringHex(hmacType string, hmacKey string, values ...string) string {
data := strings.Join(values[:], "")
if data == "" {
return ""
}
bytes := hmacString(hmacType, hmacKey, data)
// Get result and encode as hexadecimal string
return hex.EncodeToString(mac.Sum(nil))
return hex.EncodeToString(bytes)
}

func hmacStringBase64(hmacType string, hmacKey string, values ...string) string {
data := strings.Join(values[:], "")
if data == "" {
return ""
}
bytes := hmacString(hmacType, hmacKey, data)

// Get result and encode as hexadecimal string
return base64.StdEncoding.EncodeToString(bytes)
}

func uuidString() string {
uuid, err := uuid.NewRandom()
if err != nil {
return ""
}
return uuid.String()
}
30 changes: 27 additions & 3 deletions x-pack/filebeat/input/httpjson/internal/v2/value_tpl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,14 +253,14 @@ func TestValueTpl(t *testing.T) {
expectedVal: "4",
},
{
name: "func sha1 hmac",
name: "func sha1 hmac Hex",
value: `[[hmac "sha1" "secret" "string1" "string2"]]`,
paramCtx: emptyTransformContext(),
paramTr: transformable{},
expectedVal: "87eca1e7cba012b2dd4a907c2ad4345a252a38f4",
},
{
name: "func sha256 hmac",
name: "func sha256 hmac Hex",
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")]]`,
Expand All @@ -269,7 +269,7 @@ func TestValueTpl(t *testing.T) {
expectedVal: "adc61cd206e146f2d1337504e760ea70f3d2e34bedf28d07802e0e776568a06b",
},
{
name: "func invalid hmac",
name: "func invalid hmac Hex",
value: `[[hmac "md5" "secret" "string1" "string2"]]`,
paramCtx: emptyTransformContext(),
paramTr: transformable{},
Expand Down Expand Up @@ -331,6 +331,30 @@ func TestValueTpl(t *testing.T) {
paramTr: transformable{},
expectedVal: `"foo,bar":1`,
},
{
name: "func sha1 hmac Base64",
value: `[[hmacBase64 "sha1" "secret" "string1" "string2"]]`,
paramCtx: emptyTransformContext(),
paramTr: transformable{},
expectedVal: "h+yh58ugErLdSpB8KtQ0WiUqOPQ=",
},
{
name: "func sha256 hmac Base64",
setup: func() { timeNow = func() time.Time { return time.Unix(1627697597, 0).UTC() } },
teardown: func() { timeNow = time.Now },
value: `[[hmacBase64 "sha256" "secret" "string1" "string2"]]`,
paramCtx: emptyTransformContext(),
paramTr: transformable{},
expectedVal: "HlglO6yRZs0Ts3MjmgnRKtTJk3fr9nt8LmeliVKZyAA=",
},
{
name: "func invalid hmac Base64",
value: `[[hmacBase64 "md5" "secret" "string1" "string2"]]`,
paramCtx: emptyTransformContext(),
paramTr: transformable{},
expectedVal: "",
expectedError: errEmptyTemplateResult.Error(),
},
}

for _, tc := range cases {
Expand Down