Skip to content

Commit

Permalink
x-pack/filebeat/input/httpjson: Add hexDecode function (#31886)
Browse files Browse the repository at this point in the history
* Add hexDecode function to enhance httpjson

* Add CHANGELOG.next.asciidoc entry

* Update documentation to include example

* Conditionally return empty string in case of error
  • Loading branch information
darshan-elastic authored and chrisberkhout committed Jun 1, 2023
1 parent 1ff7ab6 commit c33dd17
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ https://github.com/elastic/beats/compare/v8.2.0\...main[Check the HEAD diff]
- 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 authentication fields to RabbitMQ module documents. {issue}31159[31159] {pull}31680[31680]
- 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 "b0a92a08a9b4883aa3aa2d0957be12a678cbdbb32dc5db09fe68239a09872f96"]]`; Expected Output: `"\xb0\xa9*\b\xa9\xb4\x88:\xa3\xaa-\tW\xbe\x12\xa6x\xcb۳-\xc5\xdb\t\xfeh#\x9a\t\x87/\x96"`
- `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
9 changes: 9 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,14 @@ func hashStrings(typ string, data []string) []byte {
return h.Sum(nil)
}

func hexDecode(enc string) string {
decodedString, err := hex.DecodeString(enc)
if err != nil {
return ""
}
return string(decodedString)
}

func uuidString() string {
uuid, err := uuid.NewRandom()
if err != nil {
Expand Down
23 changes: 23 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,29 @@ 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 invalid hexDecode string",
value: `[[hexDecode "abcdefghijklmnopqrstuvwxyz"]]`,
paramCtx: emptyTransformContext(),
paramTr: transformable{},
expectedVal: "",
expectedError: errEmptyTemplateResult.Error(),
},
{
name: "func join",
value: `[[join .last_response.body.strarr ","]] [[join .last_response.body.iarr ","]] ` +
Expand Down

0 comments on commit c33dd17

Please sign in to comment.