Skip to content

Commit

Permalink
feat(ec2-route-table): filter shared resource
Browse files Browse the repository at this point in the history
  • Loading branch information
ekristen committed Jan 31, 2025
1 parent a5b409c commit a21e0a7
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
7 changes: 7 additions & 0 deletions resources/ec2-route-table.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package resources

import (
"context"
"errors"
"fmt"

"github.com/gotidy/ptr"
Expand Down Expand Up @@ -55,6 +56,7 @@ func (l *EC2RouteTableLister) List(_ context.Context, o interface{}) ([]resource

resources = append(resources, &EC2RouteTable{
svc: svc,
accountID: opts.AccountID,
routeTable: out,
defaultVPC: defVpcID == ptr.ToString(out.VpcId),
vpc: vpc,
Expand All @@ -67,6 +69,7 @@ func (l *EC2RouteTableLister) List(_ context.Context, o interface{}) ([]resource

type EC2RouteTable struct {
svc *ec2.EC2
accountID *string
routeTable *ec2.RouteTable
defaultVPC bool
vpc *ec2.Vpc
Expand All @@ -80,6 +83,10 @@ func (r *EC2RouteTable) Filter() error {
}
}

if ptr.ToString(r.vpc.OwnerId) != ptr.ToString(r.accountID) {
return errors.New("not owned by account, likely shared")
}

return nil
}

Expand Down
45 changes: 45 additions & 0 deletions resources/ec2-route-table_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package resources

import (
"testing"

"github.com/aws/aws-sdk-go/service/ec2"
"github.com/gotidy/ptr"
"github.com/stretchr/testify/assert"
)

func Test_EC2RouteTable_Filter(t *testing.T) {
cases := []struct {
ownerID *string
accountID *string
filtered bool
}{
{
ownerID: ptr.String("123456789012"),
accountID: ptr.String("123456789012"),
filtered: false,
},
{
ownerID: ptr.String("123456789012"),
accountID: ptr.String("123456789013"),
filtered: true,
},
}

for _, c := range cases {
r := EC2RouteTable{
svc: nil,
accountID: c.accountID,
routeTable: &ec2.RouteTable{OwnerId: c.ownerID},
vpc: &ec2.Vpc{VpcId: ptr.String("vpc-12345678"), OwnerId: c.ownerID},
}

err := r.Filter()

if c.filtered {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
}
}

0 comments on commit a21e0a7

Please sign in to comment.