Skip to content

Commit

Permalink
Add base64Decode template functions (#28385) (#28436)
Browse files Browse the repository at this point in the history
(cherry picked from commit 6ad8689)

Co-authored-by: Marc Guasch <[email protected]>
  • Loading branch information
mergify[bot] and marc-gr authored Oct 14, 2021
1 parent e54d1f7 commit 7a510af
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -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*
Expand Down
2 changes: 2 additions & 0 deletions x-pack/filebeat/docs/inputs/input-httpjson.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -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")]]`
Expand Down
22 changes: 17 additions & 5 deletions x-pack/filebeat/input/httpjson/internal/v2/value_tpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -271,17 +273,27 @@ 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
}
// Create a new HMAC by defining the hash type and the key (as byte array)
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
Expand All @@ -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)
}
Expand All @@ -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)
Expand Down
15 changes: 15 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 @@ -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 {
Expand Down

0 comments on commit 7a510af

Please sign in to comment.