diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index 37bb60d04e3f..220efad3cd14 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -240,6 +240,7 @@ is collected by it. - Add cache processor. {pull}36786[36786] - Avoid unwanted publication of Azure entity records. {pull}36753[36753] - Avoid unwanted publication of Okta entity records. {pull}36770[36770] +- Add support for Digest Authentication to CEL input. {issue}35514[35514] {pull}36932[36932] *Auditbeat* diff --git a/NOTICE.txt b/NOTICE.txt index 6ae9f58daba1..9b05ce7f9c02 100644 --- a/NOTICE.txt +++ b/NOTICE.txt @@ -20004,6 +20004,37 @@ The above copyright notice and this permission notice shall be included in all c THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +-------------------------------------------------------------------------------- +Dependency : github.com/icholy/digest +Version: v0.1.22 +Licence type (autodetected): MIT +-------------------------------------------------------------------------------- + +Contents of probable licence file $GOMODCACHE/github.com/icholy/digest@v0.1.22/LICENSE: + +MIT License + +Copyright (c) 2020 Ilia Choly + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + + -------------------------------------------------------------------------------- Dependency : github.com/elastic/dhcp Version: v0.0.0-20200227161230-57ec251c7eb3 diff --git a/go.mod b/go.mod index e4955abcc912..67d334db40b7 100644 --- a/go.mod +++ b/go.mod @@ -213,6 +213,7 @@ require ( github.com/googleapis/gax-go/v2 v2.7.0 github.com/gorilla/handlers v1.5.1 github.com/gorilla/mux v1.8.0 + github.com/icholy/digest v0.1.22 github.com/lestrrat-go/jwx/v2 v2.0.11 github.com/otiai10/copy v1.12.0 github.com/pierrec/lz4/v4 v4.1.16 diff --git a/go.sum b/go.sum index a1aecdb59dfd..ede6e20350bf 100644 --- a/go.sum +++ b/go.sum @@ -1196,6 +1196,8 @@ github.com/huandu/xstrings v1.0.0/go.mod h1:4qWG/gcEcfX4z/mBDHJ++3ReCw9ibxbsNJbc github.com/hudl/fargo v1.3.0/go.mod h1:y3CKSmjA+wD2gak7sUSXTAoopbhU08POFhmITJgmKTg= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= +github.com/icholy/digest v0.1.22 h1:dRIwCjtAcXch57ei+F0HSb5hmprL873+q7PoVojdMzM= +github.com/icholy/digest v0.1.22/go.mod h1:uLAeDdWKIWNFMH0wqbwchbTQOmJWhzSnL7zmqSPqEEc= github.com/imdario/mergo v0.3.4/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA= github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA= github.com/imdario/mergo v0.3.6/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA= diff --git a/x-pack/filebeat/docs/inputs/input-cel.asciidoc b/x-pack/filebeat/docs/inputs/input-cel.asciidoc index 35fbc4654f22..11a6248cbbb4 100644 --- a/x-pack/filebeat/docs/inputs/input-cel.asciidoc +++ b/x-pack/filebeat/docs/inputs/input-cel.asciidoc @@ -22,6 +22,7 @@ This input supports: * Auth ** Basic +** Digest ** OAuth2 * Retrieval at a configurable interval * Pagination @@ -239,7 +240,7 @@ As noted above the `cel` input provides functions, macros, and global variables In addition to the extensions provided in the packages listed above, a global variable `useragent` is also provided which gives the user CEL program access to the {beatname_lc} user-agent string. -Additionally, it supports authentication via Basic auth, HTTP Headers or oauth2. +Additionally, it supports authentication via Basic Authentication, Digest Authentication or OAuth2. Example configurations with authentication: @@ -248,10 +249,19 @@ Example configurations with authentication: filebeat.inputs: - type: cel resource.url: http://localhost - request.transforms: - - set: - target: header.Authorization - value: 'Basic aGVsbG86d29ybGQ=' + auth.basic: + user: user@domain.tld + password: P@$$W0₹D +---- + +["source","yaml",subs="attributes"] +---- +filebeat.inputs: +- type: cel + resource.url: http://localhost + auth.digest: + user: user@domain.tld + password: P@$$W0₹D ---- ["source","yaml",subs="attributes"] @@ -399,6 +409,29 @@ The user to authenticate with. The password to use. +[float] +==== `auth.digest.enabled` + +When set to `false`, disables the digest auth configuration. Default: `true`. + +NOTE: digest auth settings are disabled if either `enabled` is set to `false` or +the `auth.digest` section is missing. + +[float] +==== `auth.digest.user` + +The user to authenticate with. + +[float] +==== `auth.digest.password` + +The password to use. + +[float] +==== `auth.digest.no_reuse` + +When set to `true`, Digest Authentication challenges are not reused. + [float] ==== `auth.oauth2.enabled` diff --git a/x-pack/filebeat/input/cel/config_auth.go b/x-pack/filebeat/input/cel/config_auth.go index 5c80507fdadd..0b39c6cfdb4e 100644 --- a/x-pack/filebeat/input/cel/config_auth.go +++ b/x-pack/filebeat/input/cel/config_auth.go @@ -24,12 +24,23 @@ import ( ) type authConfig struct { - Basic *basicAuthConfig `config:"basic"` - OAuth2 *oAuth2Config `config:"oauth2"` + Basic *basicAuthConfig `config:"basic"` + Digest *digestAuthConfig `config:"digest"` + OAuth2 *oAuth2Config `config:"oauth2"` } func (c authConfig) Validate() error { - if c.Basic.isEnabled() && c.OAuth2.isEnabled() { + var n int + if c.Basic.isEnabled() { + n++ + } + if c.Digest.isEnabled() { + n++ + } + if c.OAuth2.isEnabled() { + n++ + } + if n > 1 { return errors.New("only one kind of auth can be enabled") } return nil @@ -41,18 +52,43 @@ type basicAuthConfig struct { Password string `config:"password"` } -// IsEnabled returns true if the `enable` field is set to true in the yaml. -func (b *basicAuthConfig) isEnabled() bool { - return b != nil && (b.Enabled == nil || *b.Enabled) +// isEnabled returns true if the `enable` field is set to true in the yaml. +func (c *basicAuthConfig) isEnabled() bool { + return c != nil && (c.Enabled == nil || *c.Enabled) +} + +// Validate checks if oauth2 config is valid. +func (c *basicAuthConfig) Validate() error { + if !c.isEnabled() { + return nil + } + + if c.User == "" || c.Password == "" { + return errors.New("both user and password must be set") + } + + return nil +} + +type digestAuthConfig struct { + Enabled *bool `config:"enabled"` + User string `config:"user"` + Password string `config:"password"` + NoReuse *bool `config:"no_reuse"` +} + +// isEnabled returns true if the `enable` field is set to true in the yaml. +func (c *digestAuthConfig) isEnabled() bool { + return c != nil && (c.Enabled == nil || *c.Enabled) } // Validate checks if oauth2 config is valid. -func (b *basicAuthConfig) Validate() error { - if !b.isEnabled() { +func (c *digestAuthConfig) Validate() error { + if !c.isEnabled() { return nil } - if b.User == "" || b.Password == "" { + if c.User == "" || c.Password == "" { return errors.New("both user and password must be set") } @@ -107,7 +143,7 @@ type oAuth2Config struct { OktaJWKJSON common.JSONBlob `config:"okta.jwk_json"` } -// IsEnabled returns true if the `enable` field is set to true in the yaml. +// isEnabled returns true if the `enable` field is set to true in the yaml. func (o *oAuth2Config) isEnabled() bool { return o != nil && (o.Enabled == nil || *o.Enabled) } diff --git a/x-pack/filebeat/input/cel/config_test.go b/x-pack/filebeat/input/cel/config_test.go index e19c08399cbf..988acbefaf84 100644 --- a/x-pack/filebeat/input/cel/config_test.go +++ b/x-pack/filebeat/input/cel/config_test.go @@ -39,26 +39,45 @@ func TestGetProviderIsCanonical(t *testing.T) { } func TestIsEnabled(t *testing.T) { - oauth2 := oAuth2Config{} - if !oauth2.isEnabled() { - t.Errorf("OAuth2 not enabled by default") + type enabler interface { + isEnabled() bool + take(*bool) } + for _, test := range []struct { + name string + auth enabler + }{ + {name: "basic", auth: &basicAuthConfig{}}, + {name: "digest", auth: &digestAuthConfig{}}, + {name: "OAuth2", auth: &oAuth2Config{}}, + } { + t.Run(test.name, func(t *testing.T) { + if !test.auth.isEnabled() { + t.Errorf("auth not enabled by default") + } - var enabled bool - for i := 0; i < 4; i++ { - oauth2.Enabled = &enabled - if got := oauth2.isEnabled(); got != enabled { - t.Errorf("unexpected OAuth2 enabled state on iteration %d: got:%t want:%t", i, got, enabled) - } - enabled = !enabled - } + var enabled bool + for i := 0; i < 4; i++ { + test.auth.take(&enabled) + if got := test.auth.isEnabled(); got != enabled { + t.Errorf("unexpected auth enabled state on iteration %d: got:%t want:%t", i, got, enabled) + } + enabled = !enabled + } - oauth2.Enabled = nil - if !oauth2.isEnabled() { - t.Errorf("OAuth2 not enabled if nilled") + test.auth.take(nil) + if !test.auth.isEnabled() { + t.Errorf("auth not enabled if nilled") + } + }) } } +// take methods are for testing only. +func (c *basicAuthConfig) take(on *bool) { c.Enabled = on } +func (c *digestAuthConfig) take(on *bool) { c.Enabled = on } +func (c *oAuth2Config) take(on *bool) { c.Enabled = on } + func TestOAuth2GetTokenURL(t *testing.T) { const host = "http://localhost" for _, test := range []struct { @@ -143,6 +162,31 @@ var oAuth2ValidationTests = []struct { }, }, }, + { + name: "can't_set_oauth2_and_digest_auth_together", + wantErr: errors.New("only one kind of auth can be enabled accessing 'auth'"), + input: map[string]interface{}{ + "auth.digest.user": "user", + "auth.digest.password": "pass", + "auth.oauth2": map[string]interface{}{ + "token_url": "localhost", + "client": map[string]interface{}{ + "id": "a_client_id", + "secret": "a_client_secret", + }, + }, + }, + }, + { + name: "can't_set_basic_and_digest_auth_together", + wantErr: errors.New("only one kind of auth can be enabled accessing 'auth'"), + input: map[string]interface{}{ + "auth.basic.user": "user", + "auth.basic.password": "pass", + "auth.digest.user": "user", + "auth.digest.password": "pass", + }, + }, { name: "can_set_oauth2_and_basic_auth_together_if_oauth2_is_disabled", input: map[string]interface{}{ diff --git a/x-pack/filebeat/input/cel/input.go b/x-pack/filebeat/input/cel/input.go index ea4958b9c30e..499ae97f7fac 100644 --- a/x-pack/filebeat/input/cel/input.go +++ b/x-pack/filebeat/input/cel/input.go @@ -24,6 +24,7 @@ import ( "time" retryablehttp "github.com/hashicorp/go-retryablehttp" + "github.com/icholy/digest" "github.com/rcrowley/go-metrics" "go.elastic.co/ecszap" "go.uber.org/zap" @@ -688,6 +689,19 @@ func newClient(ctx context.Context, cfg config, log *logp.Logger) (*http.Client, return nil, err } + if cfg.Auth.Digest.isEnabled() { + var noReuse bool + if cfg.Auth.Digest.NoReuse != nil { + noReuse = *cfg.Auth.Digest.NoReuse + } + c.Transport = &digest.Transport{ + Transport: c.Transport, + Username: cfg.Auth.Digest.User, + Password: cfg.Auth.Digest.Password, + NoReuse: noReuse, + } + } + if cfg.Resource.Tracer != nil { w := zapcore.AddSync(cfg.Resource.Tracer) go func() { diff --git a/x-pack/filebeat/input/cel/input_test.go b/x-pack/filebeat/input/cel/input_test.go index bc0cf1714713..3eb7d77efb88 100644 --- a/x-pack/filebeat/input/cel/input_test.go +++ b/x-pack/filebeat/input/cel/input_test.go @@ -7,6 +7,7 @@ package cel import ( "context" + "flag" "fmt" "io" "math/rand" @@ -31,8 +32,11 @@ import ( "github.com/elastic/elastic-agent-libs/mapstr" ) +var runRemote = flag.Bool("run_remote", false, "run tests using remote endpoints") + var inputTests = []struct { name string + remote bool server func(*testing.T, http.HandlerFunc, map[string]interface{}) handler http.HandlerFunc config map[string]interface{} @@ -1055,6 +1059,29 @@ var inputTests = []struct { }, // Authenticated access tests. + { + // Test case modelled on `curl --digest -u test_user:secret_password https://httpbin.org/digest-auth/auth/test_user/secret_password/md5`. + name: "digest_remote", + remote: true, + server: func(_ *testing.T, _ http.HandlerFunc, _ map[string]interface{}) {}, + config: map[string]interface{}{ + "resource.url": "https://httpbin.org/digest-auth/auth/test_user/secret_password/md5", + "interval": 1, + "auth.digest.user": "test_user", + "auth.digest.password": "secret_password", + "program": ` + bytes(get(state.url).Body).as(body, { + "events": [body.decode_json()] + }) + `, + }, + want: []map[string]interface{}{ + { + "authenticated": true, + "user": "test_user", + }, + }, + }, { name: "OAuth2", server: func(t *testing.T, h http.HandlerFunc, config map[string]interface{}) { @@ -1248,6 +1275,9 @@ func TestInput(t *testing.T) { if reason, skip := skipOnWindows[test.name]; runtime.GOOS == "windows" && skip { t.Skip(reason) } + if test.remote && !*runRemote { + t.Skip("skipping remote endpoint test") + } if test.server != nil { test.server(t, test.handler, test.config)