|
3 | 3 | )
|
4 | 4 | import importlib
|
5 | 5 | import json
|
| 6 | +import logging |
6 | 7 |
|
7 | 8 | from more_itertools import (
|
8 | 9 | one,
|
|
20 | 21 | from azul.deployment import (
|
21 | 22 | aws,
|
22 | 23 | )
|
| 24 | +from azul.http import ( |
| 25 | + http_client, |
| 26 | +) |
| 27 | +from azul.logging import ( |
| 28 | + configure_script_logging, |
| 29 | +) |
23 | 30 | from azul.modules import (
|
24 | 31 | load_app_module,
|
25 | 32 | )
|
|
35 | 42 | JSONs,
|
36 | 43 | )
|
37 | 44 |
|
| 45 | +log = logging.getLogger(__name__) |
| 46 | +configure_script_logging(log) |
| 47 | + |
38 | 48 |
|
39 | 49 | @dataclass(frozen=True)
|
40 | 50 | class Application:
|
@@ -110,6 +120,18 @@ def check_waf_rules(rules: JSONs) -> JSONs:
|
110 | 120 | return rules
|
111 | 121 |
|
112 | 122 |
|
| 123 | +def public_ip() -> str: |
| 124 | + """ |
| 125 | + Return the public IPv4 address of the machine running this code. |
| 126 | + """ |
| 127 | + url = 'https://checkip.amazonaws.com' |
| 128 | + http = http_client(log) |
| 129 | + response = http.request('GET', url) |
| 130 | + assert response.status == 200, R('Unexpected response', response) |
| 131 | + ip_address = response.data.decode().strip() |
| 132 | + return ip_address |
| 133 | + |
| 134 | + |
113 | 135 | zones_by_domain = {
|
114 | 136 | domain: Zone.for_domain(domain)
|
115 | 137 | for app in apps
|
@@ -147,6 +169,17 @@ def check_waf_rules(rules: JSONs) -> JSONs:
|
147 | 169 | emit_tf({
|
148 | 170 | 'data': [
|
149 | 171 | {
|
| 172 | + 'aws_nat_gateway': { |
| 173 | + **{ |
| 174 | + f'gitlab_{zone}': { |
| 175 | + 'filter': { |
| 176 | + 'name': 'tag:Name', |
| 177 | + 'values': [f'azul-gitlab_{zone}'] |
| 178 | + }, |
| 179 | + } |
| 180 | + for zone in range(vpc.num_zones) |
| 181 | + } |
| 182 | + }, |
150 | 183 | 'aws_route53_zone': {
|
151 | 184 | zone.slug: {
|
152 | 185 | 'name': zone.name,
|
@@ -223,6 +256,20 @@ def check_waf_rules(rules: JSONs) -> JSONs:
|
223 | 256 | ],
|
224 | 257 | 'resource': [
|
225 | 258 | {
|
| 259 | + 'aws_wafv2_ip_set': { |
| 260 | + 'it_v4_ips': { |
| 261 | + 'name': config.qualified_resource_name('it_v4_ips'), |
| 262 | + 'scope': 'REGIONAL', |
| 263 | + 'ip_address_version': 'IPV4', |
| 264 | + 'addresses': [ |
| 265 | + public_ip() + '/32', |
| 266 | + *[ |
| 267 | + f'${{data.aws_nat_gateway.gitlab_{zone}.public_ip}}/32' |
| 268 | + for zone in range(vpc.num_zones) |
| 269 | + ] |
| 270 | + ] |
| 271 | + } |
| 272 | + }, |
226 | 273 | 'aws_wafv2_web_acl': {
|
227 | 274 | 'api_gateway': {
|
228 | 275 | 'name': config.qualified_resource_name('api_gateway'),
|
@@ -501,7 +548,157 @@ def check_waf_rules(rules: JSONs) -> JSONs:
|
501 | 548 | config.waf_rate_limit_alarm,
|
502 | 549 | config.waf_rate_limit,
|
503 | 550 | ]
|
504 |
| - ] |
| 551 | + ], |
| 552 | + { |
| 553 | + 'name': 'allow_it_requests', |
| 554 | + 'statement': { |
| 555 | + 'and_statement': [ |
| 556 | + { |
| 557 | + 'statement': [ |
| 558 | + { |
| 559 | + 'ip_set_reference_statement': { |
| 560 | + 'arn': '${aws_wafv2_ip_set.%s.arn}' % 'it_v4_ips' |
| 561 | + } |
| 562 | + }, |
| 563 | + { |
| 564 | + 'byte_match_statement': { |
| 565 | + 'field_to_match': { |
| 566 | + 'method': {} |
| 567 | + }, |
| 568 | + 'positional_constraint': 'EXACTLY', |
| 569 | + 'search_string': 'PUT', |
| 570 | + 'text_transformation': { |
| 571 | + 'priority': 0, |
| 572 | + 'type': 'NONE' |
| 573 | + } |
| 574 | + } |
| 575 | + }, |
| 576 | + { |
| 577 | + 'regex_match_statement': { |
| 578 | + 'regex_string': '^(/fetch)?/manifest/files', |
| 579 | + 'field_to_match': { |
| 580 | + 'uri_path': {} |
| 581 | + }, |
| 582 | + 'text_transformation': { |
| 583 | + 'priority': 0, |
| 584 | + 'type': 'NONE' |
| 585 | + } |
| 586 | + } |
| 587 | + } |
| 588 | + ] |
| 589 | + } |
| 590 | + ] |
| 591 | + }, |
| 592 | + 'action': { |
| 593 | + 'allow': {} |
| 594 | + }, |
| 595 | + 'visibility_config': { |
| 596 | + 'metric_name': 'allow_it_requests', |
| 597 | + 'sampled_requests_enabled': True, |
| 598 | + 'cloudwatch_metrics_enabled': True |
| 599 | + } |
| 600 | + }, |
| 601 | + { |
| 602 | + 'name': config.waf_rate_limit_files.name, |
| 603 | + 'statement': { |
| 604 | + 'rate_based_statement': { |
| 605 | + 'limit': config.waf_rate_limit_files.value, |
| 606 | + 'evaluation_window_sec': config.waf_rate_limit_files.period, |
| 607 | + 'aggregate_key_type': 'IP', |
| 608 | + 'scope_down_statement': { |
| 609 | + 'regex_match_statement': { |
| 610 | + 'regex_string': '^(/fetch)?/repository/files', |
| 611 | + 'field_to_match': { |
| 612 | + 'uri_path': {} |
| 613 | + }, |
| 614 | + 'text_transformation': { |
| 615 | + 'priority': 0, |
| 616 | + 'type': 'NONE' |
| 617 | + } |
| 618 | + } |
| 619 | + } |
| 620 | + } |
| 621 | + }, |
| 622 | + 'action': { |
| 623 | + 'block': { |
| 624 | + 'custom_response': { |
| 625 | + 'response_code': 429, |
| 626 | + 'response_header': [ |
| 627 | + { |
| 628 | + 'name': 'Retry-After', |
| 629 | + 'value': str(config.waf_rate_limit_files.retry_after) |
| 630 | + } |
| 631 | + ] |
| 632 | + } |
| 633 | + } |
| 634 | + }, |
| 635 | + 'visibility_config': { |
| 636 | + 'metric_name': config.waf_rate_limit_files.name, |
| 637 | + 'sampled_requests_enabled': True, |
| 638 | + 'cloudwatch_metrics_enabled': True |
| 639 | + } |
| 640 | + }, |
| 641 | + { |
| 642 | + 'name': config.waf_rate_limit_manifests.name, |
| 643 | + 'statement': { |
| 644 | + 'rate_based_statement': { |
| 645 | + 'limit': config.waf_rate_limit_manifests.value, |
| 646 | + 'evaluation_window_sec': config.waf_rate_limit_manifests.period, |
| 647 | + 'aggregate_key_type': 'IP', |
| 648 | + 'scope_down_statement': { |
| 649 | + 'and_statement': [ |
| 650 | + { |
| 651 | + 'statement': [ |
| 652 | + { |
| 653 | + 'byte_match_statement': { |
| 654 | + 'field_to_match': { |
| 655 | + 'method': {} |
| 656 | + }, |
| 657 | + 'positional_constraint': 'EXACTLY', |
| 658 | + 'search_string': 'PUT', |
| 659 | + 'text_transformation': { |
| 660 | + 'priority': 0, |
| 661 | + 'type': 'NONE' |
| 662 | + } |
| 663 | + } |
| 664 | + }, |
| 665 | + { |
| 666 | + 'regex_match_statement': { |
| 667 | + 'regex_string': '^(/fetch)?/manifest/files', |
| 668 | + 'field_to_match': { |
| 669 | + 'uri_path': {} |
| 670 | + }, |
| 671 | + 'text_transformation': { |
| 672 | + 'priority': 0, |
| 673 | + 'type': 'NONE' |
| 674 | + } |
| 675 | + } |
| 676 | + } |
| 677 | + ] |
| 678 | + } |
| 679 | + ] |
| 680 | + } |
| 681 | + } |
| 682 | + }, |
| 683 | + 'action': { |
| 684 | + 'block': { |
| 685 | + 'custom_response': { |
| 686 | + 'response_code': 429, |
| 687 | + 'response_header': [ |
| 688 | + { |
| 689 | + 'name': 'Retry-After', |
| 690 | + 'value': str(config.waf_rate_limit_manifests.retry_after) |
| 691 | + } |
| 692 | + ] |
| 693 | + } |
| 694 | + } |
| 695 | + }, |
| 696 | + 'visibility_config': { |
| 697 | + 'metric_name': config.waf_rate_limit_manifests.name, |
| 698 | + 'sampled_requests_enabled': True, |
| 699 | + 'cloudwatch_metrics_enabled': True |
| 700 | + } |
| 701 | + } |
505 | 702 | ])
|
506 | 703 | ]),
|
507 | 704 | 'scope': 'REGIONAL',
|
|
0 commit comments