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

x-pack/filebeat/input/httpjson: Add hexDecode function #31886

Merged
merged 6 commits into from
Jun 20, 2022
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 @@ -105,6 +105,7 @@ https://github.com/elastic/beats/compare/v8.2.0\...main[Check the HEAD diff]
- Add template helper function for hashing strings. {issue}31613[31613] {pull}31630[31630]
- Add extended okta.debug_context.debug_data handling. {pull}31676[31676]
- Add `auth.oauth2.google.jwt_json` option to `httpjson` input. {pull}31750[31750]
- Add template helper function for decoding hexadecimal strings. {pull}31886[31886]

*Auditbeat*

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 @@ -221,6 +221,7 @@ Some built-in helper functions are provided to work with the input state inside
- `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")]]`
- `hashBase64`: calculates the hash of a list of strings concatenated together. Returns a base64 encoded hash. Supports sha1 or sha256. Example `[[hash "sha256" "string1" "string2" (formatDate (now) "RFC1123")]]`
- `hexDecode`: Decodes the hexadecimal string. Any hexadecimal string will be converted to its bytes representation. Example `[[hexDecode "string"]]`
darshan-elastic marked this conversation as resolved.
Show resolved Hide resolved
- `uuid`: returns a random UUID such as `a11e8780-e3e7-46d0-8e76-f66e75acf019`. Example: `[[ uuid ]]`
- `userAgent`: generates the User Agent with optional additional values. If no arguments are provided, it will generate the default User Agent that is added to all requests by default. It is recommended to delete the existing User-Agent header before setting a new one. Example: `[[ userAgent "integration/1.2.3" ]]` would generate `Elastic-Filebeat/8.1.0 (darwin; amd64; 9b893e88cfe109e64638d65c58fd75c2ff695402; 2021-12-15 13:20:00 +0000 UTC; integration_name/1.2.3)`
- `beatInfo`: returns a map containing information about the Beat. Available keys in the map are `goos` (running operating system), `goarch` (running system architecture), `commit` (git commit of current build), `buildtime` (compile time of current build), `version` (version of current build). Example: `[[ beatInfo.version ]]` returns `{version}`.
Expand Down
6 changes: 6 additions & 0 deletions x-pack/filebeat/input/httpjson/value_tpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func (t *valueTpl) Unpack(in string) error {
"div": div,
"hmac": hmacStringHex,
"hash": hashStringHex,
"hexDecode": hexDecode,
"base64Encode": base64Encode,
"base64EncodeNoPad": base64EncodeNoPad,
"base64Decode": base64Decode,
Expand Down Expand Up @@ -362,6 +363,11 @@ func hashStrings(typ string, data []string) []byte {
return h.Sum(nil)
}

func hexDecode(enc string) string {
decodedString, _ := hex.DecodeString(enc)
darshan-elastic marked this conversation as resolved.
Show resolved Hide resolved
return string(decodedString)
}

func uuidString() string {
uuid, err := uuid.NewRandom()
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions x-pack/filebeat/input/httpjson/value_tpl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,21 @@ func TestValueTpl(t *testing.T) {
expectedVal: "",
expectedError: errEmptyTemplateResult.Error(),
},
{
name: "func hexDecode string",
value: `[[hexDecode "b0a92a08a9b4883aa3aa2d0957be12a678cbdbb32dc5db09fe68239a09872f96"]]`,
paramCtx: emptyTransformContext(),
paramTr: transformable{},
expectedVal: "\xb0\xa9*\b\xa9\xb4\x88:\xa3\xaa-\tW\xbe\x12\xa6x\xcb۳-\xc5\xdb\t\xfeh#\x9a\t\x87/\x96",
},
{
name: "func hexDecode empty string",
value: `[[hexDecode ""]]`,
paramCtx: emptyTransformContext(),
paramTr: transformable{},
expectedVal: "",
expectedError: errEmptyTemplateResult.Error(),
},
{
name: "func join",
value: `[[join .last_response.body.strarr ","]] [[join .last_response.body.iarr ","]] ` +
Expand Down