Skip to content

Commit 0d72dcf

Browse files
committed
Rewrite validate_domain_name() as a Puppet 4.x function
The 3.x function rely on is_domain_name() which is deprecated. Rewrite it using the more modern puppet 4.x function to rely on data types for better parameters validation. While here, adjust the Stdlib::Fqdn data type to ensure the last component (TLD) is non-numeric as [suggested by @ekohl](#1282 (comment)), and add a Stdlib::Dns_zone data type that is basically the same but with a trailing dot.
1 parent 3cb8536 commit 0d72dcf

File tree

6 files changed

+83
-63
lines changed

6 files changed

+83
-63
lines changed

Diff for: lib/puppet/functions/validate_domain_name.rb

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
# @summary
4+
# Validate that all values passed are syntactically correct domain names.
5+
# Fail compilation if any value fails this check.
6+
Puppet::Functions.create_function(:validate_domain_name) do
7+
# @param values A domain name or an array of domain names to check
8+
#
9+
# @return
10+
# passes when the given values are syntactically correct domain names or raise an error when they are not and fails compilation
11+
#
12+
# @example Passing examples
13+
# $my_domain_name = 'server.domain.tld'
14+
# validate_domain_name($my_domain_name)
15+
# validate_domain_name('domain.tld', 'puppet.com', $my_domain_name)
16+
#
17+
# @exapmle Failing examples (causing compilation to abort)
18+
# validate_domain_name(1)
19+
# validate_domain_name(true)
20+
# validate_domain_name('invalid domain')
21+
# validate_domain_name('-foo.example.com')
22+
# validate_domain_name('www.example.2com')
23+
dispatch :validate_domain_name do
24+
repeated_param 'Variant[Stdlib::Fqdn, Stdlib::Dns_zone]', :values
25+
end
26+
27+
def validate_domain_name(*_values); end
28+
end

Diff for: lib/puppet/parser/functions/validate_domain_name.rb

-48
This file was deleted.

Diff for: spec/functions/validate_domain_name_spec.rb

+13-14
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
describe 'validate_domain_name' do
66
describe 'signature validation' do
77
it { is_expected.not_to eq(nil) }
8-
it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
98
end
109

1110
describe 'valid inputs' do
@@ -18,20 +17,20 @@
1817
end
1918

2019
describe 'invalid inputs' do
21-
it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, %r{is not a string}) }
22-
it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, %r{is not a string}) }
23-
it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{is not a string}) }
24-
it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, %r{is not a string}) }
20+
it { is_expected.to run.with_params([]).and_raise_error(ArgumentError, %r{got Array}) }
21+
it { is_expected.to run.with_params({}).and_raise_error(ArgumentError, %r{got Hash}) }
22+
it { is_expected.to run.with_params(1).and_raise_error(ArgumentError, %r{got Integer}) }
23+
it { is_expected.to run.with_params(true).and_raise_error(ArgumentError, %r{got Boolean}) }
2524

26-
it { is_expected.to run.with_params('foo.example.com', []).and_raise_error(Puppet::ParseError, %r{is not a string}) }
27-
it { is_expected.to run.with_params('foo.example.com', {}).and_raise_error(Puppet::ParseError, %r{is not a string}) }
28-
it { is_expected.to run.with_params('foo.example.com', 1).and_raise_error(Puppet::ParseError, %r{is not a string}) }
29-
it { is_expected.to run.with_params('foo.example.com', true).and_raise_error(Puppet::ParseError, %r{is not a string}) }
25+
it { is_expected.to run.with_params('foo.example.com', []).and_raise_error(ArgumentError, %r{got Array}) }
26+
it { is_expected.to run.with_params('foo.example.com', {}).and_raise_error(ArgumentError, %r{got Hash}) }
27+
it { is_expected.to run.with_params('foo.example.com', 1).and_raise_error(ArgumentError, %r{got Integer}) }
28+
it { is_expected.to run.with_params('foo.example.com', true).and_raise_error(ArgumentError, %r{got Boolean}) }
3029

31-
it { is_expected.to run.with_params('').and_raise_error(Puppet::ParseError, %r{is not a syntactically correct domain name}) }
32-
it { is_expected.to run.with_params('invalid domain').and_raise_error(Puppet::ParseError, %r{is not a syntactically correct domain name}) }
33-
it { is_expected.to run.with_params('-foo.example.com').and_raise_error(Puppet::ParseError, %r{is not a syntactically correct domain name}) }
34-
it { is_expected.to run.with_params('www.example.2com').and_raise_error(Puppet::ParseError, %r{is not a syntactically correct domain name}) }
35-
it { is_expected.to run.with_params('192.168.1.1').and_raise_error(Puppet::ParseError, %r{is not a syntactically correct domain name}) }
30+
it { is_expected.to run.with_params('').and_raise_error(ArgumentError, %r{got ''}) }
31+
it { is_expected.to run.with_params('invalid domain').and_raise_error(ArgumentError, %r{got 'invalid domain'}) }
32+
it { is_expected.to run.with_params('-foo.example.com').and_raise_error(ArgumentError, %r{got '-foo\.example\.com'}) }
33+
it { is_expected.to run.with_params('www.example.2com').and_raise_error(ArgumentError, %r{got 'www\.example\.2com'}) }
34+
it { is_expected.to run.with_params('192.168.1.1').and_raise_error(ArgumentError, %r{got '192\.168\.1\.1'}) }
3635
end
3736
end

Diff for: spec/type_aliases/dns_zone_spec.rb

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'Stdlib::Dns_zone' do
6+
describe 'accepts dns zones' do
7+
[
8+
'.',
9+
'com.',
10+
'example.com.',
11+
].each do |value|
12+
describe value.inspect do
13+
it { is_expected.to allow_value(value) }
14+
end
15+
end
16+
end
17+
18+
describe 'rejects other values' do
19+
[
20+
true,
21+
false,
22+
'',
23+
'iAmAString',
24+
{},
25+
{ 'key' => 'value' },
26+
{ 1 => 2 },
27+
:undef,
28+
3,
29+
'2com.',
30+
'com2.',
31+
'www..com.',
32+
'127.0.0.1',
33+
].each do |value|
34+
describe value.inspect do
35+
it { is_expected.not_to allow_value(value) }
36+
end
37+
end
38+
end
39+
end

Diff for: types/dns_zone.pp

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# @summary Validate a DNS zone name
2+
type Stdlib::Dns_zone = Pattern[/\A((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)*[[:alpha:]]+\.|\.)\z/]

Diff for: types/fqdn.pp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# @summary Validate a Fully Qualified Domain Name
2-
type Stdlib::Fqdn = Pattern[/\A(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])\z/]
2+
type Stdlib::Fqdn = Pattern[/\A((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)*[[:alpha:]]+)\z/]

0 commit comments

Comments
 (0)