Puppet Function: validate_ldap_dn

Defined in:
lib/puppet/parser/functions/validate_ldap_dn.rb
Function type:
Ruby 3.x API

Overview

validate_ldap_dn()Any

Validate that all passed values are LDAP distinguished names. Abort catalog compilation if any value fails this check.

Returns:

  • (Any)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/puppet/parser/functions/validate_ldap_dn.rb', line 17

newfunction(:validate_ldap_dn, :doc => <<-EOS
  Validate that all passed values are LDAP distinguished names. Abort catalog
  compilation if any value fails this check.
  EOS
) do |arguments|

  raise(Puppet::ParseError, 'validate_ldap_dn(): Wrong number of ' +
    "arguments given (#{arguments.size} for 1)") if arguments.size != 1

  # RFC 1779/2253
  re = <<-'EOR'
  ^
  (?<comp>
    (?<tv>
      (?:
        [[:alpha:]] [[:alnum:]_-]*
        |
        (?:
          (?:
            oid
            |
            OID
          )
          \.
        )?
        [[:digit:]]+ (?: \. [[:digit:]]+ )*
      )
      [[:space:]]* = [[:space:]]*
      (?:
        \# (?: [[:xdigit:]]{2} )+
        |
        (?:
          [^,=\+<>#;\\"]
          |
          \\ [,=\+<>#;\\"]
          |
          \\ [[:xdigit:]]{2}
        )*
        |
        "
        (?:
          [^\\"]
          |
          \\ [,=\+<>#;\\"]
          |
          \\ [[:xdigit:]]{2}
        )*
        "
      )
    )
    (?: [[:space:]]* \+ [[:space:]]* \g<tv> )*
  )
  (?: [[:space:]]* [,;] [[:space:]]* \g<comp> )*
  $
  EOR

  if RUBY_VERSION < '1.9'
    # :nocov:
    dn = Oniguruma::ORegexp.new(re, :options => Oniguruma::OPTION_EXTEND)
    # :nocov:
  else
    dn = Regexp.new(re, Regexp::EXTENDED)
  end

  item = arguments[0]

  unless item.is_a?(Array)
    item = [item]
  end

  if item.size == 0
    raise(Puppet::ParseError, 'validate_ldap_dn(): Requires an array ' +
      'with at least 1 element')
  end

  item.each do |i|
    unless i.is_a?(String)
      raise(Puppet::ParseError, 'validate_ldap_dn(): Requires either an ' +
        'array or string to work with')
    end

    unless i =~ dn
      raise(Puppet::ParseError, "#{i.inspect} is not a valid LDAP " +
        "distinguished name")
    end
  end
end