Skip to content

Commit

Permalink
feat: allow config of single AWS region by cli arg / config
Browse files Browse the repository at this point in the history
Will also work if you have no config or cli argument set but do have
AWS_REGION set in your environment.

Signed-off-by: Bradley Jones <bradley.jones@anchore.com>
  • Loading branch information
bradleyjones committed Dec 20, 2022
1 parent 8110070 commit e0723b9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
7 changes: 7 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,11 @@ func init() {
fmt.Printf("unable to bind flag '%s': %+v", opt, err)
os.Exit(1)
}

opt = "region"
rootCmd.Flags().StringP(opt, "r", "", "If set overrides the AWS_REGION environment variable/region specified in ECF config")
if err := viper.BindPFlag(opt, rootCmd.Flags().Lookup(opt)); err != nil {
fmt.Printf("unable to bind flag '%s': %+v", opt, err)
os.Exit(1)
}
}
35 changes: 32 additions & 3 deletions ecg/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
"github.com/anchore/elastic-container-gatherer/internal/config"
"github.com/anchore/elastic-container-gatherer/internal/log"
"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/ecs"
)

type channels struct {
Expand Down Expand Up @@ -60,9 +62,11 @@ func PeriodicallyGetInventoryReport(cfg *config.Application) {

// GetInventoryReport is an atomic method for getting in-use image results, in parallel for multiple clusters
func GetInventoryReport(cfg *config.Application) (inventory.Report, error) {
sess, err := session.NewSession(&aws.Config{
Region: aws.String("eu-west-2")}, // TODO - make this configurable
)
sessConfig := &aws.Config{}
if cfg.Region != "" {
sessConfig.Region = aws.String(cfg.Region)
}
sess, err := session.NewSession(sessConfig)
if err != nil {
log.Errorf("Failed to create AWS session: %w", err)
}
Expand All @@ -72,6 +76,31 @@ func GetInventoryReport(cfg *config.Application) (inventory.Report, error) {
return inventory.Report{}, err
}

svc := ecs.New(sess)
input := &ecs.ListClustersInput{}

result, err := svc.ListClusters(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case ecs.ErrCodeServerException:
fmt.Println(ecs.ErrCodeServerException, aerr.Error())
case ecs.ErrCodeClientException:
fmt.Println(ecs.ErrCodeClientException, aerr.Error())
case ecs.ErrCodeInvalidParameterException:
fmt.Println(ecs.ErrCodeInvalidParameterException, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
}

fmt.Println(result)

return inventory.Report{
Timestamp: time.Now().UTC().Format(time.RFC3339),
Results: []inventory.ReportItem{},
Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type Application struct {
IgnoreNotRunning bool `mapstructure:"ignore-not-running"`
PollingIntervalSeconds int `mapstructure:"polling-interval-seconds"`
AnchoreDetails AnchoreInfo `mapstructure:"anchore"`
Region string `mapstructure:"region"`
}

// MissingTagConf details the policy for handling missing tags when reporting images
Expand Down

0 comments on commit e0723b9

Please sign in to comment.