|
12 | 12 | from contextlib import suppress |
13 | 13 | from importlib import import_module |
14 | 14 | from importlib.metadata import entry_points |
15 | | -from logging import getLogger |
16 | 15 | from pathlib import Path |
17 | 16 |
|
18 | 17 | from cryptography.fernet import Fernet |
@@ -629,6 +628,48 @@ def enable_v4_hook(settings): |
629 | 628 | return data |
630 | 629 |
|
631 | 630 |
|
| 631 | +def api_root_hook(settings): |
| 632 | + # protocol://host:port/{API_ROOT}{domain}/api/{version}/ |
| 633 | + # All of the below are DEPRECATED, and should be replaced by calling |
| 634 | + # pulpcore.plugin.find_url.find_api_root() (q.v.) |
| 635 | + if settings.API_ROOT_REWRITE_HEADER: |
| 636 | + api_root = "/<path:api_root>/" |
| 637 | + else: |
| 638 | + api_root = settings.API_ROOT |
| 639 | + return { |
| 640 | + "V3_API_ROOT": api_root + "api/v3/", |
| 641 | + "V3_DOMAIN_API_ROOT": api_root + "<slug:pulp_domain>/api/v3/", |
| 642 | + "V3_API_ROOT_NO_FRONT_SLASH": (api_root + "api/v3/").lstrip("/"), |
| 643 | + "V3_DOMAIN_API_ROOT_NO_FRONT_SLASH": (api_root + "<slug:pulp_domain>/api/v3/").lstrip("/"), |
| 644 | + } |
| 645 | + |
| 646 | + |
| 647 | +def forbidden_checksums_hook(settings): |
| 648 | + return { |
| 649 | + "FORBIDDEN_CHECKSUMS": sorted( |
| 650 | + set(constants.ALL_KNOWN_CONTENT_CHECKSUMS).difference( |
| 651 | + settings.ALLOWED_CONTENT_CHECKSUMS |
| 652 | + ) |
| 653 | + ), |
| 654 | + } |
| 655 | + |
| 656 | + |
| 657 | +def db_encryption_key_hook(settings): |
| 658 | + if Path(sys.argv[0]).name in ["pytest", "sphinx-build"] or ( |
| 659 | + len(sys.argv) >= 2 and sys.argv[1] in ["collectstatic", "openapi"] |
| 660 | + ): |
| 661 | + return {} |
| 662 | + try: |
| 663 | + with open(settings.DB_ENCRYPTION_KEY, "rb") as key_file: |
| 664 | + Fernet(key_file.read()) |
| 665 | + except Exception as ex: |
| 666 | + raise ImproperlyConfigured( |
| 667 | + "Could not load DB_ENCRYPTION_KEY file '{file}': {err}".format( |
| 668 | + file=settings.DB_ENCRYPTION_KEY, err=ex |
| 669 | + ) |
| 670 | + ) |
| 671 | + return {} |
| 672 | + |
632 | 673 | del preload_settings |
633 | 674 |
|
634 | 675 | settings = DjangoDynaconf( |
@@ -657,40 +698,11 @@ def enable_v4_hook(settings): |
657 | 698 | otel_middleware_hook, |
658 | 699 | saml2_settings_hook, |
659 | 700 | enable_v4_hook, |
| 701 | + api_root_hook, |
| 702 | + forbidden_checksums_hook, |
| 703 | + db_encryption_key_hook, |
660 | 704 | ), |
661 | 705 | dynaboxify=False, |
662 | 706 | ) |
663 | 707 |
|
664 | | -_logger = getLogger(__name__) |
665 | | - |
666 | | - |
667 | | -if not ( |
668 | | - Path(sys.argv[0]).name in ["pytest", "sphinx-build"] |
669 | | - or (len(sys.argv) >= 2 and sys.argv[1] in ["collectstatic", "openapi"]) |
670 | | -): |
671 | | - try: |
672 | | - with open(DB_ENCRYPTION_KEY, "rb") as key_file: |
673 | | - Fernet(key_file.read()) |
674 | | - except Exception as ex: |
675 | | - raise ImproperlyConfigured( |
676 | | - ("Could not load DB_ENCRYPTION_KEY file '{file}': {err}").format( |
677 | | - file=DB_ENCRYPTION_KEY, err=ex |
678 | | - ) |
679 | | - ) |
680 | | - |
681 | | - |
682 | | -FORBIDDEN_CHECKSUMS = set(constants.ALL_KNOWN_CONTENT_CHECKSUMS).difference( |
683 | | - ALLOWED_CONTENT_CHECKSUMS |
684 | | -) |
685 | | - |
686 | | -# protocol://host:port/{API_ROOT}{domain}/api/{version}/ |
687 | | -# All of the below are DEPRECATED, and should be replaced by calling |
688 | | -# pulpcore.plugin.find_url.find_api_root() (q.v.) |
689 | | -if settings.API_ROOT_REWRITE_HEADER: |
690 | | - api_root = "/<path:api_root>/" |
691 | | -else: |
692 | | - api_root = settings.API_ROOT |
693 | | -settings.set("V3_API_ROOT", api_root + "api/v3/") # Not user configurable |
694 | | -settings.set("V3_DOMAIN_API_ROOT", api_root + "<slug:pulp_domain>/api/v3/") |
695 | | -settings.set("V3_API_ROOT_NO_FRONT_SLASH", settings.V3_API_ROOT.lstrip("/")) |
696 | | -settings.set("V3_DOMAIN_API_ROOT_NO_FRONT_SLASH", settings.V3_DOMAIN_API_ROOT.lstrip("/")) |
| 708 | +# HERE ENDS DYNACONF EXTENSION LOAD (No more code below this line) |
0 commit comments