77
88from pyms .constants import CONFIGMAP_FILE_ENVIRONMENT , LOGGER_NAME , DEFAULT_CONFIGMAP_FILENAME
99from pyms .exceptions import AttrDoesNotExistException , ConfigDoesNotFoundException
10- from pyms .utils .crypt import Crypt
1110from pyms .utils .files import LoadFile
1211
1312logger = logging .getLogger (LOGGER_NAME )
@@ -22,6 +21,7 @@ class ConfFile(dict):
2221 * config: Allow to pass a dictionary to ConfFile without use a file
2322 """
2423 _empty_init = False
24+ _crypt = None
2525
2626 def __init__ (self , * args , ** kwargs ):
2727 """
@@ -35,7 +35,9 @@ def __init__(self, *args, **kwargs):
3535 ```
3636 """
3737 self ._loader = LoadFile (kwargs .get ("path" ), CONFIGMAP_FILE_ENVIRONMENT , DEFAULT_CONFIGMAP_FILENAME )
38- self ._crypt = Crypt (path = kwargs .get ("path" ))
38+ self ._crypt_cls = kwargs .get ("crypt" )
39+ if self ._crypt_cls :
40+ self ._crypt = self ._crypt_cls (path = kwargs .get ("path" ))
3941 self ._empty_init = kwargs .get ("empty_init" , False )
4042 config = kwargs .get ("config" )
4143 if config is None :
@@ -52,7 +54,7 @@ def __init__(self, *args, **kwargs):
5254 super (ConfFile , self ).__init__ (config )
5355
5456 def to_flask (self ) -> Dict :
55- return ConfFile (config = {k .upper (): v for k , v in self .items ()})
57+ return ConfFile (config = {k .upper (): v for k , v in self .items ()}, crypt = self . _crypt_cls )
5658
5759 def set_config (self , config : Dict ) -> Dict :
5860 """
@@ -63,10 +65,14 @@ def set_config(self, config: Dict) -> Dict:
6365 """
6466 config = dict (self .normalize_config (config ))
6567 pop_encripted_keys = []
68+ add_decripted_keys = []
6669 for k , v in config .items ():
6770 if k .lower ().startswith ("enc_" ):
6871 k_not_crypt = re .compile (re .escape ('enc_' ), re .IGNORECASE )
69- setattr (self , k_not_crypt .sub ('' , k ), self ._crypt .decrypt (v ))
72+ decrypted_key = k_not_crypt .sub ('' , k )
73+ decrypted_value = self ._crypt .decrypt (v ) if self ._crypt else None
74+ setattr (self , decrypted_key , decrypted_value )
75+ add_decripted_keys .append ((decrypted_key , decrypted_value ))
7076 pop_encripted_keys .append (k )
7177 else :
7278 setattr (self , k , v )
@@ -75,12 +81,15 @@ def set_config(self, config: Dict) -> Dict:
7581 for x in pop_encripted_keys :
7682 config .pop (x )
7783
84+ for k , v in add_decripted_keys :
85+ config [k ] = v
86+
7887 return config
7988
8089 def normalize_config (self , config : Dict ) -> Iterable [Tuple [Text , Union [Dict , Text , bool ]]]:
8190 for key , item in config .items ():
8291 if isinstance (item , dict ):
83- item = ConfFile (config = item , empty_init = self ._empty_init )
92+ item = ConfFile (config = item , empty_init = self ._empty_init , crypt = self . _crypt_cls )
8493 yield self .normalize_keys (key ), item
8594
8695 @staticmethod
@@ -103,7 +112,7 @@ def __getattr__(self, name, *args, **kwargs):
103112 return aux_dict
104113 except KeyError :
105114 if self ._empty_init :
106- return ConfFile (config = {}, empty_init = self ._empty_init )
115+ return ConfFile (config = {}, empty_init = self ._empty_init , crypt = self . _crypt_cls )
107116 raise AttrDoesNotExistException ("Variable {} not exist in the config file" .format (name ))
108117
109118 def reload (self ):
0 commit comments