|
| 1 | +// Copyright (c) 2024 The Jaeger Authors. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package auth |
| 5 | + |
| 6 | +import ( |
| 7 | + "flag" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/Shopify/sarama" |
| 11 | + "github.com/spf13/viper" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | + "go.uber.org/zap" |
| 15 | + "go.uber.org/zap/zaptest" |
| 16 | + |
| 17 | + "github.com/jaegertracing/jaeger/pkg/config" |
| 18 | + "github.com/jaegertracing/jaeger/pkg/config/tlscfg" |
| 19 | +) |
| 20 | + |
| 21 | +func addFlags(flags *flag.FlagSet) { |
| 22 | + configPrefix := "kafka.auth" |
| 23 | + AddFlags(configPrefix, flags) |
| 24 | +} |
| 25 | + |
| 26 | +func Test_InitFromViper(t *testing.T) { |
| 27 | + configPrefix := "kafka.auth" |
| 28 | + v, command := config.Viperize(addFlags) |
| 29 | + command.ParseFlags([]string{ |
| 30 | + "--kafka.auth.authentication=tls", |
| 31 | + "--kafka.auth.kerberos.service-name=kafka", |
| 32 | + "--kafka.auth.kerberos.realm=EXAMPLE.COM", |
| 33 | + "--kafka.auth.kerberos.use-keytab=true", |
| 34 | + "--kafka.auth.kerberos.username=user", |
| 35 | + "--kafka.auth.kerberos.password=password", |
| 36 | + "--kafka.auth.kerberos.config-file=/path/to/krb5.conf", |
| 37 | + "--kafka.auth.kerberos.keytab-file=/path/to/keytab", |
| 38 | + "--kafka.auth.kerberos.disable-fast-negotiation=true", |
| 39 | + "--kafka.auth.tls.enabled=false", |
| 40 | + "--kafka.auth.plaintext.username=user", |
| 41 | + "--kafka.auth.plaintext.password=password", |
| 42 | + "--kafka.auth.plaintext.mechanism=SCRAM-SHA-256", |
| 43 | + "--kafka.auth.tls.ca=failing", |
| 44 | + }) |
| 45 | + |
| 46 | + authConfig := &AuthenticationConfig{} |
| 47 | + err := authConfig.InitFromViper(configPrefix, v) |
| 48 | + require.EqualError(t, err, "failed to process Kafka TLS options: kafka.auth.tls.* options cannot be used when kafka.auth.tls.enabled is false") |
| 49 | + |
| 50 | + command.ParseFlags([]string{"--kafka.auth.tls.ca="}) |
| 51 | + v.BindPFlags(command.Flags()) |
| 52 | + err = authConfig.InitFromViper(configPrefix, v) |
| 53 | + require.NoError(t, err) |
| 54 | + |
| 55 | + expectedConfig := &AuthenticationConfig{ |
| 56 | + Authentication: "tls", |
| 57 | + Kerberos: KerberosConfig{ |
| 58 | + ServiceName: "kafka", |
| 59 | + Realm: "EXAMPLE.COM", |
| 60 | + UseKeyTab: true, |
| 61 | + Username: "user", |
| 62 | + Password: "password", |
| 63 | + ConfigPath: "/path/to/krb5.conf", |
| 64 | + KeyTabPath: "/path/to/keytab", |
| 65 | + DisablePAFXFast: true, |
| 66 | + }, |
| 67 | + TLS: tlscfg.Options{ |
| 68 | + Enabled: true, |
| 69 | + }, |
| 70 | + PlainText: PlainTextConfig{ |
| 71 | + Username: "user", |
| 72 | + Password: "password", |
| 73 | + Mechanism: "SCRAM-SHA-256", |
| 74 | + }, |
| 75 | + } |
| 76 | + assert.Equal(t, expectedConfig, authConfig) |
| 77 | +} |
| 78 | + |
| 79 | +// Test plaintext with different mechanisms |
| 80 | +func testPlaintext(v *viper.Viper, t *testing.T, configPrefix string, logger *zap.Logger, mechanism string, saramaConfig *sarama.Config) { |
| 81 | + v.Set(configPrefix+plainTextPrefix+suffixPlainTextMechanism, mechanism) |
| 82 | + authConfig := &AuthenticationConfig{} |
| 83 | + err := authConfig.InitFromViper(configPrefix, v) |
| 84 | + require.NoError(t, err) |
| 85 | + require.NoError(t, authConfig.SetConfiguration(saramaConfig, logger)) |
| 86 | +} |
| 87 | + |
| 88 | +func TestSetConfiguration(t *testing.T) { |
| 89 | + logger := zaptest.NewLogger(t) |
| 90 | + saramaConfig := sarama.NewConfig() |
| 91 | + configPrefix := "kafka.auth" |
| 92 | + v, command := config.Viperize(addFlags) |
| 93 | + |
| 94 | + // Table-driven test cases |
| 95 | + tests := []struct { |
| 96 | + name string |
| 97 | + authType string |
| 98 | + expectedError string |
| 99 | + plainTextMechanisms []string |
| 100 | + }{ |
| 101 | + { |
| 102 | + name: "Invalid authentication method", |
| 103 | + authType: "fail", |
| 104 | + expectedError: "Unknown/Unsupported authentication method fail to kafka cluster", |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "Kerberos authentication", |
| 108 | + authType: "kerberos", |
| 109 | + expectedError: "", |
| 110 | + }, |
| 111 | + { |
| 112 | + name: "Plaintext authentication with SCRAM-SHA-256", |
| 113 | + authType: "plaintext", |
| 114 | + expectedError: "", |
| 115 | + plainTextMechanisms: []string{"SCRAM-SHA-256"}, |
| 116 | + }, |
| 117 | + { |
| 118 | + name: "Plaintext authentication with SCRAM-SHA-512", |
| 119 | + authType: "plaintext", |
| 120 | + expectedError: "", |
| 121 | + plainTextMechanisms: []string{"SCRAM-SHA-512"}, |
| 122 | + }, |
| 123 | + { |
| 124 | + name: "Plaintext authentication with PLAIN", |
| 125 | + authType: "plaintext", |
| 126 | + expectedError: "", |
| 127 | + plainTextMechanisms: []string{"PLAIN"}, |
| 128 | + }, |
| 129 | + { |
| 130 | + name: "No authentication", |
| 131 | + authType: " ", |
| 132 | + expectedError: "", |
| 133 | + }, |
| 134 | + { |
| 135 | + name: "TLS authentication", |
| 136 | + authType: "tls", |
| 137 | + expectedError: "", |
| 138 | + }, |
| 139 | + { |
| 140 | + name: "TLS authentication with invalid cipher suite", |
| 141 | + authType: "tls", |
| 142 | + expectedError: "error loading tls config: failed to get cipher suite ids from cipher suite names: cipher suite fail not supported or doesn't exist", |
| 143 | + }, |
| 144 | + } |
| 145 | + |
| 146 | + for _, tt := range tests { |
| 147 | + t.Run(tt.name, func(t *testing.T) { |
| 148 | + command.ParseFlags([]string{ |
| 149 | + "--kafka.auth.authentication=" + tt.authType, |
| 150 | + }) |
| 151 | + authConfig := &AuthenticationConfig{} |
| 152 | + defer authConfig.TLS.Close() |
| 153 | + err := authConfig.InitFromViper(configPrefix, v) |
| 154 | + require.NoError(t, err) |
| 155 | + |
| 156 | + if tt.authType == "tls" && tt.expectedError != "" { |
| 157 | + authConfig.TLS.CipherSuites = []string{"fail"} |
| 158 | + } |
| 159 | + |
| 160 | + if len(tt.plainTextMechanisms) > 0 { |
| 161 | + for _, mechanism := range tt.plainTextMechanisms { |
| 162 | + testPlaintext(v, t, configPrefix, logger, mechanism, saramaConfig) |
| 163 | + } |
| 164 | + } else { |
| 165 | + err = authConfig.SetConfiguration(saramaConfig, logger) |
| 166 | + if tt.expectedError != "" { |
| 167 | + require.EqualError(t, err, tt.expectedError) |
| 168 | + } else { |
| 169 | + require.NoError(t, err) |
| 170 | + } |
| 171 | + } |
| 172 | + }) |
| 173 | + } |
| 174 | +} |
0 commit comments