Skip to content

Commit

Permalink
use aws config on config object for aws authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
MeNsaaH committed Oct 30, 2020
1 parent 1173b89 commit 8ac8581
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 46 deletions.
11 changes: 2 additions & 9 deletions config/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 8,12 @@ import (
log "github.com/sirupsen/logrus"
)

// AwsConfig Related Configurations
type AwsConfig struct {
// AWS Configs
Config aws.Config
AccessKeyID string
SecretAccessKey string
DefaultRegion string
}

func loadAwsConfig(accessKeyID, secretAccessKey, defaultRegion string) aws.Config {
var (
err error
cfg aws.Config
)

if accessKeyID != "" && secretAccessKey != "" {
cfg, err = awsCfg.LoadDefaultConfig(
awsCfg.WithCredentialsProvider(credentials.StaticCredentialsProvider{
Expand All @@ -33,6 25,7 @@ func loadAwsConfig(accessKeyID, secretAccessKey, defaultRegion string) aws.Confi
} else {
cfg, err = awsCfg.LoadDefaultConfig(awsCfg.WithRegion(defaultRegion))
}

if err != nil {
log.Fatal(err)
}
Expand Down
8 changes: 3 additions & 5 deletions config/config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 18,9 @@ database:
type: sqlite

aws:
# Env: Access Key ID
# AWS_SECRET_ACCESS_KEY=SECRET
# Env: AWS_SECRET_ACCESS_KEY
secretAccessKey: blank
# Secret Access Key
# AWS_ACCESS_KEY_ID=AKID
# ENV: AWS_ACCESS_KEY_ID
accessKeyID: blank
# AWS_REGION=blank
# ENV: AWS_REGION
defaultRegion: us-east-2
13 changes: 9 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 7,7 @@ import (
"path/filepath"
"strings"

"github.com/aws/aws-sdk-go-v2/aws"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -45,7 46,7 @@ type Config struct {
Name string
Providers []string
Database *DatabaseConfig
Aws *AwsConfig
Aws *aws.Config
RefreshInterval int32
LogPath string

Expand All @@ -72,7 73,8 @@ func LoadConfig() *Config {
viper.SetDefault("StaticPath", "web/static")
// viper.SetDefault("DbType", "sqlite") // Default Database type is sqlite
viper.SetDefault("LogPath", path.Join(workingDir, "logs"))
viper.SetDefault("RefreshInterval", 4) // interval between running refresh and checking for resources to updates
viper.SetDefault("RefreshInterval", 4) // interval between running refresh and checking for resources to updates
viper.SetDefault("aws.DefaultRegion", "us-east-2") // Default AWS Region for users https://docs.aws.amazon.com/emr/latest/ManagementGuide/emr-plan-region.html

// Load Config file
if configPath := viper.GetString("Config"); configPath != "" {
Expand Down Expand Up @@ -115,7 117,10 @@ func LoadConfig() *Config {
User: viper.GetString("Database.User"),
Password: viper.GetString("Database.Password"),
}
config.Aws = &AwsConfig{}

awsConfig := loadAwsConfig(viper.GetString("aws.AccessKeyID"), viper.GetString("aws.SecretAccessKey"), viper.GetString("aws.DefaultRegion"))
config.Aws = &awsConfig

config.RefreshInterval = viper.GetInt32("RefreshInterval")

config.LogPath = viper.GetString("LogPath")
Expand Down Expand Up @@ -144,7 149,7 @@ func GetDB() *DatabaseConfig {
}

// GetAWS Return database config
func GetAWS() *AwsConfig {
func GetAWS() *aws.Config {
return config.Aws
}

Expand Down
19 changes: 0 additions & 19 deletions provider/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 3,6 @@ package aws
import (
"fmt"

"github.com/aws/aws-sdk-go-v2/aws"
awsCfg "github.com/aws/aws-sdk-go-v2/config"
log "github.com/sirupsen/logrus"

"github.com/mensaah/reka/config"
Expand All @@ -21,18 19,6 @@ func GetName() string {
return providerName
}

func GetConfig() aws.Config {
cfg, err := awsCfg.LoadDefaultConfig()
if err != nil {
panic("unable to load SDK config, " err.Error())
}

// Set the AWS Region that the service clients should use
// cfg.Region = endpoints.UsEast2RegionID
cfg.Region = "us-east-2"
return cfg
}

// NewResource Returns a new Resource object
func NewResource(id, manager string) *types.Resource {
resource := types.Resource{}
Expand All @@ -53,11 39,6 @@ func NewProvider() (*types.Provider, error) {
// Setup Logger
aws.Logger = logger

// Get and Load AWS Config
awsConfig := config.GetAWS()
// Set AWS Config
awsConfig.Config = GetConfig()

cfg := config.GetConfig()

ec2Manager := newEC2Manager(cfg, logFile)
Expand Down
5 changes: 3 additions & 2 deletions provider/aws/ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 40,9 @@ func getInstanceDetails(svc *ec2.Client, output *ec2.DescribeInstancesOutput, re
}

// GetAllEC2Instances Get all instances
func GetAllEC2Instances(cfg aws.Config, region string, logger *log.Entry) ([]*types.Resource, error) {
func GetAllEC2Instances(cfg aws.Config, logger *log.Entry) ([]*types.Resource, error) {
logger.Debug("Fetching EC2 Instances")

svc := ec2.NewFromConfig(cfg)
params := &ec2.DescribeInstancesInput{}

Expand All @@ -51,7 52,7 @@ func GetAllEC2Instances(cfg aws.Config, region string, logger *log.Entry) ([]*ty
if err != nil {
return nil, err
}
instances, err := getInstanceDetails(svc, resp, region, logger)
instances, err := getInstanceDetails(svc, resp, cfg.Region, logger)
if err != nil {
return nil, err
}
Expand Down
9 changes: 4 additions & 5 deletions provider/aws/ec2_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 23,16 @@ func newEC2Manager(cfg *config.Config, logPath string) types.ResourceManager {
Config: cfg,
Logger: logger,
GetAll: func() ([]*types.Resource, error) {
region := "us-east-2"
return GetAllEC2Instances(cfg.Aws.Config, region, logger)
return GetAllEC2Instances(*cfg.Aws, logger)
},
Destroy: func(resources []*types.Resource) error {
return TerminateEC2Instances(cfg.Aws.Config, resources, logger)
return TerminateEC2Instances(*cfg.Aws, resources, logger)
},
Stop: func(resources []*types.Resource) error {
return StopEC2Instances(cfg.Aws.Config, resources, logger)
return StopEC2Instances(*cfg.Aws, resources, logger)
},
Resume: func(resources []*types.Resource) error {
return ResumeEC2Instances(cfg.Aws.Config, resources, logger)
return ResumeEC2Instances(*cfg.Aws, resources, logger)
},
}
return ec2Manager
Expand Down
4 changes: 2 additions & 2 deletions provider/aws/s3_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 22,10 @@ func newS3Manager(cfg *config.Config, logPath string) types.ResourceManager {
Config: cfg,
Logger: logger,
GetAll: func() ([]*types.Resource, error) {
return getAllS3Buckets(cfg.Aws.Config, logger)
return getAllS3Buckets(*cfg.Aws, logger)
},
Destroy: func(resources []*types.Resource) error {
return destroyS3Buckets(cfg.Aws.Config, resources, logger)
return destroyS3Buckets(*cfg.Aws, resources, logger)
},
}
}

0 comments on commit 8ac8581

Please sign in to comment.