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

Add function to properly import gpg keys #123

Open
wants to merge 4 commits into
base: master
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
24 changes: 24 additions & 0 deletions lib/puppet/functions/yum/get_gpg_keys.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Puppet::Functions.create_function(:'yum::get_gpg_keys') do
dispatch :get_gpg_keys do
param 'String', :key_file
end

def get_gpg_keys(key_file)
keys = []
if File.exist?(key_file)
cmd = "/usr/bin/gpg #{key_file}"
outt = Puppet::Util::Execution.execute(cmd).split("\n")
# Iterate thru each output line
outt.each do |line|
# Only public keys
if line[0..2] == 'pub'
the_key = line.split(' ')[1].split('/')[1].downcase
keys.push(the_key)
end
end
else
Puppet.warning("Key file '#(key_file)' does not exist")
end
keys
end
end
39 changes: 18 additions & 21 deletions manifests/gpgkey.pp
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,25 @@
mode => $mode,
}

$rpmname = "gpg-pubkey-$(gpg --with-colons ${path} | \
head -n 1 | \
cut -d: -f5 | \
cut -c9-16 | \
tr '[A-Z]' '[a-z]')"

case $ensure {
'present', default: {
exec { "rpm-import-${name}":
path => '/bin:/usr/bin:/sbin/:/usr/sbin',
command => "rpm --import ${path}",
unless => "rpm -q ${rpmname}",
require => File[$path],
$keys = yum::get_gpg_keys($path)
$keys.each |String $key| {
$the_rpmname = "gpg-pubkey-${key}"
case $ensure {
'present', default: {
exec { "rpm-import-${name}":
path => '/bin:/usr/bin:/sbin/:/usr/sbin',
command => "rpm --import ${path}",
unless => "rpm -q ${the_rpmname}",
require => File[$path],
}
}
}

'absent': {
exec { "rpm-delete-${name}":
path => '/bin:/usr/bin:/sbin/:/usr/sbin',
command => "rpm -e ${rpmname}",
onlyif => ["test -f ${path}", "rpm -q ${rpmname}"],
before => File[$path],
'absent': {
exec { "rpm-delete-${name}":
path => '/bin:/usr/bin:/sbin/:/usr/sbin',
command => "rpm -e ${the_rpmname}",
onlyif => ["test -f ${path}", "rpm -q ${the_rpmname}"],
before => File[$path],
}
}
}
}
Expand Down