/
rdsmysql.go
52 lines (44 loc) · 1.36 KB
/
rdsmysql.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
// Package rdsmysql is a SQL driver that allows IAM Database Authentication for Amazon RDS and Amazon Aurora.
//
// https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.html
// https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/UsingWithRDS.IAMDBAuth.html
package rdsmysql
import (
"context"
"database/sql/driver"
"errors"
"fmt"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/go-sql-driver/mysql"
)
// Driver is a mysql driver using IAM DB Auth.
//
// https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.html
// https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/UsingWithRDS.IAMDBAuth.html
type Driver struct {
Session *session.Session
}
var _ driver.Driver = &Driver{}
var _ driver.DriverContext = &Driver{}
// Open opens new connection.
func (d *Driver) Open(name string) (driver.Conn, error) {
c, err := d.OpenConnector(name)
if err != nil {
return nil, err
}
return c.Connect(context.Background())
}
// OpenConnector opens new connection.
func (d *Driver) OpenConnector(name string) (driver.Connector, error) {
if d.Session.Config.Region == nil {
return nil, errors.New("rdsmysql: region is missing")
}
config, err := mysql.ParseDSN(name)
if err != nil {
return nil, fmt.Errorf("fail to parse dns: %w", err)
}
return &Connector{
Session: d.Session,
Config: config,
}, nil
}