Merge pull request #158 from wfarr/validate_ip
[#20862] Add functions to validate ipv4 and ipv6 addresses
This commit is contained in:
commit
ca7633c0e4
4 changed files with 228 additions and 0 deletions
48
lib/puppet/parser/functions/validate_ipv4_address.rb
Normal file
48
lib/puppet/parser/functions/validate_ipv4_address.rb
Normal file
|
@ -0,0 +1,48 @@
|
|||
module Puppet::Parser::Functions
|
||||
|
||||
newfunction(:validate_ipv4_address, :doc => <<-ENDHEREDOC
|
||||
Validate that all values passed are valid IPv4 addresses.
|
||||
Fail compilation if any value fails this check.
|
||||
|
||||
The following values will pass:
|
||||
|
||||
$my_ip = "1.2.3.4"
|
||||
validate_ipv4_address($my_ip)
|
||||
validate_bool("8.8.8.8", "172.16.0.1", $my_ip)
|
||||
|
||||
The following values will fail, causing compilation to abort:
|
||||
|
||||
$some_array = [ 1, true, false, "garbage string", "3ffe:505:2" ]
|
||||
validate_ipv4_address($some_array)
|
||||
|
||||
ENDHEREDOC
|
||||
) do |args|
|
||||
|
||||
require "ipaddr"
|
||||
rescuable_exceptions = [ ArgumentError ]
|
||||
|
||||
if defined?(IPAddr::InvalidAddressError)
|
||||
rescuable_exceptions << IPAddr::InvalidAddressError
|
||||
end
|
||||
|
||||
unless args.length > 0 then
|
||||
raise Puppet::ParseError, ("validate_ipv4_address(): wrong number of arguments (#{args.length}; must be > 0)")
|
||||
end
|
||||
|
||||
args.each do |arg|
|
||||
unless arg.is_a?(String)
|
||||
raise Puppet::ParseError, "#{arg.inspect} is not a string."
|
||||
end
|
||||
|
||||
begin
|
||||
unless IPAddr.new(arg).ipv4?
|
||||
raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv4 address."
|
||||
end
|
||||
rescue *rescuable_exceptions
|
||||
raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv4 address."
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
49
lib/puppet/parser/functions/validate_ipv6_address.rb
Normal file
49
lib/puppet/parser/functions/validate_ipv6_address.rb
Normal file
|
@ -0,0 +1,49 @@
|
|||
module Puppet::Parser::Functions
|
||||
|
||||
newfunction(:validate_ipv6_address, :doc => <<-ENDHEREDOC
|
||||
Validate that all values passed are valid IPv6 addresses.
|
||||
Fail compilation if any value fails this check.
|
||||
|
||||
The following values will pass:
|
||||
|
||||
$my_ip = "3ffe:505:2"
|
||||
validate_ipv6_address(1)
|
||||
validate_ipv6_address($my_ip)
|
||||
validate_bool("fe80::baf6:b1ff:fe19:7507", $my_ip)
|
||||
|
||||
The following values will fail, causing compilation to abort:
|
||||
|
||||
$some_array = [ true, false, "garbage string", "1.2.3.4" ]
|
||||
validate_ipv6_address($some_array)
|
||||
|
||||
ENDHEREDOC
|
||||
) do |args|
|
||||
|
||||
require "ipaddr"
|
||||
rescuable_exceptions = [ ArgumentError ]
|
||||
|
||||
if defined?(IPAddr::InvalidAddressError)
|
||||
rescuable_exceptions << IPAddr::InvalidAddressError
|
||||
end
|
||||
|
||||
unless args.length > 0 then
|
||||
raise Puppet::ParseError, ("validate_ipv6_address(): wrong number of arguments (#{args.length}; must be > 0)")
|
||||
end
|
||||
|
||||
args.each do |arg|
|
||||
unless arg.is_a?(String)
|
||||
raise Puppet::ParseError, "#{arg.inspect} is not a string."
|
||||
end
|
||||
|
||||
begin
|
||||
unless IPAddr.new(arg).ipv6?
|
||||
raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv6 address."
|
||||
end
|
||||
rescue *rescuable_exceptions
|
||||
raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv6 address."
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
|
@ -0,0 +1,64 @@
|
|||
#! /usr/bin/env/ruby -S rspec
|
||||
|
||||
require "spec_helper"
|
||||
|
||||
describe Puppet::Parser::Functions.function(:validate_ipv4_address) do
|
||||
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
|
||||
|
||||
describe "when calling validate_ipv4_address from puppet" do
|
||||
describe "when given IPv4 address strings" do
|
||||
it "should compile with one argument" do
|
||||
Puppet[:code] = "validate_ipv4_address('1.2.3.4')"
|
||||
scope.compiler.compile
|
||||
end
|
||||
|
||||
it "should compile with multiple arguments" do
|
||||
Puppet[:code] = "validate_ipv4_address('1.2.3.4', '5.6.7.8')"
|
||||
scope.compiler.compile
|
||||
end
|
||||
end
|
||||
|
||||
describe "when given an IPv6 address" do
|
||||
it "should not compile" do
|
||||
Puppet[:code] = "validate_ipv4_address('3ffe:505')"
|
||||
expect {
|
||||
scope.compiler.compile
|
||||
}.to raise_error(Puppet::ParseError, /not a valid IPv4 address/)
|
||||
end
|
||||
end
|
||||
|
||||
describe "when given other strings" do
|
||||
it "should not compile" do
|
||||
Puppet[:code] = "validate_ipv4_address('hello', 'world')"
|
||||
expect {
|
||||
scope.compiler.compile
|
||||
}.to raise_error(Puppet::ParseError, /not a valid IPv4 address/)
|
||||
end
|
||||
end
|
||||
|
||||
describe "when given numbers" do
|
||||
it "should not compile" do
|
||||
Puppet[:code] = "validate_ipv4_address(1, 2)"
|
||||
expect {
|
||||
scope.compiler.compile
|
||||
}.to raise_error(Puppet::ParseError, /is not a valid IPv4 address/)
|
||||
end
|
||||
end
|
||||
|
||||
describe "when given booleans" do
|
||||
it "should not compile" do
|
||||
Puppet[:code] = "validate_ipv4_address(true, false)"
|
||||
expect {
|
||||
scope.compiler.compile
|
||||
}.to raise_error(Puppet::ParseError, /is not a string/)
|
||||
end
|
||||
end
|
||||
|
||||
it "should not compile when no arguments are passed" do
|
||||
Puppet[:code] = "validate_ipv4_address()"
|
||||
expect {
|
||||
scope.compiler.compile
|
||||
}.to raise_error(Puppet::ParseError, /wrong number of arguments/)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,67 @@
|
|||
#! /usr/bin/env/ruby -S rspec
|
||||
|
||||
require "spec_helper"
|
||||
|
||||
describe Puppet::Parser::Functions.function(:validate_ipv6_address) do
|
||||
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
|
||||
|
||||
describe "when calling validate_ipv6_address from puppet" do
|
||||
describe "when given IPv6 address strings" do
|
||||
it "should compile with one argument" do
|
||||
Puppet[:code] = "validate_ipv6_address('3ffe:0505:0002::')"
|
||||
scope.compiler.compile
|
||||
end
|
||||
|
||||
it "should compile with multiple arguments" do
|
||||
Puppet[:code] = "validate_ipv6_address('3ffe:0505:0002::', '3ffe:0505:0001::')"
|
||||
scope.compiler.compile
|
||||
end
|
||||
end
|
||||
|
||||
describe "when given an ipv4 address" do
|
||||
it "should not compile" do
|
||||
Puppet[:code] = "validate_ipv6_address('1.2.3.4')"
|
||||
expect {
|
||||
scope.compiler.compile
|
||||
}.to raise_error(Puppet::ParseError, /not a valid IPv6 address/)
|
||||
end
|
||||
end
|
||||
|
||||
describe "when given other strings" do
|
||||
it "should not compile" do
|
||||
Puppet[:code] = "validate_ipv6_address('hello', 'world')"
|
||||
expect {
|
||||
scope.compiler.compile
|
||||
}.to raise_error(Puppet::ParseError, /not a valid IPv6 address/)
|
||||
end
|
||||
end
|
||||
|
||||
# 1.8.7 is EOL'd and also absolutely insane about ipv6
|
||||
unless RUBY_VERSION == '1.8.7'
|
||||
describe "when given numbers" do
|
||||
it "should not compile" do
|
||||
Puppet[:code] = "validate_ipv6_address(1, 2)"
|
||||
expect {
|
||||
scope.compiler.compile
|
||||
}.to raise_error(Puppet::ParseError, /not a valid IPv6 address/)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "when given booleans" do
|
||||
it "should not compile" do
|
||||
Puppet[:code] = "validate_ipv6_address(true, false)"
|
||||
expect {
|
||||
scope.compiler.compile
|
||||
}.to raise_error(Puppet::ParseError, /is not a string/)
|
||||
end
|
||||
end
|
||||
|
||||
it "should not compile when no arguments are passed" do
|
||||
Puppet[:code] = "validate_ipv6_address()"
|
||||
expect {
|
||||
scope.compiler.compile
|
||||
}.to raise_error(Puppet::ParseError, /wrong number of arguments/)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue