Skip to content

Commit 48a8f76

Browse files
authored
Merge pull request #753 from frapex/master
Add validate_domain_name function
2 parents 0c86974 + fe7ccd8 commit 48a8f76

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

README.md

+26
Original file line numberDiff line numberDiff line change
@@ -2003,6 +2003,32 @@ validate_cmd($haproxycontent, '/usr/sbin/haproxy -f % -c', 'Haproxy failed to va
20032003
20042004
*Type*: statement.
20052005
2006+
#### `validate_domain_name`
2007+
2008+
**Deprecated. Will be removed in a future version of stdlib. See [`validate_legacy`](#validate_legacy).**
2009+
2010+
Validate that all values passed are syntactically correct domain names. Aborts catalog compilation if any value fails this check.
2011+
2012+
The following values pass:
2013+
2014+
~~~
2015+
$my_domain_name = 'server.domain.tld'
2016+
validate_domain_name($my_domain_name)
2017+
validate_domain_name('domain.tld', 'puppet.com', $my_domain_name)
2018+
~~~
2019+
2020+
The following values fail, causing compilation to abort:
2021+
2022+
~~~
2023+
validate_domain_name(1)
2024+
validate_domain_name(true)
2025+
validate_domain_name('invalid domain')
2026+
validate_domain_name('-foo.example.com')
2027+
validate_domain_name('www.example.2com')
2028+
~~~
2029+
2030+
*Type*: statement.
2031+
20062032
#### `validate_hash`
20072033
20082034
**Deprecated. Will be removed in a future version of stdlib. See [`validate_legacy`](#validate_legacy).**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module Puppet::Parser::Functions
2+
newfunction(:validate_domain_name, :doc => <<-ENDHEREDOC
3+
Validate that all values passed are syntactically correct domain names.
4+
Fail compilation if any value fails this check.
5+
6+
The following values will pass:
7+
8+
$my_domain_name = 'server.domain.tld'
9+
validate_domain_name($my_domain_name)
10+
validate_domain_name('domain.tld', 'puppet.com', $my_domain_name)
11+
12+
The following values will fail, causing compilation to abort:
13+
14+
validate_domain_name(1)
15+
validate_domain_name(true)
16+
validate_domain_name('invalid domain')
17+
validate_domain_name('-foo.example.com')
18+
validate_domain_name('www.example.2com')
19+
20+
ENDHEREDOC
21+
) do |args|
22+
23+
rescuable_exceptions = [ArgumentError]
24+
25+
if args.empty?
26+
raise Puppet::ParseError, "validate_domain_name(): wrong number of arguments (#{args.length}; must be > 0)"
27+
end
28+
29+
args.each do |arg|
30+
raise Puppet::ParseError, "#{arg.inspect} is not a string." unless arg.is_a?(String)
31+
32+
begin
33+
raise Puppet::ParseError, "#{arg.inspect} is not a syntactically correct domain name" unless function_is_domain_name([arg])
34+
rescue *rescuable_exceptions
35+
raise Puppet::ParseError, "#{arg.inspect} is not a syntactically correct domain name"
36+
end
37+
end
38+
end
39+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
require 'spec_helper'
2+
3+
describe 'validate_domain_name' do
4+
describe 'signature validation' do
5+
it { is_expected.not_to eq(nil) }
6+
it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
7+
end
8+
9+
describe 'valid inputs' do
10+
it { is_expected.to run.with_params('com', 'com.') }
11+
it { is_expected.to run.with_params('x.com', 'x.com.') }
12+
it { is_expected.to run.with_params('foo.example.com', 'foo.example.com.') }
13+
it { is_expected.to run.with_params('2foo.example.com', '2foo.example.com.') }
14+
it { is_expected.to run.with_params('www.2foo.example.com', 'www.2foo.example.com.') }
15+
it { is_expected.to run.with_params('domain.tld', 'puppet.com') }
16+
end
17+
18+
describe 'invalid inputs' do
19+
it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, /is not a string/) }
20+
it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /is not a string/) }
21+
it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /is not a string/) }
22+
it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, /is not a string/) }
23+
24+
it { is_expected.to run.with_params('foo.example.com', []).and_raise_error(Puppet::ParseError, /is not a string/) }
25+
it { is_expected.to run.with_params('foo.example.com', {}).and_raise_error(Puppet::ParseError, /is not a string/) }
26+
it { is_expected.to run.with_params('foo.example.com', 1).and_raise_error(Puppet::ParseError, /is not a string/) }
27+
it { is_expected.to run.with_params('foo.example.com', true).and_raise_error(Puppet::ParseError, /is not a string/) }
28+
29+
it { is_expected.to run.with_params('').and_raise_error(Puppet::ParseError, /is not a syntactically correct domain name/) }
30+
it { is_expected.to run.with_params('invalid domain').and_raise_error(Puppet::ParseError, /is not a syntactically correct domain name/) }
31+
it { is_expected.to run.with_params('-foo.example.com').and_raise_error(Puppet::ParseError, /is not a syntactically correct domain name/) }
32+
it { is_expected.to run.with_params('www.example.2com').and_raise_error(Puppet::ParseError, /is not a syntactically correct domain name/) }
33+
it { is_expected.to run.with_params('192.168.1.1').and_raise_error(Puppet::ParseError, /is not a syntactically correct domain name/) }
34+
end
35+
end

0 commit comments

Comments
 (0)