diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index 76cd69e97e71..fec065ef74f1 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -428,6 +428,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d - Move processing to ingest node for AWS vpcflow fileset. {pull}28168[28168] - Release zoom module as GA. {pull}28106[28106] - Add support for secondary object attribute handling in ThreatIntel MISP module {pull}28124[28124] +- Add `base64Decode` and `base64DecodeNoPad` functions to `httpsjon` templates. {pull}28385[28385] *Heartbeat* diff --git a/x-pack/filebeat/docs/inputs/input-httpjson.asciidoc b/x-pack/filebeat/docs/inputs/input-httpjson.asciidoc index 665ce3eb8ba7..f92f9b637878 100644 --- a/x-pack/filebeat/docs/inputs/input-httpjson.asciidoc +++ b/x-pack/filebeat/docs/inputs/input-httpjson.asciidoc @@ -205,6 +205,8 @@ Some built-in helper functions are provided to work with the input state inside - `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"]]` +- `base64Decode`: Decodes the base64 string. Any binary output will be converted to a UTF8 string. +- `base64DecodeNoPad`: Decodes the base64 string without padding. Any binary output will be converted to a UTF8 string. - `join`: joins a list 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")]]` diff --git a/x-pack/filebeat/input/httpjson/internal/v2/value_tpl.go b/x-pack/filebeat/input/httpjson/internal/v2/value_tpl.go index ed37af4f3dd0..c340e287ca41 100644 --- a/x-pack/filebeat/input/httpjson/internal/v2/value_tpl.go +++ b/x-pack/filebeat/input/httpjson/internal/v2/value_tpl.go @@ -60,6 +60,8 @@ func (t *valueTpl) Unpack(in string) error { "hmac": hmacStringHex, "base64Encode": base64Encode, "base64EncodeNoPad": base64EncodeNoPad, + "base64Decode": base64Decode, + "base64DecodeNoPad": base64DecodeNoPad, "join": join, "sprintf": fmt.Sprintf, "hmacBase64": hmacStringBase64, @@ -271,7 +273,17 @@ func base64EncodeNoPad(values ...string) string { return base64.RawStdEncoding.EncodeToString([]byte(data)) } -func hmacString(hmacType string, hmacKey string, data string) []byte { +func base64Decode(enc string) string { + dec, _ := base64.StdEncoding.DecodeString(enc) + return string(dec) +} + +func base64DecodeNoPad(enc string) string { + dec, _ := base64.RawStdEncoding.DecodeString(enc) + return string(dec) +} + +func hmacString(hmacType string, hmacKey []byte, data string) []byte { if data == "" { return nil } @@ -279,9 +291,9 @@ func hmacString(hmacType string, hmacKey string, data string) []byte { var mac hash.Hash switch hmacType { case "sha256": - mac = hmac.New(sha256.New, []byte(hmacKey)) + mac = hmac.New(sha256.New, hmacKey) case "sha1": - mac = hmac.New(sha1.New, []byte(hmacKey)) + mac = hmac.New(sha1.New, hmacKey) default: // Upstream config validation prevents this from happening. return nil @@ -298,7 +310,7 @@ func hmacStringHex(hmacType string, hmacKey string, values ...string) string { if data == "" { return "" } - bytes := hmacString(hmacType, hmacKey, data) + bytes := hmacString(hmacType, []byte(hmacKey), data) // Get result and encode as hexadecimal string return hex.EncodeToString(bytes) } @@ -308,7 +320,7 @@ func hmacStringBase64(hmacType string, hmacKey string, values ...string) string if data == "" { return "" } - bytes := hmacString(hmacType, hmacKey, data) + bytes := hmacString(hmacType, []byte(hmacKey), data) // Get result and encode as hexadecimal string return base64.StdEncoding.EncodeToString(bytes) diff --git a/x-pack/filebeat/input/httpjson/internal/v2/value_tpl_test.go b/x-pack/filebeat/input/httpjson/internal/v2/value_tpl_test.go index 14caa3ca1d3e..06768eb478a8 100644 --- a/x-pack/filebeat/input/httpjson/internal/v2/value_tpl_test.go +++ b/x-pack/filebeat/input/httpjson/internal/v2/value_tpl_test.go @@ -367,6 +367,21 @@ func TestValueTpl(t *testing.T) { expectedVal: "", expectedError: errEmptyTemplateResult.Error(), }, + { + name: "func base64Decode 2 strings", + value: `[[base64Decode "c3RyaW5nMXN0cmluZzI="]]`, + paramCtx: emptyTransformContext(), + paramTr: transformable{}, + expectedVal: "string1string2", + }, + { + name: "func base64Decode no value", + value: `[[base64Decode ""]]`, + paramCtx: emptyTransformContext(), + paramTr: transformable{}, + expectedVal: "", + expectedError: errEmptyTemplateResult.Error(), + }, } for _, tc := range cases {