Binding to localhost, UNIX sockets and SSL
Three ways to narrow who can reach a dRuby server: bind to the loopback interface, use a UNIX domain socket with file permissions, or use drbssl with certificates.
Updated
The safest dRuby server is one that cannot be reached from the network at all. If every process is on one machine, bind to localhost or use a UNIX domain socket. If the calls must cross machines, keep them on a private network, add an ACL, and consider drbssl:// with certificates on both sides.
Bind to localhost
require 'drb/drb'
DRb.start_service('druby://localhost:8787', Object.new)
DRb.thread.join
The service listens on the loopback interface, so connections from other machines never arrive. Compare that with a URI that leaves the host out, such as druby://:8787, which listens on every interface the machine has.
Clients on the same machine that pass blocks or callback objects should bind their own service the same way:
DRb.start_service('druby://localhost:0')
Loopback keeps other machines out. It does not separate users: any local user or program can still connect to the port.
Use a UNIX domain socket
require 'drb/drb'
require 'drb/unix'
DRb.start_service('drbunix:/tmp/app.sock', Object.new, { UNIXFileMode: 0600 })
DRb.thread.join
A UNIX socket is a file, so file permissions decide who can connect. UNIXFileMode: 0600 limits it to the user that owns the server process. This is the tightest option for processes on one machine. Putting the socket in a directory that only that user can enter is a sensible extra step.
Reach a remote server through a tunnel
When a single user needs to reach a dRuby server on another machine, one common approach avoids opening the port at all: leave the server bound to localhost and forward a local port to it over SSH. The client connects to a druby://localhost:... URI on its own machine and SSH carries the traffic, with SSH's encryption and login.
Callbacks need a route in the opposite direction, so this suits clients that only send plain values and receive plain values back.
drbssl://
drb/ssl adds a protocol that wraps the connection in SSL/TLS. Certificates and verification are set through the config hash.
# server.rb
require 'drb/drb'
require 'drb/ssl'
config = {
SSLCertificate: OpenSSL::X509::Certificate.new(File.read('server-cert.pem')),
SSLPrivateKey: OpenSSL::PKey.read(File.read('server-key.pem')),
SSLCACertificateFile: 'ca-cert.pem',
SSLVerifyMode: OpenSSL::SSL::VERIFY_PEER | OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT
}
DRb.start_service('drbssl://localhost:8787', Object.new, config)
DRb.thread.join
# client.rb
require 'drb/drb'
require 'drb/ssl'
config = {
SSLCertificate: OpenSSL::X509::Certificate.new(File.read('client-cert.pem')),
SSLPrivateKey: OpenSSL::PKey.read(File.read('client-key.pem')),
SSLCACertificateFile: 'ca-cert.pem',
SSLVerifyMode: OpenSSL::SSL::VERIFY_PEER
}
DRb.start_service('drbssl://localhost:0', nil, config)
remote = DRbObject.new_with_uri('drbssl://localhost:8787')
The file names are placeholders for a certificate authority you create and the certificates it signs. The important setting is the verify mode on the server: VERIFY_PEER together with VERIFY_FAIL_IF_NO_PEER_CERT makes the server refuse clients that do not present a certificate signed by your CA. Without client verification, SSL hides the traffic but still lets anyone connect.
The client's settings come from the config of its own running service, which is why they are passed to DRb.start_service there.
What SSL changes and what it does not
- It encrypts traffic and, with verification on both sides, limits connections to holders of a certificate you issued.
- It does not make hostile peers safe. A peer with a valid certificate has the same power over the server as any other dRuby client, for the reasons on why dRuby must not face untrusted networks.
Summary
| Setup | Who can connect |
|---|---|
drbunix: with mode 0600 |
One local user |
druby://localhost |
Any local user or program |
druby:// plus ACL |
Any user on the allowed addresses |
drbssl:// with client certificates |
Holders of a certificate from your CA |