@@ -43,12 +43,61 @@ class GMNStartupChecks(django.apps.AppConfig):
4343 name = 'd1_gmn.app.startup'
4444
4545 def ready (self ):
46- self ._check_cert_file ('CLIENT_CERT_PATH' )
47- self ._check_cert_file ('CLIENT_CERT_PRIVATE_KEY_PATH' )
46+ self ._assert_readable_file_if_set ('CLIENT_CERT_PATH' )
47+ self ._assert_readable_file_if_set ('CLIENT_CERT_PRIVATE_KEY_PATH' )
48+
49+ self ._assert_is_type ('SCIMETA_VALIDATION_ENABLED' , bool )
50+ self ._assert_is_type ('SCIMETA_VALIDATION_MAX_SIZE' , int )
51+ self ._assert_is_in (
52+ 'SCIMETA_VALIDATION_OVER_SIZE_ACTION' , ('reject' , 'accept' )
53+ )
54+
4855 self ._warn_unsafe_for_prod ()
4956 self ._check_resource_map_create ()
5057 self ._create_sciobj_store_root ()
5158
59+ def _assert_is_type (self , setting_name , valid_type ):
60+ v = getattr (django .conf .settings , setting_name , None )
61+ if not isinstance (v , valid_type ):
62+ self .raise_config_error (setting_name , valid_type )
63+
64+ def _assert_is_in (self , setting_name , valid_list ):
65+ if getattr (django .conf .settings , setting_name , None ) not in valid_list :
66+ self .raise_config_error (setting_name , valid_list )
67+
68+ def _assert_readable_file_if_set (self , setting_name ):
69+ v = getattr (django .conf .settings , setting_name , None )
70+ if v is None :
71+ return
72+ self ._assert_is_type (setting_name , str )
73+ if not os .path .isfile (v ):
74+ self .raise_config_error (
75+ setting_name , v , str , valid_str = 'a path to a readable file'
76+ )
77+ try :
78+ with open (v , 'r' ) as f :
79+ f .read (1 )
80+ except EnvironmentError as e :
81+ self .raise_config_error (
82+ setting_name , v , str , valid_str = 'a path to a readable file. error="{}"'
83+ .format (str (e ))
84+ )
85+
86+ def raise_config_error (self , setting_name , cur_val , exp_type , valid_str = None ):
87+ valid_str = valid_str or (
88+ 'a whole number' if isinstance (exp_type , int ) else 'a number'
89+ if isinstance (exp_type , float ) else 'a string'
90+ if isinstance (exp_type , str ) else 'True or False' if
91+ isinstance (exp_type , bool ) else 'one of {}' .format (', ' .join (exp_type ))
92+ if isinstance (exp_type ,
93+ (list , tuple )) else 'of type {}' .format (exp_type .__name__ )
94+ )
95+ msg_str = u'Configuration error: Setting {} must be {}. current="{}"' .format (
96+ setting_name , valid_str , str (cur_val )
97+ )
98+ logging .error (msg_str )
99+ raise django .core .exceptions .ImproperlyConfigured (msg_str )
100+
52101 def _warn_unsafe_for_prod (self ):
53102 """Warn on settings that are not safe for production"""
54103 safe_settings_list = [
@@ -65,21 +114,6 @@ def _warn_unsafe_for_prod(self):
65114 'safe="{}"' .format (setting_str , setting_current , setting_safe )
66115 )
67116
68- def _check_cert_file (self , cert_pem_setting ):
69- cert_pem_path = getattr (django .conf .settings , cert_pem_setting , None )
70- if cert_pem_path is None :
71- logging .warn (
72- 'Certificate path not set. setting="{}"' .format (cert_pem_setting )
73- )
74- return
75- try :
76- self ._assert_readable_file (cert_pem_path )
77- except ValueError as e :
78- raise django .core .exceptions .ImproperlyConfigured (
79- u'Configuration error: Invalid certificate path. '
80- u'setting="{}". msg="{}"' .format (cert_pem_setting , str (e ))
81- )
82-
83117 def _check_resource_map_create (self ):
84118 if (
85119 django .conf .settings .RESOURCE_MAP_CREATE not in RESOURCE_MAP_CREATE_MODE_LIST
@@ -144,15 +178,3 @@ def _create_sciobj_store_root(self):
144178 d1_gmn .app .sciobj_store .get_gmn_version (),
145179 )
146180 )
147-
148- def _assert_readable_file (self , file_path ):
149- if not os .path .isfile (file_path ):
150- raise ValueError ('Not a valid file path. path="{}"' .format (file_path ))
151- try :
152- with open (file_path , 'r' ) as f :
153- f .read (1 )
154- except EnvironmentError as e :
155- raise ValueError (
156- 'Unable to read file. path="{}" error="{}"' .
157- format (file_path , e .message )
158- )
0 commit comments