var ErrNoConnectionFormat = awserr.New("NoConnectionFormat", "No connection format was specified", nil)
ErrNoConnectionFormat will be returned during build if no format had been specified
func BuildAuthToken(endpoint, region, dbUser string, creds *credentials.Credentials) (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 * creds - Credentials 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.
authToken, err := BuildAuthToken(dbEndpoint, awsRegion, dbUser, awsCreds) // 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.
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 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(endpoint, region, dbUser, dbName string, creds *credentials.Credentials) ConnectionStringBuilder
NewConnectionStringBuilder will return an ConnectionStringBuilder
func (b ConnectionStringBuilder) Build() (string, error)
Build will return a new connection string that can be used to open a connection to the desired database.
Example: b := rdsutils.NewConnectionStringBuilder(endpoint, region, user, dbname, creds) connectStr, err := b.WithTCPFormat().Build() if err != nil { panic(err) } const dbType = "mysql" db, err := sql.Open(dbType, connectStr)
func (b ConnectionStringBuilder) WithDBName(dbName string) ConnectionStringBuilder
WithDBName will return a builder with the given database name
func (b ConnectionStringBuilder) WithEndpoint(endpoint string) ConnectionStringBuilder
WithEndpoint will return a builder with the given endpoint
func (b ConnectionStringBuilder) WithFormat(f ConnectionFormat) ConnectionStringBuilder
WithFormat will return a builder with the given connection format
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, creds) connectStr, err := b.WithParams(v).WithTCPFormat().Build()
func (b ConnectionStringBuilder) WithRegion(region string) ConnectionStringBuilder
WithRegion will return a builder with the given region
func (b ConnectionStringBuilder) WithTCPFormat() ConnectionStringBuilder
WithTCPFormat will set the format to TCP and return the modified builder
func (b ConnectionStringBuilder) WithUser(user string) ConnectionStringBuilder
WithUser will return a builder with the given user