Skip to content

Commit

Permalink
Add write access to 'url.value' from 'request.transforms'. (#27937) (#…
Browse files Browse the repository at this point in the history
…27962)

(cherry picked from commit 680fb5b)

Co-authored-by: Marc Guasch <marc-gr@users.noreply.github.com>
  • Loading branch information
mergify[bot] and marc-gr committed Sep 16, 2021
1 parent 77f42ae commit 7c8c06e
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 70 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Expand Up @@ -439,6 +439,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d

- Add `ignore_empty_value` flag to `httpjson` `split` processor. {pull}27880[27880]
- Update Cisco ASA/FTD ingest pipeline grok/dissect patterns for multiple message IDs. {issue}26869[26869] {pull}26879[26879]
- Add write access to `url.value` from `request.transforms` in `httpjson` input. {pull}27937[27937]

*Heartbeat*

Expand Down
2 changes: 1 addition & 1 deletion x-pack/filebeat/docs/inputs/input-httpjson.asciidoc
Expand Up @@ -466,7 +466,7 @@ Available transforms for request: [`append`, `delete`, `set`].

Can read state from: [`.last_response.*`, `.last_event.*`, `.cursor.*`, `.header.*`, `.url.*`, `.body.*`].

Can write state to: [`header.*`, `url.params.*`, `body.*`].
Can write state to: [`body.*`, `header.*`, `url.*`].

["source","yaml",subs="attributes"]
----
Expand Down
2 changes: 1 addition & 1 deletion x-pack/filebeat/input/httpjson/internal/v2/pagination.go
Expand Up @@ -19,7 +19,7 @@ const paginationNamespace = "pagination"
func registerPaginationTransforms() {
registerTransform(paginationNamespace, appendName, newAppendPagination)
registerTransform(paginationNamespace, deleteName, newDeletePagination)
registerTransform(paginationNamespace, setName, newSetPagination)
registerTransform(paginationNamespace, setName, newSetRequestPagination)
}

type pagination struct {
Expand Down
2 changes: 1 addition & 1 deletion x-pack/filebeat/input/httpjson/internal/v2/request.go
Expand Up @@ -22,7 +22,7 @@ const requestNamespace = "request"
func registerRequestTransforms() {
registerTransform(requestNamespace, appendName, newAppendRequest)
registerTransform(requestNamespace, deleteName, newDeleteRequest)
registerTransform(requestNamespace, setName, newSetRequest)
registerTransform(requestNamespace, setName, newSetRequestPagination)
}

type httpClient struct {
Expand Down
26 changes: 3 additions & 23 deletions x-pack/filebeat/input/httpjson/internal/v2/transform_set.go
Expand Up @@ -39,7 +39,7 @@ type set struct {

func (set) transformName() string { return setName }

func newSetRequest(cfg *common.Config, log *logp.Logger) (transform, error) {
func newSetRequestPagination(cfg *common.Config, log *logp.Logger) (transform, error) {
set, err := newSet(cfg, log)
if err != nil {
return nil, err
Expand All @@ -52,6 +52,8 @@ func newSetRequest(cfg *common.Config, log *logp.Logger) (transform, error) {
set.runFunc = setHeader
case targetURLParams:
set.runFunc = setURLParams
case targetURLValue:
set.runFunc = setURLValue
default:
return nil, fmt.Errorf("invalid target type: %s", set.targetInfo.Type)
}
Expand All @@ -75,28 +77,6 @@ func newSetResponse(cfg *common.Config, log *logp.Logger) (transform, error) {
return &set, nil
}

func newSetPagination(cfg *common.Config, log *logp.Logger) (transform, error) {
set, err := newSet(cfg, log)
if err != nil {
return nil, err
}

switch set.targetInfo.Type {
case targetBody:
set.runFunc = setBody
case targetHeader:
set.runFunc = setHeader
case targetURLParams:
set.runFunc = setURLParams
case targetURLValue:
set.runFunc = setURLValue
default:
return nil, fmt.Errorf("invalid target type: %s", set.targetInfo.Type)
}

return &set, nil
}

func newSet(cfg *common.Config, log *logp.Logger) (set, error) {
c := &setConfig{}
if err := cfg.Unpack(c); err != nil {
Expand Down
52 changes: 10 additions & 42 deletions x-pack/filebeat/input/httpjson/internal/v2/transform_set_test.go
Expand Up @@ -41,72 +41,40 @@ func TestNewSet(t *testing.T) {
expectedErr: "invalid target: cursor.foo",
},
{
name: "newSetRequest targets body",
constructor: newSetRequest,
name: "newSetRequestPagination targets body",
constructor: newSetRequestPagination,
config: map[string]interface{}{
"target": "body.foo",
},
expectedTarget: targetInfo{Name: "foo", Type: "body"},
},
{
name: "newSetRequest targets header",
constructor: newSetRequest,
name: "newSetRequestPagination targets header",
constructor: newSetRequestPagination,
config: map[string]interface{}{
"target": "header.foo",
},
expectedTarget: targetInfo{Name: "foo", Type: "header"},
},
{
name: "newSetRequest targets url param",
constructor: newSetRequest,
name: "newSetRequestPagination targets url param",
constructor: newSetRequestPagination,
config: map[string]interface{}{
"target": "url.params.foo",
},
expectedTarget: targetInfo{Name: "foo", Type: "url.params"},
},
{
name: "newSetRequest targets something else",
constructor: newSetRequest,
config: map[string]interface{}{
"target": "cursor.foo",
},
expectedErr: "invalid target: cursor.foo",
},
{
name: "newSetPagination targets body",
constructor: newSetPagination,
config: map[string]interface{}{
"target": "body.foo",
},
expectedTarget: targetInfo{Name: "foo", Type: "body"},
},
{
name: "newSetPagination targets header",
constructor: newSetPagination,
config: map[string]interface{}{
"target": "header.foo",
},
expectedTarget: targetInfo{Name: "foo", Type: "header"},
},
{
name: "newSetPagination targets url param",
constructor: newSetPagination,
config: map[string]interface{}{
"target": "url.params.foo",
},
expectedTarget: targetInfo{Name: "foo", Type: "url.params"},
},
{
name: "newSetPagination targets url value",
constructor: newSetPagination,
name: "newSetRequestPagination targets url value",
constructor: newSetRequestPagination,
config: map[string]interface{}{
"target": "url.value",
},
expectedTarget: targetInfo{Type: "url.value"},
},
{
name: "newSetPagination targets something else",
constructor: newSetPagination,
name: "newSetRequestPagination targets something else",
constructor: newSetRequestPagination,
config: map[string]interface{}{
"target": "cursor.foo",
},
Expand Down
4 changes: 2 additions & 2 deletions x-pack/filebeat/input/httpjson/internal/v2/transform_test.go
Expand Up @@ -46,7 +46,7 @@ func TestTransformableClone(t *testing.T) {
}

func TestNewTransformsFromConfig(t *testing.T) {
registerTransform("test", setName, newSetRequest)
registerTransform("test", setName, newSetRequestPagination)
t.Cleanup(func() { registeredTransforms = newRegistry() })

cases := []struct {
Expand Down Expand Up @@ -126,7 +126,7 @@ func TestNewBasicTransformsFromConfig(t *testing.T) {
return fakeTransform{}, nil
}

registerTransform("test", setName, newSetRequest)
registerTransform("test", setName, newSetRequestPagination)
registerTransform("test", "fake", fakeConstr)
t.Cleanup(func() { registeredTransforms = newRegistry() })

Expand Down

0 comments on commit 7c8c06e

Please sign in to comment.