-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
client.go
235 lines (213 loc) · 6.72 KB
/
client.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package proxy
import (
"crypto/tls"
"github.com/grepplabs/kafka-proxy/config"
"github.com/grepplabs/kafka-proxy/pkg/apis"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"net"
"sync"
"time"
)
// Conn represents a connection from a client to a specific instance.
type Conn struct {
BrokerAddress string
LocalConnection net.Conn
}
// Client is a type to handle connecting to a Server. All fields are required
// unless otherwise specified.
type Client struct {
conns *ConnSet
// Kafka Net configuration
config *config.Config
// Config of Proxy request-response processor (instance p)
processorConfig ProcessorConfig
dialer Dialer
tcpConnOptions TCPConnOptions
stopRun chan struct{}
stopOnce sync.Once
saslPlainAuth *SASLPlainAuth
authClient *AuthClient
}
func NewClient(conns *ConnSet, c *config.Config, netAddressMappingFunc config.NetAddressMappingFunc, passwordAuthenticator apis.PasswordAuthenticator, tokenProvider apis.TokenProvider, tokenInfo apis.TokenInfo) (*Client, error) {
tlsConfig, err := newTLSClientConfig(c)
if err != nil {
return nil, err
}
dialer, err := newDialer(c, tlsConfig)
if err != nil {
return nil, err
}
tcpConnOptions := TCPConnOptions{
KeepAlive: c.Kafka.KeepAlive,
WriteBufferSize: c.Kafka.ConnectionWriteBufferSize,
ReadBufferSize: c.Kafka.ConnectionReadBufferSize,
}
forbiddenApiKeys := make(map[int16]struct{})
if len(c.Kafka.ForbiddenApiKeys) != 0 {
logrus.Warnf("Kafka operations for Api Keys %v will be forbidden.", c.Kafka.ForbiddenApiKeys)
for _, apiKey := range c.Kafka.ForbiddenApiKeys {
forbiddenApiKeys[int16(apiKey)] = struct{}{}
}
}
if c.Auth.Local.Enable && passwordAuthenticator == nil {
return nil, errors.New("Auth.Local.Enable is enabled but passwordAuthenticator is nil")
}
if c.Auth.Gateway.Client.Enable && tokenProvider == nil {
return nil, errors.New("Auth.Gateway.Client.Enable is enabled but tokenProvider is nil")
}
if c.Auth.Gateway.Server.Enable && tokenInfo == nil {
return nil, errors.New("Auth.Gateway.Server.Enable is enabled but tokenInfo is nil")
}
return &Client{conns: conns, config: c, dialer: dialer, tcpConnOptions: tcpConnOptions, stopRun: make(chan struct{}, 1),
saslPlainAuth: &SASLPlainAuth{
clientID: c.Kafka.ClientID,
writeTimeout: c.Kafka.WriteTimeout,
readTimeout: c.Kafka.ReadTimeout,
username: c.Kafka.SASL.Username,
password: c.Kafka.SASL.Password,
},
authClient: &AuthClient{
enabled: c.Auth.Gateway.Client.Enable,
magic: c.Auth.Gateway.Client.Magic,
method: c.Auth.Gateway.Client.Method,
timeout: c.Auth.Gateway.Client.Timeout,
tokenProvider: tokenProvider,
},
processorConfig: ProcessorConfig{
MaxOpenRequests: c.Kafka.MaxOpenRequests,
NetAddressMappingFunc: netAddressMappingFunc,
RequestBufferSize: c.Proxy.RequestBufferSize,
ResponseBufferSize: c.Proxy.ResponseBufferSize,
ReadTimeout: c.Kafka.ReadTimeout,
WriteTimeout: c.Kafka.WriteTimeout,
LocalSasl: &LocalSasl{
enabled: c.Auth.Local.Enable,
timeout: c.Auth.Local.Timeout,
localAuthenticator: passwordAuthenticator},
AuthServer: &AuthServer{
enabled: c.Auth.Gateway.Server.Enable,
magic: c.Auth.Gateway.Server.Magic,
method: c.Auth.Gateway.Server.Method,
timeout: c.Auth.Gateway.Server.Timeout,
tokenInfo: tokenInfo,
},
ForbiddenApiKeys: forbiddenApiKeys,
}}, nil
}
func newDialer(c *config.Config, tlsConfig *tls.Config) (Dialer, error) {
var rawDialer Dialer
if c.Socks5.ProxyAddress != "" {
logrus.Infof("Kafka clients will connect through the SOCKS5 proxy %s", c.Socks5.ProxyAddress)
rawDialer = &socks5Dialer{
directDialer: directDialer{
dialTimeout: c.Kafka.DialTimeout,
keepAlive: c.Kafka.KeepAlive,
},
proxyNetwork: "tcp",
proxyAddr: c.Socks5.ProxyAddress,
username: c.Socks5.Username,
password: c.Socks5.Password,
}
} else {
rawDialer = directDialer{
dialTimeout: c.Kafka.DialTimeout,
keepAlive: c.Kafka.KeepAlive,
}
}
if c.Kafka.TLS.Enable {
if tlsConfig == nil {
return nil, errors.New("tlsConfig must not be nil")
}
tlsDialer := tlsDialer{
timeout: c.Kafka.DialTimeout,
rawDialer: rawDialer,
config: tlsConfig,
}
return tlsDialer, nil
}
return rawDialer, nil
}
// Run causes the client to start waiting for new connections to connSrc and
// proxy them to the destination instance. It blocks until connSrc is closed.
func (c *Client) Run(connSrc <-chan Conn) error {
STOP:
for {
select {
case conn := <-connSrc:
go withRecover(func() { c.handleConn(conn) })
case <-c.stopRun:
break STOP
}
}
logrus.Info("Closing connections")
if err := c.conns.Close(); err != nil {
logrus.Infof("closing client had error: %v", err)
}
logrus.Info("Proxy is stopped")
return nil
}
func (c *Client) Close() {
c.stopOnce.Do(func() {
close(c.stopRun)
})
}
func (c *Client) handleConn(conn Conn) {
proxyConnectionsTotal.WithLabelValues(conn.BrokerAddress).Inc()
server, err := c.DialAndAuth(conn.BrokerAddress)
if err != nil {
logrus.Infof("couldn't connect to %s: %v", conn.BrokerAddress, err)
conn.LocalConnection.Close()
return
}
if tcpConn, ok := server.(*net.TCPConn); ok {
if err := c.tcpConnOptions.setTCPConnOptions(tcpConn); err != nil {
logrus.Infof("WARNING: Error while setting TCP options for kafka connection %s on %v: %v", conn.BrokerAddress, server.LocalAddr(), err)
}
}
c.conns.Add(conn.BrokerAddress, conn.LocalConnection)
localDesc := "local connection on " + conn.LocalConnection.LocalAddr().String() + " from " + conn.LocalConnection.RemoteAddr().String() + " (" + conn.BrokerAddress + ")"
copyThenClose(c.processorConfig, server, conn.LocalConnection, conn.BrokerAddress, conn.BrokerAddress, localDesc)
if err := c.conns.Remove(conn.BrokerAddress, conn.LocalConnection); err != nil {
logrus.Info(err)
}
}
func (c *Client) DialAndAuth(brokerAddress string) (net.Conn, error) {
conn, err := c.dialer.Dial("tcp", brokerAddress)
if err != nil {
return nil, err
}
if err := conn.SetDeadline(time.Time{}); err != nil {
conn.Close()
return nil, err
}
err = c.auth(conn)
if err != nil {
return nil, err
}
return conn, nil
}
func (c *Client) auth(conn net.Conn) error {
if c.config.Auth.Gateway.Client.Enable {
if err := c.authClient.sendAndReceiveGatewayAuth(conn); err != nil {
conn.Close()
return err
}
if err := conn.SetDeadline(time.Time{}); err != nil {
conn.Close()
return err
}
}
if c.config.Kafka.SASL.Enable {
err := c.saslPlainAuth.sendAndReceiveSASLPlainAuth(conn)
if err != nil {
conn.Close()
return err
}
if err := conn.SetDeadline(time.Time{}); err != nil {
conn.Close()
return err
}
}
return nil
}