• 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

95.4
/internal/inventory/awsfetcher/fetcher_ec2_instance.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 awsfetcher
19

20
import (
21
        "context"
22

23
        "github.com/elastic/cloudbeat/internal/dataprovider/providers/cloud"
24
        "github.com/elastic/cloudbeat/internal/infra/clog"
25
        "github.com/elastic/cloudbeat/internal/inventory"
26
        "github.com/elastic/cloudbeat/internal/resources/providers/awslib/ec2"
27
        "github.com/elastic/cloudbeat/internal/resources/utils/pointers"
28
)
29

30
type ec2InstanceFetcher struct {
31
        logger      *clog.Logger
32
        provider    ec2InstancesProvider
33
        AccountId   string
34
        AccountName string
35
}
36

37
type ec2InstancesProvider interface {
38
        DescribeInstances(ctx context.Context) ([]*ec2.Ec2Instance, error)
39
}
40

41
func newEc2InstancesFetcher(logger *clog.Logger, identity *cloud.Identity, provider ec2InstancesProvider) inventory.AssetFetcher {
1✔
42
        return &ec2InstanceFetcher{
1✔
43
                logger:      logger,
1✔
44
                provider:    provider,
1✔
45
                AccountId:   identity.Account,
1✔
46
                AccountName: identity.AccountAlias,
1✔
47
        }
1✔
48
}
1✔
49

50
func (e *ec2InstanceFetcher) Fetch(ctx context.Context, assetChannel chan<- inventory.AssetEvent) {
1✔
51
        e.logger.Info("Fetching EC2 Instances")
1✔
52
        defer e.logger.Info("Fetching EC2 Instances - Finished")
1✔
53

1✔
54
        instances, err := e.provider.DescribeInstances(ctx)
1✔
55
        if err != nil {
1✔
UNCOV
56
                e.logger.Errorf("Could not list ec2 instances: %v", err)
×
57
                return
×
58
        }
×
59

60
        for _, instance := range instances {
4✔
61
                if instance == nil {
4✔
62
                        continue
1✔
63
                }
64

65
                iamFetcher := inventory.EmptyEnricher()
2✔
66
                if instance.IamInstanceProfile != nil {
3✔
67
                        iamFetcher = inventory.WithIAM(inventory.AssetIAM{
1✔
68
                                Id:  instance.IamInstanceProfile.Id,
1✔
69
                                Arn: instance.IamInstanceProfile.Arn,
1✔
70
                        })
1✔
71
                }
1✔
72

73
                subnetIds := []string{}
2✔
74
                if id := pointers.Deref(instance.SubnetId); id != "" {
3✔
75
                        subnetIds = append(subnetIds, id)
1✔
76
                }
1✔
77
                assetChannel <- inventory.NewAssetEvent(
2✔
78
                        inventory.AssetClassificationAwsEc2Instance,
2✔
79
                        []string{instance.GetResourceArn(), pointers.Deref(instance.InstanceId)},
2✔
80
                        instance.GetResourceName(),
2✔
81

2✔
82
                        inventory.WithRawAsset(instance),
2✔
83
                        inventory.WithTags(e.getTags(instance)),
2✔
84
                        inventory.WithCloud(inventory.AssetCloud{
2✔
85
                                Provider:         inventory.AwsCloudProvider,
2✔
86
                                Region:           instance.Region,
2✔
87
                                AvailabilityZone: e.getAvailabilityZone(instance),
2✔
88
                                Account: inventory.AssetCloudAccount{
2✔
89
                                        Id:   e.AccountId,
2✔
90
                                        Name: e.AccountName,
2✔
91
                                },
2✔
92
                                Instance: &inventory.AssetCloudInstance{
2✔
93
                                        Id:   pointers.Deref(instance.InstanceId),
2✔
94
                                        Name: instance.GetResourceName(),
2✔
95
                                },
2✔
96
                                Machine: &inventory.AssetCloudMachine{
2✔
97
                                        MachineType: string(instance.InstanceType),
2✔
98
                                },
2✔
99
                                Service: &inventory.AssetCloudService{
2✔
100
                                        Name: "AWS EC2",
2✔
101
                                },
2✔
102
                        }),
2✔
103
                        inventory.WithHost(inventory.AssetHost{
2✔
104
                                Architecture:    string(instance.Architecture),
2✔
105
                                ImageId:         instance.ImageId,
2✔
106
                                InstanceType:    string(instance.InstanceType),
2✔
107
                                Platform:        string(instance.Platform),
2✔
108
                                PlatformDetails: instance.PlatformDetails,
2✔
109
                        }),
2✔
110
                        iamFetcher,
2✔
111
                        inventory.WithNetwork(inventory.AssetNetwork{
2✔
112
                                NetworkId:        instance.VpcId,
2✔
113
                                SubnetIds:        subnetIds,
2✔
114
                                Ipv6Address:      instance.Ipv6Address,
2✔
115
                                PublicIpAddress:  instance.PublicIpAddress,
2✔
116
                                PrivateIpAddress: instance.PrivateIpAddress,
2✔
117
                                PublicDnsName:    instance.PublicDnsName,
2✔
118
                                PrivateDnsName:   instance.PrivateDnsName,
2✔
119
                        }),
2✔
120
                )
2✔
121
        }
122
}
123

124
func (e *ec2InstanceFetcher) getTags(instance *ec2.Ec2Instance) map[string]string {
2✔
125
        tags := make(map[string]string, len(instance.Tags))
2✔
126
        for _, t := range instance.Tags {
4✔
127
                if t.Key == nil {
2✔
UNCOV
128
                        continue
×
129
                }
130

131
                tags[*t.Key] = pointers.Deref(t.Value)
2✔
132
        }
133
        return tags
2✔
134
}
135

136
func (e *ec2InstanceFetcher) getAvailabilityZone(instance *ec2.Ec2Instance) *string {
2✔
137
        if instance.Placement == nil {
3✔
138
                return nil
1✔
139
        }
1✔
140

141
        return instance.Placement.AvailabilityZone
1✔
142
}
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