From 455a1c2e079cb8114c21e8a2518e61e2f01aa5ef Mon Sep 17 00:00:00 2001 From: Antoine Toulme Date: Wed, 10 Jan 2024 23:35:26 -0800 Subject: [PATCH] [extension/basicauth] Remove extraneous validation logic --- .chloggen/basicauth_validation.yaml | 29 +++++++++++++++++++ extension/basicauthextension/extension.go | 8 ++--- .../basicauthextension/extension_test.go | 16 ++-------- extension/basicauthextension/factory.go | 2 +- extension/basicauthextension/factory_test.go | 8 ----- 5 files changed, 35 insertions(+), 28 deletions(-) create mode 100755 .chloggen/basicauth_validation.yaml diff --git a/.chloggen/basicauth_validation.yaml b/.chloggen/basicauth_validation.yaml new file mode 100755 index 000000000000..92ad70605252 --- /dev/null +++ b/.chloggen/basicauth_validation.yaml @@ -0,0 +1,29 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: bug_fix + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: basicauthextension + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Accept empty usernames. + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [30470] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: | + Per https://datatracker.ietf.org/doc/html/rfc2617#section-2, username and password may be empty strings (""). + The validation used to enforce that usernames cannot be empty. + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [] diff --git a/extension/basicauthextension/extension.go b/extension/basicauthextension/extension.go index b7f49b018568..08114aae258b 100644 --- a/extension/basicauthextension/extension.go +++ b/extension/basicauthextension/extension.go @@ -33,18 +33,14 @@ type basicAuth struct { matchFunc func(username, password string) bool } -func newClientAuthExtension(cfg *Config) (auth.Client, error) { - if cfg.ClientAuth == nil || cfg.ClientAuth.Username == "" { - return nil, errNoCredentialSource - } - +func newClientAuthExtension(cfg *Config) auth.Client { ba := basicAuth{ clientAuth: cfg.ClientAuth, } return auth.NewClient( auth.WithClientRoundTripper(ba.roundTripper), auth.WithClientPerRPCCredentials(ba.perRPCCredentials), - ), nil + ) } func newServerAuthExtension(cfg *Config) (auth.Server, error) { diff --git a/extension/basicauthextension/extension_test.go b/extension/basicauthextension/extension_test.go index a255c120d370..265689d0b6c6 100644 --- a/extension/basicauthextension/extension_test.go +++ b/extension/basicauthextension/extension_test.go @@ -228,14 +228,13 @@ func (m *mockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) } func TestBasicAuth_ClientValid(t *testing.T) { - ext, err := newClientAuthExtension(&Config{ + ext := newClientAuthExtension(&Config{ ClientAuth: &ClientAuthSettings{ Username: "username", Password: "password", }, }) require.NotNil(t, ext) - require.NoError(t, err) require.NoError(t, ext.Start(context.Background(), componenttest.NewNopHost())) @@ -274,29 +273,20 @@ func TestBasicAuth_ClientValid(t *testing.T) { } func TestBasicAuth_ClientInvalid(t *testing.T) { - t.Run("no username", func(t *testing.T) { - _, err := newClientAuthExtension(&Config{ - ClientAuth: &ClientAuthSettings{ - Username: "", - }, - }) - assert.Error(t, err) - }) t.Run("invalid username format", func(t *testing.T) { - ext, err := newClientAuthExtension(&Config{ + ext := newClientAuthExtension(&Config{ ClientAuth: &ClientAuthSettings{ Username: "user:name", Password: "password", }, }) require.NotNil(t, ext) - require.NoError(t, err) require.NoError(t, ext.Start(context.Background(), componenttest.NewNopHost())) base := &mockRoundTripper{} - _, err = ext.RoundTripper(base) + _, err := ext.RoundTripper(base) assert.Error(t, err) _, err = ext.PerRPCCredentials() diff --git a/extension/basicauthextension/factory.go b/extension/basicauthextension/factory.go index 4a7442e7ec30..b58e6b1dda5f 100644 --- a/extension/basicauthextension/factory.go +++ b/extension/basicauthextension/factory.go @@ -31,5 +31,5 @@ func createExtension(_ context.Context, _ extension.CreateSettings, cfg componen if cfg.(*Config).Htpasswd != nil { return newServerAuthExtension(cfg.(*Config)) } - return newClientAuthExtension(cfg.(*Config)) + return newClientAuthExtension(cfg.(*Config)), nil } diff --git a/extension/basicauthextension/factory_test.go b/extension/basicauthextension/factory_test.go index 39c6bc824744..ec59b7c91556 100644 --- a/extension/basicauthextension/factory_test.go +++ b/extension/basicauthextension/factory_test.go @@ -22,14 +22,6 @@ func TestCreateDefaultConfig(t *testing.T) { assert.NoError(t, componenttest.CheckConfigStruct(actual)) } -func TestCreateExtension_DefaultConfig(t *testing.T) { - cfg := createDefaultConfig() - - ext, err := createExtension(context.Background(), extensiontest.NewNopCreateSettings(), cfg) - assert.Equal(t, err, errNoCredentialSource) - assert.Nil(t, ext) -} - func TestCreateExtension_ValidConfig(t *testing.T) { cfg := &Config{ Htpasswd: &HtpasswdSettings{