Skip to content

Commit

Permalink
Merge pull request #681 from tailwarden/tech-1020/add-network-interface
Browse files Browse the repository at this point in the history
added support for EC2 Network Interfaces
  • Loading branch information
eneskaya authored Mar 31, 2023
2 parents ec45de8 + 12e86ed commit 55be150
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions providers/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func listOfSupportedServices() []providers.FetchDataFunction {
apigateway.Apis,
elasticache.Clusters,
cloudwatch.Alarms,
ec2.NetworkInterfaces,
cloudwatch.Dashboards,
ec2.ElasticIps,
}
Expand Down
74 changes: 74 additions & 0 deletions providers/aws/ec2/network_interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package ec2

import (
"context"
"fmt"
"time"

log "github.com/sirupsen/logrus"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/sts"
. "github.com/tailwarden/komiser/models"
. "github.com/tailwarden/komiser/providers"
)

func NetworkInterfaces(ctx context.Context, client ProviderClient) ([]Resource, error) {
var config ec2.DescribeNetworkInterfacesInput
resources := make([]Resource, 0)
ec2Client := ec2.NewFromConfig(*client.AWSClient)

stsClient := sts.NewFromConfig(*client.AWSClient)
stsOutput, err := stsClient.GetCallerIdentity(ctx, &sts.GetCallerIdentityInput{})
if err != nil {
return resources, err
}

accountId := stsOutput.Account

for {
output, err := ec2Client.DescribeNetworkInterfaces(ctx, &config)
if err != nil {
return resources, err
}

for _, iface := range output.NetworkInterfaces {
tags := make([]Tag, 0)
for _, tag := range iface.TagSet {
tags = append(tags, Tag{
Key: *tag.Key,
Value: *tag.Value,
})
}

resourceArn := fmt.Sprintf("arn:aws:ec2:%s:%s:network-interface/%s", client.AWSClient.Region, *accountId, *iface.NetworkInterfaceId)

resources = append(resources, Resource{
Provider: "AWS",
Account: client.Name,
Service: "Network Interface",
Region: client.AWSClient.Region,
ResourceId: resourceArn,
Cost: 0,
Name: *iface.NetworkInterfaceId,
FetchedAt: time.Now(),
Tags: tags,
Link: fmt.Sprintf("https:/%s.console.aws.amazon.com/ec2/home?region=%s#NetworkInterface:networkInterfaceId=%s", client.AWSClient.Region, client.AWSClient.Region, *iface.NetworkInterfaceId),
})
}
if aws.ToString(output.NextToken) == "" {
break
}

config.NextToken = output.NextToken
}
log.WithFields(log.Fields{
"provider": "AWS",
"account": client.Name,
"region": client.AWSClient.Region,
"service": "Network Interface",
"resources": len(resources),
}).Info("Fetched resources")
return resources, nil
}

0 comments on commit 55be150

Please sign in to comment.