-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Increase codecov of pkg/kafka (#5682)
## Which problem is this PR solving? - #5068 ## Description of the changes - added test case for ` pkg/kafka/auth/config.go` and updated `internal/tracegen/worker_test.go` ## How was this change tested? - ## Checklist - [ ] I have read https://github.com/jaegertracing/jaeger/blob/master/CONTRIBUTING_GUIDELINES.md - [ ] I have signed all commits - [ ] I have added unit tests for the new functionality - [ ] I have run lint and test steps successfully - for `jaeger`: `make lint test` - for `jaeger-ui`: `yarn lint` and `yarn test` --------- Signed-off-by: mehul gautam <[email protected]> Signed-off-by: mehul gautam <[email protected]> Co-authored-by: mehul gautam <[email protected]> Co-authored-by: Yuri Shkuro <[email protected]>
- Loading branch information
1 parent
42bffdf
commit b39252c
Showing
7 changed files
with
450 additions
and
23 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,174 @@ | ||
// Copyright (c) 2024 The Jaeger Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package auth | ||
|
||
import ( | ||
"flag" | ||
"testing" | ||
|
||
"github.com/Shopify/sarama" | ||
"github.com/spf13/viper" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zaptest" | ||
|
||
"github.com/jaegertracing/jaeger/pkg/config" | ||
"github.com/jaegertracing/jaeger/pkg/config/tlscfg" | ||
) | ||
|
||
func addFlags(flags *flag.FlagSet) { | ||
configPrefix := "kafka.auth" | ||
AddFlags(configPrefix, flags) | ||
} | ||
|
||
func Test_InitFromViper(t *testing.T) { | ||
configPrefix := "kafka.auth" | ||
v, command := config.Viperize(addFlags) | ||
command.ParseFlags([]string{ | ||
"--kafka.auth.authentication=tls", | ||
"--kafka.auth.kerberos.service-name=kafka", | ||
"--kafka.auth.kerberos.realm=EXAMPLE.COM", | ||
"--kafka.auth.kerberos.use-keytab=true", | ||
"--kafka.auth.kerberos.username=user", | ||
"--kafka.auth.kerberos.password=password", | ||
"--kafka.auth.kerberos.config-file=/path/to/krb5.conf", | ||
"--kafka.auth.kerberos.keytab-file=/path/to/keytab", | ||
"--kafka.auth.kerberos.disable-fast-negotiation=true", | ||
"--kafka.auth.tls.enabled=false", | ||
"--kafka.auth.plaintext.username=user", | ||
"--kafka.auth.plaintext.password=password", | ||
"--kafka.auth.plaintext.mechanism=SCRAM-SHA-256", | ||
"--kafka.auth.tls.ca=failing", | ||
}) | ||
|
||
authConfig := &AuthenticationConfig{} | ||
err := authConfig.InitFromViper(configPrefix, v) | ||
require.EqualError(t, err, "failed to process Kafka TLS options: kafka.auth.tls.* options cannot be used when kafka.auth.tls.enabled is false") | ||
|
||
command.ParseFlags([]string{"--kafka.auth.tls.ca="}) | ||
v.BindPFlags(command.Flags()) | ||
err = authConfig.InitFromViper(configPrefix, v) | ||
require.NoError(t, err) | ||
|
||
expectedConfig := &AuthenticationConfig{ | ||
Authentication: "tls", | ||
Kerberos: KerberosConfig{ | ||
ServiceName: "kafka", | ||
Realm: "EXAMPLE.COM", | ||
UseKeyTab: true, | ||
Username: "user", | ||
Password: "password", | ||
ConfigPath: "/path/to/krb5.conf", | ||
KeyTabPath: "/path/to/keytab", | ||
DisablePAFXFast: true, | ||
}, | ||
TLS: tlscfg.Options{ | ||
Enabled: true, | ||
}, | ||
PlainText: PlainTextConfig{ | ||
Username: "user", | ||
Password: "password", | ||
Mechanism: "SCRAM-SHA-256", | ||
}, | ||
} | ||
assert.Equal(t, expectedConfig, authConfig) | ||
} | ||
|
||
// Test plaintext with different mechanisms | ||
func testPlaintext(v *viper.Viper, t *testing.T, configPrefix string, logger *zap.Logger, mechanism string, saramaConfig *sarama.Config) { | ||
v.Set(configPrefix+plainTextPrefix+suffixPlainTextMechanism, mechanism) | ||
authConfig := &AuthenticationConfig{} | ||
err := authConfig.InitFromViper(configPrefix, v) | ||
require.NoError(t, err) | ||
require.NoError(t, authConfig.SetConfiguration(saramaConfig, logger)) | ||
} | ||
|
||
func TestSetConfiguration(t *testing.T) { | ||
logger := zaptest.NewLogger(t) | ||
saramaConfig := sarama.NewConfig() | ||
configPrefix := "kafka.auth" | ||
v, command := config.Viperize(addFlags) | ||
|
||
// Table-driven test cases | ||
tests := []struct { | ||
name string | ||
authType string | ||
expectedError string | ||
plainTextMechanisms []string | ||
}{ | ||
{ | ||
name: "Invalid authentication method", | ||
authType: "fail", | ||
expectedError: "Unknown/Unsupported authentication method fail to kafka cluster", | ||
}, | ||
{ | ||
name: "Kerberos authentication", | ||
authType: "kerberos", | ||
expectedError: "", | ||
}, | ||
{ | ||
name: "Plaintext authentication with SCRAM-SHA-256", | ||
authType: "plaintext", | ||
expectedError: "", | ||
plainTextMechanisms: []string{"SCRAM-SHA-256"}, | ||
}, | ||
{ | ||
name: "Plaintext authentication with SCRAM-SHA-512", | ||
authType: "plaintext", | ||
expectedError: "", | ||
plainTextMechanisms: []string{"SCRAM-SHA-512"}, | ||
}, | ||
{ | ||
name: "Plaintext authentication with PLAIN", | ||
authType: "plaintext", | ||
expectedError: "", | ||
plainTextMechanisms: []string{"PLAIN"}, | ||
}, | ||
{ | ||
name: "No authentication", | ||
authType: " ", | ||
expectedError: "", | ||
}, | ||
{ | ||
name: "TLS authentication", | ||
authType: "tls", | ||
expectedError: "", | ||
}, | ||
{ | ||
name: "TLS authentication with invalid cipher suite", | ||
authType: "tls", | ||
expectedError: "error loading tls config: failed to get cipher suite ids from cipher suite names: cipher suite fail not supported or doesn't exist", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
command.ParseFlags([]string{ | ||
"--kafka.auth.authentication=" + tt.authType, | ||
}) | ||
authConfig := &AuthenticationConfig{} | ||
defer authConfig.TLS.Close() | ||
err := authConfig.InitFromViper(configPrefix, v) | ||
require.NoError(t, err) | ||
|
||
if tt.authType == "tls" && tt.expectedError != "" { | ||
authConfig.TLS.CipherSuites = []string{"fail"} | ||
} | ||
|
||
if len(tt.plainTextMechanisms) > 0 { | ||
for _, mechanism := range tt.plainTextMechanisms { | ||
testPlaintext(v, t, configPrefix, logger, mechanism, saramaConfig) | ||
} | ||
} else { | ||
err = authConfig.SetConfiguration(saramaConfig, logger) | ||
if tt.expectedError != "" { | ||
require.EqualError(t, err, tt.expectedError) | ||
} else { | ||
require.NoError(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright (c) 2024 The Jaeger Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package auth | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/Shopify/sarama" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSetKerberosConfiguration(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
config KerberosConfig | ||
}{ | ||
{ | ||
name: "With KeyTab", | ||
config: KerberosConfig{ | ||
ServiceName: "service", | ||
Realm: "realm", | ||
UseKeyTab: true, | ||
Username: "username", | ||
Password: "password", | ||
ConfigPath: "/path/to/config", | ||
KeyTabPath: "/path/to/keytab", | ||
DisablePAFXFast: true, | ||
}, | ||
}, | ||
{ | ||
name: "Without KeyTab", | ||
config: KerberosConfig{ | ||
ServiceName: "service", | ||
Realm: "realm", | ||
UseKeyTab: false, | ||
Username: "username", | ||
Password: "password", | ||
ConfigPath: "/path/to/config", | ||
DisablePAFXFast: false, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
saramaConfig := sarama.NewConfig() | ||
|
||
setKerberosConfiguration(&tt.config, saramaConfig) | ||
|
||
assert.Equal(t, sarama.SASLMechanism("GSSAPI"), saramaConfig.Net.SASL.Mechanism) | ||
assert.True(t, saramaConfig.Net.SASL.Enable) | ||
assert.Equal(t, tt.config.Username, saramaConfig.Net.SASL.GSSAPI.Username) | ||
assert.Equal(t, tt.config.Realm, saramaConfig.Net.SASL.GSSAPI.Realm) | ||
assert.Equal(t, tt.config.ServiceName, saramaConfig.Net.SASL.GSSAPI.ServiceName) | ||
assert.Equal(t, tt.config.DisablePAFXFast, saramaConfig.Net.SASL.GSSAPI.DisablePAFXFAST) | ||
assert.Equal(t, tt.config.ConfigPath, saramaConfig.Net.SASL.GSSAPI.KerberosConfigPath) | ||
|
||
if tt.config.UseKeyTab { | ||
assert.Equal(t, tt.config.KeyTabPath, saramaConfig.Net.SASL.GSSAPI.KeyTabPath) | ||
assert.Equal(t, sarama.KRB5_KEYTAB_AUTH, saramaConfig.Net.SASL.GSSAPI.AuthType) | ||
} else { | ||
assert.Equal(t, tt.config.Password, saramaConfig.Net.SASL.GSSAPI.Password) | ||
assert.Equal(t, sarama.KRB5_USER_AUTH, saramaConfig.Net.SASL.GSSAPI.AuthType) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.