• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

elastic / cloudbeat / 13013465319

28 Jan 2025 03:10PM UTC coverage: 75.703% (+0.06%) from 75.647%
13013465319

push

github

web-flow
[8.17](backport #2936) Use custom logger to downgrade canceled context errors to warnings (#2956)

155 of 184 new or added lines in 122 files covered. (84.24%)

131 existing lines in 44 files now uncovered.

8693 of 11483 relevant lines covered (75.7%)

16.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.72
/internal/vulnerability/fetcher.go
1
// Licensed to Elasticsearch B.V. under one or more contributor
2
// license agreements. See the NOTICE file distributed with
3
// this work for additional information regarding copyright
4
// ownership. Elasticsearch B.V. licenses this file to you under
5
// the Apache License, Version 2.0 (the "License"); you may
6
// not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
//
9
//     http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17

18
package vulnerability
19

20
import (
21
        "context"
22
        "sort"
23

24
        "github.com/elastic/cloudbeat/internal/infra/clog"
25
        "github.com/elastic/cloudbeat/internal/resources/providers/awslib/ec2"
26
)
27

28
type VulnerabilityFetcher struct {
29
        log      *clog.Logger
30
        provider instancesProvider
31
        ch       chan *ec2.Ec2Instance
32
}
33

34
type instancesProvider interface {
35
        DescribeInstances(ctx context.Context) ([]*ec2.Ec2Instance, error)
36
        DescribeVolumes(ctx context.Context, instances []*ec2.Ec2Instance) ([]*ec2.Volume, error)
37
}
38

39
func NewVulnerabilityFetcher(log *clog.Logger, provider instancesProvider) VulnerabilityFetcher {
8✔
40
        log.Debug("VulnerabilityFetcher: New")
8✔
41
        ch := make(chan *ec2.Ec2Instance)
8✔
42
        return VulnerabilityFetcher{
8✔
43
                log:      log,
8✔
44
                ch:       ch,
8✔
45
                provider: provider,
8✔
46
        }
8✔
47
}
8✔
48

49
func (f VulnerabilityFetcher) FetchInstances(ctx context.Context) error {
7✔
50
        defer close(f.ch)
7✔
51
        f.log.Info("Starting VulnerabilityFetcher.FetchInstances")
7✔
52
        ins, err := f.provider.DescribeInstances(ctx)
7✔
53
        if err != nil {
8✔
54
                f.log.Errorf("VulnerabilityFetcher.FetchInstances DescribeInstances failed: %v", err)
1✔
55
                return err
1✔
56
        }
1✔
57
        f.log.Infof("VulnerabilityFetcher.FetchInstances found %d results", len(ins))
6✔
58

6✔
59
        err = f.attachRootVolumes(ctx, ins)
6✔
60
        if err != nil {
8✔
61
                f.log.Errorf("VulnerabilityFetcher.FetchInstances attachRootVolumes failed: %v", err)
2✔
62
        } else {
6✔
63
                f.sortByRootVolumeSize(ins)
4✔
64
        }
4✔
65

66
        for _, in := range ins {
18✔
67
                select {
12✔
68
                case <-ctx.Done():
2✔
69
                        f.log.Info("VulnerabilityFetcher.FetchInstances context canceled")
2✔
70
                        return nil
2✔
71
                case f.ch <- in:
10✔
72
                }
73
        }
74
        f.log.Info("VulnerabilityFetcher.FetchInstances finished")
4✔
75
        return nil
4✔
76
}
77

78
func (f VulnerabilityFetcher) attachRootVolumes(ctx context.Context, instances []*ec2.Ec2Instance) error {
6✔
79
        volumes, err := f.provider.DescribeVolumes(ctx, instances)
6✔
80
        if err != nil {
8✔
81
                return err
2✔
82
        }
2✔
83

84
        volumesMapping := make(map[string][]*ec2.Volume)
4✔
85
        for _, vol := range volumes {
26✔
86
                volumesMapping[vol.InstanceId] = append(volumesMapping[vol.InstanceId], vol)
22✔
87
        }
22✔
88

89
        for _, ins := range instances {
16✔
90
                instanceVolumes := volumesMapping[*ins.InstanceId]
12✔
91
                for _, vol := range instanceVolumes {
30✔
92
                        if vol.Device == *ins.RootDeviceName {
29✔
93
                                ins.RootVolume = vol
11✔
94
                                break
11✔
95
                        }
96
                }
97
        }
98

99
        return nil
4✔
100
}
101

102
func (f VulnerabilityFetcher) sortByRootVolumeSize(instances []*ec2.Ec2Instance) {
4✔
103
        sort.Slice(instances, func(i, j int) bool {
16✔
104
                if instances[i].RootVolume == nil {
12✔
UNCOV
105
                        return false
×
106
                }
×
107

108
                if instances[j].RootVolume == nil {
14✔
109
                        return true
2✔
110
                }
2✔
111
                return instances[i].RootVolume.Size < instances[j].RootVolume.Size
10✔
112
        })
113
}
114

115
func (f VulnerabilityFetcher) GetChan() chan *ec2.Ec2Instance {
7✔
116
        return f.ch
7✔
117
}
7✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc