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] adding base64encode func to value template #27681

Merged
merged 4 commits into from
Sep 3, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -772,6 +772,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Update `tags` and `threatintel.indicator.provider` fields in `threatintel.anomali` ingest pipeline {issue}24746[24746] {pull}27141[27141]
- Move AWS module and filesets to GA. {pull}27428[27428]
- update ecs.version to ECS 1.11.0. {pull}27107[27107]
- Add base64 Encode functionality to httpjson input. {pull}27681[27681]

*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 @@ -204,6 +204,7 @@ Some built-in helper functions are provided to work with the input state inside
- `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")]]`
- `base64Encode`: Joins and base64 encodes all supplied strings. Example `[[base64Encode "string1" "string2"]]`

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
11 changes: 11 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 @@ -9,6 +9,7 @@ import (
"crypto/hmac"
"crypto/sha1"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"errors"
"hash"
Expand Down Expand Up @@ -54,6 +55,7 @@ func (t *valueTpl) Unpack(in string) error {
"mul": mul,
"div": div,
"hmac": hmacString,
"base64Encode": base64Encode,
}).
Delims(leftDelim, rightDelim).
Parse(in)
Expand Down Expand Up @@ -243,6 +245,15 @@ func div(a, b int64) int64 {
return a / b
}

func base64Encode(values ...string) string {
data := strings.Join(values[:], "")
P1llus marked this conversation as resolved.
Show resolved Hide resolved
if data == "" {
return ""
}

return base64.StdEncoding.EncodeToString([]byte(data))
}

func hmacString(hmacType string, hmacKey string, values ...string) string {
data := strings.Join(values[:], "")
if data == "" {
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 @@ -276,6 +276,21 @@ func TestValueTpl(t *testing.T) {
expectedVal: "",
expectedError: errEmptyTemplateResult.Error(),
},
{
name: "func base64Encode 2 strings",
value: `[[base64Encode "string1" "string2"]]`,
paramCtx: emptyTransformContext(),
paramTr: transformable{},
expectedVal: "c3RyaW5nMXN0cmluZzI=",
},
{
name: "func base64Encode no value",
value: `[[base64Encode ""]]`,
paramCtx: emptyTransformContext(),
paramTr: transformable{},
expectedVal: "",
expectedError: errEmptyTemplateResult.Error(),
},
}

for _, tc := range cases {
Expand Down