2323logging .basicConfig (format = '%(levelname)s: %(message)s' , level = logging .DEBUG )
2424
2525
26+ def get_config (path ):
27+ """Start with vanilla settings and override them with anything in the yaml file."""
28+
29+ config = {
30+ 'stores' : ['CA' , 'ROOT' , 'MY' ],
31+ 'filters' : None
32+ }
33+
34+ if path is not None :
35+ try :
36+ with open (path , 'r' , encoding = 'utf-8' ) as fp :
37+ config .update (yaml .load (fp , Loader = yaml .FullLoader ))
38+ except FileNotFoundError as e :
39+ logger .debug (str (e ))
40+ sys .exit ('Cannot find config file: %s' % args .config )
41+ return config
42+
43+
2644def parse_cert (cert ):
2745 """Given a certificate, get all of the interesting information we want
2846 in order to help manage the renewal process for energy markets.
@@ -41,6 +59,11 @@ def parse_cert(cert):
4159 if duns :
4260 duns = str (duns [1 ])
4361
62+ try :
63+ organization = cert .subject .get_attributes_for_oid (NameOID .ORGANIZATION_NAME )[0 ].value
64+ except IndexError :
65+ organization = None
66+
4467 # Everything else is straightforward.
4568 results = {
4669 'DUNS' : duns ,
@@ -49,29 +72,17 @@ def parse_cert(cert):
4972 'Valid From' : cert .not_valid_before .strftime ('%Y-%m-%d' ),
5073 'Valid To' : cert .not_valid_after .strftime ('%Y-%m-%d' ),
5174 'Subject' : cert .subject ,
52- 'Organization' : cert . subject . get_attributes_for_oid ( NameOID . ORGANIZATION_NAME )[ 0 ]. value
75+ 'Organization' : organization
5376 }
5477
5578 return results
5679
5780
58- def find_certs (options ):
81+ def find_certs (config ):
5982 """Search the local machine stores for certificates and store the
6083 interesting ones in a .csv file."""
6184 # pylint: disable=unused-variable
6285
63- if options .config is not None :
64- try :
65- with open (options .config , 'r' , encoding = 'utf-8' ) as fp :
66- config = yaml .load (fp , Loader = yaml .FullLoader )
67- except FileNotFoundError as e :
68- logger .debug (str (e ))
69- sys .exit ('Cannot find config file: %s' % args .config )
70- else :
71- sys .exit ('For now, you need to have a config file.' )
72-
73- config ['args' ] = args
74-
7586 fieldnames = [
7687 'Store' ,
7788 'Market' ,
@@ -97,6 +108,17 @@ def find_certs(options):
97108 except IndexError :
98109 continue
99110
111+ if config ['filters' ] is None :
112+ # keep it
113+ cert_info = parse_cert (certificate )
114+ cert_info .update ({
115+ 'Store' : store ,
116+ 'Issuer' : issuer ,
117+ 'Market' : None
118+ })
119+ rows .append (cert_info )
120+ continue
121+
100122 # Keep any of the matching issuers
101123 for market in config ['filters' ]:
102124
@@ -176,4 +198,7 @@ def find_certs(options):
176198 args .verbose = max (args .verbose , 0 )
177199 logger .setLevel (log_levels [args .verbose ])
178200
179- find_certs (args )
201+ app_config = get_config (args .config )
202+ app_config ['args' ] = args
203+
204+ find_certs (app_config )
0 commit comments