-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding example for rdsutils and also adding scheme if one wasn't prov…
…ided when creating authentication tokens
- Loading branch information
Showing
4 changed files
with
94 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Example | ||
|
||
This is an example using the AWS SDK for Go to create an Amazon RDS DB token using the | ||
rdsutils package. | ||
|
||
# Usage | ||
|
||
```sh | ||
go run iam_authetnication.go <region> <db user> <db name> <endpoint to database> <iam arn> | ||
``` | ||
|
||
Output: | ||
``` | ||
Successfully opened connection to database | ||
``` |
45 changes: 45 additions & 0 deletions
45
example/service/rds/rdsutils/authentication/iam_authentication.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package main | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/go-sql-driver/mysql" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/credentials/stscreds" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/rds/rdsutils" | ||
) | ||
|
||
// Usage ./iam_authentication <region> <db user> <db name> <endpoint to database> <iam arn> | ||
func main() { | ||
if len(os.Args) < 5 { | ||
log.Println("USAGE ERROR: go run concatenateObjects.go <region> <endpoint to database> <iam arn>") | ||
os.Exit(1) | ||
} | ||
|
||
awsRegion := os.Args[1] | ||
dbUser := os.Args[2] | ||
dbName := os.Args[3] | ||
dbEndpoint := os.Args[4] | ||
awsCreds := stscreds.NewCredentials(session.New(&aws.Config{Region: &awsRegion}), os.Args[5]) | ||
authToken, err := rdsutils.BuildAuthToken(dbEndpoint, awsRegion, dbUser, awsCreds) | ||
|
||
// Create the MySQL DNS string for the DB connection | ||
// user:password@protocol(endpoint)/dbname?<params> | ||
dnsStr := fmt.Sprintf("%s:%s@tcp(%s)/%s?tls=true", | ||
dbUser, authToken, dbEndpoint, dbName, | ||
) | ||
|
||
driver := mysql.MySQLDriver{} | ||
_ = driver | ||
// Use db to perform SQL operations on database | ||
if _, err = sql.Open("mysql", dnsStr); err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println("Successfully opened connection to database") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters