Project

General

Profile

Bug #7714 » ruby-doc_drb-20130118.patch

Much updated version of the previous patch. - vbatts (Vincent Batts), 01/20/2013 01:19 AM

View differences:

lib/drb/drb.rb
# An exception wrapping an error object
class DRbRemoteError < DRbError
# Create a new DRb::DRbRemoteError
def initialize(error)
@reason = error.class.to_s
super("#{error.message} (#{error.class})")
......
end
end
# Class to wrap an Array.
#
# === Description
#
# A DRbArray can be provided in a DRb call. When the marshalled value
# of a DRbArray object is unmarshalled, it returns the wrapped Array.
#
#
# === Examples
#
# a = %w{ cats and dogs }
# => ["cats", "and", "dogs"]
# da = DRb::DRbArray.new(a)
# => #<DRb::DRbArray:0x0000000068b0f0 @ary=["cats", "and", "dogs"]>
# bin = Marshal.dump(h)
# => "\x04\bu:\x12DRb::DRbArray*\x04\b[\bI\"\tcats\x06:\x06ETI\"\band\x06;\0TI\"\tdogs\x06;\0T"
# Marshal.load(bin)
# => ["cats", "and", "dogs"]
#
class DRbArray
# Create a DRb::DRbArray object
#
# +ary+ is an Array
def initialize(ary)
@ary = ary.collect { |obj|
if obj.kind_of? DRbUndumped
......
}
end
def self._load(s)
def self._load(s) # :nodoc:
Marshal::load(s)
end
def _dump(lv)
def _dump(lv) # :nodoc:
Marshal.dump(@ary)
end
end
......
end
private
def make_proxy(obj, error=false)
def make_proxy(obj, error=false) # :nodoc:
if error
DRbRemoteError.new(obj)
else
......
# Communicates over a TCP socket.
class DRbTCPSocket
private
def self.parse_uri(uri)
def self.parse_uri(uri) # :nodoc:
if uri =~ /^druby:\/\/(.*?):(\d+)(\?(.*))?$/
host = $1
port = $2.to_i
......
public
# Open a client connection to +uri+ using configuration +config+.
# Open a client connection to +uri+ (String)
# using configuration +config+.
#
# Can raise DRbBadScheme or DRbBadURI if +uri+
# is not 'druby:...' or is malformed
# See DRb::DRbServer.new for information on the uri.
def self.open(uri, config)
host, port, = parse_uri(uri)
host.untaint
......
self.new(uri, soc, config)
end
# Return the hostname (String) of this server
def self.getservername
host = Socket::gethostname
begin
......
end
end
# For the +host+ (String) provided
# return a TCPServer on +port+ (Fixnum)
def self.open_server_inaddr_any(host, port)
infos = Socket::getaddrinfo(host, nil,
Socket::AF_UNSPEC,
......
self.new_with(uri, ref)
end
# Create a DRb::DRbObject given the reference information to the
# remote host uri String, and ref
def self.new_with(uri, ref)
it = self.allocate
it.instance_variable_set(:@uri, uri)
......
end
end
# Given the +uri+ String of another dRuby host,
# execute the &blok provided.
#
# Returns Array with [<boolean of success>, <result object>]
def self.with_friend(uri)
friend = DRb.fetch_server(uri)
return yield() unless friend
......
Thread.current['DRb'] = save if friend
end
# Given +uri+ String and a +result+, that responds like an Exception,
# return an Array of the backtrace, relative the +uri+
def self.prepare_backtrace(uri, result)
prefix = "(#{uri}) "
bt = []
......
# Set the default value for the :acl option.
#
# +acl+ is expected to be an ACL object.
#
# See #new(). The initial default value is nil.
def self.default_acl(acl)
@@acl = acl
......
@@idconv = idconv
end
# Set the default value for the :safe_level option.
#
# See #new(). The initial default value is 0.
def self.default_safe_level(level)
@@safe_level = level
end
......
# :argc_limit :: the maximum number of arguments to a remote
# method accepted by the server. Defaults to
# 256.
# :safe_level :: The safety level of the DRbServer. The attribute
# sets the $SAFE for methods performed in
# #main_loop. Defaults to 0.
#
# The default values of these options can be modified on
# a class-wide basis by the class methods #default_argc_limit,
......
# The configuration of this DRbServer
attr_reader :config
# The safety level of this DRbServer
#
# This is to be a Fixnum, corresponding with $SAFE
#
# Default +safe_level+ is 0
attr_reader :safe_level
# Set whether to operate in verbose mode.
......
@thread.alive?
end
# Check whether +uri+ (String) is in the set of known URIs
def here?(uri)
@exported_uri.include?(uri)
end
......
end
private
def run
def run # :nodoc:
Thread.start do
begin
while true
......
# Set the default id conv object.
#
# This expected to be an instance, like DRb::DRbIdConv,
# that responds to #to_id and #to_obj
#
# See DRbServer#default_id_conv.
def install_id_conv(idconv)
DRbServer.default_id_conv(idconv)
end
module_function :install_id_conv
# Set the default acl.
# Set the default ACL +acl+.
#
# See DRb::DRbServer.default_acl.
def install_acl(acl)
......
module_function :install_acl
@mutex = Mutex.new
# Get the semaphore for this DRb module
#
# Returns a Mutex instance
def mutex
@mutex
end
module_function :mutex
@server = {}
# Recored the DRb::DRbServer +server+
#
# This is called when a new DRb::DRbServer instance is created.
#
# require 'drb'
#
# s = DRb::DRbServer.new
# => #<DRb::DRbServer:0x00000001dcb440 @config={:idconv=>#<DRb::...
# DRb.fetch_server(s.uri)
# => #<DRb::DRbServer:0x00000001dcb440 @config={:idconv=>#<DRb::...
#
def regist_server(server)
@server[server.uri] = server
mutex.synchronize do
......
end
module_function :regist_server
# Remove the DRb::DRbServer +server+ from the registered list
def remove_server(server)
@server.delete(server.uri)
end
module_function :remove_server
# Return the DRb::DRbServer object, provided the String +uri+.
#
# This is provided from the registered servers.
# See also #regist_server and #remove_server
def fetch_server(uri)
@server[uri]
end
lib/drb/ssl.rb
module DRb
# DRb::DRbSSLSocket handles making a dRuby connection over an SSL connection
class DRbSSLSocket < DRbTCPSocket
# SSLConfig handles the needed SSL information for establishing
# a DRbSSLSocket connection, including generating the X509 / RSA pair.
#
# An instance of this config can be passed to DRbSSLSocket.new,
# DRbSSLSocket.open and DRbSSLSocket.open_server
#
# See DRb::DRbSSLSocket::SSLConfig.new for more details
class SSLConfig
# Default values for a SSLConfig instance.
#
# See DRb::DRbSSLSocket::SSLConfig.new for more details
DEFAULT = {
:SSLCertificate => nil,
:SSLPrivateKey => nil,
......
:SSLCertComment => "Generated by Ruby/OpenSSL"
}
# Create a new DRb::DRbSSLSocket::SSLConfig instance
#
# The DRb::DRbSSLSocket will take either a +config+ Hash or an instance
# of SSLConfg, and will setup the certificate for its session for the
# configuration. If want it to generate a generic certificate, the bare
# minimum is to provide the :SSLCertName
#
# === Config options
# From +config+ Hash
#
# :SSLCertificate :: An instance of OpenSSL::X509::Certificate.
# If this is not provided, then a generic X509 is
# generated, with a correspond :SSLPrivateKey
# :SSLPrivateKey :: A private key instance, like OpenSSL::PKey::RSA.
# This key must be the key that signed the
# :SSLCertificate
# :SSLClientCA :: An OpenSSL::X509::Certificate, or Array of certificates
# that will used as ClientCAs in the SSL Context
# :SSLCACertificatePath :: A path to the directory of CA certificates.
# The certificates are to be in PEM format.
# :SSLCACertificateFile :: A path to a CA certificate file, in PEM format.
# :SSLTmpDhCallback :: A DH callback. See
# OpenSSL::SSL::SSLContext.tmp_dh_callback
# :SSLVerifyMode :: This is the SSL verification mode.
# See OpenSSL::SSL::VERIFY_* for available modes.
# Default is OpenSSL::SSL::VERIFY_NONE
# :SSLVerifyDepth :: Number of CA certificates to walk, when verify a
# certificate chain.
# :SSLVerifyCallback :: A callback to be used for additional verification.
# See OpenSSL::SSL::SSLContext.verify_callback
# :SSLCertificateStore :: A OpenSSL::X509::Store used for verification
# of certificates
# :SSLCertName :: Issuer name for the certificate.
# This is required when generating the certificate
# (if :SSLCertificate and :SSLPrivateKey were not given)
# The value of this is to be an Array of pairs. i.e.
# [["C", "Raleigh"], ["ST","North Carolina"], ["CN","fqdn.example.com"]]
# :SSLCertComment :: A comment to be used for generating the certificate.
# Default is "Generated by Ruby/OpenSSL"
#
#
# === Example
# These values can be added after the fact, like a Hash.
#
# require 'drb/ssl'
# c = DRb::DRbSSLSocket::SSLConfig.new {}
# c[:SSLCertificate] = OpenSSL::X509::Certificate.new(File.read('mycert.crt'))
# c[:SSLPrivateKey] = OpenSSL::PKey::RSA.new(File.read('mycert.key'))
# c[:SSLVerifyMode] = OpenSSL::SSL::VERIFY_PEER
# c[:SSLCACertificatePath] = "/etc/ssl/certs/"
# c.setup_certificate
#
# or
#
# require 'drb/ssl'
# c = DRb::DRbSSLSocket::SSLConfig.new({
# :SSLCertName => [["CN" => DRb::DRbSSLSocket.getservername]]
# })
# c.setup_certificate
#
def initialize(config)
@config = config
@cert = config[:SSLCertificate]
......
@ssl_ctx = nil
end
# A convenience method to access the values like a Hash
def [](key);
@config[key] || DEFAULT[key]
end
# Connect to IO +tcp+,
# with context of the current certificate configuration
def connect(tcp)
ssl = ::OpenSSL::SSL::SSLSocket.new(tcp, @ssl_ctx)
ssl.sync = true
......
ssl
end
# Accept connection to IO +tcp+,
# with context of the current certificate configuration
def accept(tcp)
ssl = OpenSSL::SSL::SSLSocket.new(tcp, @ssl_ctx)
ssl.sync = true
......
ssl
end
# Ensures that :SSLCertificate and :SSLPrivateKey have been provided
# or that a new certificate is generated with the other parameters
# provided.
def setup_certificate
if @cert && @pkey
return
......
@pkey = rsa
end
# Establish the OpenSSL::SSL::SSLContext with the configuration
# parameters provided.
def setup_ssl_context
ctx = ::OpenSSL::SSL::SSLContext.new
ctx.cert = @cert
......
end
end
# Parse the dRuby +uri+ for an SSL connection.
#
# Expects drbssl://...
#
# Raises DRbBadScheme or DRbBadURI if +uri+ is not matching or malformed
def self.parse_uri(uri)
if uri =~ /^drbssl:\/\/(.*?):(\d+)(\?(.*))?$/
host = $1
......
end
end
# Return an DRb::DRbSSLSocket instance as a client-side connection,
# with the SSL connected.
#
# +uri+ is the URI we are connected to.
# +config+ is our configuration. Either a Hash or DRb::DRbSSLSocket::SSLConfig
#
def self.open(uri, config)
host, port, = parse_uri(uri)
host.untaint
......
self.new(uri, ssl, ssl_conf, true)
end
# Return an DRb::DRbSSLSocket instance as a server-side connection,
# with the SSL connected.
#
# +uri+ is the URI we are connected to.
# +config+ is our configuration. Either a Hash or DRb::DRbSSLSocket::SSLConfig
#
def self.open_server(uri, config)
uri = 'drbssl://:0' unless uri
host, port, = parse_uri(uri)
......
self.new(@uri, soc, ssl_conf, false)
end
# This is a convenience method to parse +uri+ and separate out any
# additional options appended in the +uri+.
#
# Returns an option-less uri and the option => [uri,option]
#
# The +config+ is completely unused, so passing nil is sufficient.
def self.uri_option(uri, config)
host, port, option = parse_uri(uri)
return "drbssl://#{host}:#{port}", option
end
# Create a DRb::DRbSSLSocket instance.
#
# +uri+ is the URI we are connected to.
# +soc+ is the tcp socket we are bound to.
# +config+ is our configuration. Either a Hash or SSLConfig
# +is_established+ is a boolean of whether +soc+ is currenly established
def initialize(uri, soc, config, is_established)
@ssl = is_established ? soc : nil
super(uri, soc.to_io, config)
end
# Returns the SSL stream
def stream; @ssl; end
# Closes the SSL stream before closing the dRuby connection.
def close
if @ssl
@ssl.close
(2-2/2)