From 5861ebf0e647550ece3fd95e36b01dd69fbeaa6e Mon Sep 17 00:00:00 2001 From: Marc Guasch Date: Thu, 28 Oct 2021 16:11:42 +0200 Subject: [PATCH] [filebeat][httpjson] Set url as a pointer to ensure access to all methods (#28695) * Set url as a pointer to ensure access to all methods * Add changelog line * Fix text (cherry picked from commit 47dba855dc0fbb54325fbe20463dd6232bab4e00) --- CHANGELOG.next.asciidoc | 1 + x-pack/filebeat/input/httpjson/request_test.go | 4 ++-- x-pack/filebeat/input/httpjson/response.go | 12 +++++++----- x-pack/filebeat/input/httpjson/response_test.go | 8 +++++--- x-pack/filebeat/input/httpjson/transform.go | 6 +++--- .../filebeat/input/httpjson/transform_set_test.go | 4 ++-- x-pack/filebeat/input/httpjson/value_tpl_test.go | 14 +++++++++++++- 7 files changed, 33 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index d526b72e188f..47fe65f03790 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -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* diff --git a/x-pack/filebeat/input/httpjson/request_test.go b/x-pack/filebeat/input/httpjson/request_test.go index 34ec55f52742..3c1e01f7f287 100644 --- a/x-pack/filebeat/input/httpjson/request_test.go +++ b/x-pack/filebeat/input/httpjson/request_test.go @@ -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, @@ -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, diff --git a/x-pack/filebeat/input/httpjson/response.go b/x-pack/filebeat/input/httpjson/response.go index 21fd85e20f69..f6acb4e5fee7 100644 --- a/x-pack/filebeat/input/httpjson/response.go +++ b/x-pack/filebeat/input/httpjson/response.go @@ -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, } } diff --git a/x-pack/filebeat/input/httpjson/response_test.go b/x-pack/filebeat/input/httpjson/response_test.go index a855ea87f640..86bdb811cb1e 100644 --- a/x-pack/filebeat/input/httpjson/response_test.go +++ b/x-pack/filebeat/input/httpjson/response_test.go @@ -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"}, }, @@ -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"]) diff --git a/x-pack/filebeat/input/httpjson/transform.go b/x-pack/filebeat/input/httpjson/transform.go index d64e57f4390f..cfc3968bf97f 100644 --- a/x-pack/filebeat/input/httpjson/transform.go +++ b/x-pack/filebeat/input/httpjson/transform.go @@ -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 { @@ -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 { diff --git a/x-pack/filebeat/input/httpjson/transform_set_test.go b/x-pack/filebeat/input/httpjson/transform_set_test.go index f459dbeb82f1..be39db8e0cce 100644 --- a/x-pack/filebeat/input/httpjson/transform_set_test.go +++ b/x-pack/filebeat/input/httpjson/transform_set_test.go @@ -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 } diff --git a/x-pack/filebeat/input/httpjson/value_tpl_test.go b/x-pack/filebeat/input/httpjson/value_tpl_test.go index 7e32ceb0b417..ad6aab449de1 100644 --- a/x-pack/filebeat/input/httpjson/value_tpl_test.go +++ b/x-pack/filebeat/input/httpjson/value_tpl_test.go @@ -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]]", @@ -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 }