forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 2
/
plugin_mysql_server.go
260 lines (226 loc) · 9.07 KB
/
plugin_mysql_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
/*
Copyright 2017 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreedto in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package vtqueryserver
import (
"flag"
"fmt"
"net"
"os"
"syscall"
"golang.org/x/net/context"
"github.com/xsec-lab/go/mysql"
"github.com/xsec-lab/go/sqltypes"
"github.com/xsec-lab/go/vt/callerid"
"github.com/xsec-lab/go/vt/log"
"github.com/xsec-lab/go/vt/mysqlproxy"
"github.com/xsec-lab/go/vt/servenv"
"github.com/xsec-lab/go/vt/vttls"
querypb "github.com/xsec-lab/go/vt/proto/query"
)
var (
mysqlServerPort = flag.Int("mysqlproxy_server_port", -1, "If set, also listen for MySQL binary protocol connections on this port.")
mysqlServerBindAddress = flag.String("mysqlproxy_server_bind_address", "", "Binds on this address when listening to MySQL binary protocol. Useful to restrict listening to 'localhost' only for instance.")
mysqlServerSocketPath = flag.String("mysqlproxy_server_socket_path", "", "This option specifies the Unix socket file to use when listening for local connections. By default it will be empty and it won't listen to a unix socket")
mysqlAuthServerImpl = flag.String("mysql_auth_server_impl", "static", "Which auth server implementation to use.")
mysqlAllowClearTextWithoutTLS = flag.Bool("mysql_allow_clear_text_without_tls", false, "If set, the server will allow the use of a clear text password over non-SSL connections.")
mysqlSslCert = flag.String("mysqlproxy_server_ssl_cert", "", "Path to the ssl cert for mysql server plugin SSL")
mysqlSslKey = flag.String("mysqlproxy_server_ssl_key", "", "Path to ssl key for mysql server plugin SSL")
mysqlSslCa = flag.String("mysqlproxy_server_ssl_ca", "", "Path to ssl CA for mysql server plugin SSL. If specified, server will require and validate client certs.")
mysqlSlowConnectWarnThreshold = flag.Duration("mysqlproxy_slow_connect_warn_threshold", 0, "Warn if it takes more than the given threshold for a mysql connection to establish")
mysqlConnReadTimeout = flag.Duration("mysql_server_read_timeout", 0, "connection read timeout")
mysqlConnWriteTimeout = flag.Duration("mysql_server_write_timeout", 0, "connection write timeout")
mysqlQueryTimeout = flag.Duration("mysql_server_query_timeout", 0, "mysql query timeout")
)
// proxyHandler implements the Listener interface.
// It stores the Session in the ClientData of a Connection, if a transaction
// is in progress.
type proxyHandler struct {
mp *mysqlproxy.Proxy
}
func newProxyHandler(mp *mysqlproxy.Proxy) *proxyHandler {
return &proxyHandler{
mp: mp,
}
}
func (mh *proxyHandler) NewConnection(c *mysql.Conn) {
}
func (mh *proxyHandler) ConnectionClosed(c *mysql.Conn) {
// Rollback if there is an ongoing transaction. Ignore error.
var ctx context.Context
var cancel context.CancelFunc
if *mysqlQueryTimeout != 0 {
ctx, cancel = context.WithTimeout(context.Background(), *mysqlQueryTimeout)
defer cancel()
} else {
ctx = context.Background()
}
session, _ := c.ClientData.(*mysqlproxy.ProxySession)
if session != nil && session.TransactionID != 0 {
_ = mh.mp.Rollback(ctx, session)
}
}
func (mh *proxyHandler) ComQuery(c *mysql.Conn, query string, callback func(*sqltypes.Result) error) error {
var ctx context.Context
var cancel context.CancelFunc
if *mysqlQueryTimeout != 0 {
ctx, cancel = context.WithTimeout(context.Background(), *mysqlQueryTimeout)
defer cancel()
} else {
ctx = context.Background()
}
// Fill in the ImmediateCallerID with the UserData returned by
// the AuthServer plugin for that user. If nothing was
// returned, use the User. This lets the plugin map a MySQL
// user used for authentication to a Vitess User used for
// Table ACLs and Vitess authentication in general.
im := c.UserData.Get()
ef := callerid.NewEffectiveCallerID(
c.User, /* principal: who */
c.RemoteAddr().String(), /* component: running client process */
"mysqlproxy MySQL Connector" /* subcomponent: part of the client */)
ctx = callerid.NewContext(ctx, ef, im)
session, _ := c.ClientData.(*mysqlproxy.ProxySession)
if session == nil {
session = &mysqlproxy.ProxySession{
Options: &querypb.ExecuteOptions{
IncludedFields: querypb.ExecuteOptions_ALL,
},
Autocommit: true,
}
if c.Capabilities&mysql.CapabilityClientFoundRows != 0 {
session.Options.ClientFoundRows = true
}
}
if c.SchemaName != "" {
session.TargetString = c.SchemaName
}
session, result, err := mh.mp.Execute(ctx, session, query, make(map[string]*querypb.BindVariable))
c.ClientData = session
err = mysql.NewSQLErrorFromError(err)
if err != nil {
return err
}
return callback(result)
}
func (mh *proxyHandler) WarningCount(c *mysql.Conn) uint16 {
return 0
}
var mysqlListener *mysql.Listener
var mysqlUnixListener *mysql.Listener
// initiMySQLProtocol starts the mysql protocol.
// It should be called only once in a process.
func initMySQLProtocol() {
log.Infof("initializing mysql protocol")
// Flag is not set, just return.
if *mysqlServerPort < 0 && *mysqlServerSocketPath == "" {
return
}
// If no mysqlproxy was created, just return.
if mysqlProxy == nil {
log.Fatalf("mysqlProxy not initialized")
return
}
// Initialize registered AuthServer implementations (or other plugins)
for _, initFn := range pluginInitializers {
initFn()
}
authServer := mysql.GetAuthServer(*mysqlAuthServerImpl)
// Create a Listener.
var err error
mh := newProxyHandler(mysqlProxy)
if *mysqlServerPort >= 0 {
mysqlListener, err = mysql.NewListener("tcp", net.JoinHostPort(*mysqlServerBindAddress, fmt.Sprintf("%v", *mysqlServerPort)), authServer, mh, *mysqlConnReadTimeout, *mysqlConnWriteTimeout)
if err != nil {
log.Exitf("mysql.NewListener failed: %v", err)
}
if *mysqlSslCert != "" && *mysqlSslKey != "" {
mysqlListener.TLSConfig, err = vttls.ServerConfig(*mysqlSslCert, *mysqlSslKey, *mysqlSslCa)
if err != nil {
log.Exitf("grpcutils.TLSServerConfig failed: %v", err)
return
}
}
mysqlListener.AllowClearTextWithoutTLS = *mysqlAllowClearTextWithoutTLS
// Check for the connection threshold
if *mysqlSlowConnectWarnThreshold != 0 {
log.Infof("setting mysql slow connection threshold to %v", mysqlSlowConnectWarnThreshold)
mysqlListener.SlowConnectWarnThreshold = *mysqlSlowConnectWarnThreshold
}
// Start listening for tcp
go mysqlListener.Accept()
log.Infof("listening on %s:%d", *mysqlServerBindAddress, *mysqlServerPort)
}
if *mysqlServerSocketPath != "" {
// Let's create this unix socket with permissions to all users. In this way,
// clients can connect to mysqlproxy mysql server without being mysqlproxy user
oldMask := syscall.Umask(000)
mysqlUnixListener, err = newMysqlUnixSocket(*mysqlServerSocketPath, authServer, mh)
_ = syscall.Umask(oldMask)
if err != nil {
log.Exitf("mysql.NewListener failed: %v", err)
return
}
// Listen for unix socket
go mysqlUnixListener.Accept()
}
}
// newMysqlUnixSocket creates a new unix socket mysql listener. If a socket file already exists, attempts
// to clean it up.
func newMysqlUnixSocket(address string, authServer mysql.AuthServer, handler mysql.Handler) (*mysql.Listener, error) {
listener, err := mysql.NewListener("unix", address, authServer, handler, *mysqlConnReadTimeout, *mysqlConnWriteTimeout)
switch err := err.(type) {
case nil:
return listener, nil
case *net.OpError:
log.Warningf("Found existent socket when trying to create new unix mysql listener: %s, attempting to clean up", address)
// err.Op should never be different from listen, just being extra careful
// in case in the future other errors are returned here
if err.Op != "listen" {
return nil, err
}
_, dialErr := net.Dial("unix", address)
if dialErr == nil {
log.Errorf("Existent socket '%s' is still accepting connections, aborting", address)
return nil, err
}
removeFileErr := os.Remove(address)
if removeFileErr != nil {
log.Errorf("Couldn't remove existent socket file: %s", address)
return nil, err
}
listener, listenerErr := mysql.NewListener("unix", address, authServer, handler, *mysqlConnReadTimeout, *mysqlConnWriteTimeout)
return listener, listenerErr
default:
return nil, err
}
}
func shutdownMySQLProtocol() {
log.Infof("shutting down mysql protocol")
if mysqlListener != nil {
mysqlListener.Close()
mysqlListener = nil
}
if mysqlUnixListener != nil {
mysqlUnixListener.Close()
mysqlUnixListener = nil
}
}
func init() {
servenv.OnRun(initMySQLProtocol)
servenv.OnTerm(shutdownMySQLProtocol)
}
var pluginInitializers []func()
// RegisterPluginInitializer lets plugins register themselves to be init'ed at servenv.OnRun-time
func RegisterPluginInitializer(initializer func()) {
pluginInitializers = append(pluginInitializers, initializer)
}