Skip to content

Commit

Permalink
x-pack/filebeat/input/cel: add support for digest authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
efd6 committed Oct 23, 2023
1 parent 11f4dee commit ffb9729
Show file tree
Hide file tree
Showing 9 changed files with 330 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,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*

Expand Down
31 changes: 31 additions & 0 deletions NOTICE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]/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
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
36 changes: 35 additions & 1 deletion x-pack/filebeat/docs/inputs/input-cel.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ This input supports:

* Auth
** Basic
** Digest
** OAuth2
* Retrieval at a configurable interval
* Pagination
Expand Down Expand Up @@ -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:

Expand All @@ -253,6 +254,16 @@ filebeat.inputs:
resource.url: http://localhost
----

["source","yaml",subs="attributes"]
----
filebeat.inputs:
- type: cel
auth.digest:
user: [email protected]
password: P@$$W0₹D
resource.url: http://localhost
----

["source","yaml",subs="attributes"]
----
filebeat.inputs:
Expand Down Expand Up @@ -398,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`

Expand Down
46 changes: 41 additions & 5 deletions x-pack/filebeat/input/cel/config_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -41,7 +52,7 @@ type basicAuthConfig struct {
Password string `config:"password"`
}

// 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 (b *basicAuthConfig) isEnabled() bool {
return b != nil && (b.Enabled == nil || *b.Enabled)
}
Expand All @@ -59,6 +70,31 @@ func (b *basicAuthConfig) Validate() error {
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 (d *digestAuthConfig) isEnabled() bool {
return d != nil && (d.Enabled == nil || *d.Enabled)
}

// Validate checks if oauth2 config is valid.
func (d *digestAuthConfig) Validate() error {
if !d.isEnabled() {
return nil
}

if d.User == "" || d.Password == "" {
return errors.New("both user and password must be set")
}

return nil
}

// An oAuth2Provider represents a supported oauth provider.
type oAuth2Provider string

Expand Down Expand Up @@ -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)
}
Expand Down
72 changes: 58 additions & 14 deletions x-pack/filebeat/input/cel/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (b *basicAuthConfig) take(on *bool) { b.Enabled = on }
func (d *digestAuthConfig) take(on *bool) { d.Enabled = on }
func (o *oAuth2Config) take(on *bool) { o.Enabled = on }

func TestOAuth2GetTokenURL(t *testing.T) {
const host = "http://localhost"
for _, test := range []struct {
Expand Down Expand Up @@ -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{}{
Expand Down
14 changes: 14 additions & 0 deletions x-pack/filebeat/input/cel/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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() {
Expand Down
Loading

0 comments on commit ffb9729

Please sign in to comment.