Skip to content

Commit

Permalink
Add cloudfront
Browse files Browse the repository at this point in the history
  • Loading branch information
RamanaReddy0M committed May 27, 2024
1 parent 7f61b3d commit 489d193
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 13 deletions.
35 changes: 22 additions & 13 deletions pkg/providers/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/apigateway"
"github.com/aws/aws-sdk-go/service/cloudfront"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/aws/aws-sdk-go/service/elb"
Expand All @@ -21,18 +22,19 @@ import (

// Provider is a data provider for aws API
type Provider struct {
id string
ec2Client *ec2.EC2
route53Client *route53.Route53
s3Client *s3.S3
ecsClient *ecs.ECS
lambdaClient *lambda.Lambda
apiGateway *apigateway.APIGateway
albClient *elbv2.ELBV2
elbClient *elb.ELB
lightsailClient *lightsail.Lightsail
regions *ec2.DescribeRegionsOutput
session *session.Session
id string
ec2Client *ec2.EC2
route53Client *route53.Route53
s3Client *s3.S3
ecsClient *ecs.ECS
lambdaClient *lambda.Lambda
apiGateway *apigateway.APIGateway
albClient *elbv2.ELBV2
elbClient *elb.ELB
lightsailClient *lightsail.Lightsail
cloudFrontClient *cloudfront.CloudFront
regions *ec2.DescribeRegionsOutput
session *session.Session
}

// New creates a new provider client for aws API
Expand Down Expand Up @@ -66,12 +68,13 @@ func New(options schema.OptionBlock) (*Provider, error) {
albClient := elbv2.New(session)
elbClient := elb.New(session)
lightsailClient := lightsail.New(session)
cloudFrontClient := cloudfront.New(session)

regions, err := ec2Client.DescribeRegions(&ec2.DescribeRegionsInput{})
if err != nil {
return nil, errors.Wrap(err, "could not get list of regions")
}
return &Provider{ec2Client: ec2Client, id: id, regions: regions, route53Client: route53Client, s3Client: s3Client, ecsClient: ecsClient, apiGateway: apiGateway, lambdaClient: lambdaClient, albClient: albClient, elbClient: elbClient, lightsailClient: lightsailClient, session: session}, nil
return &Provider{ec2Client: ec2Client, id: id, regions: regions, route53Client: route53Client, s3Client: s3Client, ecsClient: ecsClient, apiGateway: apiGateway, lambdaClient: lambdaClient, albClient: albClient, elbClient: elbClient, lightsailClient: lightsailClient, cloudFrontClient: cloudFrontClient, session: session}, nil
}

const apiAccessKey = "aws_access_key"
Expand Down Expand Up @@ -136,6 +139,11 @@ func (p *Provider) Resources(ctx context.Context) (*schema.Resources, error) {
if err != nil {
return nil, err
}
cloudfrontProvider := &cloudfrontProvider{cloudFrontClient: p.cloudFrontClient, id: p.id, session: p.session}
cloudfrontResources, err := cloudfrontProvider.GetResource(ctx)
if err != nil {
return nil, err
}

finalList := schema.NewResources()
finalList.Merge(list)
Expand All @@ -146,5 +154,6 @@ func (p *Provider) Resources(ctx context.Context) (*schema.Resources, error) {
finalList.Merge(lambdaAndApiGateways)
finalList.Merge(albs)
finalList.Merge(elbs)
finalList.Merge(cloudfrontResources)
return finalList, nil
}
54 changes: 54 additions & 0 deletions pkg/providers/aws/cloudfront.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package aws

import (
"context"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudfront"
"github.com/pkg/errors"
"github.com/projectdiscovery/cloudlist/pkg/schema"
)

// cloudfrontProvider is a provider for AWS CloudFront API
type cloudfrontProvider struct {
id string
cloudFrontClient *cloudfront.CloudFront
session *session.Session
}

// GetResource returns all the resources in the store for a provider.
func (cp *cloudfrontProvider) GetResource(ctx context.Context) (*schema.Resources, error) {
list := schema.NewResources()

err := listCloudFrontResources(cp.cloudFrontClient, list)
if err != nil {
return nil, errors.Wrap(err, "could not list CloudFront resources")
}
return list, nil
}

func listCloudFrontResources(cloudFrontClient *cloudfront.CloudFront, list *schema.Resources) error {
req := &cloudfront.ListDistributionsInput{MaxItems: aws.Int64(400)}
for {
distributions, err := cloudFrontClient.ListDistributions(req)
if err != nil {
return errors.Wrap(err, "could not list distributions")
}

for _, distribution := range distributions.DistributionList.Items {
resource := &schema.Resource{
Provider: "aws",
ID: aws.StringValue(distribution.Id),
DNSName: aws.StringValue(distribution.DomainName),
Public: true,
}
list.Append(resource)
}
if aws.StringValue(distributions.DistributionList.NextMarker) == "" {
break
}
req.SetMarker(aws.StringValue(distributions.DistributionList.NextMarker))
}
return nil
}

0 comments on commit 489d193

Please sign in to comment.