Skip to content

Instantly share code, notes, and snippets.

@Ghostbird
Last active October 19, 2024 18:04
Show Gist options
  • Save Ghostbird/83eb5bcd2ffd4a6b6966137a2e1c4caf to your computer and use it in GitHub Desktop.
Save Ghostbird/83eb5bcd2ffd4a6b6966137a2e1c4caf to your computer and use it in GitHub Desktop.
Simple bash script to add apt keys + repositories in a better way than using apt-key. It is intended to make it easy to convert instructions online that still use apt-key, to the newer mechanism.
#!/bin/bash
# Simple script to add apt keys in a better way than using apt-key
# It is intended to make it easy to convert instructions online
# that still use apt-key, to the newer mechanism.
# Download from: https://gist.githubusercontent.com/Ghostbird/83eb5bcd2ffd4a6b6966137a2e1c4caf
# MIT LICENCE:
# Copyright © 2022 Gijsbert “Ghostbird” ter Horst
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
KEYRING_DIR='/usr/share/keyrings'
APT_DIR='/etc/apt/sources.list.d'
set -e
usage()
{
cat << EOF
Usage: $0 [-h] [-k KEYRING_DIR] [-a APT_DIR] KEY_URI APT_CONF NAME
Add a repository to apt and add a its key to keyrings
Make sure to escape positional arguments with spaces!
DEFAULTS:
Keyrings are in ${KEYRING_DIR}
Apt sources are in ${APT_DIR}
ARGUMENTS:
KEY_URI Uri where to HTTP GET the key
APT_CONF Configuration that should be in the apt sources file.
Do not included a [signed-by], it will be generated.
NAME Basename to use for keyring and apt sources file
OPTIONS:
-h Show this message
-k DIR Alternative path to keyrings directory
-a DIR Alternative path to apt sources directory
ERROR CODES:
1 Invalid or missing arguments
2 Excess arguments. Probably an argument was not properly quoted.
3 Execution would clobber existing file, investigate and resolve manually.
EOF
}
while getopts "hk:a:" OPTION
do
case $OPTION in
h)
usage
exit 1
;;
k)
KEYRING_DIR=$OPTARG
;;
a)
APT_DIR=$OPTARG
;;
?)
usage
exit 1
;;
esac
done
KEY_URI=${@:$OPTIND:1}
APT_CONF=${@:$OPTIND+1:1}
NAME=${@:$OPTIND+2:1}
EXCESS=${@:$OPTIND+3:1}
if [ \( -z "${KEY_URI}" \) -o \( -z "${APT_CONF}" \) -o \( -z "${NAME}" \) ]
then
usage
1>&2 echo "ERROR: Missing required argument!"
exit 1
fi
if [ -n "${EXCESS}" ]
then
usage
1>&2 echo "ERROR: Excess positional arguments! Please quote your arguments properly!"
exit 2
fi
KEYRING_FILE="${KEYRING_DIR}/${NAME}.gpg"
APT_FILE="${APT_DIR}/${NAME}.list"
# Prevent clobbering
if [ -e "${KEYRING_FILE}" ]
then
1>&2 echo "ERROR: File exists: ${KEYRING_FILE}"
exit 3
fi
if [ -e "${APT_FILE}" ]
then
1>&2 echo "ERROR: File exists: ${APT_FILE}"
exit 3
fi
cd $(mktemp -d)
curl -fsS -o key.tmp "${KEY_URI}"
# Ensure dearmoured
if [ -n "$(file key.tmp | grep 'PGP public key block')" ]
then
cat key.tmp | gpg --dearmour > key2.tmp
mv key2.tmp key.tmp
fi
sudo install -D -o root -g root -m 644 key.tmp "${KEYRING_FILE}"
echo "${APT_CONF}" | sed 's/http:/https:/' | sed 's/^deb https/deb [] https/' | sed -E "s|^deb *\[|deb \[signed-by=${KEYRING_FILE}|" | sed -E "s|^deb (.+)|deb \1\ndeb-src \1|" | sudo tee "${APT_FILE}" > /dev/null
rm -r $(pwd)
cd - > /dev/null
@Ghostbird
Copy link
Author

Added reference to this URI, and MIT licence

@Ghostbird
Copy link
Author

Updated to automatically also add deb-src line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment