| 1 | package awsjob |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "net/netip" |
| 7 | "reflect" |
| 8 | "slices" |
| 9 | "testing" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/isolatedjob" |
| 12 | "github.com/aws/aws-sdk-go-v2/aws" |
| 13 | "github.com/aws/aws-sdk-go-v2/service/ec2" |
| 14 | ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types" |
| 15 | "github.com/aws/aws-sdk-go-v2/service/lambdacore" |
| 16 | coretypes "github.com/aws/aws-sdk-go-v2/service/lambdacore/types" |
| 17 | ) |
| 18 | |
| 19 | // The EC2 half of stub. A nil func field answers with the network |
| 20 | // testConfig describes, set up correctly. |
| 21 | |
| 22 | func (s *stub) DescribeVpcAttribute(_ context.Context, in *ec2.DescribeVpcAttributeInput, _ ...func(*ec2.Options)) (*ec2.DescribeVpcAttributeOutput, error) { |
| 23 | s.record("DescribeVpcAttribute") |
| 24 | if s.vpcAttr != nil { |
| 25 | return s.vpcAttr(in) |
| 26 | } |
| 27 | return vpcAttr(in, false, false), nil |
| 28 | } |
| 29 | |
| 30 | func (s *stub) DescribeSubnets(_ context.Context, in *ec2.DescribeSubnetsInput, _ ...func(*ec2.Options)) (*ec2.DescribeSubnetsOutput, error) { |
| 31 | s.record("DescribeSubnets") |
| 32 | if s.subnets != nil { |
| 33 | return s.subnets(in) |
| 34 | } |
| 35 | return &ec2.DescribeSubnetsOutput{Subnets: []ec2types.Subnet{{SubnetId: aws.String("subnet-1"), VpcId: aws.String("vpc-1")}}}, nil |
| 36 | } |
| 37 | |
| 38 | func (s *stub) DescribeRouteTables(_ context.Context, in *ec2.DescribeRouteTablesInput, _ ...func(*ec2.Options)) (*ec2.DescribeRouteTablesOutput, error) { |
| 39 | s.record("DescribeRouteTables") |
| 40 | if s.routeTables != nil { |
| 41 | return s.routeTables(in) |
| 42 | } |
| 43 | if filterValue(in.Filters, "association.subnet-id") != "subnet-1" { |
| 44 | return &ec2.DescribeRouteTablesOutput{}, nil |
| 45 | } |
| 46 | return &ec2.DescribeRouteTablesOutput{RouteTables: []ec2types.RouteTable{localTable()}}, nil |
| 47 | } |
| 48 | |
| 49 | func (s *stub) DescribeSecurityGroups(_ context.Context, in *ec2.DescribeSecurityGroupsInput, _ ...func(*ec2.Options)) (*ec2.DescribeSecurityGroupsOutput, error) { |
| 50 | s.record("DescribeSecurityGroups") |
| 51 | if s.securityGroups != nil { |
| 52 | return s.securityGroups(in) |
| 53 | } |
| 54 | return &ec2.DescribeSecurityGroupsOutput{SecurityGroups: []ec2types.SecurityGroup{goodGroup()}}, nil |
| 55 | } |
| 56 | |
| 57 | func (s *stub) GetNetworkConnector(_ context.Context, in *lambdacore.GetNetworkConnectorInput, _ ...func(*lambdacore.Options)) (*lambdacore.GetNetworkConnectorOutput, error) { |
| 58 | s.record("GetNetworkConnector") |
| 59 | if s.connector != nil { |
| 60 | return s.connector(in) |
| 61 | } |
| 62 | return goodConnector(), nil |
| 63 | } |
| 64 | |
| 65 | // goodConnector is the pinned connector version as testConfig expects it. |
| 66 | func goodConnector() *lambdacore.GetNetworkConnectorOutput { |
| 67 | return &lambdacore.GetNetworkConnectorOutput{ |
| 68 | Arn: aws.String("arn:aws:lambda:eu-west-1:123456789012:network-connector:nc-1"), |
| 69 | VersionArn: aws.String(testConnector), |
| 70 | Version: aws.Int64(3), |
| 71 | State: coretypes.NetworkConnectorStateActive, |
| 72 | Configuration: &coretypes.NetworkConnectorConfigurationMemberVpcEgressConfiguration{Value: coretypes.NetworkConnectorVpcEgressConfiguration{ |
| 73 | AssociatedComputeResourceTypes: []coretypes.ComputeResourceType{coretypes.ComputeResourceTypeMicroVm}, |
| 74 | SubnetIds: []string{"subnet-1"}, |
| 75 | SecurityGroupIds: []string{"sg-1"}, |
| 76 | NetworkProtocol: coretypes.NetworkProtocolIPv4, |
| 77 | }}, |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | func vpcAttr(in *ec2.DescribeVpcAttributeInput, support, hostnames bool) *ec2.DescribeVpcAttributeOutput { |
| 82 | out := &ec2.DescribeVpcAttributeOutput{VpcId: in.VpcId} |
| 83 | switch in.Attribute { |
| 84 | case ec2types.VpcAttributeNameEnableDnsSupport: |
| 85 | out.EnableDnsSupport = &ec2types.AttributeBooleanValue{Value: aws.Bool(support)} |
| 86 | case ec2types.VpcAttributeNameEnableDnsHostnames: |
| 87 | out.EnableDnsHostnames = &ec2types.AttributeBooleanValue{Value: aws.Bool(hostnames)} |
| 88 | } |
| 89 | return out |
| 90 | } |
| 91 | |
| 92 | func filterValue(filters []ec2types.Filter, name string) string { |
| 93 | for _, f := range filters { |
| 94 | if aws.ToString(f.Name) == name && len(f.Values) == 1 { |
| 95 | return f.Values[0] |
| 96 | } |
| 97 | } |
| 98 | return "" |
| 99 | } |
| 100 | |
| 101 | func localTable() ec2types.RouteTable { |
| 102 | return ec2types.RouteTable{ |
| 103 | RouteTableId: aws.String("rtb-1"), |
| 104 | VpcId: aws.String("vpc-1"), |
| 105 | Routes: []ec2types.Route{ |
| 106 | {DestinationCidrBlock: aws.String("10.0.0.0/16"), GatewayId: aws.String("local")}, |
| 107 | {DestinationIpv6CidrBlock: aws.String("2001:db8::/56"), GatewayId: aws.String("local")}, |
| 108 | }, |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | func hostRule(port int32, cidrs ...string) ec2types.IpPermission { |
| 113 | p := ec2types.IpPermission{IpProtocol: aws.String("tcp"), FromPort: aws.Int32(port), ToPort: aws.Int32(port)} |
| 114 | for _, c := range cidrs { |
| 115 | p.IpRanges = append(p.IpRanges, ec2types.IpRange{CidrIp: aws.String(c)}) |
| 116 | } |
| 117 | return p |
| 118 | } |
| 119 | |
| 120 | func goodGroup() ec2types.SecurityGroup { |
| 121 | return ec2types.SecurityGroup{ |
| 122 | GroupId: aws.String("sg-1"), |
| 123 | VpcId: aws.String("vpc-1"), |
| 124 | IpPermissionsEgress: []ec2types.IpPermission{ |
| 125 | hostRule(3128, "10.0.1.10/32"), |
| 126 | hostRule(443, "10.0.1.11/32"), |
| 127 | }, |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | func TestAuditNetwork(t *testing.T) { |
| 132 | want := testConfig().Egress |
| 133 | withGroup := func(change func(*ec2types.SecurityGroup)) func(*ec2.DescribeSecurityGroupsInput) (*ec2.DescribeSecurityGroupsOutput, error) { |
| 134 | return func(*ec2.DescribeSecurityGroupsInput) (*ec2.DescribeSecurityGroupsOutput, error) { |
| 135 | g := goodGroup() |
| 136 | change(&g) |
| 137 | return &ec2.DescribeSecurityGroupsOutput{SecurityGroups: []ec2types.SecurityGroup{g}}, nil |
| 138 | } |
| 139 | } |
| 140 | withRoutes := func(routes ...ec2types.Route) func(*ec2.DescribeRouteTablesInput) (*ec2.DescribeRouteTablesOutput, error) { |
| 141 | return func(*ec2.DescribeRouteTablesInput) (*ec2.DescribeRouteTablesOutput, error) { |
| 142 | rt := localTable() |
| 143 | rt.Routes = append(rt.Routes, routes...) |
| 144 | return &ec2.DescribeRouteTablesOutput{RouteTables: []ec2types.RouteTable{rt}}, nil |
| 145 | } |
| 146 | } |
| 147 | withConnector := func(change func(*coretypes.NetworkConnectorVpcEgressConfiguration)) func(*lambdacore.GetNetworkConnectorInput) (*lambdacore.GetNetworkConnectorOutput, error) { |
| 148 | return func(*lambdacore.GetNetworkConnectorInput) (*lambdacore.GetNetworkConnectorOutput, error) { |
| 149 | out := goodConnector() |
| 150 | change(&out.Configuration.(*coretypes.NetworkConnectorConfigurationMemberVpcEgressConfiguration).Value) |
| 151 | return out, nil |
| 152 | } |
| 153 | } |
| 154 | withConnectorOut := func(change func(*lambdacore.GetNetworkConnectorOutput)) func(*lambdacore.GetNetworkConnectorInput) (*lambdacore.GetNetworkConnectorOutput, error) { |
| 155 | return func(*lambdacore.GetNetworkConnectorInput) (*lambdacore.GetNetworkConnectorOutput, error) { |
| 156 | out := goodConnector() |
| 157 | change(out) |
| 158 | return out, nil |
| 159 | } |
| 160 | } |
| 161 | good := isolatedjob.NetworkAudit{ResolverDisabled: true, IngressClosed: true, RouteOutAbsent: true, EgressOnly: want} |
| 162 | |
| 163 | rows := []struct { |
| 164 | name string |
| 165 | setup func(*stub) |
| 166 | want isolatedjob.NetworkAudit |
| 167 | }{ |
| 168 | {"all good", func(*stub) {}, good}, |
| 169 | {"dns support on", func(s *stub) { |
| 170 | s.vpcAttr = func(in *ec2.DescribeVpcAttributeInput) (*ec2.DescribeVpcAttributeOutput, error) { |
| 171 | return vpcAttr(in, true, false), nil |
| 172 | } |
| 173 | }, isolatedjob.NetworkAudit{IngressClosed: true, RouteOutAbsent: true, EgressOnly: want}}, |
| 174 | {"dns hostnames on", func(s *stub) { |
| 175 | s.vpcAttr = func(in *ec2.DescribeVpcAttributeInput) (*ec2.DescribeVpcAttributeOutput, error) { |
| 176 | return vpcAttr(in, false, true), nil |
| 177 | } |
| 178 | }, isolatedjob.NetworkAudit{IngressClosed: true, RouteOutAbsent: true, EgressOnly: want}}, |
| 179 | {"dns value missing", func(s *stub) { |
| 180 | s.vpcAttr = func(in *ec2.DescribeVpcAttributeInput) (*ec2.DescribeVpcAttributeOutput, error) { |
| 181 | return &ec2.DescribeVpcAttributeOutput{VpcId: in.VpcId}, nil |
| 182 | } |
| 183 | }, isolatedjob.NetworkAudit{IngressClosed: true, RouteOutAbsent: true, EgressOnly: want}}, |
| 184 | {"ingress rule", func(s *stub) { |
| 185 | s.securityGroups = withGroup(func(g *ec2types.SecurityGroup) { |
| 186 | g.IpPermissions = []ec2types.IpPermission{hostRule(22, "10.0.0.0/16")} |
| 187 | }) |
| 188 | }, isolatedjob.NetworkAudit{ResolverDisabled: true, RouteOutAbsent: true, EgressOnly: want}}, |
| 189 | {"internet gateway route", func(s *stub) { |
| 190 | s.routeTables = withRoutes(ec2types.Route{DestinationCidrBlock: aws.String("0.0.0.0/0"), GatewayId: aws.String("igw-1")}) |
| 191 | }, isolatedjob.NetworkAudit{ResolverDisabled: true, IngressClosed: true, EgressOnly: want}}, |
| 192 | {"nat route", func(s *stub) { |
| 193 | s.routeTables = withRoutes(ec2types.Route{DestinationCidrBlock: aws.String("0.0.0.0/0"), NatGatewayId: aws.String("nat-1")}) |
| 194 | }, isolatedjob.NetworkAudit{ResolverDisabled: true, IngressClosed: true, EgressOnly: want}}, |
| 195 | {"egress-only gateway route", func(s *stub) { |
| 196 | s.routeTables = withRoutes(ec2types.Route{DestinationIpv6CidrBlock: aws.String("::/0"), EgressOnlyInternetGatewayId: aws.String("eigw-1")}) |
| 197 | }, isolatedjob.NetworkAudit{ResolverDisabled: true, IngressClosed: true, EgressOnly: want}}, |
| 198 | {"local route with a second target", func(s *stub) { |
| 199 | s.routeTables = withRoutes(ec2types.Route{DestinationCidrBlock: aws.String("10.9.0.0/16"), GatewayId: aws.String("local"), TransitGatewayId: aws.String("tgw-1")}) |
| 200 | }, isolatedjob.NetworkAudit{ResolverDisabled: true, IngressClosed: true, EgressOnly: want}}, |
| 201 | {"connector with Elastic IP type None", func(s *stub) { |
| 202 | s.connector = withConnector(func(c *coretypes.NetworkConnectorVpcEgressConfiguration) { |
| 203 | c.ElasticIpConfiguration = &coretypes.ElasticIpConfiguration{Type: coretypes.ElasticIpTypeNone} |
| 204 | }) |
| 205 | }, good}, |
| 206 | {"main table when no explicit association", func(s *stub) { |
| 207 | s.routeTables = func(in *ec2.DescribeRouteTablesInput) (*ec2.DescribeRouteTablesOutput, error) { |
| 208 | if filterValue(in.Filters, "association.main") == "true" && filterValue(in.Filters, "vpc-id") == "vpc-1" { |
| 209 | return &ec2.DescribeRouteTablesOutput{RouteTables: []ec2types.RouteTable{localTable()}}, nil |
| 210 | } |
| 211 | return &ec2.DescribeRouteTablesOutput{}, nil |
| 212 | } |
| 213 | }, good}, |
| 214 | {"route tables over two pages", func(s *stub) { |
| 215 | s.routeTables = func(in *ec2.DescribeRouteTablesInput) (*ec2.DescribeRouteTablesOutput, error) { |
| 216 | if in.NextToken == nil { |
| 217 | return &ec2.DescribeRouteTablesOutput{NextToken: aws.String("p2")}, nil |
| 218 | } |
| 219 | return withRoutes(ec2types.Route{DestinationCidrBlock: aws.String("0.0.0.0/0"), GatewayId: aws.String("igw-1")})(in) |
| 220 | } |
| 221 | }, isolatedjob.NetworkAudit{ResolverDisabled: true, IngressClosed: true, EgressOnly: want}}, |
| 222 | {"no route table found", func(s *stub) { |
| 223 | s.routeTables = func(*ec2.DescribeRouteTablesInput) (*ec2.DescribeRouteTablesOutput, error) { |
| 224 | return &ec2.DescribeRouteTablesOutput{}, nil |
| 225 | } |
| 226 | }, isolatedjob.NetworkAudit{ResolverDisabled: true, IngressClosed: true, EgressOnly: want}}, |
| 227 | {"extra egress host", func(s *stub) { |
| 228 | s.securityGroups = withGroup(func(g *ec2types.SecurityGroup) { |
| 229 | g.IpPermissionsEgress = append(g.IpPermissionsEgress, hostRule(443, "10.0.1.12/32")) |
| 230 | }) |
| 231 | }, isolatedjob.NetworkAudit{ResolverDisabled: true, IngressClosed: true, RouteOutAbsent: true, |
| 232 | EgressOnly: append(slices.Clone(want), netip.MustParseAddrPort("10.0.1.12:443"))}}, |
| 233 | {"one egress host missing", func(s *stub) { |
| 234 | s.securityGroups = withGroup(func(g *ec2types.SecurityGroup) { g.IpPermissionsEgress = g.IpPermissionsEgress[:1] }) |
| 235 | }, isolatedjob.NetworkAudit{ResolverDisabled: true, IngressClosed: true, RouteOutAbsent: true, EgressOnly: want[:1]}}, |
| 236 | {"ipv6 host and protocol 6", func(s *stub) { |
| 237 | s.securityGroups = withGroup(func(g *ec2types.SecurityGroup) { |
| 238 | g.IpPermissionsEgress = append(g.IpPermissionsEgress, ec2types.IpPermission{IpProtocol: aws.String("6"), |
| 239 | FromPort: aws.Int32(443), ToPort: aws.Int32(443), Ipv6Ranges: []ec2types.Ipv6Range{{CidrIpv6: aws.String("2001:db8::5/128")}}}) |
| 240 | }) |
| 241 | }, isolatedjob.NetworkAudit{ResolverDisabled: true, IngressClosed: true, RouteOutAbsent: true, |
| 242 | EgressOnly: append(slices.Clone(want), netip.MustParseAddrPort("[2001:db8::5]:443"))}}, |
| 243 | } |
| 244 | for _, row := range rows { |
| 245 | t.Run(row.name, func(t *testing.T) { |
| 246 | s := &stub{} |
| 247 | row.setup(s) |
| 248 | got, err := newTestRunner(t, s).AuditNetwork(context.Background()) |
| 249 | if err != nil { |
| 250 | t.Fatalf("AuditNetwork = %v", err) |
| 251 | } |
| 252 | if !reflect.DeepEqual(got, row.want) { |
| 253 | t.Errorf("AuditNetwork = %+v, want %+v", got, row.want) |
| 254 | } |
| 255 | if passes := got.OK(want) == nil; passes != reflect.DeepEqual(row.want, good) { |
| 256 | t.Errorf("OK(want) passes = %v for %s", passes, row.name) |
| 257 | } |
| 258 | }) |
| 259 | } |
| 260 | |
| 261 | // Slice 1a: no egress configured and a group with no rules at all. |
| 262 | t.Run("no egress against a group with no rules", func(t *testing.T) { |
| 263 | cfg := testConfig() |
| 264 | cfg.Egress = nil |
| 265 | r, err := New(cfg, &stub{securityGroups: withGroup(func(g *ec2types.SecurityGroup) { g.IpPermissionsEgress = nil })}) |
| 266 | if err != nil { |
| 267 | t.Fatalf("New(no egress) = %v", err) |
| 268 | } |
| 269 | got, err := r.AuditNetwork(context.Background()) |
| 270 | if err != nil || len(got.EgressOnly) != 0 || got.OK(cfg.Egress) != nil { |
| 271 | t.Fatalf("AuditNetwork = (%+v, %v), want no egress and a pass; OK = %v", got, err, got.OK(cfg.Egress)) |
| 272 | } |
| 273 | if got.OK(want) == nil { |
| 274 | t.Fatal("a group with no rules passes against a non-empty egress set") |
| 275 | } |
| 276 | }) |
| 277 | |
| 278 | t.Run("reads the configured ids", func(t *testing.T) { |
| 279 | var attrs []ec2types.VpcAttributeName |
| 280 | s := &stub{ |
| 281 | connector: func(in *lambdacore.GetNetworkConnectorInput) (*lambdacore.GetNetworkConnectorOutput, error) { |
| 282 | if aws.ToString(in.Identifier) != testConnector { |
| 283 | t.Errorf("GetNetworkConnector identifier = %q, want the pinned %q", aws.ToString(in.Identifier), testConnector) |
| 284 | } |
| 285 | return goodConnector(), nil |
| 286 | }, |
| 287 | vpcAttr: func(in *ec2.DescribeVpcAttributeInput) (*ec2.DescribeVpcAttributeOutput, error) { |
| 288 | if aws.ToString(in.VpcId) != "vpc-1" { |
| 289 | t.Errorf("DescribeVpcAttribute vpc = %q, want vpc-1", aws.ToString(in.VpcId)) |
| 290 | } |
| 291 | attrs = append(attrs, in.Attribute) |
| 292 | return vpcAttr(in, false, false), nil |
| 293 | }, |
| 294 | subnets: func(in *ec2.DescribeSubnetsInput) (*ec2.DescribeSubnetsOutput, error) { |
| 295 | if !reflect.DeepEqual(in.SubnetIds, []string{"subnet-1"}) { |
| 296 | t.Errorf("DescribeSubnets ids = %q, want [subnet-1]", in.SubnetIds) |
| 297 | } |
| 298 | return &ec2.DescribeSubnetsOutput{Subnets: []ec2types.Subnet{{SubnetId: aws.String("subnet-1"), VpcId: aws.String("vpc-1")}}}, nil |
| 299 | }, |
| 300 | securityGroups: func(in *ec2.DescribeSecurityGroupsInput) (*ec2.DescribeSecurityGroupsOutput, error) { |
| 301 | if !reflect.DeepEqual(in.GroupIds, []string{"sg-1"}) { |
| 302 | t.Errorf("DescribeSecurityGroups ids = %q, want [sg-1]", in.GroupIds) |
| 303 | } |
| 304 | return &ec2.DescribeSecurityGroupsOutput{SecurityGroups: []ec2types.SecurityGroup{goodGroup()}}, nil |
| 305 | }, |
| 306 | } |
| 307 | if _, err := newTestRunner(t, s).AuditNetwork(context.Background()); err != nil { |
| 308 | t.Fatalf("AuditNetwork = %v", err) |
| 309 | } |
| 310 | slices.Sort(attrs) |
| 311 | if want := []ec2types.VpcAttributeName{ec2types.VpcAttributeNameEnableDnsHostnames, ec2types.VpcAttributeNameEnableDnsSupport}; !reflect.DeepEqual(attrs, want) { |
| 312 | t.Errorf("VPC attributes read = %v, want %v", attrs, want) |
| 313 | } |
| 314 | }) |
| 315 | |
| 316 | refusals := map[string]func(*stub){ |
| 317 | "connector read fails": func(s *stub) { |
| 318 | s.connector = func(*lambdacore.GetNetworkConnectorInput) (*lambdacore.GetNetworkConnectorOutput, error) { |
| 319 | return nil, errors.New("denied") |
| 320 | } |
| 321 | }, |
| 322 | "connector answers for another version": func(s *stub) { |
| 323 | s.connector = withConnectorOut(func(o *lambdacore.GetNetworkConnectorOutput) { |
| 324 | o.VersionArn = aws.String("arn:aws:lambda:eu-west-1:123456789012:network-connector:nc-1:4") |
| 325 | }) |
| 326 | }, |
| 327 | "connector answers unqualified": func(s *stub) { |
| 328 | s.connector = withConnectorOut(func(o *lambdacore.GetNetworkConnectorOutput) { o.VersionArn = nil }) |
| 329 | }, |
| 330 | "connector not active": func(s *stub) { |
| 331 | s.connector = withConnectorOut(func(o *lambdacore.GetNetworkConnectorOutput) { o.State = coretypes.NetworkConnectorStateFailed }) |
| 332 | }, |
| 333 | "connector of an unknown type": func(s *stub) { |
| 334 | s.connector = withConnectorOut(func(o *lambdacore.GetNetworkConnectorOutput) { |
| 335 | o.Configuration = &coretypes.UnknownUnionMember{Tag: "SomethingNew"} |
| 336 | }) |
| 337 | }, |
| 338 | "connector with no configuration": func(s *stub) { |
| 339 | s.connector = withConnectorOut(func(o *lambdacore.GetNetworkConnectorOutput) { o.Configuration = nil }) |
| 340 | }, |
| 341 | "connector for functions": func(s *stub) { |
| 342 | s.connector = withConnector(func(c *coretypes.NetworkConnectorVpcEgressConfiguration) { |
| 343 | c.AssociatedComputeResourceTypes = []coretypes.ComputeResourceType{coretypes.ComputeResourceTypeWebFunction} |
| 344 | }) |
| 345 | }, |
| 346 | "connector in another subnet": func(s *stub) { |
| 347 | s.connector = withConnector(func(c *coretypes.NetworkConnectorVpcEgressConfiguration) { c.SubnetIds = []string{"subnet-9"} }) |
| 348 | }, |
| 349 | "connector with an extra subnet": func(s *stub) { |
| 350 | s.connector = withConnector(func(c *coretypes.NetworkConnectorVpcEgressConfiguration) { |
| 351 | c.SubnetIds = []string{"subnet-1", "subnet-9"} |
| 352 | }) |
| 353 | }, |
| 354 | "connector with another group": func(s *stub) { |
| 355 | s.connector = withConnector(func(c *coretypes.NetworkConnectorVpcEgressConfiguration) { c.SecurityGroupIds = []string{"sg-9"} }) |
| 356 | }, |
| 357 | "connector with an extra group": func(s *stub) { |
| 358 | s.connector = withConnector(func(c *coretypes.NetworkConnectorVpcEgressConfiguration) { |
| 359 | c.SecurityGroupIds = []string{"sg-1", "sg-open"} |
| 360 | }) |
| 361 | }, |
| 362 | "connector with service-managed Elastic IPs": func(s *stub) { |
| 363 | s.connector = withConnector(func(c *coretypes.NetworkConnectorVpcEgressConfiguration) { |
| 364 | c.ElasticIpConfiguration = &coretypes.ElasticIpConfiguration{Type: coretypes.ElasticIpTypeServiceManaged} |
| 365 | }) |
| 366 | }, |
| 367 | "vpc attribute read fails": func(s *stub) { |
| 368 | s.vpcAttr = func(*ec2.DescribeVpcAttributeInput) (*ec2.DescribeVpcAttributeOutput, error) { |
| 369 | return nil, errors.New("denied") |
| 370 | } |
| 371 | }, |
| 372 | "subnet read fails": func(s *stub) { |
| 373 | s.subnets = func(*ec2.DescribeSubnetsInput) (*ec2.DescribeSubnetsOutput, error) { return nil, errors.New("denied") } |
| 374 | }, |
| 375 | "route table read fails": func(s *stub) { |
| 376 | s.routeTables = func(*ec2.DescribeRouteTablesInput) (*ec2.DescribeRouteTablesOutput, error) { |
| 377 | return nil, errors.New("denied") |
| 378 | } |
| 379 | }, |
| 380 | "security group read fails": func(s *stub) { |
| 381 | s.securityGroups = func(*ec2.DescribeSecurityGroupsInput) (*ec2.DescribeSecurityGroupsOutput, error) { |
| 382 | return nil, errors.New("denied") |
| 383 | } |
| 384 | }, |
| 385 | "subnet in another vpc": func(s *stub) { |
| 386 | s.subnets = func(*ec2.DescribeSubnetsInput) (*ec2.DescribeSubnetsOutput, error) { |
| 387 | return &ec2.DescribeSubnetsOutput{Subnets: []ec2types.Subnet{{SubnetId: aws.String("subnet-1"), VpcId: aws.String("vpc-2")}}}, nil |
| 388 | } |
| 389 | }, |
| 390 | "subnet not found": func(s *stub) { |
| 391 | s.subnets = func(*ec2.DescribeSubnetsInput) (*ec2.DescribeSubnetsOutput, error) { |
| 392 | return &ec2.DescribeSubnetsOutput{}, nil |
| 393 | } |
| 394 | }, |
| 395 | "group in another vpc": func(s *stub) { |
| 396 | s.securityGroups = withGroup(func(g *ec2types.SecurityGroup) { g.VpcId = aws.String("vpc-2") }) |
| 397 | }, |
| 398 | "group not found": func(s *stub) { |
| 399 | s.securityGroups = func(*ec2.DescribeSecurityGroupsInput) (*ec2.DescribeSecurityGroupsOutput, error) { |
| 400 | return &ec2.DescribeSecurityGroupsOutput{}, nil |
| 401 | } |
| 402 | }, |
| 403 | "two explicit route tables": func(s *stub) { |
| 404 | s.routeTables = func(*ec2.DescribeRouteTablesInput) (*ec2.DescribeRouteTablesOutput, error) { |
| 405 | return &ec2.DescribeRouteTablesOutput{RouteTables: []ec2types.RouteTable{localTable(), localTable()}}, nil |
| 406 | } |
| 407 | }, |
| 408 | "egress to a network": func(s *stub) { |
| 409 | s.securityGroups = withGroup(func(g *ec2types.SecurityGroup) { |
| 410 | g.IpPermissionsEgress = append(g.IpPermissionsEgress, hostRule(443, "0.0.0.0/0")) |
| 411 | }) |
| 412 | }, |
| 413 | "egress to every protocol": func(s *stub) { |
| 414 | s.securityGroups = withGroup(func(g *ec2types.SecurityGroup) { |
| 415 | g.IpPermissionsEgress = append(g.IpPermissionsEgress, ec2types.IpPermission{IpProtocol: aws.String("-1"), |
| 416 | IpRanges: []ec2types.IpRange{{CidrIp: aws.String("10.0.1.10/32")}}}) |
| 417 | }) |
| 418 | }, |
| 419 | "egress over a port range": func(s *stub) { |
| 420 | s.securityGroups = withGroup(func(g *ec2types.SecurityGroup) { |
| 421 | r := hostRule(443, "10.0.1.10/32") |
| 422 | r.ToPort = aws.Int32(444) |
| 423 | g.IpPermissionsEgress = append(g.IpPermissionsEgress, r) |
| 424 | }) |
| 425 | }, |
| 426 | "egress over udp": func(s *stub) { |
| 427 | s.securityGroups = withGroup(func(g *ec2types.SecurityGroup) { |
| 428 | r := hostRule(53, "10.0.1.10/32") |
| 429 | r.IpProtocol = aws.String("udp") |
| 430 | g.IpPermissionsEgress = append(g.IpPermissionsEgress, r) |
| 431 | }) |
| 432 | }, |
| 433 | "egress to a prefix list": func(s *stub) { |
| 434 | s.securityGroups = withGroup(func(g *ec2types.SecurityGroup) { |
| 435 | r := hostRule(443) |
| 436 | r.PrefixListIds = []ec2types.PrefixListId{{PrefixListId: aws.String("pl-1")}} |
| 437 | g.IpPermissionsEgress = append(g.IpPermissionsEgress, r) |
| 438 | }) |
| 439 | }, |
| 440 | "egress to a group": func(s *stub) { |
| 441 | s.securityGroups = withGroup(func(g *ec2types.SecurityGroup) { |
| 442 | r := hostRule(443) |
| 443 | r.UserIdGroupPairs = []ec2types.UserIdGroupPair{{GroupId: aws.String("sg-2")}} |
| 444 | g.IpPermissionsEgress = append(g.IpPermissionsEgress, r) |
| 445 | }) |
| 446 | }, |
| 447 | } |
| 448 | for name, setup := range refusals { |
| 449 | t.Run(name, func(t *testing.T) { |
| 450 | s := &stub{} |
| 451 | setup(s) |
| 452 | got, err := newTestRunner(t, s).AuditNetwork(context.Background()) |
| 453 | if err == nil { |
| 454 | t.Fatalf("AuditNetwork = %+v, nil; want an error", got) |
| 455 | } |
| 456 | if !reflect.DeepEqual(got, isolatedjob.NetworkAudit{}) { |
| 457 | t.Errorf("AuditNetwork with an error = %+v, want the zero audit (every property failed)", got) |
| 458 | } |
| 459 | }) |
| 460 | } |
| 461 | } |
| 462 | |