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 (elastic#28695)

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

* Add changelog line

* Fix text

(cherry picked from commit 47dba85)
  • Loading branch information
marc-gr committed Feb 24, 2022
1 parent 6e9dd49 commit 5861ebf
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 @@ -48,6 +48,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d

- auditd: Prevent mapping explosion when truncated EXECVE records are ingested. {pull}30382[30382]
- elasticsearch: fix duplicate ingest when using a common appender configuration {issue}30428[30428] {pull}30440[30440]
- 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/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/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/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/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
4 changes: 2 additions & 2 deletions x-pack/filebeat/input/httpjson/transform_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,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/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 @@ -424,7 +436,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 5861ebf

Please sign in to comment.