Skip to content

Commit

Permalink
[filebeat][httpjson] Set url as a pointer to ensure access to all met…
Browse files Browse the repository at this point in the history
…hods (#28695) (#28698)

* Set url as a pointer to ensure access to all methods

* Add changelog line

* Fix text

(cherry picked from commit 47dba85)

Co-authored-by: Marc Guasch <[email protected]>
  • Loading branch information
mergify[bot] and marc-gr authored Oct 29, 2021
1 parent b0134c7 commit 49b222a
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Improve Cisco ASA/FTD parsing of messages - better support for identity FW messages. Change network.bytes, source.bytes, and destination.bytes to long from integer since value can exceed integer capacity. Add descriptions for various processors for easier pipeline editing in Kibana UI. {pull}23766[23766]
- Fix initialization of http client in Cloudfoundry input. {issue}28271[28271] {pull}28277[28277]
- Fix aws-s3 input by checking if GetObject API call response content type exists. {pull}28457[28457]
- Set `url` as a pointer in the `httpjson` template context to ensure access to all methods. {pull}28695[28695]

*Heartbeat*

Expand Down
4 changes: 2 additions & 2 deletions x-pack/filebeat/input/httpjson/internal/v2/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestCtxAfterDoRequest(t *testing.T) {
assert.EqualValues(t,
&response{
page: 1,
url: newURL(fmt.Sprintf("%s?%s", testServer.URL, "%24filter=alertCreationTime+ge+2002-10-02T14%3A50%3A00Z")),
url: *(newURL(fmt.Sprintf("%s?%s", testServer.URL, "%24filter=alertCreationTime+ge+2002-10-02T14%3A50%3A00Z"))),
body: common.MapStr{"@timestamp": "2002-10-02T15:00:00Z", "foo": "bar"},
},
lastResp,
Expand Down Expand Up @@ -127,7 +127,7 @@ func TestCtxAfterDoRequest(t *testing.T) {
assert.EqualValues(t,
&response{
page: 1,
url: newURL(fmt.Sprintf("%s?%s", testServer.URL, "%24filter=alertCreationTime+ge+2002-10-02T15%3A00%3A00Z")),
url: *(newURL(fmt.Sprintf("%s?%s", testServer.URL, "%24filter=alertCreationTime+ge+2002-10-02T15%3A00%3A00Z"))),
body: common.MapStr{"@timestamp": "2002-10-02T15:00:01Z", "foo": "bar"},
},
lastResp,
Expand Down
12 changes: 7 additions & 5 deletions x-pack/filebeat/input/httpjson/internal/v2/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,12 @@ func (resp *response) templateValues() common.MapStr {
return common.MapStr{}
}
return common.MapStr{
"header": resp.header.Clone(),
"page": resp.page,
"url.value": resp.url.String(),
"url.params": resp.url.Query(),
"body": resp.body,
"header": resp.header.Clone(),
"page": resp.page,
"url": common.MapStr{
"value": resp.url.String(),
"params": resp.url.Query(),
},
"body": resp.body,
}
}
8 changes: 5 additions & 3 deletions x-pack/filebeat/input/httpjson/internal/v2/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
func TestTemplateValues(t *testing.T) {
resp := &response{
page: 1,
url: newURL("http://test?p1=v1"),
url: *(newURL("http://test?p1=v1")),
header: http.Header{
"Authorization": []string{"Bearer token"},
},
Expand All @@ -28,8 +28,10 @@ func TestTemplateValues(t *testing.T) {
vals := resp.templateValues()

assert.Equal(t, resp.page, vals["page"])
assert.Equal(t, resp.url.String(), vals["url.value"])
assert.EqualValues(t, resp.url.Query(), vals["url.params"])
v, _ := vals.GetValue("url.value")
assert.Equal(t, resp.url.String(), v)
v, _ = vals.GetValue("url.params")
assert.EqualValues(t, resp.url.Query(), v)
assert.EqualValues(t, resp.header, vals["header"])
assert.EqualValues(t, resp.body, vals["body"])

Expand Down
6 changes: 3 additions & 3 deletions x-pack/filebeat/input/httpjson/internal/v2/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (tr transformable) body() common.MapStr {
}

func (tr transformable) setURL(v url.URL) {
tr.Put("url", v)
tr.Put("url", &v)
}

func (tr transformable) url() url.URL {
Expand All @@ -170,12 +170,12 @@ func (tr transformable) url() url.URL {
return url.URL{}
}

u, ok := val.(url.URL)
u, ok := val.(*url.URL)
if !ok {
return url.URL{}
}

return u
return *u
}

type transform interface {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ func TestDifferentSetValueTypes(t *testing.T) {
assert.EqualValues(t, exp, tr.body())
}

func newURL(u string) url.URL {
func newURL(u string) *url.URL {
url, _ := url.Parse(u)
return *url
return url
}
14 changes: 13 additions & 1 deletion x-pack/filebeat/input/httpjson/internal/v2/value_tpl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ func TestValueTpl(t *testing.T) {
setup func()
teardown func()
}{
{
name: "can access Go types in context",
value: `[[.last_response.header.Get "foo"]] [[.last_response.url.params.Get "foo"]] [[.url.Host]] [[.url.Query.Get "bar"]]`,
paramCtx: &transformContext{
firstEvent: &common.MapStr{},
lastEvent: &common.MapStr{},
lastResponse: newTestResponse(common.MapStr{"param": 25}, http.Header{"Foo": []string{"bar"}}, "http://localhost?foo=bar"),
},
paramTr: transformable{"url": newURL("http://localhost?bar=bazz")},
paramDefVal: "",
expectedVal: "bar bar localhost bazz",
},
{
name: "can render values from ctx",
value: "[[.last_response.body.param]]",
Expand Down Expand Up @@ -318,7 +330,7 @@ func newTestResponse(body common.MapStr, header http.Header, url string) *respon
resp.header = header
}
if url != "" {
resp.url = newURL(url)
resp.url = *(newURL(url))
}
return resp
}

0 comments on commit 49b222a

Please sign in to comment.