-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[extension/basicauth] Implement
configauth.ClientAuthenticator
(#8847)
* Add support for client basic auth * Change Readme * Add changelog entry * separate out client and server authenticator * Fix Readme documentation Co-authored-by: Stepan Rakitin <[email protected]> * Update Readme.md * modify internal components test file * Apply suggestions from code review Co-authored-by: Juraci Paixão Kröhling <[email protected]> * add rpc support * review comments and minor fixes * address comments and make func signatures private * gofmt lint error fix Co-authored-by: Stepan Rakitin <[email protected]> Co-authored-by: Juraci Paixão Kröhling <[email protected]>
- Loading branch information
1 parent
e627875
commit e841b5e
Showing
12 changed files
with
427 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,60 @@ | ||
# Basic Authenticator | ||
|
||
This extension implements `configauth.ServerAuthenticator` to authenticate clients using HTTP Basic Authentication. The authenticator type has to be set to `basicauth`. | ||
This extension implements both `configauth.ServerAuthenticator` and `configauth.ClientAuthenticator` to authenticate clients and servers using Basic Authentication. The authenticator type has to be set to `basicauth`. | ||
|
||
If authentication is successful `client.Info.Auth` will expose the following attributes: | ||
When used as ServerAuthenticator, if the authentication is successful `client.Info.Auth` will expose the following attributes: | ||
|
||
- `username`: The username of the authenticated user. | ||
- `raw`: Raw base64 encoded credentials. | ||
|
||
The configuration should specify only one instance of `basicauth` extension for either client or server authentication. | ||
|
||
The following are the configuration options: | ||
|
||
- `htpasswd.file`: The path to the htpasswd file. | ||
- `htpasswd.inline`: The htpasswd file inline content. | ||
- `client_auth.username`: Username to use for client authentication. | ||
- `client_auth.password`: Password to use for client authentication. | ||
|
||
To configure the extension as a server authenticator, either one of `htpasswd.file` or `htpasswd.inline` has to be set. If both are configured, `htpasswd.inline` credentials take precedence. | ||
|
||
To configure the extension as a client authenticator, `client_auth` has to be set. | ||
|
||
If both the options are configured, the extension will throw an error. | ||
## Configuration | ||
|
||
```yaml | ||
extensions: | ||
basicauth: | ||
basicauth/server: | ||
htpasswd: | ||
file: .htpasswd | ||
inline: | | ||
${BASIC_AUTH_USERNAME}:${BASIC_AUTH_PASSWORD} | ||
basicauth/client: | ||
client_auth: | ||
username: username | ||
password: password | ||
|
||
receivers: | ||
otlp: | ||
protocols: | ||
http: | ||
auth: | ||
authenticator: basicauth | ||
authenticator: basicauth/server | ||
|
||
processors: | ||
|
||
exporters: | ||
logging: | ||
logLevel: debug | ||
otlp: | ||
auth: | ||
authenticator: basicauth/client | ||
|
||
service: | ||
extensions: [basicauth] | ||
extensions: [basicauth/server, basicauth/client] | ||
pipelines: | ||
traces: | ||
receivers: [otlp] | ||
processors: [] | ||
exporters: [logging] | ||
exporters: [otlp] | ||
``` | ||
### htpasswd | ||
- `file`: The path to the htpasswd file. | ||
- `inline`: The htpasswd file inline content. | ||
|
||
If both `file` and `inline` are configured, `inline` credentials take precedence. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package basicauthextension | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/collector/component/componenttest" | ||
"go.opentelemetry.io/collector/config" | ||
"go.opentelemetry.io/collector/service/servicetest" | ||
) | ||
|
||
func TestLoadConfig(t *testing.T) { | ||
factories, err := componenttest.NopFactories() | ||
require.NoError(t, err) | ||
|
||
factory := NewFactory() | ||
factories.Extensions[typeStr] = factory | ||
cfg, err := servicetest.LoadConfigAndValidate(filepath.Join("testdata", "valid_config.yml"), factories) | ||
|
||
require.NoError(t, err) | ||
require.NotNil(t, cfg) | ||
|
||
ext0 := cfg.Extensions[config.NewComponentIDWithName(typeStr, "server")] | ||
assert.Equal(t, &Config{ | ||
ExtensionSettings: config.NewExtensionSettings(config.NewComponentIDWithName(typeStr, "server")), | ||
Htpasswd: &HtpasswdSettings{ | ||
Inline: "username1:password1\nusername2:password2\n", | ||
}, | ||
}, ext0) | ||
|
||
ext1 := cfg.Extensions[config.NewComponentIDWithName(typeStr, "client")] | ||
assert.Equal(t, | ||
&Config{ | ||
ExtensionSettings: config.NewExtensionSettings(config.NewComponentIDWithName(typeStr, "client")), | ||
ClientAuth: &ClientAuthSettings{ | ||
Username: "username", | ||
Password: "password", | ||
}, | ||
}, | ||
ext1) | ||
|
||
assert.Equal(t, 2, len(cfg.Service.Extensions)) | ||
assert.Equal(t, config.NewComponentIDWithName(typeStr, "client"), cfg.Service.Extensions[0]) | ||
assert.Equal(t, config.NewComponentIDWithName(typeStr, "server"), cfg.Service.Extensions[1]) | ||
} | ||
|
||
func TestLoadConfigError(t *testing.T) { | ||
factories, err := componenttest.NopFactories() | ||
require.NoError(t, err) | ||
|
||
factory := NewFactory() | ||
factories.Extensions[typeStr] = factory | ||
t.Run("invalid config both present", func(t *testing.T) { | ||
_, err = servicetest.LoadConfigAndValidate(filepath.Join("testdata", "invalid_config_both.yml"), factories) | ||
assert.Error(t, err) | ||
}) | ||
t.Run("invalid config none present", func(t *testing.T) { | ||
_, err = servicetest.LoadConfigAndValidate(filepath.Join("testdata", "invalid_config_none.yml"), factories) | ||
assert.Error(t, err) | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.