Select Git revision
-
Jano Hendriks authoredJano Hendriks authored
secrets_manager.go 5.82 KiB
package secrets_manager
import (
"encoding/base64"
"encoding/json"
credentials2 "github.com/aws/aws-sdk-go/aws/credentials"
"os"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/logs"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/struct_utils"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/secretsmanager"
"github.com/aws/aws-secretsmanager-caching-go/secretcache"
)
type DatabaseCredentials struct {
Username string `json:"username"`
Password string `json:"password"`
Engine string `json:"engine"`
Host string `json:"host"`
Port int `json:"port"`
InstanceIdentifier string `json:"dbInstanceIdentifier"`
ReadOnlyHost string `json:"aurora_read_only_host"`
}
type S3UploadCredentials struct {
AccessKeyID string `json:"accessKeyID"`
SecretKey string `json:"secretKey"`
}
var (
secretCache, _ = secretcache.New()
secretManagerRegion = "af-south-1"
)
var secretManagerSession *secretsmanager.SecretsManager
func GetDatabaseCredentials(secretID string, isDebug bool) (DatabaseCredentials, error) {
secret, _ := GetSecret(secretID, isDebug)
var credentials DatabaseCredentials
err := struct_utils.UnmarshalJSON([]byte(secret), &credentials)
if err != nil {
return DatabaseCredentials{}, err
}
return credentials, nil
}
func GetS3UploadCredentials(secretID string, isDebug bool) (*credentials2.Credentials, error) {
secret, _ := GetSecret(secretID, isDebug)
var credentials S3UploadCredentials
err := struct_utils.UnmarshalJSON([]byte(secret), &credentials)
if err != nil {
return nil, err
}
return credentials2.NewStaticCredentials(credentials.AccessKeyID, credentials.SecretKey, ""), nil
}
// getSecretManagerSession Instantiates a new Secrets Manager client session
func getSecretManagerSession(isDebug bool) (err error) {
// If a session already exists, use it
if secretManagerSession != nil {
return nil
}
logs.Info("Creating a new Secrets Manager session")
awsSession, err := session.NewSession()
if err != nil {