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 apt::keyring defined type #1120

Closed
wants to merge 4 commits into from
Closed
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
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,23 @@ include apt
<a id="add-gpg-keys"></a>

### Add GPG keys
You can fetch GPG keys via HTTP, Puppet URI, or local filesystem. The key must be in binary format for apt to read it properly.

#### Fetch via HTTP
```puppet
apt::keyring {'puppetlabs-keyring.gpg':
source => 'https://apt.puppetlabs.com/keyring.gpg',
}
```
#### Fetch via Puppet URI
```puppet
apt::keyring {'puppetlabs-keyring.gpg':
source => 'puppet:///modules/my_module/local_puppetlabs-keyring.gpg',
}
```
Alternatively `apt::key` can be used.

**Warning** `apt::key` is deprecated in the latest Debian and Ubuntu releases. Please use apt::keyring instead.

**Warning:** Using short key IDs presents a serious security issue, potentially leaving you open to collision attacks. We recommend you always use full fingerprints to identify your GPG keys. This module allows short keys, but issues a security warning if you use them.

Expand Down
8 changes: 8 additions & 0 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@
# @param keys
# Creates new `apt::key` resources. Valid options: a hash to be passed to the create_resources function linked above.
#
# @param keyrings
# Creates new `apt::keyring` resources. Valid options: a hash to be passed to the create_resources function linked above.
#
# @param ppas
# Creates new `apt::ppa` resources. Valid options: a hash to be passed to the create_resources function linked above.
#
Expand Down Expand Up @@ -159,6 +162,7 @@
Apt::Proxy $proxy = $apt::params::proxy,
Hash $sources = $apt::params::sources,
Hash $keys = $apt::params::keys,
Hash $keyrings = $apt::params::keyrings,
Hash $ppas = $apt::params::ppas,
Hash $pins = $apt::params::pins,
Hash $settings = $apt::params::settings,
Expand Down Expand Up @@ -347,6 +351,10 @@
if $keys {
create_resources('apt::key', $keys)
}
# manage keyrings if present
if $keyrings {
create_resources('apt::keyring', $keyrings)
}
# manage ppas if present
if $ppas {
create_resources('apt::ppa', $ppas)
Expand Down
67 changes: 67 additions & 0 deletions manifests/keyring.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# @summary Manage GPG keyrings for apt repositories
#
# @example Install the puppetlabs apt source with keyring.
# apt::source { 'puppet7-release':
# location => 'http://apt.puppetlabs.com',
# repos => 'main',
# keyring => '/etc/apt/keyrings/puppetlabs-keyring.gpg',
# }
# apt::keyring {'puppetlabs-keyring.gpg':
# source => 'https://apt.puppetlabs.com/keyring.gpg',
# }
#
# @param keyring_dir
# Path to the directory where the keyring will be stored.
#
# @param keyring_filename
# Optional filename for the keyring.
#
# @param keyring_file
# File path of the keyring.
#
# @param keyring_file_mode
# File permissions of the keyring.
#
# @param source
# Source of the keyring file. Mutually exclusive with 'content'.
#
# @param content
# Content of the keyring file. Mutually exclusive with 'source'.
#
# @param ensure
# Ensure presence or absence of the resource.
#
define apt::keyring (
Stdlib::Absolutepath $keyring_dir = '/etc/apt/keyrings',

Choose a reason for hiding this comment

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

This is already defined as apt::params::trusted_gpg_d

Choose a reason for hiding this comment

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

True, but for new-style APT keyrings, /etc/apt/keyrings is the preferred location.

Changing it only here means the new-style keyrings can be deployed to /etc/apt/keyrings by default, without affecting the rest of the module.

Optional[String] $keyring_filename = $name,
Stdlib::Absolutepath $keyring_file = "${keyring_dir}/${keyring_filename}",
String $keyring_file_mode = '0644',
Optional[Stdlib::Filesource] $source = undef,
Optional[String] $content = undef,
Enum['present','absent'] $ensure = 'present',
) {
ensure_resource('file', $keyring_dir, { ensure => 'directory', mode => '0755', })
if $source and $content {
Copy link
Collaborator

Choose a reason for hiding this comment

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

We probably want a similar check when none of source and content is set. The provided unit test in this regard is incomplete because I would expect such code to fail.

Choose a reason for hiding this comment

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

Good idea. I have added another condition which will catch this scenario.

fail("Parameters 'source' and 'content' are mutually exclusive")
} elsif ! $source and ! $content {
fail("One of 'source' or 'content' parameters are required")
}
case $ensure {
'present': {
file { $keyring_file:
ensure => 'file',
mode => $keyring_file_mode,
source => $source,
content => $content,
}
}
'absent': {
file { $keyring_file:
ensure => $ensure,
}
}
default: {
fail("Invalid 'ensure' value '${ensure}' for apt::keyring")
}
}
}
1 change: 1 addition & 0 deletions manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
$proxy = {}
$sources = {}
$keys = {}
$keyrings = {}
$ppas = {}
$pins = {}
$settings = {}
Expand Down
18 changes: 18 additions & 0 deletions spec/defines/keyring_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

require 'spec_helper'

describe 'apt::keyring' do
let(:title) { 'namevar' }
let(:params) do
{}
end

on_supported_os.each do |os, os_facts|
context "on #{os}" do
let(:facts) { os_facts }

it { is_expected.to compile }

Check failure on line 15 in spec/defines/keyring_spec.rb

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)

