Last active
February 24, 2025 17:05
-
-
Save tsundokul/7930aea52513c5c0052b0448853a4bf7 to your computer and use it in GitHub Desktop.
Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
import sys | |
import subprocess | |
# Configuration | |
keyserver_url = "https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x" | |
gpg_dir = "/etc/apt/trusted.gpg.d" | |
# Ensure the trusted.gpg.d directory exists | |
os.makedirs(gpg_dir, exist_ok=True) | |
def install_key(hash_value): | |
try: | |
# Generate unique name for the key file | |
key_name = f"{hash_value}.gpg" | |
key_path = os.path.join("/tmp", key_name) | |
print(f"Processing key: {hash_value}") | |
# Download the GPG key | |
curl_command = f"curl -s '{keyserver_url}{hash_value}'" | |
gpg_command = f"gpg --dearmor > {key_path}" | |
full_command = f"{curl_command} | {gpg_command}" | |
subprocess.run(full_command, shell=True, check=True) | |
# Install the key | |
install_command = f"sudo install -o root -g root -m 644 {key_path} {gpg_dir}/{key_name}" | |
subprocess.run(install_command, shell=True, check=True) | |
print(f"Key {hash_value} installed successfully.") | |
# Clean up temporary key file | |
os.remove(key_path) | |
except subprocess.CalledProcessError as e: | |
print(f"Error processing key {hash_value}: {e}") | |
except Exception as e: | |
print(f"Unexpected error with key {hash_value}: {e}") | |
def main(): | |
# Ensure a key hash is provided as an argument | |
if len(sys.argv) != 2: | |
print("Usage: python3 update_apt_key.py <key_hash>") | |
return | |
# Get the key hash from the command line | |
key_hash = sys.argv[1].strip() | |
# Process the key | |
install_key(key_hash) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment