Skip to content

Commit d1c1dc1

Browse files
committed
added GPG key verification
1 parent 44612a6 commit d1c1dc1

File tree

3 files changed

+41
-46
lines changed

3 files changed

+41
-46
lines changed

stackinator/builder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ def generate(self, recipe):
233233
pre_install_hook=recipe.pre_install_hook,
234234
spack_version=spack_version,
235235
spack_meta=spack_meta,
236-
mirrors=recipe.mirrors,
236+
mirrors=recipe.mirrors.mirrors,
237237
exclude_from_cache=["nvhpc", "cuda", "perl"],
238238
verbose=False,
239239
)

stackinator/mirror.py

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import urllib.error
66
from typing import ByteString, Optional, List, Dict
77
import magic
8+
import base64
89

910
import 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

stackinator/recipe.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ def __init__(self, args):
173173
# load the optional mirrors.yaml from system config, and add any additional
174174
# mirrors specified on the command line.
175175
self._logger.debug("Configuring mirrors.")
176-
self._mirrors = mirror.Mirrors(self.system_config_path, args.cache)
177-
self._cache = [mirror for mirror in self.mirrors if mirror["buildcache"]]
176+
self.mirrors = mirror.Mirrors(self.system_config_path, args.cache)
177+
self.cache = self.mirrors.build_cache_mirror
178178

179179
# optional post install hook
180180
if self.post_install_hook is not None:
@@ -232,14 +232,6 @@ def pre_install_hook(self):
232232
if hook_path.exists() and hook_path.is_file():
233233
return hook_path
234234
return None
235-
236-
@property
237-
def mirrors(self):
238-
return self._mirrors
239-
240-
@property
241-
def cache(self):
242-
return self._cache
243235

244236
@property
245237
def config(self):

0 commit comments

Comments
 (0)