Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: code restructure & post per cluster #29

Merged
merged 9 commits into from
Mar 8, 2023
4 changes: 2 additions & 2 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/anchore/anchore-ecs-inventory/ecg"
"github.com/anchore/anchore-ecs-inventory/internal/config"
"github.com/anchore/anchore-ecs-inventory/internal/logger"
"github.com/anchore/anchore-ecs-inventory/pkg"
)

var (
Expand Down Expand Up @@ -61,7 +61,7 @@ func initLogging() {

logger.InitLogger(logConfig)
log = logger.Log
ecg.SetLogger(logger.Log)
pkg.SetLogger(logger.Log)
}

func logAppConfig() {
Expand Down
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/anchore/anchore-ecs-inventory/ecg"
"github.com/anchore/anchore-ecs-inventory/internal/config"
"github.com/anchore/anchore-ecs-inventory/pkg"
)

// rootCmd represents the base command when called without any subcommands
Expand Down Expand Up @@ -43,7 +43,7 @@ var rootCmd = &cobra.Command{
}
*/

ecg.PeriodicallyGetInventoryReport(appConfig.PollingIntervalSeconds, appConfig.AnchoreDetails, appConfig.Region)
pkg.PeriodicallyGetInventoryReport(appConfig.PollingIntervalSeconds, appConfig.AnchoreDetails, appConfig.Region)
},
}

Expand Down
200 changes: 0 additions & 200 deletions ecg/lib.go

This file was deleted.

2 changes: 1 addition & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"github.com/spf13/viper"
"gopkg.in/yaml.v2"

"github.com/anchore/anchore-ecs-inventory/ecg/connection"
"github.com/anchore/anchore-ecs-inventory/internal"
"github.com/anchore/anchore-ecs-inventory/pkg/connection"
)

const redacted = "******"
Expand Down
2 changes: 1 addition & 1 deletion internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"

"github.com/anchore/anchore-ecs-inventory/ecg/connection"
"github.com/anchore/anchore-ecs-inventory/pkg/connection"
)

func TestLoadConfigFromFileCliConfigPath(t *testing.T) {
Expand Down
1 change: 0 additions & 1 deletion internal/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ func InitLogger(logConfig LogConfig) {
var cfg zap.Config

level, err := zap.ParseAtomicLevel(logConfig.Level)

if err != nil {
log.Printf("Invalid log level: %s, defaulting to `info`", logConfig.Level)
level = zap.NewAtomicLevelAt(zap.InfoLevel)
Expand Down
File renamed without changes.
82 changes: 82 additions & 0 deletions pkg/inventory/ecs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package inventory

import (
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/aws/aws-sdk-go/service/ecs/ecsiface"

"github.com/anchore/anchore-ecs-inventory/pkg/reporter"
)

// Check if AWS are present, should be stored in ~/.aws/credentials
func checkAWSCredentials(sess *session.Session) error {
_, err := sess.Config.Credentials.Get()
if err != nil {
// TODO: Add some logs here detailing where to put the credentials
return fmt.Errorf("unable to get AWS credentials: %w", err)
}
return nil
}

func fetchClusters(client ecsiface.ECSAPI) ([]*string, error) {
input := &ecs.ListClustersInput{}

result, err := client.ListClusters(input)
if err != nil {
return nil, err
}

return result.ClusterArns, nil
}

func fetchTasksFromCluster(client ecsiface.ECSAPI, cluster string) ([]*string, error) {
input := &ecs.ListTasksInput{
Cluster: aws.String(cluster),
}

result, err := client.ListTasks(input)
if err != nil {
return nil, err
}

return result.TaskArns, nil
}

func fetchImagesFromTasks(client ecsiface.ECSAPI, cluster string, tasks []*string) ([]reporter.ReportImage, error) {
input := &ecs.DescribeTasksInput{
Cluster: aws.String(cluster),
Tasks: tasks,
}

results, err := client.DescribeTasks(input)
if err != nil {
return []reporter.ReportImage{}, err
}

uniqueImages := make(map[string]reporter.ReportImage)

for _, task := range results.Tasks {
for _, container := range task.Containers {
digest := ""
if container.ImageDigest != nil {
digest = *container.ImageDigest
}
uniqueName := fmt.Sprintf("%s@%s", *container.Image, digest)
uniqueImages[uniqueName] = reporter.ReportImage{
Tag: *container.Image,
RepoDigest: digest,
}
}
}

// convert map of unique images to a slice
images := []reporter.ReportImage{}
for _, image := range uniqueImages {
images = append(images, image)
}

return images, nil
}
38 changes: 38 additions & 0 deletions pkg/inventory/ecs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package inventory

import (
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/stretchr/testify/assert"
)

func TestFetchClusters(t *testing.T) {
mockSvc := &mockECSClient{}

clusters, err := fetchClusters(mockSvc)

assert.NoError(t, err)
assert.Equal(t, 2, len(clusters))
}

func TestFetchTasksFromCluster(t *testing.T) {
mockSvc := &mockECSClient{}

tasks, err := fetchTasksFromCluster(mockSvc, "cluster-1")

assert.NoError(t, err)
assert.Equal(t, 2, len(tasks))
}

func TestFetchImagesFromTasks(t *testing.T) {
mockSvc := &mockECSClient{}

images, err := fetchImagesFromTasks(mockSvc, "cluster-1", []*string{
aws.String("arn:aws:ecs:us-east-1:123456789012:task/cluster-1/12345678-1234-1234-1234-000000000000"),
aws.String("arn:aws:ecs:us-east-1:123456789012:task/cluster-1/12345678-1234-1234-1234-111111111111"),
})

assert.NoError(t, err)
assert.Equal(t, 3, len(images))
}
Loading