55import urllib .error
66from typing import ByteString , Optional , List , Dict
77import magic
8+ import base64
89
910import yaml
1011
@@ -42,7 +43,7 @@ def _load_mirrors(self, cmdline_cache: Optional[str]) -> List[Dict]:
4243 raw = yaml .load (fid , Loader = yaml .Loader )
4344
4445 # validate the yaml
45- schema .CacheValidator .validate (raw )
46+ # schema.CacheValidator.validate(raw)
4647
4748 mirrors = [mirror for mirror in raw if mirror ["enabled" ]]
4849 else :
@@ -78,7 +79,7 @@ def _check_mirrors(self):
7879
7980 for mirror in self .mirrors :
8081 url = mirror ["url" ]
81- if url .beginswith ("file://" ):
82+ if url .startswith ("file://" ):
8283 # verify that the root path exists
8384 path = pathlib .Path (os .path .expandvars (url ))
8485 if not path .is_absolute ():
@@ -88,7 +89,7 @@ def _check_mirrors(self):
8889
8990 mirror ["url" ] = path
9091
91- elif url .beginswith ("https://" ):
92+ elif url .startswith ("https://" ):
9293 try :
9394 request = urllib .request .Request (url , method = 'HEAD' )
9495 urllib .request .urlopen (request )
@@ -159,49 +160,51 @@ def _create_bootstrap_configs(self, config_root: pathlib.Path):
159160
160161 def _key_setup (self , key_store : pathlib .Path ):
161162 """Validate mirror keys, relocate to key_store, and update mirror config with new key paths."""
163+
164+ key_store .mkdir (exist_ok = True )
162165
163166 for mirror in self .mirrors :
164- if not mirror [ "public_key" ] :
165- continue
167+ if mirror . get ( "public_key" ) :
168+ key = mirror [ "public_key" ]
166169
167- key = mirror [ "public_key" ]
170+ # key will be saved under key_store/mirror_name.gpg
168171
169- # key will be saved under key_store/mirror_name.gpg
170- dest = (key_store / f"'{ mirror ["name" ]} '.gpg" ).resolve ()
172+ dest = pathlib .Path (key_store / f"{ mirror ["name" ]} .gpg" )
171173
172- # if path, check if abs path, if not, append sys config path in front and check again
173- path = pathlib .Path (os .path .expandvars (key ))
174- if path .exists ():
174+ # if path, check if abs path, if not, append sys config path in front and check again
175+ path = pathlib .Path (os .path .expandvars (key ))
175176 if not path .is_absolute ():
176177 #try prepending system config path
177178 path = self ._system_config_root / path
179+
180+ if path .exists ():
178181 if not path .is_file ():
179182 raise MirrorError (
180- f"The key path '{ path } ' is not a file. "
183+ f"The key path '{ path } ' is not a file. \n "
181184 f"Check the key listed in mirrors.yaml in system config." )
182-
183- file_type = magic .from_file (path )
184-
185- if not file_type .startswith ("OpenPGP Public Key" ):
185+
186+ with open (path , 'rb' ) as reader :
187+ binary_key = reader .read ()
188+
189+ # convert base64 key to binary
190+ else :
191+ try :
192+ binary_key = base64 .b64decode (key )
193+ except ValueError :
194+ raise MirrorError (
195+ f"Key for mirror { mirror ["name" ]} is not valid. \n "
196+ f"Must be a path to a GPG public key or a base64 encoded GPG public key. \n "
197+ f"Check the key listed in mirrors.yaml in system config." )
198+
199+ file_type = magic .from_buffer (binary_key , mime = True )
200+ print ("magic type:" , file_type )
201+ if file_type != "application/x-gnupg-keyring" :
186202 raise MirrorError (
187- f"' { path } ' is not a valid GPG key. "
203+ f"Key for mirror { mirror [ "name" ] } is not a valid GPG key. \n "
188204 f"Check the key listed in mirrors.yaml in system config." )
189-
205+
190206 # copy key to new destination in key store
191- with open (path , 'r' ) as reader , open (dest , 'w' ) as writer :
192- data = reader .read ()
193- writer .write (data )
194-
195- else :
196- try :
197- key = base64 .b64decode (key )
198- except ValueError as err :
199- pass
200- magic .from_buffer (key )
201-
202- # if PGP key, convert to binary, ???, convert back
203- with open (dest , "wb" ) as file :
204- file .write (key )
205-
206- # update mirror with new path
207- mirror ["key" ] = dest
207+ with open (dest , 'wb' ) as writer :
208+ writer .write (binary_key )
209+ # update mirror with new path
210+ mirror ["public_key" ] = dest
0 commit comments