Skip to content

Commit c6e36f1

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 a241039 commit c6e36f1

File tree

7 files changed

+101
-65
lines changed

7 files changed

+101
-65
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 [Undef]
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+
# @example 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
@@ -15,23 +14,23 @@
1514
it { is_expected.to run.with_params('2foo.example.com', '2foo.example.com.') }
1615
it { is_expected.to run.with_params('www.2foo.example.com', 'www.2foo.example.com.') }
1716
it { is_expected.to run.with_params('domain.tld', 'puppet.com') }
17+
it { is_expected.to run.with_params('www.example.2com') }
18+
it { is_expected.to run.with_params('10.10.10.10.10') }
1819
end
1920

2021
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}) }
22+
it { is_expected.to run.with_params([]).and_raise_error(ArgumentError, %r{got Array}) }
23+
it { is_expected.to run.with_params({}).and_raise_error(ArgumentError, %r{got Hash}) }
24+
it { is_expected.to run.with_params(1).and_raise_error(ArgumentError, %r{got Integer}) }
25+
it { is_expected.to run.with_params(true).and_raise_error(ArgumentError, %r{got Boolean}) }
2526

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}) }
27+
it { is_expected.to run.with_params('foo.example.com', []).and_raise_error(ArgumentError, %r{got Array}) }
28+
it { is_expected.to run.with_params('foo.example.com', {}).and_raise_error(ArgumentError, %r{got Hash}) }
29+
it { is_expected.to run.with_params('foo.example.com', 1).and_raise_error(ArgumentError, %r{got Integer}) }
30+
it { is_expected.to run.with_params('foo.example.com', true).and_raise_error(ArgumentError, %r{got Boolean}) }
3031

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}) }
32+
it { is_expected.to run.with_params('').and_raise_error(ArgumentError, %r{got ''}) }
33+
it { is_expected.to run.with_params('invalid domain').and_raise_error(ArgumentError, %r{got 'invalid domain'}) }
34+
it { is_expected.to run.with_params('-foo.example.com').and_raise_error(ArgumentError, %r{got '-foo\.example\.com'}) }
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+
'10.10.10.10.10.',
12+
'xn--5ea.pf.',
13+
].each do |value|
14+
describe value.inspect do
15+
it { is_expected.to allow_value(value) }
16+
end
17+
end
18+
end
19+
20+
describe 'rejects other values' do
21+
[
22+
true,
23+
false,
24+
'',
25+
'iAmAString',
26+
{},
27+
{ 'key' => 'value' },
28+
{ 1 => 2 },
29+
:undef,
30+
3,
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: spec/type_aliases/fqdn_spec.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44

55
describe 'Stdlib::Fqdn' do
66
describe 'valid handling' do
7-
['example', 'example.com', 'www.example.com'].each do |value|
7+
[
8+
'example',
9+
'example.com',
10+
'www.example.com',
11+
'10.10.10.10.10',
12+
].each do |value|
813
describe value.inspect do
914
it { is_expected.to allow_value(value) }
1015
end

Diff for: spec/type_aliases/host_spec.rb

+13-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,19 @@
44

55
describe 'Stdlib::Host' do
66
describe 'valid handling' do
7-
['example', 'example.com', 'www.example.com', '2001:0db8:85a3:0000:0000:8a2e:0370:7334', 'fa76:8765:34ac:0823:ab76:eee9:0987:1111', '2001:0db8::1', '224.0.0.0', '255.255.255.255',
8-
'0.0.0.0', '192.88.99.0'].each do |value|
7+
[
8+
'example',
9+
'example.com',
10+
'www.example.com',
11+
'2001:0db8:85a3:0000:0000:8a2e:0370:7334',
12+
'fa76:8765:34ac:0823:ab76:eee9:0987:1111',
13+
'2001:0db8::1',
14+
'224.0.0.0',
15+
'255.255.255.255',
16+
'0.0.0.0',
17+
'192.88.99.0',
18+
'10.10.10.10.10',
19+
].each do |value|
920
describe value.inspect do
1021
it { is_expected.to allow_value(value) }
1122
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((([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])\.)+|\.)\z/]

0 commit comments

Comments
 (0)