package rdsutils
import "github.com/aws/aws-sdk-go-v2/service/rds/rdsutils"
Package rdsutils is used to generate authentication tokens used to connect to a givent Amazon Relational Database Service (RDS) database.
Before using the authentication please visit the docs here to ensure the database has the proper policies to allow for IAM token authentication. https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.html#UsingWithRDS.IAMDBAuth.Availability
When building the connection string, there are two required parameters that are needed to be set on the query.
tls
allowCleartextPasswords must be set to true
Example creating a basic auth token with the builder: v := url.Values{} v.Add("tls", "tls_profile_name") v.Add("allowCleartextPasswords", "true") b := rdsutils.NewConnectionStringBuilder(endpoint, region, user, dbname, credProvider) connectStr, err := b.WithTCPFormat().WithParams(v).Build()
Index ¶
- Variables
- func BuildAuthToken(ctx context.Context, endpoint, region, dbUser string, signer HTTPV4Signer) (string, error)
- type ConnectionFormat
- type ConnectionStringBuilder
- func NewConnectionStringBuilder(endpoint, region, dbUser, dbName string, signer HTTPV4Signer) ConnectionStringBuilder
- func (b ConnectionStringBuilder) Build(ctx context.Context) (string, error)
- func (b ConnectionStringBuilder) WithDBName(dbName string) ConnectionStringBuilder
- func (b ConnectionStringBuilder) WithEndpoint(endpoint string) ConnectionStringBuilder
- func (b ConnectionStringBuilder) WithFormat(f ConnectionFormat) ConnectionStringBuilder
- func (b ConnectionStringBuilder) WithParams(params url.Values) ConnectionStringBuilder
- func (b ConnectionStringBuilder) WithRegion(region string) ConnectionStringBuilder
- func (b ConnectionStringBuilder) WithTCPFormat() ConnectionStringBuilder
- func (b ConnectionStringBuilder) WithUser(user string) ConnectionStringBuilder
- type HTTPV4Signer
Variables ¶
var ErrNoConnectionFormat = awserr.New("NoConnectionFormat", "No connection format was specified", nil)
ErrNoConnectionFormat will be returned during build if no format had been specified
Functions ¶
func BuildAuthToken ¶
func BuildAuthToken(ctx context.Context, endpoint, region, dbUser string, signer HTTPV4Signer) (string, error)
BuildAuthToken will return an authorization token used as the password for a DB connection.
* endpoint - Endpoint consists of the port needed to connect to the DB. <host>:<port> * region - Region is the location of where the DB is * dbUser - User account within the database to sign in with * signer - Signer used to be signed with
The following example shows how to use BuildAuthToken to create an authentication token for connecting to a MySQL database in RDS.
signer := v4.NewSigner(credsProvider) authToken, err := BuildAuthToken(ctx, dbEndpoint, awsRegion, dbUser, signer) // Create the MySQL DNS string for the DB connection // user:password@protocol(endpoint)/dbname?<params> connectStr = fmt.Sprintf("%s:%s@tcp(%s)/%s?allowCleartextPasswords=true&tls=rds", dbUser, authToken, dbEndpoint, dbName, ) // Use db to perform SQL operations on database db, err := sql.Open("mysql", connectStr)
See http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.html for more information on using IAM database authentication with RDS.
Types ¶
type ConnectionFormat ¶
type ConnectionFormat string
ConnectionFormat is the type of connection that will be used to connect to the database
const ( NoConnectionFormat ConnectionFormat = "" TCPFormat ConnectionFormat = "tcp" )
ConnectionFormat enums
type ConnectionStringBuilder ¶
type ConnectionStringBuilder struct {
// contains filtered or unexported fields
}
ConnectionStringBuilder is a builder that will construct a connection string with the provided parameters. params field is required to have a tls specification and allowCleartextPasswords must be set to true.
func NewConnectionStringBuilder ¶
func NewConnectionStringBuilder(endpoint, region, dbUser, dbName string, signer HTTPV4Signer) ConnectionStringBuilder
NewConnectionStringBuilder will return an ConnectionStringBuilder
func (ConnectionStringBuilder) Build ¶
func (b ConnectionStringBuilder) Build(ctx context.Context) (string, error)
Build will return a new connection string that can be used to open a connection to the desired database.
Example: signer := v4.NewSigner(credsProvider) b := rdsutils.NewConnectionStringBuilder(endpoint, region, user, dbname, signer) connectStr, err := b.WithTCPFormat().Build(ctx) if err != nil { panic(err) } const dbType = "mysql" db, err := sql.Open(dbType, connectStr)
func (ConnectionStringBuilder) WithDBName ¶
func (b ConnectionStringBuilder) WithDBName(dbName string) ConnectionStringBuilder
WithDBName will return a builder with the given database name
func (ConnectionStringBuilder) WithEndpoint ¶
func (b ConnectionStringBuilder) WithEndpoint(endpoint string) ConnectionStringBuilder
WithEndpoint will return a builder with the given endpoint
func (ConnectionStringBuilder) WithFormat ¶
func (b ConnectionStringBuilder) WithFormat(f ConnectionFormat) ConnectionStringBuilder
WithFormat will return a builder with the given connection format
func (ConnectionStringBuilder) WithParams ¶
func (b ConnectionStringBuilder) WithParams(params url.Values) ConnectionStringBuilder
WithParams will return a builder with the given params. The parameters will be included in the connection query string
Example: v := url.Values{} v.Add("tls", "rds") b := rdsutils.NewConnectionBuilder(endpoint, region, user, dbname, credProvider) connectStr, err := b.WithParams(v).WithTCPFormat().Build()
func (ConnectionStringBuilder) WithRegion ¶
func (b ConnectionStringBuilder) WithRegion(region string) ConnectionStringBuilder
WithRegion will return a builder with the given region
func (ConnectionStringBuilder) WithTCPFormat ¶
func (b ConnectionStringBuilder) WithTCPFormat() ConnectionStringBuilder
WithTCPFormat will set the format to TCP and return the modified builder
func (ConnectionStringBuilder) WithUser ¶
func (b ConnectionStringBuilder) WithUser(user string) ConnectionStringBuilder
WithUser will return a builder with the given user
type HTTPV4Signer ¶
type HTTPV4Signer interface { Presign(ctx context.Context, r *http.Request, body io.ReadSeeker, service, region string, exp time.Duration, signTime time.Time) (http.Header, error) }
HTTPV4Signer interface is used to presign a request
Source Files ¶
- Version
- v0.24.0
- Published
- Jul 22, 2020
- Platform
- windows/amd64
- Imports
- 8 packages
- Last checked
- 4 minutes ago –
Tools for package owners.