/
enidetailsbuilder.go
191 lines (169 loc) · 6.54 KB
/
enidetailsbuilder.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package builders
import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
ec2Types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/cloud-crafts/sg-ripper/pkg/core/clients"
coreTypes "github.com/cloud-crafts/sg-ripper/pkg/core/types"
"github.com/cloud-crafts/sg-ripper/pkg/core/utils"
cmap "github.com/orcaman/concurrent-map/v2"
)
type EniDetailsBuilder struct {
awsEc2Client *clients.AwsEc2Client
awsLambdaClient *clients.AwsLambdaClient
awsElbClient *clients.AwsElbClient
awsEcsClient *clients.AwsEcsClient
awsRdsClient *clients.AwsRdsClient
cache cmap.ConcurrentMap[string, *coreTypes.NetworkInterfaceDetails]
}
func NewEniBuilder(cfg aws.Config) *EniDetailsBuilder {
return &EniDetailsBuilder{
awsEc2Client: clients.NewAwsEc2Client(cfg),
awsLambdaClient: clients.NewAwsLambdaClient(cfg),
awsElbClient: clients.NewAwsElbClient(cfg),
awsEcsClient: clients.NewAwsEcsClient(cfg),
awsRdsClient: clients.NewAwsRdsClient(cfg),
cache: cmap.New[*coreTypes.NetworkInterfaceDetails](),
}
}
// FromRemoteInterfaces returns a slice of coreTypes.NetworkInterfaceDetails
func (e *EniDetailsBuilder) FromRemoteInterfaces(ctx context.Context, awsEniBatch []ec2Types.NetworkInterface) ([]coreTypes.NetworkInterfaceDetails, error) {
eniDetails := make([]coreTypes.NetworkInterfaceDetails, 0)
for _, awsEni := range awsEniBatch {
if awsEni.NetworkInterfaceId != nil {
// Check if Network Interface is already in the cache to avoid computing multiple times which resources
// are using it
if cachedEni, ok := e.cache.Get(*awsEni.NetworkInterfaceId); ok {
eniDetails = append(eniDetails, *cachedEni)
} else {
resultCh := make(chan utils.Result[any])
asyncFetchers := []func(context.Context, ec2Types.NetworkInterface, chan utils.Result[any]){
e.getLambdaAttachmentAsync, e.getEcsAttachmentAsync, e.getElbAttachmentAsync, e.getVpcAttachmentAsync,
e.getRdsAttachmentAsync,
}
for _, fn := range asyncFetchers {
go fn(ctx, awsEni, resultCh)
}
var lambdaAttachment *coreTypes.LambdaAttachment
var ecsAttachment *coreTypes.EcsAttachment
var elbAttachment *coreTypes.ElbAttachment
var vpceAttachment *coreTypes.VpceAttachment
var rdsAttachments []coreTypes.RdsAttachment
for range asyncFetchers {
res := <-resultCh
if res.Err != nil {
return nil, res.Err
}
switch res.Data.(type) {
case *coreTypes.LambdaAttachment:
lambdaAttachment = res.Data.(*coreTypes.LambdaAttachment)
case *coreTypes.EcsAttachment:
ecsAttachment = res.Data.(*coreTypes.EcsAttachment)
case *coreTypes.ElbAttachment:
elbAttachment = res.Data.(*coreTypes.ElbAttachment)
case *coreTypes.VpceAttachment:
vpceAttachment = res.Data.(*coreTypes.VpceAttachment)
case []coreTypes.RdsAttachment:
rdsAttachments = res.Data.([]coreTypes.RdsAttachment)
}
}
sgIdentifiers := make([]coreTypes.SecurityGroupIdentifier, 0)
for _, group := range awsEni.Groups {
if group.GroupId != nil {
sgIdentifiers = append(sgIdentifiers, coreTypes.SecurityGroupIdentifier{
Id: *group.GroupId,
Name: group.GroupName,
})
}
}
var primaryIPAddress string
secondaryPrivateIPAddresses := make([]string, 0)
if awsEni.PrivateIpAddress != nil {
primaryIPAddress = *awsEni.PrivateIpAddress
for _, ip := range awsEni.PrivateIpAddresses {
if ip.PrivateIpAddress != nil && primaryIPAddress != *ip.PrivateIpAddress {
secondaryPrivateIPAddresses = append(secondaryPrivateIPAddresses, *ip.PrivateIpAddress)
}
}
}
newEni := coreTypes.NetworkInterfaceDetails{
Id: *awsEni.NetworkInterfaceId,
Description: awsEni.Description,
Type: string(awsEni.InterfaceType),
ManagedByAWS: *awsEni.RequesterManaged,
Status: string(awsEni.Status),
PrivateIPAddress: primaryIPAddress,
SecondaryPrivateIPAddresses: secondaryPrivateIPAddresses,
EC2Attachment: getEC2Attachment(awsEni),
LambdaAttachment: lambdaAttachment,
ECSAttachment: ecsAttachment,
ELBAttachment: elbAttachment,
VPCEAttachment: vpceAttachment,
RDSAttachments: rdsAttachments,
SecurityGroupIdentifiers: sgIdentifiers,
}
// Add the new interface to the cache
e.cache.Set(newEni.Id, &newEni)
eniDetails = append(eniDetails, newEni)
}
}
}
return eniDetails, nil
}
func (e *EniDetailsBuilder) getLambdaAttachmentAsync(ctx context.Context, awsEni ec2Types.NetworkInterface, resultCh chan utils.Result[any]) {
lambdaAttachment, err := e.awsLambdaClient.GetLambdaAttachment(ctx, awsEni)
if err != nil {
resultCh <- utils.Result[any]{Err: err}
return
}
resultCh <- utils.Result[any]{
Data: lambdaAttachment,
}
}
func (e *EniDetailsBuilder) getEcsAttachmentAsync(ctx context.Context, awsEni ec2Types.NetworkInterface, resultCh chan utils.Result[any]) {
ecsAttachment, err := e.awsEcsClient.GetEcsAttachment(ctx, awsEni)
if err != nil {
resultCh <- utils.Result[any]{Err: err}
return
}
resultCh <- utils.Result[any]{
Data: ecsAttachment,
}
}
func (e *EniDetailsBuilder) getElbAttachmentAsync(ctx context.Context, awsEni ec2Types.NetworkInterface, resultCh chan utils.Result[any]) {
elbAttachment, err := e.awsElbClient.GetELBAttachment(ctx, awsEni)
if err != nil {
resultCh <- utils.Result[any]{Err: err}
return
}
resultCh <- utils.Result[any]{
Data: elbAttachment,
}
}
func (e *EniDetailsBuilder) getVpcAttachmentAsync(ctx context.Context, awsEni ec2Types.NetworkInterface, resultCh chan utils.Result[any]) {
vpceAttachment, err := e.awsEc2Client.GetVpceAttachment(ctx, awsEni)
if err != nil {
resultCh <- utils.Result[any]{Err: err}
return
}
resultCh <- utils.Result[any]{
Data: vpceAttachment,
}
}
func (e *EniDetailsBuilder) getRdsAttachmentAsync(ctx context.Context, awsEni ec2Types.NetworkInterface, resultCh chan utils.Result[any]) {
rdsAttachments, err := e.awsRdsClient.GetRdsAttachments(ctx, awsEni)
if err != nil {
resultCh <- utils.Result[any]{Err: err}
return
}
resultCh <- utils.Result[any]{
Data: rdsAttachments,
}
}
// Get the IDs of the EC2 instances attached to the Network Interface
func getEC2Attachment(ifc ec2Types.NetworkInterface) *coreTypes.Ec2Attachment {
if ifc.Attachment != nil && ifc.Attachment.InstanceId != nil {
return &coreTypes.Ec2Attachment{InstanceId: *ifc.Attachment.InstanceId}
}
return nil
}