apt::keyring on debian-10-x86_64 is expected to compile into a catalogue without dependency cycles Failure/Error: it { is_expected.to compile } error during compilation: Evaluation Error: Error while evaluating a Resource Statement, Evaluation Error: Error while evaluating a Function Call, One of 'source' or 'content' parameters are required (file: /home/runner/work/puppetlabs-apt/puppetlabs-apt/spec/fixtures/modules/apt/manifests/keyring.pp, line: 47, column: 5) (line: 2) on node fv-az453-940.2vhfbungzw1e3oo2z3acampsgb.dx.internal.cloudapp.net

Check failure on line 15 in spec/defines/keyring_spec.rb

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)

apt::keyring on ubuntu-20.04-x86_64 is expected to compile into a catalogue without dependency cycles Failure/Error: it { is_expected.to compile } error during compilation: Evaluation Error: Error while evaluating a Resource Statement, Evaluation Error: Error while evaluating a Function Call, One of 'source' or 'content' parameters are required (file: /home/runner/work/puppetlabs-apt/puppetlabs-apt/spec/fixtures/modules/apt/manifests/keyring.pp, line: 47, column: 5) (line: 2) on node fv-az453-940.2vhfbungzw1e3oo2z3acampsgb.dx.internal.cloudapp.net

Check failure on line 15 in spec/defines/keyring_spec.rb

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)

apt::keyring on ubuntu-22.04-x86_64 is expected to compile into a catalogue without dependency cycles Failure/Error: it { is_expected.to compile } error during compilation: Evaluation Error: Error while evaluating a Resource Statement, Evaluation Error: Error while evaluating a Function Call, One of 'source' or 'content' parameters are required (file: /home/runner/work/puppetlabs-apt/puppetlabs-apt/spec/fixtures/modules/apt/manifests/keyring.pp, line: 47, column: 5) (line: 2) on node fv-az453-940.2vhfbungzw1e3oo2z3acampsgb.dx.internal.cloudapp.net

Check failure on line 15 in spec/defines/keyring_spec.rb

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)

apt::keyring on debian-11-x86_64 is expected to compile into a catalogue without dependency cycles Failure/Error: it { is_expected.to compile } error during compilation: Evaluation Error: Error while evaluating a Resource Statement, Evaluation Error: Error while evaluating a Function Call, One of 'source' or 'content' parameters are required (file: /home/runner/work/puppetlabs-apt/puppetlabs-apt/spec/fixtures/modules/apt/manifests/keyring.pp, line: 47, column: 5) (line: 2) on node fv-az453-940.2vhfbungzw1e3oo2z3acampsgb.dx.internal.cloudapp.net

