From 5a6188e2e51f2f38407ded45474649a31af246a6 Mon Sep 17 00:00:00 2001 From: rehan99000 Date: Thu, 18 Jun 2020 18:00:34 +0500 Subject: [PATCH 1/8] added logs --- .../extensions/edly_ecommerce_app/middleware.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/ecommerce/extensions/edly_ecommerce_app/middleware.py b/ecommerce/extensions/edly_ecommerce_app/middleware.py index 0e0b3ff18a4..6d3d47254ea 100644 --- a/ecommerce/extensions/edly_ecommerce_app/middleware.py +++ b/ecommerce/extensions/edly_ecommerce_app/middleware.py @@ -32,11 +32,23 @@ def process_request(self, request): if django_settings_override_values: for config_key, config_value in django_settings_override_values.items(): current_value = getattr(settings, config_key, None) + logger.debug( + 'Config key: %s ========== Config value: %s ========== Current value: %s', + config_key, + config_value, + current_value + ) if _should_extend_config(current_value, config_value): current_value.extend(config_value) setattr(settings, config_key, current_value) else: setattr(settings, config_key, config_value) + + logger.debug( + 'After set ========== Config key: %s ========== set value: %s', + config_key, + getattr(settings, config_key, None) + ) else: logger.warning('Site configuration for site (%s) has no django settings overrides.', current_site) From 681c1a7954702fc1967c0b865f77beb34e24927d Mon Sep 17 00:00:00 2001 From: rehan99000 Date: Thu, 18 Jun 2020 18:31:25 +0500 Subject: [PATCH 2/8] allowed hosts solution test --- .../extensions/edly_ecommerce_app/helpers.py | 41 +++++++++++++++++++ .../edly_ecommerce_app/middleware.py | 11 ----- ecommerce/settings/base.py | 3 ++ 3 files changed, 44 insertions(+), 11 deletions(-) diff --git a/ecommerce/extensions/edly_ecommerce_app/helpers.py b/ecommerce/extensions/edly_ecommerce_app/helpers.py index 1d9f0a9a7ff..a7f6de0ad83 100644 --- a/ecommerce/extensions/edly_ecommerce_app/helpers.py +++ b/ecommerce/extensions/edly_ecommerce_app/helpers.py @@ -109,3 +109,44 @@ def user_is_course_creator(request): decoded_cookie_data = decode_edly_user_info_cookie(edly_user_info_cookie) return decoded_cookie_data.get('is_course_creator', False) + + +class AllowedHosts(object): + __slots__ = ('defaults', 'sites', 'cache') + + def __init__(self, defaults=None, cache=True): + self.defaults = defaults or () + self.sites = None + self.cache = cache + + def get_sites(self): + if self.cache is True and self.sites is not None: + return self.sites + self.defaults + + from django.contrib.sites.models import Site, SITE_CACHE + sites = Site.objects.all() + self.sites = tuple(site.domain for site in sites) + + # fill Site.objects.get_current()'s cache for the lifetime + # of this process. Probably. + if self.cache is True: + for site_to_cache in sites: + if site_to_cache.pk not in SITE_CACHE: + SITE_CACHE[site_to_cache.pk] = site_to_cache + + return self.sites + self.defaults + + def __iter__(self): + return iter(self.get_sites()) + + def __str__(self): + return ', '.join(self.get_sites()) + + def __contains__(self, other): + return other in self.get_sites() + + def __len__(self): + return len(self.get_sites()) + + def __add__(self, other): + return self.__class__(defaults=self.defaults + other.defaults) diff --git a/ecommerce/extensions/edly_ecommerce_app/middleware.py b/ecommerce/extensions/edly_ecommerce_app/middleware.py index 6d3d47254ea..632b64be8f1 100644 --- a/ecommerce/extensions/edly_ecommerce_app/middleware.py +++ b/ecommerce/extensions/edly_ecommerce_app/middleware.py @@ -32,23 +32,12 @@ def process_request(self, request): if django_settings_override_values: for config_key, config_value in django_settings_override_values.items(): current_value = getattr(settings, config_key, None) - logger.debug( - 'Config key: %s ========== Config value: %s ========== Current value: %s', - config_key, - config_value, - current_value - ) if _should_extend_config(current_value, config_value): current_value.extend(config_value) setattr(settings, config_key, current_value) else: setattr(settings, config_key, config_value) - logger.debug( - 'After set ========== Config key: %s ========== set value: %s', - config_key, - getattr(settings, config_key, None) - ) else: logger.warning('Site configuration for site (%s) has no django settings overrides.', current_site) diff --git a/ecommerce/settings/base.py b/ecommerce/settings/base.py index cbb5f68e193..0d76cffb70a 100644 --- a/ecommerce/settings/base.py +++ b/ecommerce/settings/base.py @@ -10,6 +10,7 @@ from oscar import OSCAR_MAIN_TEMPLATE_DIR from ecommerce.settings._oscar import * +from ecommerce.extensions.edly_ecommerce_app.helpers import AllowedHosts # PATH CONFIGURATION # Absolute filesystem path to the Django project directory @@ -229,6 +230,8 @@ # Hosts/domain names that are valid for this site # See https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts ALLOWED_HOSTS = [] +ALLOWED_HOSTS += AllowedHosts(defaults=(), cache=True) + # END SITE CONFIGURATION From 4b4f9beec54e865f3a5e174c5b9cccd9d954b17c Mon Sep 17 00:00:00 2001 From: rehan99000 Date: Thu, 18 Jun 2020 18:50:57 +0500 Subject: [PATCH 3/8] updated settings --- ecommerce/settings/base.py | 2 -- ecommerce/settings/production.py | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/ecommerce/settings/base.py b/ecommerce/settings/base.py index 0d76cffb70a..741f83e1d9a 100644 --- a/ecommerce/settings/base.py +++ b/ecommerce/settings/base.py @@ -230,8 +230,6 @@ # Hosts/domain names that are valid for this site # See https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts ALLOWED_HOSTS = [] -ALLOWED_HOSTS += AllowedHosts(defaults=(), cache=True) - # END SITE CONFIGURATION diff --git a/ecommerce/settings/production.py b/ecommerce/settings/production.py index ef2d57d52a3..8bbddd50010 100644 --- a/ecommerce/settings/production.py +++ b/ecommerce/settings/production.py @@ -36,7 +36,7 @@ def get_env_setting(setting): # HOST CONFIGURATION # See: https://docs.djangoproject.com/en/1.5/releases/1.5/#allowed-hosts-required-in-production -ALLOWED_HOSTS = ['*'] +ALLOWED_HOSTS = AllowedHosts(defaults=('*'), cache=True) # END HOST CONFIGURATION # Keep track of the names of settings that represent dicts. Instead of overriding the values in base.py, From e6ef2af3049a42ca4c57182be52a52df80e9f44f Mon Sep 17 00:00:00 2001 From: Burhan Nasir Date: Fri, 19 Jun 2020 10:29:58 +0500 Subject: [PATCH 4/8] Add Allowed Site library --- ecommerce/settings/production.py | 4 +++- requirements/base.txt | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/ecommerce/settings/production.py b/ecommerce/settings/production.py index 8bbddd50010..04e0d849eb4 100644 --- a/ecommerce/settings/production.py +++ b/ecommerce/settings/production.py @@ -8,6 +8,7 @@ # into your settings, but ImproperlyConfigured is an exception. from django.core.exceptions import ImproperlyConfigured +from allowedsites import AllowedSites from ecommerce.settings.base import * # Protocol used for construcing absolute callback URLs @@ -36,7 +37,8 @@ def get_env_setting(setting): # HOST CONFIGURATION # See: https://docs.djangoproject.com/en/1.5/releases/1.5/#allowed-hosts-required-in-production -ALLOWED_HOSTS = AllowedHosts(defaults=('*'), cache=True) +ALLOWED_HOSTS = AllowedSites(defaults=('.multisitestage.edly.io',)) + # END HOST CONFIGURATION # Keep track of the names of settings that represent dicts. Instead of overriding the values in base.py, diff --git a/requirements/base.txt b/requirements/base.txt index 28172533e7a..66c684d9215 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -122,3 +122,4 @@ unidecode==0.4.21 # via django-oscar uritemplate==3.0.0 # via coreapi urllib3==1.25.6 # via requests zeep==2.1.1 +django-allowedsites==0.1.0 From 92c5db13f59ad8705bb84d817e2cf01600e28d51 Mon Sep 17 00:00:00 2001 From: rehan99000 Date: Fri, 19 Jun 2020 11:08:43 +0500 Subject: [PATCH 5/8] added ALLOWED_HOSTS dynamically --- ecommerce/settings/base.py | 1 + ecommerce/settings/production.py | 11 +++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/ecommerce/settings/base.py b/ecommerce/settings/base.py index 741f83e1d9a..ec94f5f0de3 100644 --- a/ecommerce/settings/base.py +++ b/ecommerce/settings/base.py @@ -378,6 +378,7 @@ 'rest_framework_swagger', 'django_sites_extensions', 'corsheaders', + 'allowedsites', ] # Apps specific to this project go here. diff --git a/ecommerce/settings/production.py b/ecommerce/settings/production.py index 04e0d849eb4..2e726a64e77 100644 --- a/ecommerce/settings/production.py +++ b/ecommerce/settings/production.py @@ -37,8 +37,7 @@ def get_env_setting(setting): # HOST CONFIGURATION # See: https://docs.djangoproject.com/en/1.5/releases/1.5/#allowed-hosts-required-in-production -ALLOWED_HOSTS = AllowedSites(defaults=('.multisitestage.edly.io',)) - +ALLOWED_HOSTS = ['*'] # END HOST CONFIGURATION # Keep track of the names of settings that represent dicts. Instead of overriding the values in base.py, @@ -108,3 +107,11 @@ def get_env_setting(setting): # Edly configuration EDLY_COOKIE_SECRET_KEY = config_from_yaml.get('EDLY_COOKIE_SECRET_KEY', EDLY_COOKIE_SECRET_KEY) + +ALLOWED_HOSTS = AllowedSites(defaults=( + 'panel.edly.io', + 'panel.backend.edly.io', + '.edly.io', + 'ecommerce.healthcheck.local' + ) +) From f782393dc16aa8c70daa6a7cce5affbefcfdf85a Mon Sep 17 00:00:00 2001 From: rehan99000 Date: Fri, 19 Jun 2020 11:16:32 +0500 Subject: [PATCH 6/8] implemented changes --- .../extensions/edly_ecommerce_app/helpers.py | 41 ------------------- .../edly_ecommerce_app/middleware.py | 1 - ecommerce/settings/base.py | 1 - 3 files changed, 43 deletions(-) diff --git a/ecommerce/extensions/edly_ecommerce_app/helpers.py b/ecommerce/extensions/edly_ecommerce_app/helpers.py index a7f6de0ad83..1d9f0a9a7ff 100644 --- a/ecommerce/extensions/edly_ecommerce_app/helpers.py +++ b/ecommerce/extensions/edly_ecommerce_app/helpers.py @@ -109,44 +109,3 @@ def user_is_course_creator(request): decoded_cookie_data = decode_edly_user_info_cookie(edly_user_info_cookie) return decoded_cookie_data.get('is_course_creator', False) - - -class AllowedHosts(object): - __slots__ = ('defaults', 'sites', 'cache') - - def __init__(self, defaults=None, cache=True): - self.defaults = defaults or () - self.sites = None - self.cache = cache - - def get_sites(self): - if self.cache is True and self.sites is not None: - return self.sites + self.defaults - - from django.contrib.sites.models import Site, SITE_CACHE - sites = Site.objects.all() - self.sites = tuple(site.domain for site in sites) - - # fill Site.objects.get_current()'s cache for the lifetime - # of this process. Probably. - if self.cache is True: - for site_to_cache in sites: - if site_to_cache.pk not in SITE_CACHE: - SITE_CACHE[site_to_cache.pk] = site_to_cache - - return self.sites + self.defaults - - def __iter__(self): - return iter(self.get_sites()) - - def __str__(self): - return ', '.join(self.get_sites()) - - def __contains__(self, other): - return other in self.get_sites() - - def __len__(self): - return len(self.get_sites()) - - def __add__(self, other): - return self.__class__(defaults=self.defaults + other.defaults) diff --git a/ecommerce/extensions/edly_ecommerce_app/middleware.py b/ecommerce/extensions/edly_ecommerce_app/middleware.py index 632b64be8f1..0e0b3ff18a4 100644 --- a/ecommerce/extensions/edly_ecommerce_app/middleware.py +++ b/ecommerce/extensions/edly_ecommerce_app/middleware.py @@ -37,7 +37,6 @@ def process_request(self, request): setattr(settings, config_key, current_value) else: setattr(settings, config_key, config_value) - else: logger.warning('Site configuration for site (%s) has no django settings overrides.', current_site) diff --git a/ecommerce/settings/base.py b/ecommerce/settings/base.py index ec94f5f0de3..272309f6647 100644 --- a/ecommerce/settings/base.py +++ b/ecommerce/settings/base.py @@ -10,7 +10,6 @@ from oscar import OSCAR_MAIN_TEMPLATE_DIR from ecommerce.settings._oscar import * -from ecommerce.extensions.edly_ecommerce_app.helpers import AllowedHosts # PATH CONFIGURATION # Absolute filesystem path to the Django project directory From 17f1ed7fbb42bcaa9eba89d3179453daa919223e Mon Sep 17 00:00:00 2001 From: rehan99000 Date: Fri, 19 Jun 2020 11:27:30 +0500 Subject: [PATCH 7/8] updated ordering of imports --- ecommerce/settings/production.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ecommerce/settings/production.py b/ecommerce/settings/production.py index 2e726a64e77..e041d647c92 100644 --- a/ecommerce/settings/production.py +++ b/ecommerce/settings/production.py @@ -8,8 +8,8 @@ # into your settings, but ImproperlyConfigured is an exception. from django.core.exceptions import ImproperlyConfigured -from allowedsites import AllowedSites from ecommerce.settings.base import * +from allowedsites import AllowedSites # Protocol used for construcing absolute callback URLs PROTOCOL = 'https' From e137ddac23ff8e9c7ee4bc330b5dd315c5664c25 Mon Sep 17 00:00:00 2001 From: rehan99000 Date: Fri, 19 Jun 2020 11:41:31 +0500 Subject: [PATCH 8/8] changed to inline impor --- ecommerce/settings/production.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ecommerce/settings/production.py b/ecommerce/settings/production.py index e041d647c92..a8d0dca51d8 100644 --- a/ecommerce/settings/production.py +++ b/ecommerce/settings/production.py @@ -9,7 +9,6 @@ from django.core.exceptions import ImproperlyConfigured from ecommerce.settings.base import * -from allowedsites import AllowedSites # Protocol used for construcing absolute callback URLs PROTOCOL = 'https' @@ -108,6 +107,7 @@ def get_env_setting(setting): # Edly configuration EDLY_COOKIE_SECRET_KEY = config_from_yaml.get('EDLY_COOKIE_SECRET_KEY', EDLY_COOKIE_SECRET_KEY) +from allowedsites import AllowedSites ALLOWED_HOSTS = AllowedSites(defaults=( 'panel.edly.io', 'panel.backend.edly.io',