Skip to content

Add validate_domain_name function #753

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

Merged
merged 1 commit into from
Jul 31, 2017
Merged
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
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1958,6 +1958,32 @@ validate_cmd($haproxycontent, '/usr/sbin/haproxy -f % -c', 'Haproxy failed to va

*Type*: statement.

#### `validate_domain_name`

**Deprecated. Will be removed in a future version of stdlib. See [`validate_legacy`](#validate_legacy).**

Validate that all values passed are syntactically correct domain names. Aborts catalog compilation if any value fails this check.

The following values pass:

~~~
$my_domain_name = 'server.domain.tld'
validate_domain_name($my_domain_name)
validate_domain_name('domain.tld', 'puppet.com', $my_domain_name)
~~~

The following values fail, causing compilation to abort:

~~~
validate_domain_name(1)
validate_domain_name(true)
validate_domain_name('invalid domain')
validate_domain_name('-foo.example.com')
validate_domain_name('www.example.2com')
~~~

*Type*: statement.

#### `validate_hash`

**Deprecated. Will be removed in a future version of stdlib. See [`validate_legacy`](#validate_legacy).**
Expand Down
39 changes: 39 additions & 0 deletions lib/puppet/parser/functions/validate_domain_name.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module Puppet::Parser::Functions
newfunction(:validate_domain_name, :doc => <<-ENDHEREDOC
Validate that all values passed are syntactically correct domain names.
Fail compilation if any value fails this check.

The following values will pass:

$my_domain_name = 'server.domain.tld'
validate_domain_name($my_domain_name)
validate_domain_name('domain.tld', 'puppet.com', $my_domain_name)

The following values will fail, causing compilation to abort:

validate_domain_name(1)
validate_domain_name(true)
validate_domain_name('invalid domain')
validate_domain_name('-foo.example.com')
validate_domain_name('www.example.2com')

ENDHEREDOC
) do |args|

rescuable_exceptions = [ArgumentError]

if args.empty?
raise Puppet::ParseError, "validate_domain_name(): wrong number of arguments (#{args.length}; must be > 0)"
end

args.each do |arg|
raise Puppet::ParseError, "#{arg.inspect} is not a string." unless arg.is_a?(String)

begin
raise Puppet::ParseError, "#{arg.inspect} is not a syntactically correct domain name" unless function_is_domain_name([arg])
rescue *rescuable_exceptions
raise Puppet::ParseError, "#{arg.inspect} is not a syntactically correct domain name"
end
end
end
end
35 changes: 35 additions & 0 deletions spec/functions/validate_domain_name_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require 'spec_helper'

describe 'validate_domain_name' do
describe 'signature validation' do
it { is_expected.not_to eq(nil) }
it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
end

describe 'valid inputs' do
it { is_expected.to run.with_params('com', 'com.') }
it { is_expected.to run.with_params('x.com', 'x.com.') }
it { is_expected.to run.with_params('foo.example.com', 'foo.example.com.') }
it { is_expected.to run.with_params('2foo.example.com', '2foo.example.com.') }
it { is_expected.to run.with_params('www.2foo.example.com', 'www.2foo.example.com.') }
it { is_expected.to run.with_params('domain.tld', 'puppet.com') }
end

describe 'invalid inputs' do
it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, /is not a string/) }
it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /is not a string/) }
it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /is not a string/) }
it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, /is not a string/) }

it { is_expected.to run.with_params('foo.example.com', []).and_raise_error(Puppet::ParseError, /is not a string/) }
it { is_expected.to run.with_params('foo.example.com', {}).and_raise_error(Puppet::ParseError, /is not a string/) }
it { is_expected.to run.with_params('foo.example.com', 1).and_raise_error(Puppet::ParseError, /is not a string/) }
it { is_expected.to run.with_params('foo.example.com', true).and_raise_error(Puppet::ParseError, /is not a string/) }

it { is_expected.to run.with_params('').and_raise_error(Puppet::ParseError, /is not a syntactically correct domain name/) }
it { is_expected.to run.with_params('invalid domain').and_raise_error(Puppet::ParseError, /is not a syntactically correct domain name/) }
it { is_expected.to run.with_params('-foo.example.com').and_raise_error(Puppet::ParseError, /is not a syntactically correct domain name/) }
it { is_expected.to run.with_params('www.example.2com').and_raise_error(Puppet::ParseError, /is not a syntactically correct domain name/) }
it { is_expected.to run.with_params('192.168.1.1').and_raise_error(Puppet::ParseError, /is not a syntactically correct domain name/) }
end
end