dsn.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. // Go MySQL Driver - A MySQL-Driver for Go's database/sql package
  2. //
  3. // Copyright 2016 The Go-MySQL-Driver Authors. All rights reserved.
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public
  6. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  7. // You can obtain one at http://mozilla.org/MPL/2.0/.
  8. package mysql
  9. import (
  10. "bytes"
  11. "context"
  12. "crypto/rsa"
  13. "crypto/tls"
  14. "errors"
  15. "fmt"
  16. "math/big"
  17. "net"
  18. "net/url"
  19. "sort"
  20. "strconv"
  21. "strings"
  22. "time"
  23. )
  24. var (
  25. errInvalidDSNUnescaped = errors.New("invalid DSN: did you forget to escape a param value?")
  26. errInvalidDSNAddr = errors.New("invalid DSN: network address not terminated (missing closing brace)")
  27. errInvalidDSNNoSlash = errors.New("invalid DSN: missing the slash separating the database name")
  28. errInvalidDSNUnsafeCollation = errors.New("invalid DSN: interpolateParams can not be used with unsafe collations")
  29. )
  30. // Config is a configuration parsed from a DSN string.
  31. // If a new Config is created instead of being parsed from a DSN string,
  32. // the NewConfig function should be used, which sets default values.
  33. type Config struct {
  34. // non boolean fields
  35. User string // Username
  36. Passwd string // Password (requires User)
  37. Net string // Network (e.g. "tcp", "tcp6", "unix". default: "tcp")
  38. Addr string // Address (default: "127.0.0.1:3306" for "tcp" and "/tmp/mysql.sock" for "unix")
  39. DBName string // Database name
  40. Params map[string]string // Connection parameters
  41. ConnectionAttributes string // Connection Attributes, comma-delimited string of user-defined "key:value" pairs
  42. Collation string // Connection collation
  43. Loc *time.Location // Location for time.Time values
  44. MaxAllowedPacket int // Max packet size allowed
  45. ServerPubKey string // Server public key name
  46. TLSConfig string // TLS configuration name
  47. TLS *tls.Config // TLS configuration, its priority is higher than TLSConfig
  48. Timeout time.Duration // Dial timeout
  49. ReadTimeout time.Duration // I/O read timeout
  50. WriteTimeout time.Duration // I/O write timeout
  51. Logger Logger // Logger
  52. // boolean fields
  53. AllowAllFiles bool // Allow all files to be used with LOAD DATA LOCAL INFILE
  54. AllowCleartextPasswords bool // Allows the cleartext client side plugin
  55. AllowFallbackToPlaintext bool // Allows fallback to unencrypted connection if server does not support TLS
  56. AllowNativePasswords bool // Allows the native password authentication method
  57. AllowOldPasswords bool // Allows the old insecure password method
  58. CheckConnLiveness bool // Check connections for liveness before using them
  59. ClientFoundRows bool // Return number of matching rows instead of rows changed
  60. ColumnsWithAlias bool // Prepend table alias to column names
  61. InterpolateParams bool // Interpolate placeholders into query string
  62. MultiStatements bool // Allow multiple statements in one query
  63. ParseTime bool // Parse time values to time.Time
  64. RejectReadOnly bool // Reject read-only connections
  65. // unexported fields. new options should be come here
  66. beforeConnect func(context.Context, *Config) error // Invoked before a connection is established
  67. pubKey *rsa.PublicKey // Server public key
  68. timeTruncate time.Duration // Truncate time.Time values to the specified duration
  69. }
  70. // Functional Options Pattern
  71. // https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis
  72. type Option func(*Config) error
  73. // NewConfig creates a new Config and sets default values.
  74. func NewConfig() *Config {
  75. cfg := &Config{
  76. Loc: time.UTC,
  77. MaxAllowedPacket: defaultMaxAllowedPacket,
  78. Logger: defaultLogger,
  79. AllowNativePasswords: true,
  80. CheckConnLiveness: true,
  81. }
  82. return cfg
  83. }
  84. // Apply applies the given options to the Config object.
  85. func (c *Config) Apply(opts ...Option) error {
  86. for _, opt := range opts {
  87. err := opt(c)
  88. if err != nil {
  89. return err
  90. }
  91. }
  92. return nil
  93. }
  94. // TimeTruncate sets the time duration to truncate time.Time values in
  95. // query parameters.
  96. func TimeTruncate(d time.Duration) Option {
  97. return func(cfg *Config) error {
  98. cfg.timeTruncate = d
  99. return nil
  100. }
  101. }
  102. // BeforeConnect sets the function to be invoked before a connection is established.
  103. func BeforeConnect(fn func(context.Context, *Config) error) Option {
  104. return func(cfg *Config) error {
  105. cfg.beforeConnect = fn
  106. return nil
  107. }
  108. }
  109. func (cfg *Config) Clone() *Config {
  110. cp := *cfg
  111. if cp.TLS != nil {
  112. cp.TLS = cfg.TLS.Clone()
  113. }
  114. if len(cp.Params) > 0 {
  115. cp.Params = make(map[string]string, len(cfg.Params))
  116. for k, v := range cfg.Params {
  117. cp.Params[k] = v
  118. }
  119. }
  120. if cfg.pubKey != nil {
  121. cp.pubKey = &rsa.PublicKey{
  122. N: new(big.Int).Set(cfg.pubKey.N),
  123. E: cfg.pubKey.E,
  124. }
  125. }
  126. return &cp
  127. }
  128. func (cfg *Config) normalize() error {
  129. if cfg.InterpolateParams && cfg.Collation != "" && unsafeCollations[cfg.Collation] {
  130. return errInvalidDSNUnsafeCollation
  131. }
  132. // Set default network if empty
  133. if cfg.Net == "" {
  134. cfg.Net = "tcp"
  135. }
  136. // Set default address if empty
  137. if cfg.Addr == "" {
  138. switch cfg.Net {
  139. case "tcp":
  140. cfg.Addr = "127.0.0.1:3306"
  141. case "unix":
  142. cfg.Addr = "/tmp/mysql.sock"
  143. default:
  144. return errors.New("default addr for network '" + cfg.Net + "' unknown")
  145. }
  146. } else if cfg.Net == "tcp" {
  147. cfg.Addr = ensureHavePort(cfg.Addr)
  148. }
  149. if cfg.TLS == nil {
  150. switch cfg.TLSConfig {
  151. case "false", "":
  152. // don't set anything
  153. case "true":
  154. cfg.TLS = &tls.Config{}
  155. case "skip-verify":
  156. cfg.TLS = &tls.Config{InsecureSkipVerify: true}
  157. case "preferred":
  158. cfg.TLS = &tls.Config{InsecureSkipVerify: true}
  159. cfg.AllowFallbackToPlaintext = true
  160. default:
  161. cfg.TLS = getTLSConfigClone(cfg.TLSConfig)
  162. if cfg.TLS == nil {
  163. return errors.New("invalid value / unknown config name: " + cfg.TLSConfig)
  164. }
  165. }
  166. }
  167. if cfg.TLS != nil && cfg.TLS.ServerName == "" && !cfg.TLS.InsecureSkipVerify {
  168. host, _, err := net.SplitHostPort(cfg.Addr)
  169. if err == nil {
  170. cfg.TLS.ServerName = host
  171. }
  172. }
  173. if cfg.ServerPubKey != "" {
  174. cfg.pubKey = getServerPubKey(cfg.ServerPubKey)
  175. if cfg.pubKey == nil {
  176. return errors.New("invalid value / unknown server pub key name: " + cfg.ServerPubKey)
  177. }
  178. }
  179. if cfg.Logger == nil {
  180. cfg.Logger = defaultLogger
  181. }
  182. return nil
  183. }
  184. func writeDSNParam(buf *bytes.Buffer, hasParam *bool, name, value string) {
  185. buf.Grow(1 + len(name) + 1 + len(value))
  186. if !*hasParam {
  187. *hasParam = true
  188. buf.WriteByte('?')
  189. } else {
  190. buf.WriteByte('&')
  191. }
  192. buf.WriteString(name)
  193. buf.WriteByte('=')
  194. buf.WriteString(value)
  195. }
  196. // FormatDSN formats the given Config into a DSN string which can be passed to
  197. // the driver.
  198. //
  199. // Note: use [NewConnector] and [database/sql.OpenDB] to open a connection from a [*Config].
  200. func (cfg *Config) FormatDSN() string {
  201. var buf bytes.Buffer
  202. // [username[:password]@]
  203. if len(cfg.User) > 0 {
  204. buf.WriteString(cfg.User)
  205. if len(cfg.Passwd) > 0 {
  206. buf.WriteByte(':')
  207. buf.WriteString(cfg.Passwd)
  208. }
  209. buf.WriteByte('@')
  210. }
  211. // [protocol[(address)]]
  212. if len(cfg.Net) > 0 {
  213. buf.WriteString(cfg.Net)
  214. if len(cfg.Addr) > 0 {
  215. buf.WriteByte('(')
  216. buf.WriteString(cfg.Addr)
  217. buf.WriteByte(')')
  218. }
  219. }
  220. // /dbname
  221. buf.WriteByte('/')
  222. buf.WriteString(url.PathEscape(cfg.DBName))
  223. // [?param1=value1&...&paramN=valueN]
  224. hasParam := false
  225. if cfg.AllowAllFiles {
  226. hasParam = true
  227. buf.WriteString("?allowAllFiles=true")
  228. }
  229. if cfg.AllowCleartextPasswords {
  230. writeDSNParam(&buf, &hasParam, "allowCleartextPasswords", "true")
  231. }
  232. if cfg.AllowFallbackToPlaintext {
  233. writeDSNParam(&buf, &hasParam, "allowFallbackToPlaintext", "true")
  234. }
  235. if !cfg.AllowNativePasswords {
  236. writeDSNParam(&buf, &hasParam, "allowNativePasswords", "false")
  237. }
  238. if cfg.AllowOldPasswords {
  239. writeDSNParam(&buf, &hasParam, "allowOldPasswords", "true")
  240. }
  241. if !cfg.CheckConnLiveness {
  242. writeDSNParam(&buf, &hasParam, "checkConnLiveness", "false")
  243. }
  244. if cfg.ClientFoundRows {
  245. writeDSNParam(&buf, &hasParam, "clientFoundRows", "true")
  246. }
  247. if col := cfg.Collation; col != "" {
  248. writeDSNParam(&buf, &hasParam, "collation", col)
  249. }
  250. if cfg.ColumnsWithAlias {
  251. writeDSNParam(&buf, &hasParam, "columnsWithAlias", "true")
  252. }
  253. if cfg.InterpolateParams {
  254. writeDSNParam(&buf, &hasParam, "interpolateParams", "true")
  255. }
  256. if cfg.Loc != time.UTC && cfg.Loc != nil {
  257. writeDSNParam(&buf, &hasParam, "loc", url.QueryEscape(cfg.Loc.String()))
  258. }
  259. if cfg.MultiStatements {
  260. writeDSNParam(&buf, &hasParam, "multiStatements", "true")
  261. }
  262. if cfg.ParseTime {
  263. writeDSNParam(&buf, &hasParam, "parseTime", "true")
  264. }
  265. if cfg.timeTruncate > 0 {
  266. writeDSNParam(&buf, &hasParam, "timeTruncate", cfg.timeTruncate.String())
  267. }
  268. if cfg.ReadTimeout > 0 {
  269. writeDSNParam(&buf, &hasParam, "readTimeout", cfg.ReadTimeout.String())
  270. }
  271. if cfg.RejectReadOnly {
  272. writeDSNParam(&buf, &hasParam, "rejectReadOnly", "true")
  273. }
  274. if len(cfg.ServerPubKey) > 0 {
  275. writeDSNParam(&buf, &hasParam, "serverPubKey", url.QueryEscape(cfg.ServerPubKey))
  276. }
  277. if cfg.Timeout > 0 {
  278. writeDSNParam(&buf, &hasParam, "timeout", cfg.Timeout.String())
  279. }
  280. if len(cfg.TLSConfig) > 0 {
  281. writeDSNParam(&buf, &hasParam, "tls", url.QueryEscape(cfg.TLSConfig))
  282. }
  283. if cfg.WriteTimeout > 0 {
  284. writeDSNParam(&buf, &hasParam, "writeTimeout", cfg.WriteTimeout.String())
  285. }
  286. if cfg.MaxAllowedPacket != defaultMaxAllowedPacket {
  287. writeDSNParam(&buf, &hasParam, "maxAllowedPacket", strconv.Itoa(cfg.MaxAllowedPacket))
  288. }
  289. // other params
  290. if cfg.Params != nil {
  291. var params []string
  292. for param := range cfg.Params {
  293. params = append(params, param)
  294. }
  295. sort.Strings(params)
  296. for _, param := range params {
  297. writeDSNParam(&buf, &hasParam, param, url.QueryEscape(cfg.Params[param]))
  298. }
  299. }
  300. return buf.String()
  301. }
  302. // ParseDSN parses the DSN string to a Config
  303. func ParseDSN(dsn string) (cfg *Config, err error) {
  304. // New config with some default values
  305. cfg = NewConfig()
  306. // [user[:password]@][net[(addr)]]/dbname[?param1=value1&paramN=valueN]
  307. // Find the last '/' (since the password or the net addr might contain a '/')
  308. foundSlash := false
  309. for i := len(dsn) - 1; i >= 0; i-- {
  310. if dsn[i] == '/' {
  311. foundSlash = true
  312. var j, k int
  313. // left part is empty if i <= 0
  314. if i > 0 {
  315. // [username[:password]@][protocol[(address)]]
  316. // Find the last '@' in dsn[:i]
  317. for j = i; j >= 0; j-- {
  318. if dsn[j] == '@' {
  319. // username[:password]
  320. // Find the first ':' in dsn[:j]
  321. for k = 0; k < j; k++ {
  322. if dsn[k] == ':' {
  323. cfg.Passwd = dsn[k+1 : j]
  324. break
  325. }
  326. }
  327. cfg.User = dsn[:k]
  328. break
  329. }
  330. }
  331. // [protocol[(address)]]
  332. // Find the first '(' in dsn[j+1:i]
  333. for k = j + 1; k < i; k++ {
  334. if dsn[k] == '(' {
  335. // dsn[i-1] must be == ')' if an address is specified
  336. if dsn[i-1] != ')' {
  337. if strings.ContainsRune(dsn[k+1:i], ')') {
  338. return nil, errInvalidDSNUnescaped
  339. }
  340. return nil, errInvalidDSNAddr
  341. }
  342. cfg.Addr = dsn[k+1 : i-1]
  343. break
  344. }
  345. }
  346. cfg.Net = dsn[j+1 : k]
  347. }
  348. // dbname[?param1=value1&...&paramN=valueN]
  349. // Find the first '?' in dsn[i+1:]
  350. for j = i + 1; j < len(dsn); j++ {
  351. if dsn[j] == '?' {
  352. if err = parseDSNParams(cfg, dsn[j+1:]); err != nil {
  353. return
  354. }
  355. break
  356. }
  357. }
  358. dbname := dsn[i+1 : j]
  359. if cfg.DBName, err = url.PathUnescape(dbname); err != nil {
  360. return nil, fmt.Errorf("invalid dbname %q: %w", dbname, err)
  361. }
  362. break
  363. }
  364. }
  365. if !foundSlash && len(dsn) > 0 {
  366. return nil, errInvalidDSNNoSlash
  367. }
  368. if err = cfg.normalize(); err != nil {
  369. return nil, err
  370. }
  371. return
  372. }
  373. // parseDSNParams parses the DSN "query string"
  374. // Values must be url.QueryEscape'ed
  375. func parseDSNParams(cfg *Config, params string) (err error) {
  376. for _, v := range strings.Split(params, "&") {
  377. key, value, found := strings.Cut(v, "=")
  378. if !found {
  379. continue
  380. }
  381. // cfg params
  382. switch key {
  383. // Disable INFILE allowlist / enable all files
  384. case "allowAllFiles":
  385. var isBool bool
  386. cfg.AllowAllFiles, isBool = readBool(value)
  387. if !isBool {
  388. return errors.New("invalid bool value: " + value)
  389. }
  390. // Use cleartext authentication mode (MySQL 5.5.10+)
  391. case "allowCleartextPasswords":
  392. var isBool bool
  393. cfg.AllowCleartextPasswords, isBool = readBool(value)
  394. if !isBool {
  395. return errors.New("invalid bool value: " + value)
  396. }
  397. // Allow fallback to unencrypted connection if server does not support TLS
  398. case "allowFallbackToPlaintext":
  399. var isBool bool
  400. cfg.AllowFallbackToPlaintext, isBool = readBool(value)
  401. if !isBool {
  402. return errors.New("invalid bool value: " + value)
  403. }
  404. // Use native password authentication
  405. case "allowNativePasswords":
  406. var isBool bool
  407. cfg.AllowNativePasswords, isBool = readBool(value)
  408. if !isBool {
  409. return errors.New("invalid bool value: " + value)
  410. }
  411. // Use old authentication mode (pre MySQL 4.1)
  412. case "allowOldPasswords":
  413. var isBool bool
  414. cfg.AllowOldPasswords, isBool = readBool(value)
  415. if !isBool {
  416. return errors.New("invalid bool value: " + value)
  417. }
  418. // Check connections for Liveness before using them
  419. case "checkConnLiveness":
  420. var isBool bool
  421. cfg.CheckConnLiveness, isBool = readBool(value)
  422. if !isBool {
  423. return errors.New("invalid bool value: " + value)
  424. }
  425. // Switch "rowsAffected" mode
  426. case "clientFoundRows":
  427. var isBool bool
  428. cfg.ClientFoundRows, isBool = readBool(value)
  429. if !isBool {
  430. return errors.New("invalid bool value: " + value)
  431. }
  432. // Collation
  433. case "collation":
  434. cfg.Collation = value
  435. case "columnsWithAlias":
  436. var isBool bool
  437. cfg.ColumnsWithAlias, isBool = readBool(value)
  438. if !isBool {
  439. return errors.New("invalid bool value: " + value)
  440. }
  441. // Compression
  442. case "compress":
  443. return errors.New("compression not implemented yet")
  444. // Enable client side placeholder substitution
  445. case "interpolateParams":
  446. var isBool bool
  447. cfg.InterpolateParams, isBool = readBool(value)
  448. if !isBool {
  449. return errors.New("invalid bool value: " + value)
  450. }
  451. // Time Location
  452. case "loc":
  453. if value, err = url.QueryUnescape(value); err != nil {
  454. return
  455. }
  456. cfg.Loc, err = time.LoadLocation(value)
  457. if err != nil {
  458. return
  459. }
  460. // multiple statements in one query
  461. case "multiStatements":
  462. var isBool bool
  463. cfg.MultiStatements, isBool = readBool(value)
  464. if !isBool {
  465. return errors.New("invalid bool value: " + value)
  466. }
  467. // time.Time parsing
  468. case "parseTime":
  469. var isBool bool
  470. cfg.ParseTime, isBool = readBool(value)
  471. if !isBool {
  472. return errors.New("invalid bool value: " + value)
  473. }
  474. // time.Time truncation
  475. case "timeTruncate":
  476. cfg.timeTruncate, err = time.ParseDuration(value)
  477. if err != nil {
  478. return fmt.Errorf("invalid timeTruncate value: %v, error: %w", value, err)
  479. }
  480. // I/O read Timeout
  481. case "readTimeout":
  482. cfg.ReadTimeout, err = time.ParseDuration(value)
  483. if err != nil {
  484. return
  485. }
  486. // Reject read-only connections
  487. case "rejectReadOnly":
  488. var isBool bool
  489. cfg.RejectReadOnly, isBool = readBool(value)
  490. if !isBool {
  491. return errors.New("invalid bool value: " + value)
  492. }
  493. // Server public key
  494. case "serverPubKey":
  495. name, err := url.QueryUnescape(value)
  496. if err != nil {
  497. return fmt.Errorf("invalid value for server pub key name: %v", err)
  498. }
  499. cfg.ServerPubKey = name
  500. // Strict mode
  501. case "strict":
  502. panic("strict mode has been removed. See https://github.com/go-sql-driver/mysql/wiki/strict-mode")
  503. // Dial Timeout
  504. case "timeout":
  505. cfg.Timeout, err = time.ParseDuration(value)
  506. if err != nil {
  507. return
  508. }
  509. // TLS-Encryption
  510. case "tls":
  511. boolValue, isBool := readBool(value)
  512. if isBool {
  513. if boolValue {
  514. cfg.TLSConfig = "true"
  515. } else {
  516. cfg.TLSConfig = "false"
  517. }
  518. } else if vl := strings.ToLower(value); vl == "skip-verify" || vl == "preferred" {
  519. cfg.TLSConfig = vl
  520. } else {
  521. name, err := url.QueryUnescape(value)
  522. if err != nil {
  523. return fmt.Errorf("invalid value for TLS config name: %v", err)
  524. }
  525. cfg.TLSConfig = name
  526. }
  527. // I/O write Timeout
  528. case "writeTimeout":
  529. cfg.WriteTimeout, err = time.ParseDuration(value)
  530. if err != nil {
  531. return
  532. }
  533. case "maxAllowedPacket":
  534. cfg.MaxAllowedPacket, err = strconv.Atoi(value)
  535. if err != nil {
  536. return
  537. }
  538. // Connection attributes
  539. case "connectionAttributes":
  540. connectionAttributes, err := url.QueryUnescape(value)
  541. if err != nil {
  542. return fmt.Errorf("invalid connectionAttributes value: %v", err)
  543. }
  544. cfg.ConnectionAttributes = connectionAttributes
  545. default:
  546. // lazy init
  547. if cfg.Params == nil {
  548. cfg.Params = make(map[string]string)
  549. }
  550. if cfg.Params[key], err = url.QueryUnescape(value); err != nil {
  551. return
  552. }
  553. }
  554. }
  555. return
  556. }
  557. func ensureHavePort(addr string) string {
  558. if _, _, err := net.SplitHostPort(addr); err != nil {
  559. return net.JoinHostPort(addr, "3306")
  560. }
  561. return addr
  562. }