Check failure on line 15 in spec/defines/keyring_spec.rb

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)

apt::keyring on ubuntu-18.04-x86_64 is expected to compile into a catalogue without dependency cycles Failure/Error: it { is_expected.to compile } error during compilation: Evaluation Error: Error while evaluating a Resource Statement, Evaluation Error: Error while evaluating a Function Call, One of 'source' or 'content' parameters are required (file: /home/runner/work/puppetlabs-apt/puppetlabs-apt/spec/fixtures/modules/apt/manifests/keyring.pp, line: 47, column: 5) (line: 2) on node fv-az453-940.2vhfbungzw1e3oo2z3acampsgb.dx.internal.cloudapp.net

Check failure on line 15 in spec/defines/keyring_spec.rb

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)

apt::keyring on debian-10-x86_64 is expected to compile into a catalogue without dependency cycles Failure/Error: it { is_expected.to compile } error during compilation: Evaluation Error: Error while evaluating a Resource Statement, Evaluation Error: Error while evaluating a Function Call, One of 'source' or 'content' parameters are required (file: /home/runner/work/puppetlabs-apt/puppetlabs-apt/spec/fixtures/modules/apt/manifests/keyring.pp, line: 47, column: 5) (line: 2) on node fv-az400-771.4sktce1ui1juvaou3g2cooit5e.bx.internal.cloudapp.net

Check failure on line 15 in spec/defines/keyring_spec.rb

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)

apt::keyring on debian-11-x86_64 is expected to compile into a catalogue without dependency cycles Failure/Error: it { is_expected.to compile } error during compilation: Evaluation Error: Error while evaluating a Resource Statement, Evaluation Error: Error while evaluating a Function Call, One of 'source' or 'content' parameters are required (file: /home/runner/work/puppetlabs-apt/puppetlabs-apt/spec/fixtures/modules/apt/manifests/keyring.pp, line: 47, column: 5) (line: 2) on node fv-az400-771.4sktce1ui1juvaou3g2cooit5e.bx.internal.cloudapp.net

Check failure on line 15 in spec/defines/keyring_spec.rb

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)

apt::keyring on ubuntu-18.04-x86_64 is expected to compile into a catalogue without dependency cycles Failure/Error: it { is_expected.to compile } error during compilation: Evaluation Error: Error while evaluating a Resource Statement, Evaluation Error: Error while evaluating a Function Call, One of 'source' or 'content' parameters are required (file: /home/runner/work/puppetlabs-apt/puppetlabs-apt/spec/fixtures/modules/apt/manifests/keyring.pp, line: 47, column: 5) (line: 2) on node fv-az400-771.4sktce1ui1juvaou3g2cooit5e.bx.internal.cloudapp.net

Check failure on line 15 in spec/defines/keyring_spec.rb

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)

apt::keyring on ubuntu-20.04-x86_64 is expected to compile into a catalogue without dependency cycles Failure/Error: it { is_expected.to compile } error during compilation: Evaluation Error: Error while evaluating a Resource Statement, Evaluation Error: Error while evaluating a Function Call, One of 'source' or 'content' parameters are required (file: /home/runner/work/puppetlabs-apt/puppetlabs-apt/spec/fixtures/modules/apt/manifests/keyring.pp, line: 47, column: 5) (line: 2) on node fv-az400-771.4sktce1ui1juvaou3g2cooit5e.bx.internal.cloudapp.net

Check failure on line 15 in spec/defines/keyring_spec.rb

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)

apt::keyring on ubuntu-22.04-x86_64 is expected to compile into a catalogue without dependency cycles Failure/Error: it { is_expected.to compile } error during compilation: Evaluation Error: Error while evaluating a Resource Statement, Evaluation Error: Error while evaluating a Function Call, One of 'source' or 'content' parameters are required (file: /home/runner/work/puppetlabs-apt/puppetlabs-apt/spec/fixtures/modules/apt/manifests/keyring.pp, line: 47, column: 5) (line: 2) on node fv-az400-771.4sktce1ui1juvaou3g2cooit5e.bx.internal.cloudapp.net
end
end
end
Loading