|
3 | 3 | from boto3 import client, resource |
4 | 4 | from moto import mock_aws |
5 | 5 |
|
6 | | -from tests.providers.aws.utils import AWS_REGION_US_EAST_1, set_mocked_aws_provider |
| 6 | +from tests.providers.aws.utils import ( |
| 7 | + AWS_REGION_US_EAST_1, |
| 8 | + AWS_REGION_US_WEST_2, |
| 9 | + set_mocked_aws_provider, |
| 10 | +) |
7 | 11 |
|
8 | 12 | HOSTED_ZONE_NAME = "testdns.aws.com." |
9 | 13 |
|
@@ -309,7 +313,10 @@ def test_hosted_zone_eip_record(self): |
309 | 313 | from prowler.providers.aws.services.ec2.ec2_service import EC2 |
310 | 314 | from prowler.providers.aws.services.route53.route53_service import Route53 |
311 | 315 |
|
312 | | - aws_provider = set_mocked_aws_provider([AWS_REGION_US_EAST_1]) |
| 316 | + aws_provider = set_mocked_aws_provider( |
| 317 | + [AWS_REGION_US_EAST_1], |
| 318 | + expected_checks=["route53_dangling_ip_subdomain_takeover"], |
| 319 | + ) |
313 | 320 |
|
314 | 321 | with mock.patch( |
315 | 322 | "prowler.providers.common.provider.Provider.get_global_provider", |
@@ -387,7 +394,10 @@ def test_hosted_zone_eni_record(self): |
387 | 394 | from prowler.providers.aws.services.ec2.ec2_service import EC2 |
388 | 395 | from prowler.providers.aws.services.route53.route53_service import Route53 |
389 | 396 |
|
390 | | - aws_provider = set_mocked_aws_provider([AWS_REGION_US_EAST_1]) |
| 397 | + aws_provider = set_mocked_aws_provider( |
| 398 | + [AWS_REGION_US_EAST_1], |
| 399 | + expected_checks=["route53_dangling_ip_subdomain_takeover"], |
| 400 | + ) |
391 | 401 |
|
392 | 402 | with mock.patch( |
393 | 403 | "prowler.providers.common.provider.Provider.get_global_provider", |
@@ -426,3 +436,69 @@ def test_hosted_zone_eni_record(self): |
426 | 436 | result[0].resource_arn |
427 | 437 | == f"arn:{aws_provider.identity.partition}:route53:::hostedzone/{zone_id.replace('/hostedzone/', '')}" |
428 | 438 | ) |
| 439 | + |
| 440 | + @mock_aws |
| 441 | + def test_hosted_zone_eip_cross_region(self): |
| 442 | + """EIP in us-west-2 referenced by Route53 A record should PASS even when auditing us-east-1 only.""" |
| 443 | + conn = client("route53", region_name=AWS_REGION_US_EAST_1) |
| 444 | + ec2_west = client("ec2", region_name=AWS_REGION_US_WEST_2) |
| 445 | + |
| 446 | + address = "17.5.7.3" |
| 447 | + ec2_west.allocate_address(Domain="vpc", Address=address) |
| 448 | + |
| 449 | + zone_id = conn.create_hosted_zone( |
| 450 | + Name=HOSTED_ZONE_NAME, CallerReference=str(hash("foo")) |
| 451 | + )["HostedZone"]["Id"] |
| 452 | + |
| 453 | + record_set_name = "foo.bar.testdns.aws.com." |
| 454 | + record_ip = address |
| 455 | + conn.change_resource_record_sets( |
| 456 | + HostedZoneId=zone_id, |
| 457 | + ChangeBatch={ |
| 458 | + "Changes": [ |
| 459 | + { |
| 460 | + "Action": "CREATE", |
| 461 | + "ResourceRecordSet": { |
| 462 | + "Name": record_set_name, |
| 463 | + "Type": "A", |
| 464 | + "ResourceRecords": [{"Value": record_ip}], |
| 465 | + }, |
| 466 | + } |
| 467 | + ] |
| 468 | + }, |
| 469 | + ) |
| 470 | + from prowler.providers.aws.services.ec2.ec2_service import EC2 |
| 471 | + from prowler.providers.aws.services.route53.route53_service import Route53 |
| 472 | + |
| 473 | + # Audit only us-east-1 but enable both regions so Route53 finds the cross-region EIP |
| 474 | + aws_provider = set_mocked_aws_provider( |
| 475 | + audited_regions=[AWS_REGION_US_EAST_1], |
| 476 | + enabled_regions={AWS_REGION_US_EAST_1, AWS_REGION_US_WEST_2}, |
| 477 | + expected_checks=["route53_dangling_ip_subdomain_takeover"], |
| 478 | + ) |
| 479 | + |
| 480 | + with mock.patch( |
| 481 | + "prowler.providers.common.provider.Provider.get_global_provider", |
| 482 | + return_value=aws_provider, |
| 483 | + ): |
| 484 | + with mock.patch( |
| 485 | + "prowler.providers.aws.services.route53.route53_dangling_ip_subdomain_takeover.route53_dangling_ip_subdomain_takeover.route53_client", |
| 486 | + new=Route53(aws_provider), |
| 487 | + ): |
| 488 | + with mock.patch( |
| 489 | + "prowler.providers.aws.services.route53.route53_dangling_ip_subdomain_takeover.route53_dangling_ip_subdomain_takeover.ec2_client", |
| 490 | + new=EC2(aws_provider), |
| 491 | + ): |
| 492 | + from prowler.providers.aws.services.route53.route53_dangling_ip_subdomain_takeover.route53_dangling_ip_subdomain_takeover import ( |
| 493 | + route53_dangling_ip_subdomain_takeover, |
| 494 | + ) |
| 495 | + |
| 496 | + check = route53_dangling_ip_subdomain_takeover() |
| 497 | + result = check.execute() |
| 498 | + |
| 499 | + assert len(result) == 1 |
| 500 | + assert result[0].status == "PASS" |
| 501 | + assert ( |
| 502 | + result[0].status_extended |
| 503 | + == f"Route53 record {record_ip} (name: {record_set_name}) in Hosted Zone {HOSTED_ZONE_NAME} is not a dangling IP." |
| 504 | + ) |
0 commit comments