1010
1111def load_env_file (run_type , file_string ):
1212 """
13- This method loads a YAML config file containing environment variables,
14- decrypts whichever are encrypted, and puts them all into os.environ as
15- strings. For a YAML variable containing a list of values, the list is
16- exported into os.environ as a json string and should be loaded as such.
13+ This method reads a YAML config file containing environment variables and
14+ loads them all into os.environ as strings. See _parse_yaml_dict for more.
1715
18- It requires the YAML file to be split into a 'PLAINTEXT_VARIABLES' section
19- and an 'ENCRYPTED_VARIABLES' section. See config/sample.yaml for an example
20- config file.
16+ If the config file is divided into 'PLAINTEXT_VARIABLES' and
17+ 'ENCRYPTED_VARIABLES' sections (see config/sample.yaml for an exmaple), the
18+ 'ENCRYPTED_VARIABLES' variables will be decrypted first. Otherwise, all
19+ variables will be loaded as is.
2120
2221 Parameters
2322 ----------
@@ -36,31 +35,53 @@ def load_env_file(run_type, file_string):
3635 try :
3736 env_dict = yaml .safe_load (env_stream )
3837 except yaml .YAMLError :
39- logger .error ('Invalid YAML file: {}' .format (open_file ))
4038 raise ConfigHelperError (
4139 'Invalid YAML file: {}' .format (open_file )) from None
4240 except FileNotFoundError :
43- logger .error ('Could not find config file {}' .format (open_file ))
4441 raise ConfigHelperError (
4542 'Could not find config file {}' .format (open_file )) from None
4643
4744 if env_dict :
48- for key , value in env_dict .get ('PLAINTEXT_VARIABLES' , {}).items ():
49- if type (value ) is list :
50- os .environ [key ] = json .dumps (value )
51- else :
52- os .environ [key ] = str (value )
53-
54- kms_client = KmsClient ()
55- for key , value in env_dict .get ('ENCRYPTED_VARIABLES' , {}).items ():
56- if type (value ) is list :
57- decrypted_list = [kms_client .decrypt (v ) for v in value ]
58- os .environ [key ] = json .dumps (decrypted_list )
59- else :
60- os .environ [key ] = kms_client .decrypt (value )
61- kms_client .close ()
45+ if ('PLAINTEXT_VARIABLES' in env_dict
46+ or 'ENCRYPTED_VARIABLES' in env_dict ):
47+ _parse_yaml_dict (env_dict .get ('PLAINTEXT_VARIABLES' , {}))
48+
49+ kms_client = KmsClient ()
50+ _parse_yaml_dict (env_dict .get (
51+ 'ENCRYPTED_VARIABLES' , {}), kms_client )
52+ kms_client .close ()
53+ else :
54+ _parse_yaml_dict (env_dict )
55+
56+
57+ def _parse_yaml_dict (yaml_dict , kms_client = None ):
58+ """
59+ Loads YAML dict into os.environ. All values are stored as strings to match
60+ how AWS Lambda environment variables are stored. For list variables, the
61+ list is exported into os.environ as a json string.
62+
63+ If kms_client is not empty, decrypts the variables first.
64+
65+ Does not allow for sub-dictionaries.
66+ """
67+ for key , value in yaml_dict .items ():
68+ if type (value ) is dict :
69+ raise ConfigHelperError (
70+ 'Found sub-dictionary in YAML config' ) from None
71+ elif type (value ) is list :
72+ val = [kms_client .decrypt (v )
73+ for v in value ] if kms_client else value
74+ os .environ [key ] = json .dumps (val )
75+ else :
76+ val = kms_client .decrypt (value ) if kms_client else value
77+ os .environ [key ] = str (val )
6278
6379
6480class ConfigHelperError (Exception ):
65- def __init__ (self , message = None ):
81+ def __init__ (self , message = '' , logger = None ):
6682 self .message = message
83+ if logger is not None :
84+ logger .error (message )
85+
86+ def __str__ (self ):
87+ return self .message
0 commit comments