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 support for the Certbot Gandi plugin #295

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
47 changes: 46 additions & 1 deletion REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* [`letsencrypt`](#letsencrypt): Install and configure Certbot, the LetsEncrypt client
* [`letsencrypt::install`](#letsencryptinstall): Installs the Let's Encrypt client.
* [`letsencrypt::plugin::dns_cloudflare`](#letsencryptplugindns_cloudflare): Installs and configures the dns-cloudflare plugin
* [`letsencrypt::plugin::dns_gandi`](#letsencryptplugindns_gandi): Installs and configures the dns-gandi plugin
* [`letsencrypt::plugin::dns_rfc2136`](#letsencryptplugindns_rfc2136): Installs and configures the dns-rfc2136 plugin
* [`letsencrypt::plugin::dns_route53`](#letsencryptplugindns_route53): Installs and configures the dns-route53 plugin
* [`letsencrypt::plugin::nginx`](#letsencryptpluginnginx): install and configure the Let's Encrypt nginx plugin
Expand Down Expand Up @@ -408,6 +409,50 @@ Data type: `Stdlib::Absolutepath`

Default value: `"${letsencrypt::config_dir}/dns-cloudflare.ini"`

### <a name="letsencryptplugindns_gandi"></a>`letsencrypt::plugin::dns_gandi`

This class installs and configures the Let's Encrypt dns-gandi plugin.
https://pypi.org/project/certbot-plugin-gandi/

#### Parameters

The following parameters are available in the `letsencrypt::plugin::dns_gandi` class:

* [`api_key`](#api_key)
* [`package_name`](#package_name)
* [`config_file`](#config_file)
* [`manage_package`](#manage_package)

##### <a name="api_key"></a>`api_key`

Data type: `String[1]`

Gandi production api key secret. You can get it in you security tab of your account

##### <a name="package_name"></a>`package_name`

Data type: `Optional[String[1]]`

The name of the package to install when $manage_package is true.

Default value: ``undef``

##### <a name="config_file"></a>`config_file`

Data type: `Stdlib::Absolutepath`

The path to the configuration file.

Default value: `"${letsencrypt::config_dir}/dns-gandi.ini"`

##### <a name="manage_package"></a>`manage_package`

Data type: `Boolean`

Manage the plugin package.

Default value: ``true``

### <a name="letsencryptplugindns_rfc2136"></a>`letsencrypt::plugin::dns_rfc2136`

This class installs and configures the Let's Encrypt dns-rfc2136 plugin.
Expand Down Expand Up @@ -1059,6 +1104,6 @@ List of accepted plugins
Alias of

```puppet
Enum['apache', 'standalone', 'webroot', 'nginx', 'dns-route53', 'dns-google', 'dns-cloudflare', 'dns-rfc2136']
Enum['apache', 'standalone', 'webroot', 'nginx', 'dns-route53', 'dns-google', 'dns-cloudflare', 'dns-rfc2136', 'dns-gandi']
```

1 change: 1 addition & 0 deletions data/Debian-family.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
letsencrypt::plugin::dns_rfc2136::package_name: 'python3-certbot-dns-rfc2136'
letsencrypt::plugin::dns_route53::package_name: 'python3-certbot-dns-route53'
letsencrypt::plugin::dns_cloudflare::package_name: 'python3-certbot-dns-cloudflare'
letsencrypt::plugin::dns_gandi::package_name: 'certbot-plugin-gandi'
1 change: 1 addition & 0 deletions data/FreeBSD-family.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ letsencrypt::cron_owner_group: 'wheel'
letsencrypt::plugin::dns_rfc2136::package_name: 'py38-certbot-dns-rfc2136'
letsencrypt::plugin::dns_route53::package_name: 'py38-certbot-dns-route53'
letsencrypt::plugin::dns_cloudflare::package_name: 'py38-certbot-dns-cloudflare'
letsencrypt::plugin::dns_gandi::package_name: 'certbot-plugin-gandi'
Copy link
Member

Choose a reason for hiding this comment

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

There are currently no FreeBSD port for this :-/

The name would probably be py38-certbot-dns-gandi if there was, and in fact py39-certbot-dns-gandi because the default version of Python changed recently on FreeBSD.

Copy link
Member

Choose a reason for hiding this comment

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

I opened #296 to adjust the FreeBSD version.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you think it's a good idea to install the package with pip? I'm not sure on how to install them on freebsd and centos. I will ask help to a colleague

Copy link
Member

Choose a reason for hiding this comment

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

As a FreeBSD user, I would prefer to use a package rather installing using pip. Anybody can submit ports to FreeBSD, and they are ultimately available as packages. My guess is that if the port does not exist, nobody needed it so much they did a port for it, and maybe it is not worth it?

I would personally not set a package_name for FreeBSD in Hiera and make $letsencrypt::plugin::dns_gandi::package_name mandatory so that it fails hard if someone attempt to use it on FreeBSD.

1 change: 1 addition & 0 deletions data/RedHat-family.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ letsencrypt::configure_epel: true
letsencrypt::plugin::dns_rfc2136::package_name: 'python3-certbot-dns-rfc2136'
letsencrypt::plugin::dns_route53::package_name: 'python3-certbot-dns-route53'
letsencrypt::plugin::dns_cloudflare::package_name: 'python3-certbot-dns-cloudflare'
letsencrypt::plugin::dns_gandi::package_name: 'certbot-plugin-gandi'
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't look correct.

3 changes: 3 additions & 0 deletions data/os/Debian/11.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
letsencrypt::plugin::dns_gandi::package_provider: apt
Copy link
Member

Choose a reason for hiding this comment

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

Why do you need to set the package provider? That's rarely needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right, I will remove it

letsencrypt::plugin::dns_gandi::package_name: python3-certbot-dns-gandi
3 changes: 3 additions & 0 deletions data/os/Ubuntu/20.04.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
letsencrypt::plugin::dns_gandi::package_name: python3-certbot-dns-gandi
letsencrypt::plugin::dns_gandi::package_provider: apt
11 changes: 11 additions & 0 deletions manifests/certonly.pp
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,17 @@
}
}

'dns-gandi': {
require letsencrypt::plugin::dns_gandi
$_domains = join($domains, '\' -d \'')
$plugin_args = [
"--cert-name '${cert_name}' -d",
"'${_domains}'",
'-a certbot-plugin-gandi:dns',
"--certbot-plugin-gandi:dns-credentials ${letsencrypt::config_dir}/dns-gandi.ini",
]
}

default: {
if $ensure == 'present' {
$_domains = join($domains, '\' -d \'')
Expand Down
48 changes: 48 additions & 0 deletions manifests/plugin/dns_gandi.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# @summary Installs and configures the dns-gandi plugin
#
# This class installs and configures the Let's Encrypt dns-gandi plugin.
# https://pypi.org/project/certbot-plugin-gandi/
#
# @param api_key Gandi production api key secret. You can get it in you security tab of your account
# @param package_name The name of the package to install when $manage_package is true.
# @param config_file The path to the configuration file.
# @param manage_package Manage the plugin package.
#
class letsencrypt::plugin::dns_gandi (
String[1] $api_key,
String[1] $package_provider = 'pip',
Optional[String[1]] $package_name = undef,
Stdlib::Absolutepath $config_file = "${letsencrypt::config_dir}/dns-gandi.ini",
Boolean $manage_package = true,
) {
require letsencrypt

if $manage_package {
if ! $package_name {
fail('No package name provided for certbot dns gandi plugin.')
}

cible marked this conversation as resolved.
Show resolved Hide resolved
package { $package_name:
ensure => installed,
cible marked this conversation as resolved.
Show resolved Hide resolved
provider => $package_provider,
}
}

if $api_key {
$ini_vars = {
'certbot_plugin_gandi:dns_api_key' => $api_key,
}
} else {
fail('api_key not provided for certbot dns gandi plugin.')
}
Copy link
Member

Choose a reason for hiding this comment

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

The data type guarantees it's set

Suggested change
if $api_key {
$ini_vars = {
'certbot_plugin_gandi:dns_api_key' => $api_key,
}
} else {
fail('api_key not provided for certbot dns gandi plugin.')
}
$ini_vars = {
'certbot_plugin_gandi:dns_api_key' => $api_key,
}


file { $config_file:
ensure => file,
owner => 'root',
group => 'root',
mode => '0400',
content => epp('letsencrypt/ini.epp', {
vars => { '' => $ini_vars },
}),
}
}
23 changes: 23 additions & 0 deletions spec/acceptance/letsencrypt_plugin_dns_gandi_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require 'spec_helper_acceptance'

describe 'letsencrypt::plugin::dns_gandi' do
it_behaves_like 'an idempotent resource' do
let(:manifest) do
<<-PUPPET
include letsencrypt
class { 'letsencrypt::plugin::dns_gandi':
api_key => 'dummy-gandi-api-token',
}
PUPPET
end
end

describe file('/etc/letsencrypt/dns-gandi.ini') do
it { is_expected.to be_file }
it { is_expected.to be_owned_by 'root' }
it { is_expected.to be_grouped_into 'root' }
it { is_expected.to be_mode 400 }
end
end
59 changes: 59 additions & 0 deletions spec/classes/plugin/dns_gandi_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# frozen_string_literal: true

require 'spec_helper'

describe 'letsencrypt::plugin::dns_gandi' do
on_supported_os.each do |os, facts|
context "on #{os} based operating systems" do
let(:facts) { facts }
let(:params) { { 'api_key' => 'dummy-gandi-api-token' } }
let(:pre_condition) do
<<-PUPPET
class { 'letsencrypt':
email => 'foo@example.com',
}
PUPPET
end
let(:package_name) do
osname = facts[:os]['name']
osrelease = facts[:os]['release']['major']
osfull = "#{osname}-#{osrelease}"
case osfull
when 'Debian-10', 'AlmaLinux-8', 'RedHat-8', 'Ubuntu-18.04', 'Fedora-32', 'RedHat-7', 'CentOS-7', 'FreeBSD-12', 'FreeBSD-13'
'certbot-plugin-gandi'
when 'Debian-11', 'Ubuntu-20.04'
'python3-certbot-dns-gandi'
end
end

context 'with required parameters' do
it do
if package_name.nil?
is_expected.not_to compile
cible marked this conversation as resolved.
Show resolved Hide resolved
else
is_expected.to compile.with_all_deps
end
end

describe 'with manage_package => true' do
let(:params) { super().merge(manage_package: true) }

it do
if package_name.nil?
is_expected.not_to compile
Copy link
Member

Choose a reason for hiding this comment

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

A better way is to test for the error:

Suggested change
is_expected.not_to compile
is_expected.to compile.and_raise_error(/A Matcher For The Error/)

else
is_expected.to contain_class('letsencrypt::plugin::dns_gandi').with_package_name(package_name)
is_expected.to contain_package(package_name).with_ensure('installed')
end
end
end

describe 'with manage_package => false' do
let(:params) { super().merge(manage_package: false, package_name: 'dns-gandi-package') }

it { is_expected.not_to contain_package('dns-gandi-package') }
end
end
end
end
end
39 changes: 39 additions & 0 deletions spec/defines/letsencrypt_certonly_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,45 @@ class { 'letsencrypt::plugin::dns_cloudflare':
it { is_expected.to contain_exec('letsencrypt certonly foo.example.com').with_command "letsencrypt --text --agree-tos --non-interactive certonly --rsa-key-size 4096 -a dns-cloudflare --cert-name 'foo.example.com' -d 'foo.example.com' --dns-cloudflare --dns-cloudflare-credentials /etc/letsencrypt/dns-cloudflare.ini --dns-cloudflare-propagation-seconds 10" }
end

context 'with dns-gandi plugin' do
let(:title) { 'foo.example.com' }
let(:params) { { plugin: 'dns-gandi', letsencrypt_command: 'letsencrypt' } }
let(:pre_condition) do
<<-PUPPET
class { 'letsencrypt':
email => 'foo@example.com',
config_dir => '/etc/letsencrypt',
}
class { 'letsencrypt::plugin::dns_gandi':
package_name => 'irrelevant',
api_key => 'dummy-gandi-api-token',
}
PUPPET
end

it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_class('letsencrypt::plugin::dns_gandi') }
it { is_expected.to contain_exec('letsencrypt certonly foo.example.com').with_command "letsencrypt --text --agree-tos --non-interactive certonly --rsa-key-size 4096 -a dns-gandi --cert-name 'foo.example.com' -d 'foo.example.com' -a certbot-plugin-gandi:dns --certbot-plugin-gandi:dns-credentials /etc/letsencrypt/dns-gandi.ini" }
end

context 'with dns-gandi plugin without apy_key' do
let(:title) { 'foo.example.com' }
let(:params) { { plugin: 'dns-gandi', letsencrypt_command: 'letsencrypt' } }
let(:pre_condition) do
<<-PUPPET
class { 'letsencrypt':
email => 'foo@example.com',
config_dir => '/etc/letsencrypt',
}
class { 'letsencrypt::plugin::dns_gandi':
package_name => 'irrelevant',
}
PUPPET
end

it { is_expected.not_to compile.with_all_deps }
Copy link
Member

Choose a reason for hiding this comment

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

Here too it's better to compile and test for a specific error

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For this one, the module won't work with an empty api_key and I don't know how to handle the error:

  13) letsencrypt::certonly on fedora-36-x86_64 based operating systems with dns-gandi plugin without api_key is expected to fail to compile and raise an error matching /\/expects a value for parameter 'api_key'\//
      Failure/Error: it { is_expected.to compile.and_raise_error(%r{/expects a value for parameter 'api_key'/}) }
        error during compilation: Evaluation Error: Error while evaluating a Resource Statement, Class[Letsencrypt::Plugin::Dns_gandi]: expects a value for parameter 'api_key' (line: 6, column: 11

it { is_expected.to compile.and_raise_error(%r{/expects a value for parameter 'api_key'/}) } is not correct?

Copy link
Member

Choose a reason for hiding this comment

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

The / is redundant. In Ruby you can define a regex as /my regex/ or %r{my regex} but %r{/my regex/} means you expect / in the output. This is useful if you want to test /var/lib is empty is in the error. Compare %r{/var/lib is empty} to /\/var\/lib is empty/.

end

context 'with custom plugin' do
let(:title) { 'foo.example.com' }
let(:params) { { plugin: 'apache' } }
Expand Down
2 changes: 1 addition & 1 deletion spec/type_aliases/plugin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'spec_helper'

describe 'Letsencrypt::Plugin' do
it { is_expected.to allow_values('apache', 'standalone', 'webroot', 'nginx', 'dns-route53', 'dns-google', 'dns-cloudflare', 'dns-rfc2136') }
it { is_expected.to allow_values('apache', 'standalone', 'webroot', 'nginx', 'dns-route53', 'dns-google', 'dns-cloudflare', 'dns-rfc2136', 'dns-gandi') }
it { is_expected.not_to allow_value(nil) }
it { is_expected.not_to allow_value('foo') }
it { is_expected.not_to allow_value('custom') }
Expand Down
1 change: 1 addition & 0 deletions types/plugin.pp
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
'dns-google',
'dns-cloudflare',
'dns-rfc2136',
'dns-gandi',
]