forked from open-telemetry/opentelemetry-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Pavol Loffay <ploffay@redhat.com>
- Loading branch information
1 parent
471c4a6
commit 9994700
Showing
17 changed files
with
308 additions
and
4 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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// 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 kafkaexporter | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/Shopify/sarama" | ||
|
||
"go.opentelemetry.io/collector/config/configtls" | ||
) | ||
|
||
// ConfigureAuthentication configures authentication in sarama.Config. | ||
func ConfigureAuthentication(config Authentication, saramaConfig *sarama.Config) error { | ||
switch AuthType(strings.TrimSpace(string(config.Type))) { | ||
case AuthTypeNone, "": | ||
return nil | ||
case AuthTypeTLS: | ||
return configureTLS(config.TLS, saramaConfig) | ||
case AuthTypeKerberos: | ||
configureKerberos(config.Kerberos, saramaConfig) | ||
return nil | ||
case AuthTypePlaintext: | ||
configurePlaintext(config.PlainText, saramaConfig) | ||
return nil | ||
default: | ||
return fmt.Errorf("unknown/unsupported authentication method %v to kafka cluster", config.Type) | ||
} | ||
} | ||
|
||
func configurePlaintext(config PlainTextConfig, saramaConfig *sarama.Config) { | ||
saramaConfig.Net.SASL.Enable = true | ||
saramaConfig.Net.SASL.User = config.Username | ||
saramaConfig.Net.SASL.Password = config.Password | ||
} | ||
|
||
func configureTLS(config configtls.TLSClientSetting, saramaConfig *sarama.Config) error { | ||
tlsConfig, err := config.LoadTLSConfig() | ||
if err != nil { | ||
return fmt.Errorf("error loading tls config: %w", err) | ||
} | ||
saramaConfig.Net.TLS.Enable = true | ||
saramaConfig.Net.TLS.Config = tlsConfig | ||
return nil | ||
} | ||
|
||
func configureKerberos(config KerberosConfig, saramaConfig *sarama.Config) { | ||
saramaConfig.Net.SASL.Mechanism = sarama.SASLTypeGSSAPI | ||
saramaConfig.Net.SASL.Enable = true | ||
if config.UseKeyTab { | ||
saramaConfig.Net.SASL.GSSAPI.KeyTabPath = config.KeyTabPath | ||
saramaConfig.Net.SASL.GSSAPI.AuthType = sarama.KRB5_KEYTAB_AUTH | ||
} else { | ||
saramaConfig.Net.SASL.GSSAPI.AuthType = sarama.KRB5_USER_AUTH | ||
saramaConfig.Net.SASL.GSSAPI.Password = config.Password | ||
} | ||
saramaConfig.Net.SASL.GSSAPI.KerberosConfigPath = config.ConfigPath | ||
saramaConfig.Net.SASL.GSSAPI.Username = config.Username | ||
saramaConfig.Net.SASL.GSSAPI.Realm = config.Realm | ||
saramaConfig.Net.SASL.GSSAPI.ServiceName = config.ServiceName | ||
} |
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,97 @@ | ||
// 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 kafkaexporter | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/Shopify/sarama" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"go.opentelemetry.io/collector/config/configtls" | ||
) | ||
|
||
func TestAuthentication(t *testing.T) { | ||
saramaPlaintext := &sarama.Config{} | ||
saramaPlaintext.Net.SASL.Enable = true | ||
saramaPlaintext.Net.SASL.User = "jdoe" | ||
saramaPlaintext.Net.SASL.Password = "pass" | ||
|
||
saramaTLSCfg := &sarama.Config{} | ||
saramaTLSCfg.Net.TLS.Enable = true | ||
tlsClient := configtls.TLSClientSetting{} | ||
tlscfg, err := tlsClient.LoadTLSConfig() | ||
require.NoError(t, err) | ||
saramaTLSCfg.Net.TLS.Config = tlscfg | ||
|
||
saramaKerberosCfg := &sarama.Config{} | ||
saramaKerberosCfg.Net.SASL.Mechanism = sarama.SASLTypeGSSAPI | ||
saramaKerberosCfg.Net.SASL.Enable = true | ||
saramaKerberosCfg.Net.SASL.GSSAPI.ServiceName = "foobar" | ||
saramaKerberosCfg.Net.SASL.GSSAPI.AuthType = sarama.KRB5_USER_AUTH | ||
|
||
saramaKerberosKeyTabCfg := &sarama.Config{} | ||
saramaKerberosKeyTabCfg.Net.SASL.Mechanism = sarama.SASLTypeGSSAPI | ||
saramaKerberosKeyTabCfg.Net.SASL.Enable = true | ||
saramaKerberosKeyTabCfg.Net.SASL.GSSAPI.KeyTabPath = "/path" | ||
saramaKerberosKeyTabCfg.Net.SASL.GSSAPI.AuthType = sarama.KRB5_KEYTAB_AUTH | ||
|
||
tests := []struct { | ||
auth Authentication | ||
saramaConfig *sarama.Config | ||
err string | ||
}{ | ||
{ | ||
auth: Authentication{Type: AuthType("foo")}, | ||
err: "unknown/unsupported authentication method", | ||
}, | ||
{ | ||
auth: Authentication{Type: AuthTypePlaintext, PlainText: PlainTextConfig{Username: "jdoe", Password: "pass"}}, | ||
saramaConfig: saramaPlaintext, | ||
}, | ||
{ | ||
auth: Authentication{Type: AuthTypeTLS, TLS: configtls.TLSClientSetting{}}, | ||
saramaConfig: saramaTLSCfg, | ||
}, | ||
{ | ||
auth: Authentication{Type: AuthTypeTLS, TLS: configtls.TLSClientSetting{ | ||
TLSSetting: configtls.TLSSetting{CAFile: "/doesnotexists"}, | ||
}}, | ||
saramaConfig: saramaTLSCfg, | ||
err: "failed to load TLS config", | ||
}, | ||
{ | ||
auth: Authentication{Type: AuthTypeKerberos, Kerberos: KerberosConfig{ServiceName: "foobar"}}, | ||
saramaConfig: saramaKerberosCfg, | ||
}, | ||
{ | ||
auth: Authentication{Type: AuthTypeKerberos, Kerberos: KerberosConfig{UseKeyTab: true, KeyTabPath: "/path"}}, | ||
saramaConfig: saramaKerberosKeyTabCfg, | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(string(test.auth.Type), func(t *testing.T) { | ||
config := &sarama.Config{} | ||
err := ConfigureAuthentication(test.auth, config) | ||
if test.err != "" { | ||
require.Error(t, err) | ||
assert.Contains(t, err.Error(), test.err) | ||
} else { | ||
assert.Equal(t, test.saramaConfig, config) | ||
} | ||
}) | ||
} | ||
} |
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
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
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
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
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
Oops, something went wrong.