A single declaration. Stores the info for a given declaration, provides the methods for determining whether a declaration matches, and handles sorting the declarations appropriately.
It should be:
IP = "#{IPv4}|#{IPv6_full}|(#{IPv6_partial}#{IPv4})".gsub(/_/,'([0-9a-fA-F]{1,4})').gsub(/\(/,'(?:')
but ruby’s ipaddr lib doesn’t support the hybrid format
Parse our input pattern and figure out what kind of allowable statement it is. The output of this is used for later matching.
The length. Only used for iprange and domain.
The pattern we’re matching against. Can be an IPAddr instance, or an array of strings, resulting from reversing a hostname or domain name.
The type of declaration: either :allow or :deny
Sort the declarations most specific first.
# File lib/puppet/network/authstore.rb, line 132 def <=>(other) compare(exact?, other.exact?) || compare(ip?, other.ip?) || ((length != other.length) && (other.length <=> length)) || compare(deny?, other.deny?) || ( ip? ? pattern.to_s <=> other.pattern.to_s : pattern <=> other.pattern) end
# File lib/puppet/network/authstore.rb, line 140 def deny? type == :deny end
# File lib/puppet/network/authstore.rb, line 144 def exact? @exact == :exact end
interpolate a pattern to replace any backreferences by the given match for instance if our pattern is $1.reductivelabs.com and we’re called with a MatchData whose capture 1 is puppet we’ll return a pattern of puppet.reductivelabs.com
# File lib/puppet/network/authstore.rb, line 198 def interpolate(match) clone = dup if @name == :dynamic clone.pattern = clone.pattern.reverse.collect do |p| p.gsub(/\$(\d)/) { |m| match[$1.to_i] } end.join(".") end clone end
Are we an IP type?
# File lib/puppet/network/authstore.rb, line 154 def ip? name == :ip end
Does this declaration match the name/ip combo?
# File lib/puppet/network/authstore.rb, line 159 def match?(name, ip) if ip? pattern.include?(IPAddr.new(ip)) else matchname?(name) end end
Set the pattern appropriately. Also sets the name and length.
# File lib/puppet/network/authstore.rb, line 168 def pattern=(pattern) if [:allow_ip, :deny_ip].include?(self.type) parse_ip(pattern) else parse(pattern) end @orig = pattern end
Mapping a type of statement into a return value.
# File lib/puppet/network/authstore.rb, line 178 def result [:allow, :allow_ip].include?(type) end
# File lib/puppet/network/authstore.rb, line 182 def to_s "#{type}: #{pattern}" end
Set the declaration type. Either :allow or :deny.
# File lib/puppet/network/authstore.rb, line 187 def type=(type) type = type.intern raise ArgumentError, "Invalid declaration type #{type}" unless VALID_TYPES.include?(type) @type = type end
# File lib/puppet/network/authstore.rb, line 148 def initialize(type, pattern) self.type = type self.pattern = pattern end