Skip to content

Commit

Permalink
Cherry-pick elastic#16354 to 7.7: Update filebeat httpjson input to s…
Browse files Browse the repository at this point in the history
…upport pagination via Header and Okta module (elastic#17670)

* Update filebeat httpjson input to support pagination via Header and Okta module (elastic#16354)

* Update filebeat httpjson input to support pagination via Header and Okta module

(cherry picked from commit 994e1f1)

* Update CHANGELOG.next.asciidoc
  • Loading branch information
Lei Qiu authored Apr 10, 2020
1 parent 9d73586 commit 1bdabda
Show file tree
Hide file tree
Showing 5 changed files with 464 additions and 74 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Added documentation for running Filebeat in Cloud Foundry. {pull}17275[17275]
- Added access_key_id, secret_access_key and session_token into aws module config. {pull}17456[17456]
- Release Google Cloud module as GA. {pull}17511[17511]
- Update filebeat httpjson input to support pagination via Header and Okta module. {pull}16354[16354]

*Heartbeat*

Expand Down
33 changes: 32 additions & 1 deletion x-pack/filebeat/docs/inputs/input-httpjson.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,32 @@ Time duration between repeated data retrievals. Default: 0s, meaning no repeated
If the HTTP API returns data in a JSON array, then this option can be set to decode these records
from the array. Default: not used.

[float]
==== `no_http_body`

If set, do not use HTTP request body. Default: false.

[float]
==== `pagination.enabled`

This option specifies whether pagination is enabled. Default: false.
This option specifies whether pagination is enabled. Default: true.

[float]
==== `pagination.extra_body_content`

Any additional data that needs to be set in the HTTP pagination request can be specified in
this JSON blob. Default: not used.

[float]
==== `pagination.header.field_name`

The field name in the HTTP Header that is used for pagination control.

[float]
==== `pagination.header.regex_pattern`

The regular expression pattern to use for retrieving the pagination information from the HTTP Header field specified above.

[float]
==== `pagination.id_field`

Expand All @@ -120,6 +135,22 @@ Required when pagination is enabled.
This specifies the URL for sending pagination request. Required if the pagination URL is different
than the HTTP API URL.

[float]
==== `rate_limit.limit`

This specifies the field in the HTTP Header of the response that specifies the total limit.

[float]
==== `rate_limit.remaining`

This specifies the field in the HTTP Header of the response that specifies the remaining quota of the rate limit.

[float]
==== `rate_limit.reset`

This specifies the field in the HTTP Header of the response that specifies the epoch time
when the rate limit will be reset.

[float]
==== `ssl`

Expand Down
62 changes: 50 additions & 12 deletions x-pack/filebeat/input/httpjson/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package httpjson

import (
"regexp"
"strings"
"time"

Expand All @@ -16,35 +17,72 @@ import (

// Config contains information about httpjson configuration
type config struct {
APIKey string `config:"api_key"`
HTTPClientTimeout time.Duration `config:"http_client_timeout"`
HTTPHeaders common.MapStr `config:"http_headers"`
HTTPMethod string `config:"http_method" validate:"required"`
HTTPRequestBody common.MapStr `config:"http_request_body"`
Interval time.Duration `config:"interval"`
JSONObjects string `config:"json_objects_array"`
Pagination *Pagination `config:"pagination"`
TLS *tlscommon.Config `config:"ssl"`
URL string `config:"url" validate:"required"`
APIKey string `config:"api_key"`
AuthenticationScheme string `config:"authentication_scheme"`
HTTPClientTimeout time.Duration `config:"http_client_timeout"`
HTTPHeaders common.MapStr `config:"http_headers"`
HTTPMethod string `config:"http_method" validate:"required"`
HTTPRequestBody common.MapStr `config:"http_request_body"`
Interval time.Duration `config:"interval"`
JSONObjects string `config:"json_objects_array"`
NoHTTPBody bool `config:"no_http_body"`
Pagination *Pagination `config:"pagination"`
RateLimit *RateLimit `config:"rate_limit"`
TLS *tlscommon.Config `config:"ssl"`
URL string `config:"url" validate:"required"`
}

// Pagination contains information about httpjson pagination settings
type Pagination struct {
IsEnabled bool `config:"enabled"`
Enabled *bool `config:"enabled"`
ExtraBodyContent common.MapStr `config:"extra_body_content"`
Header *Header `config:"header"`
IDField string `config:"id_field"`
RequestField string `config:"req_field"`
URL string `config:"url"`
}

// IsEnabled returns true if the `enable` field is set to true in the yaml.
func (p *Pagination) IsEnabled() bool {
return p != nil && (p.Enabled == nil || *p.Enabled)
}

// HTTP Header information for pagination
type Header struct {
FieldName string `config:"field_name" validate:"required"`
RegexPattern *regexp.Regexp `config:"regex_pattern" validate:"required"`
}

// HTTP Header Rate Limit information
type RateLimit struct {
Limit string `config:"limit"`
Reset string `config:"reset"`
Remaining string `config:"remaining"`
}

func (c *config) Validate() error {
switch strings.ToUpper(c.HTTPMethod) {
case "GET":
break
case "POST":
break
default:
return errors.Errorf("httpjson input: Invalid http_method, %s - ", c.HTTPMethod)
return errors.Errorf("httpjson input: Invalid http_method, %s", c.HTTPMethod)
}
if c.NoHTTPBody {
if len(c.HTTPRequestBody) > 0 {
return errors.Errorf("invalid configuration: both no_http_body and http_request_body cannot be set simultaneously")
}
if c.Pagination != nil && (len(c.Pagination.ExtraBodyContent) > 0 || c.Pagination.RequestField != "") {
return errors.Errorf("invalid configuration: both no_http_body and pagination.extra_body_content or pagination.req_field cannot be set simultaneously")
}
}
if c.Pagination != nil {
if c.Pagination.Header != nil {
if c.Pagination.RequestField != "" || c.Pagination.IDField != "" || len(c.Pagination.ExtraBodyContent) > 0 {
return errors.Errorf("invalid configuration: both pagination.header and pagination.req_field or pagination.id_field or pagination.extra_body_content cannot be set simultaneously")
}
}
}
return nil
}
Expand Down
Loading

0 comments on commit 1bdabda

Please sign in to comment.