Documentation
¶
Overview ¶
Package mysql provides a token provider for AWS RDS IAM database authentication.
- RDS IAM authentication tokens are valid for 15 minutes.
- IAM database authentication throttles connections at 200 new connections per second.
- Connections that use the same authentication token are not throttled. It is recommended that you reuse authentication tokens when possible.
The token provider is safe for concurrent use.
Example usage:
import (
"database/sql"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/go-sql-driver/mysql"
rdsauth "github.com/errm/rdsauth/mysql"
)
func main() {
// Load AWS configuration
cfg, _ := config.LoadDefaultConfig(context.TODO())
// Configure MySQL connection
mysqlConfig := mysql.NewConfig()
mysqlConfig.User = "dbuser"
mysqlConfig.Addr = "db-instance.region.rds.amazonaws.com:3306"
mysqlConfig.Net = "tcp"
// Register the token provider
mysqlCfg.Apply(mysql.BeforeConnect(rdsauth.TokenProvider(cfg, time.Minute)))
connector, _ := mysql.NewConnector(mysqlConfig)
// Open database connection
db, _ := sql.OpenDB(connector)
defer db.Close()
err := db.Ping()
if err != nil {
log.Fatal(err)
}
}
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func TokenProvider ¶
func TokenProvider(awsConfig aws.Config, gracePeriod time.Duration) func(ctx context.Context, c *mysql.Config) error
TokenProvider creates a new AWS RDS authentication token provider function. The returned function can be used as an AuthSwitchRequest handler in the MySQL driver.
The token provider caches tokens until they are close to expiration (within the grace period), reducing the number of calls to the AWS authentication service.
Parameters:
- awsConfig: AWS configuration containing credentials and region information
- gracePeriod: The duration before token expiration when a new token should be fetched
Returns:
- A function that can be used as an BeforeConnect Option for the MySQL driver
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.