forked from mostafa/xk6-kafka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_test.go
209 lines (196 loc) · 6.38 KB
/
auth_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package kafka
import (
"errors"
"testing"
"time"
"github.com/segmentio/kafka-go/sasl/plain"
"github.com/segmentio/kafka-go/sasl/scram"
"github.com/stretchr/testify/assert"
)
// TestGetDialerWithSASLPlainAndFullTLSConfig tests the creation of a dialer with SASL PLAIN and TLS config
func TestGetDialerWithSASLPlainAndFullTLSConfig(t *testing.T) {
saslConfig := SASLConfig{
Username: "test",
Password: "test",
Algorithm: SASL_Plain,
}
tlsConfig := TLSConfig{
EnableTLS: true,
InsecureSkipTLSVerify: false,
ClientCertPem: "client.cer",
ClientKeyPem: "key.pem",
ServerCaPem: "caroot.cer",
}
dialer, err := GetDialer(saslConfig, tlsConfig)
assert.Nil(t, err)
assert.NotNil(t, dialer)
assert.Equal(t, 10*time.Second, dialer.Timeout)
assert.Equal(t, false, dialer.DualStack)
assert.Nil(t, dialer.TLS)
assert.Equal(t, "PLAIN", dialer.SASLMechanism.Name())
assert.Equal(t, "test", dialer.SASLMechanism.(plain.Mechanism).Username)
assert.Equal(t, "test", dialer.SASLMechanism.(plain.Mechanism).Password)
}
// TestGetDialerWithSASLPlainWithDefaultTLSConfig tests the creation of a dialer with SASL PLAIN
func TestGetDialerWithSASLPlainWithDefaultTLSConfig(t *testing.T) {
saslConfig := SASLConfig{
Username: "test",
Password: "test",
Algorithm: SASL_Plain,
}
dialer, err := GetDialer(saslConfig, TLSConfig{EnableTLS: true})
assert.Nil(t, err)
assert.NotNil(t, dialer)
assert.Equal(t, 10*time.Second, dialer.Timeout)
assert.Equal(t, true, dialer.DualStack)
assert.NotNil(t, dialer.TLS)
assert.Equal(t, "PLAIN", dialer.SASLMechanism.Name())
assert.Equal(t, "test", dialer.SASLMechanism.(plain.Mechanism).Username)
assert.Equal(t, "test", dialer.SASLMechanism.(plain.Mechanism).Password)
}
// TestGetDialerWithSASLScramWithDefaultTLSConfig tests the creation of a dialer with SASL SCRAM
func TestGetDialerWithSASLScram(t *testing.T) {
saslConfig := SASLConfig{
Username: "test",
Password: "test",
Algorithm: SASL_SCRAM_SHA256, // Same applies with SASL_SCRAM_SHA512
}
dialer, err := GetDialer(saslConfig, TLSConfig{EnableTLS: true})
assert.Nil(t, err)
assert.NotNil(t, dialer)
assert.Equal(t, 10*time.Second, dialer.Timeout)
assert.Equal(t, true, dialer.DualStack)
assert.NotNil(t, dialer.TLS)
assert.Equal(t, scram.SHA256.Name(), dialer.SASLMechanism.Name())
}
// TestGetDialerWithSASLSSLWithNoTLSConfigFails tests the creation of a dialer with SASL SSL
func TestGetDialerWithSASLSSLWithNoTLSConfigFails(t *testing.T) {
saslConfig := SASLConfig{
Username: "test",
Password: "test",
Algorithm: SASL_SSL,
}
dialer, err := GetDialer(saslConfig, TLSConfig{EnableTLS: false})
assert.NotNil(t, err)
assert.Nil(t, dialer)
}
// TestGetDialerFails tests the creation of a dialer with SASL PLAIN and fails on invalid credentials
func TestGetDialerFails(t *testing.T) {
saslConfig := SASLConfig{
Username: "https://www.exa\t\r\n",
Password: "test",
Algorithm: SASL_SCRAM_SHA256,
}
dialer, wrappedError := GetDialer(saslConfig, TLSConfig{})
assert.Equal(t, wrappedError.Message, "Unable to create SCRAM mechanism")
// This is a stringprep (RFC3454) error wrapped inside the Xk6KafkaError
assert.Equal(t, wrappedError.Unwrap().Error(), "Error SASLprepping username 'https://www.exa\t\r\n': prohibited character (rune: '\\u0009')")
assert.Nil(t, dialer)
}
// TestGetDialerUnauthenticatedNoTLS tests the creation of an unauthenticated dialer
func TestGetDialerUnauthenticatedNoTLS(t *testing.T) {
dialer, err := GetDialer(SASLConfig{}, TLSConfig{})
assert.Nil(t, err)
assert.NotNil(t, dialer)
assert.Equal(t, 10*time.Second, dialer.Timeout)
assert.Equal(t, false, dialer.DualStack)
assert.Nil(t, dialer.TLS)
}
// TestFileExists tests the file exists function
func TestFileExists(t *testing.T) {
assert.True(t, FileExists("auth_test.go"))
assert.False(t, FileExists("test.go.not"))
}
type SimpleTLSConfig struct {
saslConfig SASLConfig
tlsConfig TLSConfig
err *Xk6KafkaError
}
// TestTlsConfig tests the creation of a TLS config
func TestTlsConfig(t *testing.T) {
tlsConfig := TLSConfig{
EnableTLS: true,
ClientCertPem: "fixtures/client.cer",
ClientKeyPem: "fixtures/client.pem",
ServerCaPem: "fixtures/caroot.cer",
}
tlsObject, err := GetTLSConfig(tlsConfig)
assert.Nil(t, err)
assert.NotNil(t, tlsObject)
}
// TestTlsConfigFails tests the creation of a TLS config and fails on invalid files and configs
func TestTlsConfigFails(t *testing.T) {
saslConfig := []*SimpleTLSConfig{
{
saslConfig: SASLConfig{},
tlsConfig: TLSConfig{},
err: &Xk6KafkaError{
Code: noTLSConfig,
Message: "No TLS config provided. Continuing with TLS disabled.",
},
},
{
saslConfig: SASLConfig{},
tlsConfig: TLSConfig{EnableTLS: true, ClientCertPem: "test.cer"},
err: &Xk6KafkaError{
Code: fileNotFound,
Message: "Client certificate file not found.",
},
},
{
saslConfig: SASLConfig{},
tlsConfig: TLSConfig{
EnableTLS: true,
ClientCertPem: "fixtures/client.cer",
},
err: &Xk6KafkaError{
Code: fileNotFound,
Message: "Client key file not found.",
},
},
{
saslConfig: SASLConfig{},
tlsConfig: TLSConfig{
EnableTLS: true,
ClientCertPem: "fixtures/client.cer",
ClientKeyPem: "fixtures/client.pem",
},
err: &Xk6KafkaError{
Code: fileNotFound,
Message: "CA certificate file not found.",
},
},
{
saslConfig: SASLConfig{},
tlsConfig: TLSConfig{
EnableTLS: true,
ClientCertPem: "fixtures/invalid-client.cer",
ClientKeyPem: "fixtures/invalid-client.pem",
},
err: &Xk6KafkaError{
Code: failedLoadX509KeyPair,
Message: "Error creating x509 key pair from client cert file \"fixtures/invalid-client.cer\" and client key file \"fixtures/invalid-client.pem\".",
OriginalError: errors.New("tls: failed to find any PEM data in certificate input"),
},
},
{
saslConfig: SASLConfig{},
tlsConfig: TLSConfig{
EnableTLS: true,
ClientCertPem: "fixtures/client.cer",
ClientKeyPem: "fixtures/client.pem",
ServerCaPem: "fixtures/invalid-caroot.cer",
},
err: &Xk6KafkaError{
Code: failedAppendCaCertFile,
Message: "Error appending CA certificate file \"fixtures/invalid-caroot.cer\".",
},
},
}
for _, c := range saslConfig {
tlsObject, err := GetTLSConfig(c.tlsConfig)
assert.NotNil(t, err)
assert.Equal(t, c.err, err)
assert.Nil(t, tlsObject)
}
}