fb6b6425d43cb7ad19daa16fecc8078f242ee541
[sfa.git] / sfa / trust / trustedroots.py
1 import os.path
2 import glob
3
4 from sfa.trust.gid import GID
5 from sfa.util.sfalogging import logger
6
7 class TrustedRoots:
8     
9     # we want to avoid reading all files in the directory
10     # this is because it's common to have backups of all kinds
11     # e.g. *~, *.hide, *-00, *.bak and the like
12     supported_extensions= [ 'gid', 'cert', 'pem' ]
13
14     def __init__(self, dir):
15         self.basedir = dir
16         # create the directory to hold the files, if not existing
17         if not os.path.isdir (self.basedir):
18             os.makedirs(self.basedir)
19
20     def add_gid(self, gid):
21         fn = os.path.join(self.basedir, gid.get_hrn() + ".gid")
22         gid.save_to_file(fn)
23
24     def get_list(self):
25         gid_list = [GID(filename=cert_file) for cert_file in self.get_file_list()]
26         return gid_list
27
28     def get_file_list(self):
29         file_list  = []
30         pattern = os.path.join(self.basedir,"*")
31         for cert_file in glob.glob(pattern):
32             if os.path.isfile(cert_file):
33                 if self.has_supported_extension(cert_file):
34                     file_list.append(cert_file) 
35                 else:
36                     logger.warning("File {} ignored - supported extensions are {}"
37                                    .format(cert_file, TrustedRoots.supported_extensions))
38         return file_list
39
40     def has_supported_extension (self,path):
41         (_,ext)=os.path.splitext(path)
42         ext=ext.replace('.','').lower()
43         return ext in TrustedRoots.supported_extensions