Skip to content

Commit

Permalink
Merge pull request #582 from overmindtech/elb-fix
Browse files Browse the repository at this point in the history
Fix mapping issues
  • Loading branch information
dylanratcliffe authored Aug 14, 2024
2 parents 8f00375 + 1d6182a commit f30db14
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
version: v1.56.1
version: v1.60.1
args: --timeout 3m
skip-cache: true # the linters require all code generation and dependecies to be present, but the cache implementation completely falls over when there is already existing content. See https://github.com/golangci/golangci-lint-action/issues/23, https://github.com/golangci/golangci-lint-action/issues/863, https://github.com/golangci/golangci-lint-action/issues/984

Expand Down
5 changes: 3 additions & 2 deletions docs-data/ec2-security-group-rule.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
"searchDescription": "Search security group rules by ARN",
"group": "AWS",
"terraformQuery": [
"aws_security_group_rule.id",
"aws_security_group_rule.security_group_rule_id"
"aws_security_group_rule.security_group_rule_id",
"aws_vpc_security_group_egress_rule.security_group_rule_id",
"aws_vpc_security_group_ingress_rule.security_group_rule_id"
],
"terraformMethod": "GET",
"terraformScope": "*",
Expand Down
3 changes: 2 additions & 1 deletion docs-data/elbv2-load-balancer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"searchDescription": "Search for ELBs by ARN",
"group": "AWS",
"terraformQuery": [
"aws_lb.arn"
"aws_lb.arn",
"aws_lb.id"
],
"terraformMethod": "SEARCH",
"terraformScope": "*",
Expand Down
5 changes: 3 additions & 2 deletions docs-data/route53-resource-record-set.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
"descriptiveType": "Route53 Record Set",
"getDescription": "Get a Route53 record Set by name",
"listDescription": "List all record sets",
"searchDescription": "Search for a record set by hosted zone ID",
"searchDescription": "Search for a record set by hosted zone ID in the format \"/hostedzone/JJN928734JH7HV\" or \"JJN928734JH7HV\" or by terraform ID in the format \"{hostedZone}_{recordName}_{type}\"",
"group": "AWS",
"terraformQuery": [
"aws_route53_record.arn"
"aws_route53_record.arn",
"aws_route53_record.id"
],
"terraformMethod": "SEARCH",
"terraformScope": "*",
Expand Down
3 changes: 2 additions & 1 deletion sources/ec2/sg_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ func securityGroupRuleOutputMapper(_ context.Context, _ *ec2.Client, scope strin
// +overmind:search Search security group rules by ARN
// +overmind:group AWS
// +overmind:terraform:queryMap aws_security_group_rule.security_group_rule_id
// +overmind:terraform:queryMap aws_security_group_rule.id
// +overmind:terraform:queryMap aws_vpc_security_group_ingress_rule.security_group_rule_id
// +overmind:terraform:queryMap aws_vpc_security_group_egress_rule.security_group_rule_id

func NewSecurityGroupRuleSource(client *ec2.Client, accountID string, region string) *sources.DescribeOnlySource[*ec2.DescribeSecurityGroupRulesInput, *ec2.DescribeSecurityGroupRulesOutput, *ec2.Client, *ec2.Options] {
return &sources.DescribeOnlySource[*ec2.DescribeSecurityGroupRulesInput, *ec2.DescribeSecurityGroupRulesOutput, *ec2.Client, *ec2.Options]{
Expand Down
6 changes: 6 additions & 0 deletions sources/elbv2/elb.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ func loadBalancerOutputMapper(ctx context.Context, client elbClient, scope strin
// +overmind:search Search for ELBs by ARN
// +overmind:group AWS
// +overmind:terraform:queryMap aws_lb.arn
// +overmind:terraform:queryMap aws_lb.id
// +overmind:terraform:method SEARCH

func NewLoadBalancerSource(client elbClient, accountID string, region string) *sources.DescribeOnlySource[*elbv2.DescribeLoadBalancersInput, *elbv2.DescribeLoadBalancersOutput, elbClient, *elbv2.Options] {
Expand All @@ -289,6 +290,11 @@ func NewLoadBalancerSource(client elbClient, accountID string, region string) *s
InputMapperList: func(scope string) (*elbv2.DescribeLoadBalancersInput, error) {
return &elbv2.DescribeLoadBalancersInput{}, nil
},
InputMapperSearch: func(ctx context.Context, client elbClient, scope, query string) (*elbv2.DescribeLoadBalancersInput, error) {
return &elbv2.DescribeLoadBalancersInput{
LoadBalancerArns: []string{query},
}, nil
},
PaginatorBuilder: func(client elbClient, params *elbv2.DescribeLoadBalancersInput) sources.Paginator[*elbv2.DescribeLoadBalancersOutput, *elbv2.Options] {
return elbv2.NewDescribeLoadBalancersPaginator(client, params)
},
Expand Down
2 changes: 1 addition & 1 deletion sources/lambda/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func functionGetFunc(ctx context.Context, client LambdaClient, scope string, inp
if err == nil && policyResponse != nil && policyResponse.Policy != nil {
// Try to parse the policy
policy := PolicyDocument{}
err := json.Unmarshal([]byte(*policyResponse.Policy), &policy)
err := json.Unmarshal([]byte(*policyResponse.Policy), &policy) //nolint:all

if err == nil {
linkedItemQueries = ExtractLinksFromPolicy(&policy)
Expand Down
2 changes: 1 addition & 1 deletion sources/lambda/policy_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ var testPolicyJSON string = `{

func TestParsePolicy(t *testing.T) {
policy := PolicyDocument{}
err := json.Unmarshal([]byte(testPolicyJSON), &policy)
err := json.Unmarshal([]byte(testPolicyJSON), &policy) //nolint:all

if err != nil {
t.Error(err)
Expand Down
2 changes: 1 addition & 1 deletion sources/route53/health_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func healthCheckListFunc(ctx context.Context, client *route53.Client, scope stri
return nil, err
}

healthChecks := make([]*HealthCheck,0, len(out.HealthChecks))
healthChecks := make([]*HealthCheck, 0, len(out.HealthChecks))

for _, healthCheck := range out.HealthChecks {
status, err := client.GetHealthCheckStatus(ctx, &route53.GetHealthCheckStatusInput{
Expand Down
38 changes: 28 additions & 10 deletions sources/route53/resource_record_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package route53
import (
"context"
"errors"
"strings"

"github.com/aws/aws-sdk-go-v2/service/route53"
"github.com/aws/aws-sdk-go-v2/service/route53/types"
Expand All @@ -14,24 +15,40 @@ func resourceRecordSetGetFunc(ctx context.Context, client *route53.Client, scope
return nil, errors.New("get is not supported for route53-resource-record-set. Use search")
}

// ResourceRecordSetSearchFunc Search func that accepts a hosted zone ID as a
// query
// ResourceRecordSetSearchFunc Search func that accepts a hosted zone or a
// terraform ID in the format {hostedZone}_{recordName}_{type}
func resourceRecordSetSearchFunc(ctx context.Context, client *route53.Client, scope, query string) ([]*types.ResourceRecordSet, error) {
out, err := client.ListResourceRecordSets(ctx, &route53.ListResourceRecordSetsInput{
HostedZoneId: &query,
})
splits := strings.Split(query, "_")

var out *route53.ListResourceRecordSetsOutput
var err error
if len(splits) == 3 {
// In this case we have a terraform ID
var max int32 = 1
out, err = client.ListResourceRecordSets(ctx, &route53.ListResourceRecordSetsInput{
HostedZoneId: &splits[0],
StartRecordName: &splits[1],
StartRecordType: types.RRType(splits[2]),
MaxItems: &max,
})
} else {
// In this case we have a hosted zone ID
out, err = client.ListResourceRecordSets(ctx, &route53.ListResourceRecordSetsInput{
HostedZoneId: &query,
})
}

if err != nil {
return nil, err
}

zones := make([]*types.ResourceRecordSet, 0, len(out.ResourceRecordSets))
records := make([]*types.ResourceRecordSet, 0, len(out.ResourceRecordSets))

for _, zone := range out.ResourceRecordSets {
zones = append(zones, &zone)
for _, record := range out.ResourceRecordSets {
records = append(records, &record)
}

return zones, nil
return records, nil
}

func resourceRecordSetItemMapper(scope string, awsItem *types.ResourceRecordSet) (*sdp.Item, error) {
Expand Down Expand Up @@ -111,9 +128,10 @@ func resourceRecordSetItemMapper(scope string, awsItem *types.ResourceRecordSet)
// +overmind:descriptiveType Route53 Record Set
// +overmind:get Get a Route53 record Set by name
// +overmind:list List all record sets
// +overmind:search Search for a record set by hosted zone ID
// +overmind:search Search for a record set by hosted zone ID in the format "/hostedzone/JJN928734JH7HV" or "JJN928734JH7HV" or by terraform ID in the format "{hostedZone}_{recordName}_{type}"
// +overmind:group AWS
// +overmind:terraform:queryMap aws_route53_record.arn
// +overmind:terraform:queryMap aws_route53_record.id
// +overmind:terraform:method SEARCH

func NewResourceRecordSetSource(client *route53.Client, accountID string, region string) *sources.GetListSource[*types.ResourceRecordSet, *route53.Client, *route53.Options] {
Expand Down
58 changes: 55 additions & 3 deletions sources/route53/resource_record_set_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package route53

import (
"context"
"fmt"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -112,13 +115,62 @@ func TestResourceRecordSetItemMapper(t *testing.T) {
func TestNewResourceRecordSetSource(t *testing.T) {
client, account, region := GetAutoConfig(t)

zoneSource := NewHostedZoneSource(client, account, region)

zones, err := zoneSource.List(context.Background(), zoneSource.Scopes()[0], true)
if err != nil {
t.Fatal(err)
}

if len(zones) == 0 {
t.Skip("no zones found")
}

source := NewResourceRecordSetSource(client, account, region)

search := zones[0].UniqueAttributeValue()
test := sources.E2ETest{
Source: source,
Timeout: 10 * time.Second,
SkipGet: true,
Source: source,
Timeout: 10 * time.Second,
SkipGet: true,
GoodSearchQuery: &search,
}

test.Run(t)

items, err := source.Search(context.Background(), zoneSource.Scopes()[0], search, true)
if err != nil {
t.Fatal(err)
}

numItems := len(items)

rawZone := strings.TrimPrefix(search, "/hostedzone/")

items, err = source.Search(context.Background(), zoneSource.Scopes()[0], rawZone, true)
if err != nil {
t.Fatal(err)
}

if len(items) != numItems {
t.Errorf("expected %d items, got %d", numItems, len(items))
}

if len(items) > 0 {
item := items[0]

// Construct a terraform style ID
name, _ := item.GetAttributes().Get("name")
typ, _ := item.GetAttributes().Get("type")
search = fmt.Sprintf("%s_%s_%s", rawZone, name, typ)

items, err := source.Search(context.Background(), zoneSource.Scopes()[0], search, true)
if err != nil {
t.Fatal(err)
}

if len(items) != 1 {
t.Errorf("expected 1 item, got %d", len(items))
}
}
}

0 comments on commit f30db14

Please sign in to comment.