/
fetcher.go
118 lines (101 loc) · 3.3 KB
/
fetcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package vulnerability
import (
"context"
"sort"
"github.com/elastic/elastic-agent-libs/logp"
"github.com/elastic/cloudbeat/internal/resources/providers/awslib/ec2"
)
type VulnerabilityFetcher struct {
log *logp.Logger
provider instancesProvider
ch chan *ec2.Ec2Instance
}
type instancesProvider interface {
DescribeInstances(ctx context.Context) ([]*ec2.Ec2Instance, error)
DescribeVolumes(ctx context.Context, instances []*ec2.Ec2Instance) ([]*ec2.Volume, error)
}
func NewVulnerabilityFetcher(log *logp.Logger, provider instancesProvider) VulnerabilityFetcher {
log.Debug("VulnerabilityFetcher: New")
ch := make(chan *ec2.Ec2Instance)
return VulnerabilityFetcher{
log: log,
ch: ch,
provider: provider,
}
}
func (f VulnerabilityFetcher) FetchInstances(ctx context.Context) error {
defer close(f.ch)
f.log.Info("Starting VulnerabilityFetcher.FetchInstances")
ins, err := f.provider.DescribeInstances(ctx)
if err != nil {
f.log.Errorf("VulnerabilityFetcher.FetchInstances DescribeInstances failed: %v", err)
return err
}
f.log.Infof("VulnerabilityFetcher.FetchInstances found %d results", len(ins))
err = f.attachRootVolumes(ctx, ins)
if err != nil {
f.log.Errorf("VulnerabilityFetcher.FetchInstances attachRootVolumes failed: %v", err)
} else {
f.sortByRootVolumeSize(ins)
}
for _, in := range ins {
select {
case <-ctx.Done():
f.log.Info("VulnerabilityFetcher.FetchInstances context canceled")
return nil
case f.ch <- in:
}
}
f.log.Info("VulnerabilityFetcher.FetchInstances finished")
return nil
}
func (f VulnerabilityFetcher) attachRootVolumes(ctx context.Context, instances []*ec2.Ec2Instance) error {
volumes, err := f.provider.DescribeVolumes(ctx, instances)
if err != nil {
return err
}
volumesMapping := make(map[string][]*ec2.Volume)
for _, vol := range volumes {
volumesMapping[vol.InstanceId] = append(volumesMapping[vol.InstanceId], vol)
}
for _, ins := range instances {
instanceVolumes := volumesMapping[*ins.InstanceId]
for _, vol := range instanceVolumes {
if vol.Device == *ins.RootDeviceName {
ins.RootVolume = vol
break
}
}
}
return nil
}
func (f VulnerabilityFetcher) sortByRootVolumeSize(instances []*ec2.Ec2Instance) {
sort.Slice(instances, func(i, j int) bool {
if instances[i].RootVolume == nil {
return false
}
if instances[j].RootVolume == nil {
return true
}
return instances[i].RootVolume.Size < instances[j].RootVolume.Size
})
}
func (f VulnerabilityFetcher) GetChan() chan *ec2.Ec2Instance {
return f.ch
}