Debian Codegraph @

mysql/azuremysql/azuremysql.go

raw
1 // Copyright 2019 The Go Cloud Development Kit Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // Package azuremysql provides connections to Azure Database for MySQL.
16 // See https://docs.microsoft.com/en-us/azure/mysql.
17 //
18 // # URLs
19 //
20 // For mysql.Open, azuremysql registers for the scheme "azuremysql".
21 // To customize the URL opener, or for more details on the URL format,
22 // see URLOpener.
23 //
24 // See https://gocloud.dev/concepts/urls/ for background information.
25 package azuremysql // import "gocloud.dev/mysql/azuremysql"
26
27 import (
28 "context"
29 "crypto/tls"
30 "database/sql"
31 "database/sql/driver"
32 "fmt"
33 "net/url"
34 "strings"
35
36 "github.com/XSAM/otelsql"
37 "github.com/go-sql-driver/mysql"
38 "gocloud.dev/azure/azuredb"
39 cdkmysql "gocloud.dev/mysql"
40 )
41
42 // URLOpener opens Azure MySQL URLs
43 // like "azuremysql://user:[email protected]/mydb".
44 type URLOpener struct {
45 // CertSource specifies how the opener will obtain the Azure Certificate
46 // Authority. If nil, it will use the default *azuredb.CertFetcher.
47 CertSource azuredb.CertPoolProvider
48 // TraceOpts contains options for OpenTelemetry.
49 TraceOpts []otelsql.Option
50 }
51
52 // Scheme is the URL scheme azuremysql registers its URLOpener under on
53 // mysql.DefaultMux.
54 const Scheme = "azuremysql"
55
56 func init() {
57 cdkmysql.DefaultURLMux().RegisterMySQL(Scheme, &URLOpener{})
58 }
59
60 // OpenMySQLURL opens an encrypted connection to an Azure MySQL database.
61 func (uo *URLOpener) OpenMySQLURL(ctx context.Context, u *url.URL) (*sql.DB, error) {
62 source := uo.CertSource
63 if source == nil {
64 source = new(azuredb.CertFetcher)
65 }
66 if u.Host == "" {
67 return nil, fmt.Errorf("open Azure database: empty endpoint")
68 }
69 password, _ := u.User.Password()
70 c := &connector{
71 cfg: &mysql.Config{
72 Net: "tcp",
73 Addr: u.Host,
74 User: u.User.Username(),
75 Passwd: password,
76 AllowCleartextPasswords: true,
77 AllowNativePasswords: true,
78 DBName: strings.TrimPrefix(u.Path, "/"),
79 },
80 provider: source,
81 sem: make(chan struct{}, 1),
82 ready: make(chan struct{}),
83 }
84 c.sem <- struct{}{}
85 return otelsql.OpenDB(c, uo.TraceOpts...), nil
86 }
87
88 type connector struct {
89 sem chan struct{} // receive to acquire, send to release
90 provider CertPoolProvider // provides the CA certificate pool
91 ready chan struct{} // closed after fetching certs successfully
92 cfg *mysql.Config
93 }
94
95 func (c *connector) Connect(ctx context.Context) (driver.Conn, error) {
96 select {
97 case <-c.sem:
98 certPool, err := c.provider.AzureCertPool(ctx)
99 if err != nil {
100 c.sem <- struct{}{} // release
101 return nil, fmt.Errorf("connect Azure MySql: %v", err)
102 }
103 c.cfg.TLS = &tls.Config{RootCAs: certPool}
104 close(c.ready)
105 // Don't release sem: make it block forever, so this case won't be run again.
106 case <-c.ready:
107 // Already succeeded.
108 case <-ctx.Done():
109 return nil, fmt.Errorf("connect Azure MySql: waiting for certificates: %v", ctx.Err())
110 }
111 inner, err := mysql.NewConnector(c.cfg)
112 if err != nil {
113 return nil, fmt.Errorf("connect Azure MySql: create connector: %v", err)
114 }
115 return inner.Connect(ctx)
116 }
117
118 func (c *connector) Driver() driver.Driver {
119 return mysql.MySQLDriver{}
120 }
121
122 var tlsConfigCounter uint32
123
124 // A CertPoolProvider obtains a certificate pool that contains the Azure CA certificate.
125 type CertPoolProvider = azuredb.CertPoolProvider
126
127 // CertFetcher is a default CertPoolProvider that can fetch CA certificates from
128 // any publicly accessible URI or File.
129 type CertFetcher = azuredb.CertFetcher