go_rds_iam

package module
v0.0.0-...-29978bd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 22, 2024 License: MIT Imports: 13 Imported by: 0

README

AWS RDS IAM Authentication

This Go package provides functionality to connect to AWS RDS instances using IAM authentication. It supports both PostgreSQL and MySQL databases and can be used in conjunction with the database/sql package and the gorm ORM library.

Installation

To use this package, you need to have Go installed on your system. You can install it by following the official Go installation guide.

Once you have Go installed, you can import this package into your Go project by adding the following line to your code:

import "github.com/RohanPoojary/go-rds-iam"

Usage

Connecting to RDS using IAM authentication

To connect to an RDS instance using IAM authentication, you'll need to create an AWS session and register the IAM driver for the desired database type (PostgreSQL or MySQL). Here's an example:


import (
    "database/sql"
    "github.com/RohanPoojary/go-rds-iam"
    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/credentials"
    "github.com/aws/aws-sdk-go/aws/session"
)

func main() {

    // Create an AWS session
    cfg := aws.Config{
        Region: &awsRegionName,
    }

    sess, err := session.NewSessionWithOptions(session.Options{
        Config:            cfg,
        SharedConfigState: session.SharedConfigEnable,
    })
    if err != nil {
        // Handle error
    }

    // Register the IAM driver for PostgreSQL
    driverName := go_rds_iam.RegisterDriver(sess, go_rds_iam.PostgresRDSType)

    // Connect to the RDS instance using the registered driver
    sqlDB, err := sql.Open(driverName, "your-rds-instance-dsn")
    if err != nil {
        // Handle error
    }

    // Use the database connection as needed
    // ...
}

Using with GORM

To connect to GORM module, you can use below snippet along with above db initialisation to override default db connections:


import (
    "gorm.io/driver/postgres"
    "database/sql"
)

func main() {

    var sqlDB *sql.DB

    // Initialise sqlDB with IAM driver as above
    // ...

    db, err := gorm.Open(postgres.New(postgres.Config{
        Conn: sqlDB,
    }), &gorm.Config{})

    if err != nil {
        // Handle error
    }
}

Contributing

Contributions to this package are welcome! If you find a bug or want to add a new feature, please open an issue or submit a pull request on the project's GitHub repository.

License

This package is licensed under the MIT License.

Documentation

Overview

Package go_rds_iam provides functionality to connect to AWS RDS instances using IAM authentication.

Index

Constants

View Source
const (
	PostgresRDSType = "postgres"
	MySQLRDSType    = "mysql"
)

Variables

This section is empty.

Functions

func RegisterDriver

func RegisterDriver(sess *session.Session, rdsType ConnectionRDSType) string

RegisterDriver registers a new database driver for the given RDS type (e.g., "postgres" or "mysql") that uses IAM authentication to connect to RDS instances.

Types

type ConnectionRDSType

type ConnectionRDSType string

type ConnectionRequest

type ConnectionRequest struct {
	RDSType ConnectionRDSType
	Region  string

	DBUser             string
	Hostname           string
	Port               int
	DBName             string
	SSLMode            string
	SSLCertificatePath string
}

ConnectionRequest represents a request to connect to an RDS instance.

type GenericIAMDriver

type GenericIAMDriver struct {
	// contains filtered or unexported fields
}

GenericIAMDriver is a database driver that uses IAM authentication to connect to RDS instances.

func (*GenericIAMDriver) Open

func (d *GenericIAMDriver) Open(dsn string) (driver.Conn, error)

Open opens a new database connection using IAM authentication.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL