-
-
Notifications
You must be signed in to change notification settings - Fork 79
/
server.go
380 lines (343 loc) · 16.6 KB
/
server.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package server
import (
"fmt"
"github.com/grepplabs/kafka-proxy/config"
"github.com/grepplabs/kafka-proxy/proxy"
"github.com/oklog/oklog/pkg/group"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"net"
"net/http"
_ "net/http/pprof"
"os"
"os/exec"
"os/signal"
"syscall"
"time"
"errors"
"github.com/grepplabs/kafka-proxy/pkg/apis"
gatewayclient "github.com/grepplabs/kafka-proxy/plugin/gateway-client/shared"
gatewayserver "github.com/grepplabs/kafka-proxy/plugin/gateway-server/shared"
localauth "github.com/grepplabs/kafka-proxy/plugin/local-auth/shared"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-plugin"
"github.com/grepplabs/kafka-proxy/pkg/registry"
// built-in plugins
_ "github.com/grepplabs/kafka-proxy/pkg/libs/googleid-info"
_ "github.com/grepplabs/kafka-proxy/pkg/libs/googleid-provider"
)
var (
c = new(config.Config)
bootstrapServersMapping = make([]string, 0)
externalServersMapping = make([]string, 0)
)
var Server = &cobra.Command{
Use: "server",
Short: "Run the kafka-proxy server",
PreRunE: func(cmd *cobra.Command, args []string) error {
SetLogger()
if err := c.InitSASLCredentials(); err != nil {
return err
}
if err := c.InitBootstrapServers(bootstrapServersMapping); err != nil {
return err
}
if err := c.InitExternalServers(externalServersMapping); err != nil {
return err
}
if err := c.Validate(); err != nil {
return err
}
return nil
},
Run: Run,
}
func init() {
// proxy
Server.Flags().StringVar(&c.Proxy.DefaultListenerIP, "default-listener-ip", "127.0.0.1", "Default listener IP")
Server.Flags().StringArrayVar(&bootstrapServersMapping, "bootstrap-server-mapping", []string{}, "Mapping of Kafka bootstrap server address to local address (host:port,host:port(,advhost:advport))")
Server.MarkFlagRequired("bootstrap-server-mapping")
Server.Flags().StringArrayVar(&externalServersMapping, "external-server-mapping", []string{}, "Mapping of Kafka server address to external address (host:port,host:port). A listener for the external address is not started")
Server.Flags().BoolVar(&c.Proxy.DisableDynamicListeners, "dynamic-listeners-disable", false, "Disable dynamic listeners.")
Server.Flags().IntVar(&c.Proxy.RequestBufferSize, "proxy-request-buffer-size", 4096, "Request buffer size pro tcp connection")
Server.Flags().IntVar(&c.Proxy.ResponseBufferSize, "proxy-response-buffer-size", 4096, "Response buffer size pro tcp connection")
Server.Flags().IntVar(&c.Proxy.ListenerReadBufferSize, "proxy-listener-read-buffer-size", 0, "Size of the operating system's receive buffer associated with the connection. If zero, system default is used")
Server.Flags().IntVar(&c.Proxy.ListenerWriteBufferSize, "proxy-listener-write-buffer-size", 0, "Sets the size of the operating system's transmit buffer associated with the connection. If zero, system default is used")
Server.Flags().DurationVar(&c.Proxy.ListenerKeepAlive, "proxy-listener-keep-alive", 60*time.Second, "Keep alive period for an active network connection. If zero, keep-alives are disabled")
Server.Flags().BoolVar(&c.Proxy.TLS.Enable, "proxy-listener-tls-enable", false, "Whether or not to use TLS listener")
Server.Flags().StringVar(&c.Proxy.TLS.ListenerCertFile, "proxy-listener-cert-file", "", "PEM encoded file with server certificate")
Server.Flags().StringVar(&c.Proxy.TLS.ListenerKeyFile, "proxy-listener-key-file", "", "PEM encoded file with private key for the server certificate")
Server.Flags().StringVar(&c.Proxy.TLS.ListenerKeyPassword, "proxy-listener-key-password", "", "Password to decrypt rsa private key")
Server.Flags().StringVar(&c.Proxy.TLS.CAChainCertFile, "proxy-listener-ca-chain-cert-file", "", "PEM encoded CA's certificate file. If provided, client certificate is required and verified")
Server.Flags().StringSliceVar(&c.Proxy.TLS.ListenerCipherSuites, "proxy-listener-cipher-suites", []string{}, "List of supported cipher suites")
Server.Flags().StringSliceVar(&c.Proxy.TLS.ListenerCurvePreferences, "proxy-listener-curve-preferences", []string{}, "List of curve preferences")
// local authentication plugin
Server.Flags().BoolVar(&c.Auth.Local.Enable, "auth-local-enable", false, "Enable local SASL/PLAIN authentication performed by listener - SASL handshake will not be passed to kafka brokers")
Server.Flags().StringVar(&c.Auth.Local.Command, "auth-local-command", "", "Path to authentication plugin binary")
Server.Flags().StringArrayVar(&c.Auth.Local.Parameters, "auth-local-param", []string{}, "Authentication plugin parameter")
Server.Flags().StringVar(&c.Auth.Local.LogLevel, "auth-local-log-level", "trace", "Log level of the auth plugin")
Server.Flags().DurationVar(&c.Auth.Local.Timeout, "auth-local-timeout", 10*time.Second, "Authentication timeout")
Server.Flags().BoolVar(&c.Auth.Gateway.Client.Enable, "auth-gateway-client-enable", false, "Enable gateway client authentication")
Server.Flags().StringVar(&c.Auth.Gateway.Client.Command, "auth-gateway-client-command", "", "Path to authentication plugin binary")
Server.Flags().StringArrayVar(&c.Auth.Gateway.Client.Parameters, "auth-gateway-client-param", []string{}, "Authentication plugin parameter")
Server.Flags().StringVar(&c.Auth.Gateway.Client.LogLevel, "auth-gateway-client-log-level", "trace", "Log level of the auth plugin")
Server.Flags().StringVar(&c.Auth.Gateway.Client.Method, "auth-gateway-client-method", "", "Authentication method")
Server.Flags().Uint64Var(&c.Auth.Gateway.Client.Magic, "auth-gateway-client-magic", 0, "Magic bytes sent in the handshake")
Server.Flags().DurationVar(&c.Auth.Gateway.Client.Timeout, "auth-gateway-client-timeout", 10*time.Second, "Authentication timeout")
Server.Flags().BoolVar(&c.Auth.Gateway.Server.Enable, "auth-gateway-server-enable", false, "Enable proxy server authentication")
Server.Flags().StringVar(&c.Auth.Gateway.Server.Command, "auth-gateway-server-command", "", "Path to authentication plugin binary")
Server.Flags().StringArrayVar(&c.Auth.Gateway.Server.Parameters, "auth-gateway-server-param", []string{}, "Authentication plugin parameter")
Server.Flags().StringVar(&c.Auth.Gateway.Server.LogLevel, "auth-gateway-server-log-level", "trace", "Log level of the auth plugin")
Server.Flags().StringVar(&c.Auth.Gateway.Server.Method, "auth-gateway-server-method", "", "Authentication method")
Server.Flags().Uint64Var(&c.Auth.Gateway.Server.Magic, "auth-gateway-server-magic", 0, "Magic bytes sent in the handshake")
Server.Flags().DurationVar(&c.Auth.Gateway.Server.Timeout, "auth-gateway-server-timeout", 10*time.Second, "Authentication timeout")
// kafka
Server.Flags().StringVar(&c.Kafka.ClientID, "kafka-client-id", "kafka-proxy", "An optional identifier to track the source of requests")
Server.Flags().IntVar(&c.Kafka.MaxOpenRequests, "kafka-max-open-requests", 256, "Maximal number of open requests pro tcp connection before sending on it blocks")
Server.Flags().DurationVar(&c.Kafka.DialTimeout, "kafka-dial-timeout", 15*time.Second, "How long to wait for the initial connection")
Server.Flags().DurationVar(&c.Kafka.WriteTimeout, "kafka-write-timeout", 30*time.Second, "How long to wait for a transmit")
Server.Flags().DurationVar(&c.Kafka.ReadTimeout, "kafka-read-timeout", 30*time.Second, "How long to wait for a response")
Server.Flags().DurationVar(&c.Kafka.KeepAlive, "kafka-keep-alive", 60*time.Second, "Keep alive period for an active network connection. If zero, keep-alives are disabled")
Server.Flags().IntVar(&c.Kafka.ConnectionReadBufferSize, "kafka-connection-read-buffer-size", 0, "Size of the operating system's receive buffer associated with the connection. If zero, system default is used")
Server.Flags().IntVar(&c.Kafka.ConnectionWriteBufferSize, "kafka-connection-write-buffer-size", 0, "Sets the size of the operating system's transmit buffer associated with the connection. If zero, system default is used")
// http://kafka.apache.org/protocol.html#protocol_api_keys
Server.Flags().IntSliceVar(&c.Kafka.ForbiddenApiKeys, "forbidden-api-keys", []int{}, "Forbidden Kafka request types. The restriction should prevent some Kafka operations e.g. 20 - DeleteTopics")
// TLS
Server.Flags().BoolVar(&c.Kafka.TLS.Enable, "tls-enable", false, "Whether or not to use TLS when connecting to the broker")
Server.Flags().BoolVar(&c.Kafka.TLS.InsecureSkipVerify, "tls-insecure-skip-verify", false, "It controls whether a client verifies the server's certificate chain and host name")
Server.Flags().StringVar(&c.Kafka.TLS.ClientCertFile, "tls-client-cert-file", "", "PEM encoded file with client certificate")
Server.Flags().StringVar(&c.Kafka.TLS.ClientKeyFile, "tls-client-key-file", "", "PEM encoded file with private key for the client certificate")
Server.Flags().StringVar(&c.Kafka.TLS.ClientKeyPassword, "tls-client-key-password", "", "Password to decrypt rsa private key")
Server.Flags().StringVar(&c.Kafka.TLS.CAChainCertFile, "tls-ca-chain-cert-file", "", "PEM encoded CA's certificate file")
// SASL
Server.Flags().BoolVar(&c.Kafka.SASL.Enable, "sasl-enable", false, "Connect using SASL/PLAIN")
Server.Flags().StringVar(&c.Kafka.SASL.Username, "sasl-username", "", "SASL user name")
Server.Flags().StringVar(&c.Kafka.SASL.Password, "sasl-password", "", "SASL user password")
Server.Flags().StringVar(&c.Kafka.SASL.JaasConfigFile, "sasl-jaas-config-file", "", "Location of JAAS config file with SASL username and password")
// Web
Server.Flags().BoolVar(&c.Http.Disable, "http-disable", false, "Disable HTTP endpoints")
Server.Flags().StringVar(&c.Http.ListenAddress, "http-listen-address", "0.0.0.0:9080", "Address that kafka-proxy is listening on")
Server.Flags().StringVar(&c.Http.MetricsPath, "http-metrics-path", "/metrics", "Path on which to expose metrics")
Server.Flags().StringVar(&c.Http.HealthPath, "http-health-path", "/health", "Path on which to health endpoint")
// Debug
Server.Flags().BoolVar(&c.Debug.Enabled, "debug-enable", false, "Enable Debug endpoint")
Server.Flags().StringVar(&c.Debug.ListenAddress, "debug-listen-address", "0.0.0.0:6060", "Debug listen address")
// Logging
Server.Flags().StringVar(&c.Log.Format, "log-format", "text", "Log format text or json")
Server.Flags().StringVar(&c.Log.Level, "log-level", "info", "Log level debug, info, warning, error, fatal or panic")
// Connect through Socks5 or HTTP CONNECT to Kafka
Server.Flags().StringVar(&c.ForwardProxy.Url, "forward-proxy", "", "URL of the forward proxy. Supported schemas are socks5 and http")
}
func Run(_ *cobra.Command, _ []string) {
logrus.Infof("Starting kafka-proxy version %s", config.Version)
var passwordAuthenticator apis.PasswordAuthenticator
if c.Auth.Local.Enable {
var err error
factory, ok := registry.GetComponent(new(apis.PasswordAuthenticatorFactory), c.Auth.Local.Command).(apis.PasswordAuthenticatorFactory)
if ok {
logrus.Infof("Using built-in PasswordAuthenticator")
passwordAuthenticator, err = factory.New(c.Auth.Local.Parameters)
if err != nil {
logrus.Fatal(err)
}
} else {
client := NewPluginClient(localauth.Handshake, localauth.PluginMap, c.Auth.Local.LogLevel, c.Auth.Local.Command, c.Auth.Local.Parameters)
defer client.Kill()
rpcClient, err := client.Client()
if err != nil {
logrus.Fatal(err)
}
raw, err := rpcClient.Dispense("passwordAuthenticator")
if err != nil {
logrus.Fatal(err)
}
passwordAuthenticator, ok = raw.(apis.PasswordAuthenticator)
if !ok {
logrus.Fatal(errors.New("unsupported PasswordAuthenticator plugin type"))
}
}
}
var tokenProvider apis.TokenProvider
if c.Auth.Gateway.Client.Enable {
var err error
factory, ok := registry.GetComponent(new(apis.TokenProviderFactory), c.Auth.Gateway.Client.Command).(apis.TokenProviderFactory)
if ok {
logrus.Infof("Using built-in TokenProvider")
tokenProvider, err = factory.New(c.Auth.Gateway.Client.Parameters)
if err != nil {
logrus.Fatal(err)
}
} else {
client := NewPluginClient(gatewayclient.Handshake, gatewayclient.PluginMap, c.Auth.Gateway.Client.LogLevel, c.Auth.Gateway.Client.Command, c.Auth.Gateway.Client.Parameters)
defer client.Kill()
rpcClient, err := client.Client()
if err != nil {
logrus.Fatal(err)
}
raw, err := rpcClient.Dispense("tokenProvider")
if err != nil {
logrus.Fatal(err)
}
tokenProvider, ok = raw.(apis.TokenProvider)
if !ok {
logrus.Fatal(errors.New("unsupported TokenProvider plugin type"))
}
}
}
var tokenInfo apis.TokenInfo
if c.Auth.Gateway.Server.Enable {
var err error
factory, ok := registry.GetComponent(new(apis.TokenInfoFactory), c.Auth.Gateway.Server.Command).(apis.TokenInfoFactory)
if ok {
logrus.Infof("Using built-in TokenInfo")
tokenInfo, err = factory.New(c.Auth.Gateway.Server.Parameters)
if err != nil {
logrus.Fatal(err)
}
} else {
client := NewPluginClient(gatewayserver.Handshake, gatewayserver.PluginMap, c.Auth.Gateway.Server.LogLevel, c.Auth.Gateway.Server.Command, c.Auth.Gateway.Server.Parameters)
defer client.Kill()
rpcClient, err := client.Client()
if err != nil {
logrus.Fatal(err)
}
raw, err := rpcClient.Dispense("tokenInfo")
if err != nil {
logrus.Fatal(err)
}
tokenInfo, ok = raw.(apis.TokenInfo)
if !ok {
logrus.Fatal(errors.New("unsupported TokenInfo plugin type"))
}
}
}
var g group.Group
{
// All active connections are stored in this variable.
connset := proxy.NewConnSet()
prometheus.MustRegister(proxy.NewCollector(connset))
listeners, err := proxy.NewListeners(c)
if err != nil {
logrus.Fatal(err)
}
connSrc, err := listeners.ListenInstances(c.Proxy.BootstrapServers)
if err != nil {
logrus.Fatal(err)
}
proxyClient, err := proxy.NewClient(connset, c, listeners.GetNetAddressMapping, passwordAuthenticator, tokenProvider, tokenInfo)
if err != nil {
logrus.Fatal(err)
}
g.Add(func() error {
logrus.Print("Ready for new connections")
return proxyClient.Run(connSrc)
}, func(error) {
proxyClient.Close()
})
}
{
cancelInterrupt := make(chan struct{})
g.Add(func() error {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
select {
case sig := <-c:
return fmt.Errorf("received signal %s", sig)
case <-cancelInterrupt:
return nil
}
}, func(error) {
close(cancelInterrupt)
})
}
if !c.Http.Disable {
httpListener, err := net.Listen("tcp", c.Http.ListenAddress)
if err != nil {
logrus.Fatal(err)
}
g.Add(func() error {
return http.Serve(httpListener, NewHTTPHandler())
}, func(error) {
httpListener.Close()
})
}
if c.Debug.Enabled {
// https://golang.org/pkg/net/http/pprof/
// https://jvns.ca/blog/2017/09/24/profiling-go-with-pprof/
debugListener, err := net.Listen("tcp", c.Debug.ListenAddress)
if err != nil {
logrus.Fatal(err)
}
g.Add(func() error {
return http.Serve(debugListener, http.DefaultServeMux)
}, func(error) {
debugListener.Close()
})
}
err := g.Run()
logrus.Info("Exit ", err)
}
func NewHTTPHandler() http.Handler {
m := http.NewServeMux()
m.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(
`<html>
<head>
<title>kafka-proxy service</title>
</head>
<body>
<h1>Kafka Proxy</h1>
<p><a href='` + c.Http.MetricsPath + `'>Metrics</a></p>
</body>
</html>`))
})
m.HandleFunc(c.Http.HealthPath, func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`OK`))
})
m.Handle(c.Http.MetricsPath, promhttp.Handler())
return m
}
func SetLogger() {
if c.Log.Format == "json" {
formatter := &logrus.JSONFormatter{
FieldMap: logrus.FieldMap{
logrus.FieldKeyTime: "@timestamp",
logrus.FieldKeyLevel: "@level",
logrus.FieldKeyMsg: "@message",
},
TimestampFormat: time.RFC3339,
}
logrus.SetFormatter(formatter)
} else {
logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true})
}
level, err := logrus.ParseLevel(c.Log.Level)
if err != nil {
logrus.Errorf("Couldn't parse log level: %s", c.Log.Level)
level = logrus.InfoLevel
}
logrus.SetLevel(level)
}
func NewPluginClient(handshakeConfig plugin.HandshakeConfig, plugins map[string]plugin.Plugin, logLevel string, command string, params []string) *plugin.Client {
jsonFormat := false
if c.Log.Format == "json" {
jsonFormat = true
}
logger := hclog.New(&hclog.LoggerOptions{
Output: os.Stdout,
Level: hclog.LevelFromString(logLevel),
Name: "plugin",
JSONFormat: jsonFormat,
TimeFormat: time.RFC3339,
})
return plugin.NewClient(&plugin.ClientConfig{
HandshakeConfig: handshakeConfig,
Plugins: plugins,
Logger: logger,
Cmd: exec.Command(command, params...),
AllowedProtocols: []plugin.Protocol{
plugin.ProtocolNetRPC, plugin.ProtocolGRPC},
})
}