vitess.io/vitess@v0.16.2/go/mysql/conn_params.go (about) 1 /* 2 Copyright 2019 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package mysql 18 19 import ( 20 "vitess.io/vitess/go/vt/vttls" 21 ) 22 23 // ConnParams contains all the parameters to use to connect to mysql. 24 type ConnParams struct { 25 Host string `json:"host"` 26 Port int `json:"port"` 27 Uname string `json:"uname"` 28 Pass string `json:"pass"` 29 DbName string `json:"dbname"` 30 UnixSocket string `json:"unix_socket"` 31 Charset string `json:"charset"` 32 Flags uint64 `json:"flags"` 33 Flavor string `json:"flavor,omitempty"` 34 35 // The following SSL flags control the SSL behavior. 36 // 37 // Not setting this value implies preferred mode unless 38 // the CapabilityClientSSL bit is set in db_flags. In the 39 // flag is set, it ends up equivalent to verify_identity mode. 40 SslMode vttls.SslMode `json:"ssl_mode"` 41 SslCa string `json:"ssl_ca"` 42 SslCaPath string `json:"ssl_ca_path"` 43 SslCert string `json:"ssl_cert"` 44 SslCrl string `json:"ssl_crl"` 45 SslKey string `json:"ssl_key"` 46 TLSMinVersion string `json:"tls_min_version"` 47 ServerName string `json:"server_name"` 48 ConnectTimeoutMs uint64 `json:"connect_timeout_ms"` 49 50 // The following is only set to force the client to connect without 51 // using CapabilityClientDeprecateEOF 52 DisableClientDeprecateEOF bool 53 54 // EnableQueryInfo sets whether the results from queries performed by this 55 // connection should include the 'info' field that MySQL usually returns. This 'info' 56 // field usually contains a human-readable text description of the executed query 57 // for informative purposes. It has no programmatic value. Returning this field is 58 // disabled by default. 59 EnableQueryInfo bool 60 } 61 62 // EnableSSL will set the right flag on the parameters. 63 func (cp *ConnParams) EnableSSL() { 64 cp.SslMode = vttls.VerifyIdentity 65 } 66 67 // SslEnabled returns if SSL is enabled. If the effective 68 // ssl mode is preferred, it checks the unix socket and 69 // hostname to see if we're not connecting to local MySQL. 70 func (cp *ConnParams) SslEnabled() bool { 71 mode := cp.EffectiveSslMode() 72 // Follow MySQL behavior to not enable SSL if it's 73 // preferred but we're using a Unix socket. 74 if mode == vttls.Preferred && cp.UnixSocket != "" { 75 return false 76 } 77 return mode != vttls.Disabled 78 } 79 80 // EnableClientFoundRows sets the flag for CLIENT_FOUND_ROWS. 81 func (cp *ConnParams) EnableClientFoundRows() { 82 cp.Flags |= CapabilityClientFoundRows 83 } 84 85 // SslRequired returns whether the connection parameters 86 // define that SSL is a requirement. If SslMode is set, it uses 87 // that to determine this, if it's not set it falls back to 88 // the legacy db_flags behavior. 89 func (cp *ConnParams) SslRequired() bool { 90 mode := cp.EffectiveSslMode() 91 return mode != vttls.Disabled && mode != vttls.Preferred 92 } 93 94 // EffectiveSslMode computes the effective SslMode. If SslMode 95 // is explicitly set, it uses that to determine this, if it's 96 // not set it falls back to the legacy db_flags behavior. 97 func (cp *ConnParams) EffectiveSslMode() vttls.SslMode { 98 if cp.SslMode == "" { 99 if (cp.Flags & CapabilityClientSSL) > 0 { 100 return vttls.VerifyIdentity 101 } 102 // Old behavior is Disabled so keep that for now. 103 return vttls.Disabled 104 } 105 return cp.SslMode 106 }