Skip to content

Commit

Permalink
[Filebeat] [httpjson] Add support for single string containing multip…
Browse files Browse the repository at this point in the history
…le relation-types in getRFC5988Link (#32811)

* case with single string but multiple rel

* misspell lint error

* Add ascii doc
  • Loading branch information
kcreddy authored Aug 25, 2022
1 parent 6c1620a commit 9cab775
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ https://github.com/elastic/beats/compare/v8.2.0\...main[Check the HEAD diff]
- add documentation for decode_xml_wineventlog processor field mappings. {pull}32456[32456]
- httpjson input: Add request tracing logger. {issue}32402[32402] {pull}32412[32412]
- Add cloudflare R2 to provider list in AWS S3 input. {pull}32620[32620]
- Add support for single string containing multiple relation-types in getRFC5988Link. {pull}32811[32811]

*Auditbeat*

Expand Down
13 changes: 10 additions & 3 deletions x-pack/filebeat/input/httpjson/value_tpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ func parseTimestampNano(ns int64) time.Time {

var regexpLinkRel = regexp.MustCompile(`<(.*)>;.*\srel\="?([^;"]*)`)

func getRFC5988Link(rel string, links []string) string {
for _, link := range links {
func getMatchLink(rel string, linksSplit []string) string {
for _, link := range linksSplit {
if !regexpLinkRel.MatchString(link) {
continue
}
Expand All @@ -231,10 +231,17 @@ func getRFC5988Link(rel string, links []string) string {

return matches[1]
}

return ""
}

func getRFC5988Link(rel string, links []string) string {
if len(links) == 1 && strings.Count(links[0], "rel=") > 1 {
linksSplit := strings.Split(links[0], ",")
return getMatchLink(rel, linksSplit)
}
return getMatchLink(rel, links)
}

func toInt(v interface{}) int64 {
vv := reflect.ValueOf(v)
switch vv.Kind() {
Expand Down
73 changes: 72 additions & 1 deletion x-pack/filebeat/input/httpjson/value_tpl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,24 @@ func TestValueTpl(t *testing.T) {
expectedVal: "2020-11-05 13:25:32 +0000 UTC",
},
{
name: "func getRFC5988Link",
name: "func getRFC5988Link single rel matches",
value: `[[ getRFC5988Link "next" .last_response.header.Link ]]`,
paramCtx: &transformContext{
firstEvent: &mapstr.M{},
lastEvent: &mapstr.M{},
lastResponse: newTestResponse(
nil,
http.Header{"Link": []string{
`<https://example.com/api/v1/users?after=00ubfjQEMYBLRUWIEDKK>; title="Page 3"; rel="next"`,
}},
"",
),
},
paramTr: transformable{},
expectedVal: "https://example.com/api/v1/users?after=00ubfjQEMYBLRUWIEDKK",
},
{
name: "func getRFC5988Link multiple rel as separate strings matches",
value: `[[ getRFC5988Link "previous" .last_response.header.Link ]]`,
paramCtx: &transformContext{
firstEvent: &mapstr.M{},
Expand All @@ -206,6 +223,60 @@ func TestValueTpl(t *testing.T) {
paramTr: transformable{},
expectedVal: "https://example.com/api/v1/users?before=00ubfjQEMYBLRUWIEDKK",
},
{
name: "func getRFC5988Link multiple rel as separate strings in random order matches",
value: `[[ getRFC5988Link "previous" .last_response.header.Link ]]`,
paramCtx: &transformContext{
firstEvent: &mapstr.M{},
lastEvent: &mapstr.M{},
lastResponse: newTestResponse(
nil,
http.Header{"Link": []string{
`<https://example.com/api/v1/users?before=00ubfjQEMYBLRUWIEDKK>; title="Page 1"; rel="previous"`,
`<https://example.com/api/v1/users?after=00ubfjQEMYBLRUWIEDKK>; title="Page 3"; rel="next"`,
}},
"",
),
},
paramTr: transformable{},
expectedVal: "https://example.com/api/v1/users?before=00ubfjQEMYBLRUWIEDKK",
},
{
name: "func getRFC5988Link multiple rel as single string matches",
value: `[[ getRFC5988Link "previous" .last_response.header.Link ]]`,
paramCtx: &transformContext{
firstEvent: &mapstr.M{},
lastEvent: &mapstr.M{},
lastResponse: newTestResponse(
nil,
http.Header{"Link": []string{
`<https://example.com/api/v1/users?before=00ubfjQEMYBLRUWIEDKK>; title="Page 1"; rel="previous",
<https://example.com/api/v1/users?after=00ubfjQEMYBLRUWIEDKK>; title="Page 3"; rel="next"`,
}},
"",
),
},
paramTr: transformable{},
expectedVal: "https://example.com/api/v1/users?before=00ubfjQEMYBLRUWIEDKK",
},
{
name: "func getRFC5988Link multiple rel as single string in random order matches",
value: `[[ getRFC5988Link "next" .last_response.header.Link ]]`,
paramCtx: &transformContext{
firstEvent: &mapstr.M{},
lastEvent: &mapstr.M{},
lastResponse: newTestResponse(
nil,
http.Header{"Link": []string{
`<https://example.com/api/v1/users?before=00ubfjQEMYBLRUWIEDKK>; title="Page 1"; rel="previous",
<https://example.com/api/v1/users?after=00ubfjQEMYBLRUWIEDKK>; title="Page 3"; rel="next"`,
}},
"",
),
},
paramTr: transformable{},
expectedVal: "https://example.com/api/v1/users?after=00ubfjQEMYBLRUWIEDKK",
},
{
name: "func getRFC5988Link does not match",
value: `[[ getRFC5988Link "previous" .last_response.header.Link ]]`,
Expand Down

0 comments on commit 9cab775

Please sign in to comment.