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] [httpjson] Add support for single string containing multiple relation-types in getRFC5988Link #32811

Merged
merged 4 commits into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -137,6 +137,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