Make sure _decrypt_key output file is not also the input.

Fixed bug which would cause the key file to be truncated, if you pass the same input and output files to gpg.
This commit is contained in:
Gabe Van Engel 2022-08-28 12:30:32 -07:00 committed by Megan Wilhite
parent a36bdf14b9
commit 692b2624cc
3 changed files with 50 additions and 12 deletions

View file

@ -2297,13 +2297,11 @@ def _decrypt_key(key):
key,
)
return False
encrypted_key = key
if not pathlib.Path(key).suffix:
encrypted_key = key + ".gpg"
cmd = ["gpg", "--yes", "--output", encrypted_key, "--dearmor", key]
decrypted_key = str(key) + ".decrypted"
cmd = ["gpg", "--yes", "--output", decrypted_key, "--dearmor", key]
if not __salt__["cmd.run_all"](cmd)["retcode"] == 0:
log.error("Failed to decrypt the key %s", key)
return encrypted_key
return decrypted_key
except UnicodeDecodeError:
log.debug("Key is not ASCII Armored. Do not need to decrypt")
return key
@ -2393,7 +2391,11 @@ def add_repo_key(
if not key:
return False
key = pathlib.Path(str(key))
shutil.copyfile(key, keydir / key.name)
if not keyfile:
keyfile = key.name
if keyfile.endswith(".decrypted"):
keyfile = keyfile[:-10]
shutil.copyfile(key, keydir / keyfile)
return True
else:
cmd.extend(["add", cached_source_path])

View file

@ -0,0 +1,31 @@
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v2
mQENBFOpvpgBCADkP656H41i8fpplEEB8IeLhugyC2rTEwwSclb8tQNYtUiGdna9
m38kb0OS2DDrEdtdQb2hWCnswxaAkUunb2qq18vd3dBvlnI+C4/xu5ksZZkRj+fW
tArNR18V+2jkwcG26m8AxIrT+m4M6/bgnSfHTBtT5adNfVcTHqiT1JtCbQcXmwVw
WbqS6v/LhcsBE//SHne4uBCK/GHxZHhQ5jz5h+3vWeV4gvxS3Xu6v1IlIpLDwUts
kT1DumfynYnnZmWTGc6SYyIFXTPJLtnoWDb9OBdWgZxXfHEcBsKGha+bXO+m2tHA
gNneN9i5f8oNxo5njrL8jkCckOpNpng18BKXABEBAAG0MlNhbHRTdGFjayBQYWNr
YWdpbmcgVGVhb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FWXxh4oRDDR+8N
1bcPnbB11b5ary99F/mqr7RgQ+YFF0uKRE3SKa7a+6cIuHEZ7Za+zhPaQlzAOZlx
fuBmScum8uQTrEF5+Um5zkwC7EXTdH1co/+/V/fpOtxIg4XO4kcugZefVm5ERfVS
MA==
=dtMN
-----END PGP PUBLIC KEY BLOCK-----

View file

@ -35,13 +35,13 @@ class Key:
@pytest.fixture
def get_key_file(state_tree, functional_files_dir):
def get_key_file(request, state_tree, functional_files_dir):
"""
Create the key file used for the repo
Create the key file used for the repo by file name passed to the test
"""
key = Key()
shutil.copy(str(functional_files_dir / key.keyname), str(state_tree))
yield key.keyname
keyname = request.param
shutil.copy(str(functional_files_dir / keyname), str(state_tree))
yield keyname
@pytest.fixture
@ -266,7 +266,7 @@ def test_mod_repo_no_file(tmp_path, revert_repo_file):
@pytest.fixture()
def add_key(request, get_key_file):
def add_key(request):
""" """
key = Key(request.param)
key.add_key()
@ -301,11 +301,15 @@ def test_get_repo_keys_keydir_not_exist(key):
assert ret
@pytest.mark.parametrize(
"get_key_file", ["salt-archive-keyring.gpg", "SALTSTACK-GPG-KEY.pub"], indirect=True
)
@pytest.mark.parametrize("aptkey", [False, True])
def test_add_del_repo_key(get_key_file, aptkey):
"""
Test both add_repo_key and del_repo_key when
aptkey is both False and True
and using both binary and armored gpg keys
"""
try:
assert aptpkg.add_repo_key("salt://{}".format(get_key_file), aptkey=aptkey)
@ -313,6 +317,7 @@ def test_add_del_repo_key(get_key_file, aptkey):
if not aptkey:
assert keyfile.is_file()
assert oct(keyfile.stat().st_mode)[-3:] == "644"
assert keyfile.read_bytes()
query_key = aptpkg.get_repo_keys(aptkey=aptkey)
assert (
query_key["0E08A149DE57BFBE"]["uid"]