Skip to content

Commit

Permalink
Remove filtering of ApiGateway namespace resources.
Browse files Browse the repository at this point in the history
WIP
  • Loading branch information
cristiangreco committed Aug 1, 2023
1 parent 646b50e commit 3477d8e
Show file tree
Hide file tree
Showing 11 changed files with 0 additions and 407 deletions.
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ As a quick start, the following IAM policy can be used to grant the all permissi
"cloudwatch:GetMetricData",
"cloudwatch:GetMetricStatistics",
"cloudwatch:ListMetrics",
"apigateway:GET",
"aps:ListWorkspaces",
"autoscaling:DescribeAutoScalingGroups",
"dms:DescribeReplicationInstances",
Expand Down Expand Up @@ -167,11 +166,6 @@ These are the bare minimum permissions required to run Static and Discovery Jobs
"cloudwatch:ListMetrics"
```

This permission is required to discover resources for the AWS/ApiGateway namespace
```json
"apigateway:GET"
```

This permission is required to discover resources for the AWS/AutoScaling namespace
```json
"autoscaling:DescribeAutoScalingGroups"
Expand Down
8 changes: 0 additions & 8 deletions pkg/clients/tagging/v1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/apigateway/apigatewayiface"
"github.com/aws/aws-sdk-go/service/apigatewayv2/apigatewayv2iface"
"github.com/aws/aws-sdk-go/service/autoscaling/autoscalingiface"
"github.com/aws/aws-sdk-go/service/databasemigrationservice/databasemigrationserviceiface"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
Expand All @@ -27,8 +25,6 @@ type client struct {
logger logging.Logger
taggingAPI resourcegroupstaggingapiiface.ResourceGroupsTaggingAPIAPI
autoscalingAPI autoscalingiface.AutoScalingAPI
apiGatewayAPI apigatewayiface.APIGatewayAPI
apiGatewayV2API apigatewayv2iface.ApiGatewayV2API
ec2API ec2iface.EC2API
dmsAPI databasemigrationserviceiface.DatabaseMigrationServiceAPI
prometheusSvcAPI prometheusserviceiface.PrometheusServiceAPI
Expand All @@ -40,8 +36,6 @@ func NewClient(
logger logging.Logger,
taggingAPI resourcegroupstaggingapiiface.ResourceGroupsTaggingAPIAPI,
autoscalingAPI autoscalingiface.AutoScalingAPI,
apiGatewayAPI apigatewayiface.APIGatewayAPI,
apiGatewayV2API apigatewayv2iface.ApiGatewayV2API,
ec2API ec2iface.EC2API,
dmsClient databasemigrationserviceiface.DatabaseMigrationServiceAPI,
prometheusClient prometheusserviceiface.PrometheusServiceAPI,
Expand All @@ -52,8 +46,6 @@ func NewClient(
logger: logger,
taggingAPI: taggingAPI,
autoscalingAPI: autoscalingAPI,
apiGatewayAPI: apiGatewayAPI,
apiGatewayV2API: apiGatewayV2API,
ec2API: ec2API,
dmsAPI: dmsClient,
prometheusSvcAPI: prometheusClient,
Expand Down
55 changes: 0 additions & 55 deletions pkg/clients/tagging/v1/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@ package v1
import (
"context"
"fmt"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/apigateway"
"github.com/aws/aws-sdk-go/service/apigatewayv2"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/databasemigrationservice"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/prometheusservice"
"github.com/aws/aws-sdk-go/service/shield"
"github.com/aws/aws-sdk-go/service/storagegateway"
"github.com/grafana/regexp"

"github.com/nerdswords/yet-another-cloudwatch-exporter/pkg/config"
"github.com/nerdswords/yet-another-cloudwatch-exporter/pkg/model"
Expand All @@ -32,57 +28,6 @@ type ServiceFilter struct {

// ServiceFilters maps a service namespace to (optional) ServiceFilter
var ServiceFilters = map[string]ServiceFilter{
"AWS/ApiGateway": {
FilterFunc: func(ctx context.Context, client client, inputResources []*model.TaggedResource) ([]*model.TaggedResource, error) {
promutil.APIGatewayAPICounter.Inc()
const maxPages = 10

var (
limit int64 = 500 // max number of results per page. default=25, max=500
input = apigateway.GetRestApisInput{Limit: &limit}
output = apigateway.GetRestApisOutput{}
pageNum int
outputResources []*model.TaggedResource
)

err := client.apiGatewayAPI.GetRestApisPagesWithContext(ctx, &input, func(page *apigateway.GetRestApisOutput, lastPage bool) bool {
promutil.APIGatewayAPICounter.Inc()
pageNum++
output.Items = append(output.Items, page.Items...)
return pageNum <= maxPages
})
if err != nil {
return nil, fmt.Errorf("error calling apiGatewayAPI.GetRestApisPages, %w", err)
}
outputV2, err := client.apiGatewayV2API.GetApisWithContext(ctx, &apigatewayv2.GetApisInput{})
promutil.APIGatewayAPIV2Counter.Inc()
if err != nil {
return nil, fmt.Errorf("error calling apiGatewayAPIv2.GetApis, %w", err)
}

for _, resource := range inputResources {
for i, gw := range output.Items {
searchString := regexp.MustCompile(fmt.Sprintf(".*restapis/%s$", *gw.Id))
if searchString.MatchString(resource.ARN) {
r := resource
r.ARN = strings.ReplaceAll(resource.ARN, *gw.Id, *gw.Name)
outputResources = append(outputResources, r)
output.Items = append(output.Items[:i], output.Items[i+1:]...)
break
}
}
for i, gw := range outputV2.Items {
searchString := regexp.MustCompile(fmt.Sprintf(".*apis/%s$", *gw.ApiId))
if searchString.MatchString(resource.ARN) {
outputResources = append(outputResources, resource)
outputV2.Items = append(outputV2.Items[:i], outputV2.Items[i+1:]...)
break
}
}
}
return outputResources, nil
},
},
"AWS/AutoScaling": {
ResourceFunc: func(ctx context.Context, client client, job *config.Job, region string) ([]*model.TaggedResource, error) {
pageNum := 0
Expand Down
191 changes: 0 additions & 191 deletions pkg/clients/tagging/v1/filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/apigateway"
"github.com/aws/aws-sdk-go/service/apigateway/apigatewayiface"
"github.com/aws/aws-sdk-go/service/apigatewayv2"
"github.com/aws/aws-sdk-go/service/apigatewayv2/apigatewayv2iface"
"github.com/aws/aws-sdk-go/service/databasemigrationservice"
"github.com/aws/aws-sdk-go/service/databasemigrationservice/databasemigrationserviceiface"

Expand All @@ -32,174 +28,6 @@ func TestValidServiceNames(t *testing.T) {
}
}

func TestApiGatewayFilterFunc(t *testing.T) {
tests := []struct {
name string
iface client
inputResources []*model.TaggedResource
outputResources []*model.TaggedResource
}{
{
"api gateway resources skip stages",
client{
apiGatewayAPI: apiGatewayClient{
getRestApisOutput: &apigateway.GetRestApisOutput{
Items: []*apigateway.RestApi{
{
ApiKeySource: nil,
BinaryMediaTypes: nil,
CreatedDate: nil,
Description: nil,
DisableExecuteApiEndpoint: nil,
EndpointConfiguration: nil,
Id: aws.String("gwid1234"),
MinimumCompressionSize: nil,
Name: aws.String("apiname"),
Policy: nil,
Tags: nil,
Version: nil,
Warnings: nil,
},
},
Position: nil,
},
},
apiGatewayV2API: apiGatewayV2Client{
getRestApisOutput: &apigatewayv2.GetApisOutput{
Items: []*apigatewayv2.Api{},
},
},
},
[]*model.TaggedResource{
{
ARN: "arn:aws:apigateway:us-east-1::/restapis/gwid1234/stages/main",
Namespace: "apigateway",
Region: "us-east-1",
Tags: []model.Tag{
{
Key: "Test",
Value: "Value",
},
},
},
{
ARN: "arn:aws:apigateway:us-east-1::/restapis/gwid1234",
Namespace: "apigateway",
Region: "us-east-1",
Tags: []model.Tag{
{
Key: "Test",
Value: "Value 2",
},
},
},
},
[]*model.TaggedResource{
{
ARN: "arn:aws:apigateway:us-east-1::/restapis/apiname",
Namespace: "apigateway",
Region: "us-east-1",
Tags: []model.Tag{
{
Key: "Test",
Value: "Value 2",
},
},
},
},
},
{
"api gateway v2",
client{
apiGatewayAPI: apiGatewayClient{
getRestApisOutput: &apigateway.GetRestApisOutput{
Items: []*apigateway.RestApi{},
},
},
apiGatewayV2API: apiGatewayV2Client{
getRestApisOutput: &apigatewayv2.GetApisOutput{
Items: []*apigatewayv2.Api{
{
CreatedDate: nil,
Description: nil,
DisableExecuteApiEndpoint: nil,
ApiId: aws.String("gwid9876"),
Name: aws.String("apiv2name"),
Tags: nil,
Version: nil,
Warnings: nil,
},
},
NextToken: nil,
},
},
},
[]*model.TaggedResource{
{
ARN: "arn:aws:apigateway:us-east-1::/apis/gwid9876/stages/$default",
Namespace: "apigateway",
Region: "us-east-1",
Tags: []model.Tag{
{
Key: "Test",
Value: "Value",
},
},
},
{
ARN: "arn:aws:apigateway:us-east-1::/apis/gwid9876",
Namespace: "apigateway",
Region: "us-east-1",
Tags: []model.Tag{
{
Key: "Test",
Value: "Value 2",
},
},
},
},
[]*model.TaggedResource{
{
ARN: "arn:aws:apigateway:us-east-1::/apis/gwid9876",
Namespace: "apigateway",
Region: "us-east-1",
Tags: []model.Tag{
{
Key: "Test",
Value: "Value 2",
},
},
},
},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
apigateway := ServiceFilters["AWS/ApiGateway"]

outputResources, err := apigateway.FilterFunc(context.Background(), test.iface, test.inputResources)
if err != nil {
t.Logf("Error from FilterFunc: %v", err)
t.FailNow()
}
if len(outputResources) != len(test.outputResources) {
t.Logf("len(outputResources) = %d, want %d", len(outputResources), len(test.outputResources))
t.Fail()
}
for i, resource := range outputResources {
if len(test.outputResources) <= i {
break
}
wantResource := *test.outputResources[i]
if !reflect.DeepEqual(*resource, wantResource) {
t.Errorf("outputResources[%d] = %+v, want %+v", i, *resource, wantResource)
}
}
})
}
}

func TestDMSFilterFunc(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -430,22 +258,3 @@ func (dms dmsClient) DescribeReplicationTasksPagesWithContext(_ aws.Context, _ *
fn(dms.describeReplicationTasksOutput, true)
return nil
}

type apiGatewayClient struct {
apigatewayiface.APIGatewayAPI
getRestApisOutput *apigateway.GetRestApisOutput
}

func (apigateway apiGatewayClient) GetRestApisPagesWithContext(_ aws.Context, _ *apigateway.GetRestApisInput, fn func(*apigateway.GetRestApisOutput, bool) bool, _ ...request.Option) error {
fn(apigateway.getRestApisOutput, true)
return nil
}

type apiGatewayV2Client struct {
apigatewayv2iface.ApiGatewayV2API
getRestApisOutput *apigatewayv2.GetApisOutput
}

func (apigateway apiGatewayV2Client) GetApisWithContext(_ aws.Context, _ *apigatewayv2.GetApisInput, _ ...request.Option) (*apigatewayv2.GetApisOutput, error) {
return apigateway.getRestApisOutput, nil
}
8 changes: 0 additions & 8 deletions pkg/clients/tagging/v2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/amp"
"github.com/aws/aws-sdk-go-v2/service/apigateway"
"github.com/aws/aws-sdk-go-v2/service/apigatewayv2"
"github.com/aws/aws-sdk-go-v2/service/autoscaling"
"github.com/aws/aws-sdk-go-v2/service/databasemigrationservice"
"github.com/aws/aws-sdk-go-v2/service/ec2"
Expand All @@ -26,8 +24,6 @@ type client struct {
logger logging.Logger
taggingAPI *resourcegroupstaggingapi.Client
autoscalingAPI *autoscaling.Client
apiGatewayAPI *apigateway.Client
apiGatewayV2API *apigatewayv2.Client
ec2API *ec2.Client
dmsAPI *databasemigrationservice.Client
prometheusSvcAPI *amp.Client
Expand All @@ -39,8 +35,6 @@ func NewClient(
logger logging.Logger,
taggingAPI *resourcegroupstaggingapi.Client,
autoscalingAPI *autoscaling.Client,
apiGatewayAPI *apigateway.Client,
apiGatewayV2API *apigatewayv2.Client,
ec2API *ec2.Client,
dmsClient *databasemigrationservice.Client,
prometheusClient *amp.Client,
Expand All @@ -51,8 +45,6 @@ func NewClient(
logger: logger,
taggingAPI: taggingAPI,
autoscalingAPI: autoscalingAPI,
apiGatewayAPI: apiGatewayAPI,
apiGatewayV2API: apiGatewayV2API,
ec2API: ec2API,
dmsAPI: dmsClient,
prometheusSvcAPI: prometheusClient,
Expand Down
Loading

0 comments on commit 3477d8e

Please sign in to comment.