Skip to content

Commit

Permalink
Optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-kolarik committed Jun 19, 2024
1 parent c1ac893 commit b88810d
Showing 1 changed file with 7 additions and 23 deletions.
30 changes: 7 additions & 23 deletions plugins/expired_gpg_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def config(self):
print(_("The following GPG key has expired on {0}:".format(expire_date)))
print(" {0}\n".format(hdr["summary"]))
print(_("For more information about the key:"))
print(" rpm -qi {0}\n".format(self.get_nvr(hdr)))
print(" rpm -qi {0}\n".format(hdr[rpm.RPMTAG_NVR]))

print(_("Installing packages signed with this key will fail due to a GPG check error.\n"
"It is recommended to remove the expired key to allow the installation of\n"
Expand All @@ -45,13 +45,6 @@ def config(self):
print(_("Failed to remove the key."))
print()

@staticmethod
def get_nvr(hdr):
"""
Get NVR string from the RPM header.
"""
return "-".join([hdr["name"], hdr["version"], hdr["release"]])

@staticmethod
def is_gpg_installed():
"""
Expand Down Expand Up @@ -85,29 +78,20 @@ def get_key_expire_date(hdr):
"""
Retrieve the GPG key expiration date, or return None if the expiration is not available.
"""
gpg_key_nvr = ExpiredGPGKeys.get_nvr(hdr)

try:
# get gpg key block as text
rpm_key_ps = subprocess.Popen(("rpm", "-q", gpg_key_nvr, "--qf", "%{DESCRIPTION}"),
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL)

# show formatted output of the gpg key
gpg_key_ps = subprocess.Popen(("gpg", "--show-keys", "--with-colon"),
stdin=rpm_key_ps.stdout,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL)
gpg_key_ps = subprocess.run(("gpg", "--show-keys", "--with-colon"),
input=hdr[rpm.RPMTAG_DESCRIPTION],
capture_output=True, text=True, check=True)

# parse the gpg key expiration time
# see also https://github.com/gpg/gnupg/blob/master/doc/DETAILS#field-7---expiration-date
head_ps = subprocess.Popen(("head", "-1"), stdin=gpg_key_ps.stdout, stdout=subprocess.PIPE)
expire_date_result = subprocess.check_output(("cut", "-d:", "-f7"), stdin=head_ps.stdout, text=True).strip()

if not expire_date_result.isnumeric():
expire_date_string = gpg_key_ps.stdout.split('\n')[0].split(':')[6]
if not expire_date_string.isnumeric():
return None

return datetime.fromtimestamp(float(expire_date_result))
return datetime.fromtimestamp(float(expire_date_string))
except subprocess.CalledProcessError as e:
logger.debug('Error when checking expired gpg keys: %s', str(e))
return None
Expand Down

0 comments on commit b88810d

Please sign in to comment.