summaryrefslogtreecommitdiffstats
path: root/lib/puppet/network
diff options
context:
space:
mode:
authorJames Turnbull <james@lovedthanlost.net>2010-01-13 08:07:30 +1100
committerJames Turnbull <james@lovedthanlost.net>2010-01-13 08:07:30 +1100
commitb6f90dfcd96123c245b6f5fd93753790006387c0 (patch)
tree1668fd8ed480dc0d0cb49c4a3d7f8a13c77dbeb9 /lib/puppet/network
parente26e8319186c57a41ea7ca58b0e8e853e9b452e3 (diff)
parentf7e14356ad7781fafa52a459d3c24372fa6c0900 (diff)
downloadpuppet-b6f90dfcd96123c245b6f5fd93753790006387c0.tar.gz
puppet-b6f90dfcd96123c245b6f5fd93753790006387c0.tar.xz
puppet-b6f90dfcd96123c245b6f5fd93753790006387c0.zip
Merge branch '0.25.x'
Conflicts: lib/puppet/ssl/host.rb spec/spec_helper.rb
Diffstat (limited to 'lib/puppet/network')
-rwxr-xr-xlib/puppet/network/authstore.rb166
-rw-r--r--lib/puppet/network/client/resource.rb17
-rw-r--r--lib/puppet/network/format.rb14
-rw-r--r--lib/puppet/network/format_handler.rb16
-rw-r--r--lib/puppet/network/formats.rb23
-rwxr-xr-xlib/puppet/network/handler/fileserver.rb1
-rw-r--r--lib/puppet/network/http/handler.rb1
-rw-r--r--lib/puppet/network/http/webrick.rb3
-rw-r--r--lib/puppet/network/server.rb2
9 files changed, 90 insertions, 153 deletions
diff --git a/lib/puppet/network/authstore.rb b/lib/puppet/network/authstore.rb
index fb3d0145b..a7029a0a0 100755
--- a/lib/puppet/network/authstore.rb
+++ b/lib/puppet/network/authstore.rb
@@ -49,7 +49,7 @@ module Puppet
return decl.result
end
- self.info "defaulting to no access for %s" % name
+ info "defaulting to no access for %s" % name
return false
end
@@ -78,11 +78,7 @@ module Puppet
end
def interpolate(match)
- declarations = @declarations.collect do |ace|
- ace.interpolate(match)
- end
- declarations.sort!
- Thread.current[:declarations] = declarations
+ Thread.current[:declarations] = @declarations.collect { |ace| ace.interpolate(match) }.sort
end
def reset_interpolation
@@ -96,8 +92,7 @@ module Puppet
# this is used if we want to override the this purely immutable list
# by a modified version in a multithread safe way.
def declarations
- return Thread.current[:declarations] if Thread.current[:declarations]
- @declarations
+ Thread.current[:declarations] || @declarations
end
# Store the results of a pattern into our hash. Basically just
@@ -130,46 +125,21 @@ module Puppet
# The length. Only used for iprange and domain.
attr_accessor :length
- # Sort the declarations specially.
+ # Sort the declarations most specific first.
def <=>(other)
- # Sort first based on whether the matches are exact.
- if r = compare(exact?, other.exact?)
- return r
- end
-
- # Then by type
- if r = compare(self.ip?, other.ip?)
- return r
- end
-
- # Next sort based on length
- unless self.length == other.length
- # Longer names/ips should go first, because they're more
- # specific.
- return other.length <=> self.length
- end
-
- # Then sort deny before allow
- if r = compare(self.deny?, other.deny?)
- return r
- end
-
- # We've already sorted by name and length, so all that's left
- # is the pattern
- if ip?
- return self.pattern.to_s <=> other.pattern.to_s
- else
- return self.pattern <=> other.pattern
- end
+ 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
def deny?
- self.type == :deny
+ type == :deny
end
- # Are we an exact match?
def exact?
- self.length.nil?
+ @exact == :exact
end
def initialize(type, pattern)
@@ -179,16 +149,12 @@ module Puppet
# Are we an IP type?
def ip?
- self.name == :ip
+ name == :ip
end
# Does this declaration match the name/ip combo?
def match?(name, ip)
- if self.ip?
- return pattern.include?(IPAddr.new(ip))
- else
- return matchname?(name)
- end
+ ip? ? pattern.include?(IPAddr.new(ip)) : matchname?(name)
end
# Set the pattern appropriately. Also sets the name and length.
@@ -199,15 +165,11 @@ module Puppet
# Mapping a type of statement into a return value.
def result
- case @type
- when :allow; true
- else
- false
- end
+ type == :allow
end
def to_s
- "%s: %s" % [self.type, self.pattern]
+ "#{type}: #{pattern}"
end
# Set the declaration type. Either :allow or :deny.
@@ -238,86 +200,54 @@ module Puppet
# -1 if the first is true, and 1 if the second is true. Used
# in the <=> operator.
def compare(me, them)
- unless me and them
- if me
- return -1
- elsif them
- return 1
- else
- return false
- end
- end
- return nil
+ (me and them) ? nil : me ? -1 : them ? 1 : nil
end
# Does the name match our pattern?
def matchname?(name)
name = munge_name(name)
- return true if self.pattern == name
-
- # If it's an exact match, then just return false, since the
- # exact didn't match.
- if exact?
- return false
- end
-
- # If every field in the pattern matches, then we consider it
- # a match.
- pattern.zip(name) do |p,n|
- unless p == n
- return false
- end
- end
-
- return true
+ (pattern == name) or (not exact? and pattern.zip(name).all? { |p,n| p == n })
end
# Convert the name to a common pattern.
def munge_name(name)
# LAK:NOTE http://snurl.com/21zf8 [groups_google_com]
- # Change to x = name.downcase.split(".",-1).reverse for FQDN support
- x = name.downcase.split(".").reverse
+ # Change to name.downcase.split(".",-1).reverse for FQDN support
+ name.downcase.split(".").reverse
end
# Parse our input pattern and figure out what kind of allowal
# statement it is. The output of this is used for later matching.
+ Octet = '(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])'
+ IPv4 = "#{Octet}\.#{Octet}\.#{Octet}\.#{Octet}"
+ IPv6_full = "_:_:_:_:_:_:_:_|_:_:_:_:_:_::_?|_:_:_:_:_::((_:)?_)?|_:_:_:_::((_:){0,2}_)?|_:_:_::((_:){0,3}_)?|_:_::((_:){0,4}_)?|_::((_:){0,5}_)?|::((_:){0,6}_)?"
+ IPv6_partial = "_:_:_:_:_:_:|_:_:_:_::(_:)?|_:_::(_:){0,2}|_::(_:){0,3}"
+ # 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
+ IP = "#{IPv4}|#{IPv6_full}".gsub(/_/,'([0-9a-fA-F]{1,4})').gsub(/\(/,'(?:')
def parse(value)
- # Use the IPAddr class to determine if we've got a
- # valid IP address.
- @length = Integer($1) if value =~ /\/(\d+)$/
- begin
- @pattern = IPAddr.new(value)
- @name = :ip
- rescue ArgumentError => detail
- case value
- when /^(\d+\.){1,3}\*$/ # an ip address with a '*' at the end
- @name = :ip
- segments = value.split(".")[0..-2]
- @length = 8*segments.length
- begin
- @pattern = IPAddr.new((segments+[0,0,0])[0,4].join(".") + "/" + @length.to_s)
- rescue ArgumentError => detail
- raise AuthStoreError, "Invalid IP address pattern %s" % value
- end
- when /^([a-zA-Z0-9][-\w]*\.)+[-\w]+$/ # a full hostname
- # Change to /^([a-zA-Z][-\w]*\.)+[-\w]+\.?$/ for FQDN support
- @name = :domain
- @pattern = munge_name(value)
- when /^\*(\.([a-zA-Z][-\w]*)){1,}$/ # *.domain.com
- @name = :domain
- @pattern = munge_name(value)
- @pattern.pop # take off the '*'
- @length = @pattern.length
- when /\$\d+/ # a backreference pattern ala $1.reductivelabs.com or 192.168.0.$1 or $1.$2
- @name = :dynamic
- @pattern = munge_name(value)
- when /^[a-zA-Z0-9][-a-zA-Z0-9_.@]*$/
- @pattern = [value]
- @length = nil # force an exact match
- @name = :opaque
- else
- raise AuthStoreError, "Invalid pattern %s" % value
- end
+ @name,@exact,@length,@pattern = *case value
+ when /^(?:#{IP})\/(\d+)$/ # 12.34.56.78/24, a001:b002::efff/120, c444:1000:2000::9:192.168.0.1/112
+ [:ip,:inexact,$1.to_i,IPAddr.new(value)]
+ when /^(#{IP})$/ # 10.20.30.40,
+ [:ip,:exact,nil,IPAddr.new(value)]
+ when /^(#{Octet}\.){1,3}\*$/ # an ip address with a '*' at the end
+ segments = value.split(".")[0..-2]
+ bits = 8*segments.length
+ [:ip,:inexact,bits,IPAddr.new((segments+[0,0,0])[0,4].join(".") + "/#{bits}")]
+ when /^(\w[-\w]*\.)+[-\w]+$/ # a full hostname
+ # Change to /^(\w[-\w]*\.)+[-\w]+\.?$/ for FQDN support
+ [:domain,:exact,nil,munge_name(value)]
+ when /^\*(\.(\w[-\w]*)){1,}$/ # *.domain.com
+ host_sans_star = munge_name(value)[0..-2]
+ [:domain,:inexact,host_sans_star.length,host_sans_star]
+ when /\$\d+/ # a backreference pattern ala $1.reductivelabs.com or 192.168.0.$1 or $1.$2
+ [:dynamic,:exact,nil,munge_name(value)]
+ when /^\w[-.@\w]*$/ # ? Just like a host name but allow '@'s and ending '.'s
+ [:opaque,:exact,nil,[value]]
+ else
+ raise AuthStoreError, "Invalid pattern %s" % value
end
end
end
diff --git a/lib/puppet/network/client/resource.rb b/lib/puppet/network/client/resource.rb
index bc4a8e53f..ad3210603 100644
--- a/lib/puppet/network/client/resource.rb
+++ b/lib/puppet/network/client/resource.rb
@@ -27,25 +27,12 @@ class Puppet::Network::Client::Resource < Puppet::Network::Client
def describe(type, name, retrieve = false, ignore = false)
Puppet.info "Describing %s[%s]" % [type.to_s.capitalize, name]
text = @driver.describe(type, name, retrieve, ignore, "yaml")
-
- object = nil
- if @local
- object = text
- else
- object = YAML::load(Base64.decode64(text))
- end
-
- return object
+ @local ? text : YAML::load(Base64.decode64(text))
end
def list(type, ignore = false, base = false)
bucket = @driver.list(type, ignore, base, "yaml")
-
- unless @local
- bucket = YAML::load(Base64.decode64(bucket))
- end
-
- return bucket
+ @local ? bucket : YAML::load(Base64.decode64(bucket))
end
end
diff --git a/lib/puppet/network/format.rb b/lib/puppet/network/format.rb
index a5be3aff8..d78124221 100644
--- a/lib/puppet/network/format.rb
+++ b/lib/puppet/network/format.rb
@@ -107,17 +107,7 @@ class Puppet::Network::Format
method = send(name)
- if type == :class
- has_method = klass.respond_to?(method)
- message = "has not implemented method '%s'" % method
- else
- has_method = klass.instance_methods.include?(method)
- message = "has not implemented instance method '%s'" % method
- end
-
- return true if has_method
-
- Puppet.debug "Format %s not supported for %s; %s" % [self.name, klass, message]
- return false
+ return klass.respond_to?(method) if type == :class
+ return klass.instance_methods.include?(method)
end
end
diff --git a/lib/puppet/network/format_handler.rb b/lib/puppet/network/format_handler.rb
index e508a0283..ea8cf35de 100644
--- a/lib/puppet/network/format_handler.rb
+++ b/lib/puppet/network/format_handler.rb
@@ -119,18 +119,30 @@ module Puppet::Network::FormatHandler
format_handler.format(b).weight <=> format_handler.format(a).weight
end
- put_preferred_format_first(result)
+ result = put_preferred_format_first(result)
+
+ Puppet.debug "#{friendly_name} supports formats: #{result.map{ |f| f.to_s }.sort.join(' ')}; using #{result.first}"
+
+ result
end
private
+ def friendly_name
+ if self.respond_to? :indirection
+ indirection.name
+ else
+ self
+ end
+ end
+
def put_preferred_format_first(list)
preferred_format = Puppet.settings[:preferred_serialization_format].to_sym
if list.include?(preferred_format)
list.delete(preferred_format)
list.unshift(preferred_format)
else
- Puppet.warning "Value of 'preferred_serialization_format' ('#{preferred_format}') is invalid, using default ('#{list.first}')"
+ Puppet.warning "Value of 'preferred_serialization_format' (#{preferred_format}) is invalid for #{friendly_name}, using default (#{list.first})"
end
list
end
diff --git a/lib/puppet/network/formats.rb b/lib/puppet/network/formats.rb
index 010c23521..a98dcbcc5 100644
--- a/lib/puppet/network/formats.rb
+++ b/lib/puppet/network/formats.rb
@@ -44,7 +44,18 @@ end
# This format combines a yaml serialization, then zlib compression and base64 encoding.
Puppet::Network::FormatHandler.create(:b64_zlib_yaml, :mime => "text/b64_zlib_yaml") do
require 'base64'
- require 'zlib'
+
+ def use_zlib?
+ Puppet.features.zlib? && Puppet[:zlib]
+ end
+
+ def requiring_zlib
+ if use_zlib?
+ yield
+ else
+ raise Puppet::Error, "the zlib library is not installed or is disabled."
+ end
+ end
def intern(klass, text)
decode(text)
@@ -70,7 +81,7 @@ Puppet::Network::FormatHandler.create(:b64_zlib_yaml, :mime => "text/b64_zlib_ya
# Because of yaml issue in ruby 1.8.1...
def supported?(klass)
- RUBY_VERSION != '1.8.1'
+ RUBY_VERSION != '1.8.1' and use_zlib?
end
# fixup invalid yaml as per:
@@ -81,11 +92,15 @@ Puppet::Network::FormatHandler.create(:b64_zlib_yaml, :mime => "text/b64_zlib_ya
end
def encode(text)
- Base64.encode64(Zlib::Deflate.deflate(text, Zlib::BEST_COMPRESSION))
+ requiring_zlib do
+ Base64.encode64(Zlib::Deflate.deflate(text, Zlib::BEST_COMPRESSION))
+ end
end
def decode(yaml)
- YAML.load(Zlib::Inflate.inflate(Base64.decode64(yaml)))
+ requiring_zlib do
+ YAML.load(Zlib::Inflate.inflate(Base64.decode64(yaml)))
+ end
end
end
diff --git a/lib/puppet/network/handler/fileserver.rb b/lib/puppet/network/handler/fileserver.rb
index 6bc6d9d4a..7049fb0dc 100755
--- a/lib/puppet/network/handler/fileserver.rb
+++ b/lib/puppet/network/handler/fileserver.rb
@@ -310,6 +310,7 @@ class Puppet::Network::Handler
mount.info "allowing %s access" % val
mount.allow(val)
rescue AuthStoreError => detail
+ puts detail.backtrace if Puppet[:trace]
raise FileServerError.new(detail.to_s,
count, @configuration.file)
end
diff --git a/lib/puppet/network/http/handler.rb b/lib/puppet/network/http/handler.rb
index 817661db1..65bb0f82c 100644
--- a/lib/puppet/network/http/handler.rb
+++ b/lib/puppet/network/http/handler.rb
@@ -97,6 +97,7 @@ module Puppet::Network::HTTP::Handler
# Execute our find.
def do_find(indirection_request, request, response)
unless result = indirection_request.model.find(indirection_request.key, indirection_request.to_hash)
+ Puppet.info("Could not find %s for '%s'" % [indirection_request.indirection_name, indirection_request.key])
return do_exception(response, "Could not find %s %s" % [indirection_request.indirection_name, indirection_request.key], 404)
end
diff --git a/lib/puppet/network/http/webrick.rb b/lib/puppet/network/http/webrick.rb
index e0fe8b621..1a3f5ddce 100644
--- a/lib/puppet/network/http/webrick.rb
+++ b/lib/puppet/network/http/webrick.rb
@@ -44,7 +44,8 @@ class Puppet::Network::HTTP::WEBrick
sock.accept
@server.run(sock)
}
- }
+ }
+ sleep 0.1 until @server.status == :Running
end
end
diff --git a/lib/puppet/network/server.rb b/lib/puppet/network/server.rb
index f21254be9..01a55df36 100644
--- a/lib/puppet/network/server.rb
+++ b/lib/puppet/network/server.rb
@@ -22,7 +22,7 @@ class Puppet::Network::Server
$stderr.reopen $stdout
Puppet::Util::Log.reopen
rescue => detail
- File.open("/tmp/daemonout", "w") { |f|
+ Puppet::Util.secure_open("/tmp/daemonout", "w") { |f|
f.puts "Could not start %s: %s" % [Puppet[:name], detail]
}
raise "Could not start %s: %s" % [Puppet[:name], detail]