Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trusted key adding / removal via updates #9

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions apply.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ exit_code=0
# defined in the caller script
rootdir="$SYSUPDATES_ROOTDIR"

# keys
printf "######## keys\n" 1>&2
cd "$rootdir"
./keys/keys.sh || exit 1

# base os
printf "######## base os\n" 1>&2
cd "$rootdir"
Expand Down
26 changes: 26 additions & 0 deletions keys/keys.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm not sure bash is always present on the system. when i started with a minimal version, had only sh and dash. the latter is usually aliased to "bash". would recommend to switch to /bin/sh like in other scripts.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On current image used there is bash. Will check this with fresh Void image. Then need to double check it works there, as I have tested this only with bash currently.


current_keys="$(gpg --list-keys --with-colons | grep '^pub' | cut -d: -f5)"
last_commit_key_id="$(git log --show-signature | grep "Primary key fingerprint" | head -n 1 | tail -c 20 | tr -d ' ')"

new_keylist="$(mktemp)"
for keyfile in keys/*.asc; do gpg --with-colons "$keyfile" 2>/dev/null | grep '^pub' | cut -d: -f5; done > "$new_keylist"
# Remove keys that are no longer present.
# But, as a safeguard, never allow removal of key that signed last commit.
for key in $current_keys; do
if ! grep -qs "$key" "$new_keylist" && [[ "$key" != "$last_commit_key_id" ]]; then
echo "Removing key $key..."
gpg --batch --yes --delete-keys "$key"
fi
done
rm "$new_keylist"

# Import new keys
for keyfile in keys/*.asc; do
keyid="$(gpg --with-colons "$keyfile" 2>/dev/null | grep '^pub' | cut -d: -f5)"
if ! grep -qs "$keyid" <<< "$current_keys"; then
echo "Importing key $keyid from $keyfile..."
gpg --import "$keyfile"
echo -e "trust\n5\ny\n" | gpg --batch --no-tty --command-fd 0 --expert --edit-key "$keyid"
fi
done