-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from anchore/code-reorg
feat: code restructure & post per cluster
- Loading branch information
Showing
18 changed files
with
384 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
Oops, something went wrong.