-
Notifications
You must be signed in to change notification settings - Fork 4
/
rdsmysql.go
53 lines (45 loc) · 1.59 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
53
// Package rdsmysql is a MySQL SQL driver that allows [IAM Database Authentication for Amazon RDS]
// and [IAM Database Authentication for Amazon Aurora].
// It also supports connecting with [the RDS proxy using IAM authentication].
//
// [IAM Database Authentication for Amazon RDS]: https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.html
// [IAM Database Authentication for Amazon Aurora]: https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/UsingWithRDS.IAMDBAuth.html
// [the RDS proxy using IAM authentication]: https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/rds-proxy.html#rds-proxy-connecting-iam
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.
type Driver struct {
// Session is an AWS session
Session *session.Session
}
var _ driver.Driver = (*Driver)(nil)
var _ driver.DriverContext = (*Driver)(nil)
// Open opens a 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 a new connector.
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("rdsmysql: fail to parse dns: %w", err)
}
return &Connector{
Session: d.Session,
Config: config,
}, nil
}