Welcome to Giant Robots Smashing Into Other Giant Robots — a weblog about development, business, design and technology — written by thoughtbot.

LDAP Server for ActiveRecord Models

I almost titled this one “More Enterprisey Goodness for Rails.”

What we’ve got here is an LDAP server that returns values from an ActiveRecord model. It runs as a daemon next to your rails application, and is fairly configurable. It’s currently designed specifically for address book applications (tested with Thunderbird and the OS X address book), so point it at your Users model and enjoy all the tab-completability you can handle in your email client. Patches, suggestions, and general accolades are more than welcome.

(also posted here)

This daemon came about as an addon to a CRM application that we are developing for a client here at Thoughtbot.

I’m standing firmly on the shoulders of others for this one. Specifically, the Ruby LDAP Server, and this post on daemonizing ruby code.

Grab the source from our SVN repository (svn.thoughtbot.com/ldap-activerecord-gateway/).

Requirements

The only requirement that the daemon imposes on the AR class is that it implement two methods:

  • Class.search(query) – returns a collection of records that somehow match the string given by query
  • record.to_ldap_entry – returns a hash representing the LDAP information returned to the client. Here’s an example hash:
1
2
3
4
5
6
  { "objectclass" =>  [ "top", "person", "organizationalPerson", "inetOrgPerson", "mozillaOrgPerson"],
    "uid" => [123],
    "sn" => ["Lastname"],
    "givenname" => ["Firstname"],
    "cn" => [ "Firstname Lastname" ],
    "mail" => [ "email@address.com" ] }

Configuration

The LDAP gateway is a separate process from your Rails application, and is not expected to live under the RAILS_DIR. You can install it in any directory you please, and it can run as any user you need (see note about the port number below).

To configure the gateway, copy the conf/ldap-server.example.yml file to conf/ldap-server.yml and edit that file.

  • rails_dir: RAILS_DIR directory for your rails application.
  • active_record_model: Name of the AR Model you’d like to serve.
  • basedn: The LDAP basedn for your server. This is installation specific, and is usually keyed to your domainname. Whatever you put here has to be entered in the client configuration as well.
  • port: The port your server will listen on. If you are not running the server as root, then this port needs to be greater than 1000. 389 is the standard LDAP port.
  • tcp_nodelay: Not sure.
  • preforked_threads: How many threads to run.
  • bind_address: The IP address on which the server will listen. Unless you have security issues, 0.0.0.0 will work just fine.
  • debug: Set this to true to get more verbose startup and logging messages.

Running the Server

Once you’ve configured the server, you can run it with “bin/ldap-server.rb start”. It should immediately daemonize itself and start logging to log/ldap-server.log. As one of the first log messages, it should state how many records it has access to.

As a final test, you should connect to the server with an addressbook client (such as Thunderbird) and try querying for known records.

Final notes

It looks like the ruby-ldapserver library that I’m using has support for dropping privileges after binding to a port, but I wasn’t able to test that functionality. Also, I believe that you should be able to run this as a different user than the one running your rails application, but that might not be true (you might get log file ownership conflicts). Again, didn’t have time to test that part.


About this entry

 

thoughtbot is hiring

We are hiring web developers and web designers in both Boston and New York, NY.

What are we up to?

We built Shoulda, an eclectic set of additions to Test::Unit; Paperclip to manage uploaded files without hassle; factory_girl a replacement for Rails fixtures; Jester, a REST/ActiveResource client written in Javascript, and Squirrel, an enhancement for ActiveRecord's find syntax; — amongst some other projects.


Chad (President) and Jon (CTO) co-authored a technical book titled Pro Active Record: Databases with Ruby and Rails, which explores the ins and outs of the ActiveRecord ruby library. You can buy it today at Amazon.com.

About thoughtbot, inc.

We are a small web application development consulting business, with offices in Boston, MA and New York, NY. If you're looking to find a team for your next web development project or your new web application — get in touch.