Last active
August 29, 2015 13:58
-
-
Save johannish/9962797 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* A simple module to query an OID (Oracle Internet Directory) server in order to | |
* look up the full connection string for a given database name. | |
*/ | |
var ldap = require('ldapjs'); | |
module.exports.byAlias = function(dbAlias, callback) { | |
var client = ldap.createClient({ | |
url: 'ldap://oid:123/', | |
timeout: "2000", // LDAP operations timeout (milliseconds) | |
connectTimeout: "5000", // TCP connection timeout (milliseconds) | |
logLevel: 'trace' | |
}); | |
var opts = { | |
scope: 'base', // Return only the exact object, no sub-objects | |
attributes: ['orclnetdescstring'], // Return only these attributes | |
// NON-standard in ldapjs. See this patch: https://github.com/mcavage/node-ldapjs/issues/134 | |
//derefAliases: 'always' // LDAP server-side dereference of alias entries | |
}; | |
var username = '', password = ''; | |
client.on('error', function(err){ | |
console.log("Client experienced an error."); | |
}); | |
client.on('timeout', function(err){ | |
console.log("Client timed out."); | |
// Unbind at the first operation timeout. Normally you wouldn't do this, but we're only | |
// doing one search, and giving up if it doesn't work. | |
client.unbind(); | |
}); | |
client.on('connectTimeout', function(err){ | |
console.log("Connection timed out."); | |
}); | |
client.bind(username, password, function (err) { | |
if (err) { | |
callback(err, null); | |
return; | |
} | |
var onUnbind = function(err) {/*TODO handle error*/}; | |
var searchBase = 'CN=' + dbAlias + ',CN=OracleContext,DC=ora'; | |
client.search(searchBase, opts, function (err, res) { | |
if (err) { | |
callback(err, null); | |
client.unbind(onUnbind); | |
return; | |
} | |
var connectionStr = null; | |
// Return the first valid ldap entry without delay, then ignore the rest (if any). | |
res.on('searchEntry', function (entry) { | |
if (!connectionStr && entry.object && entry.object.orclnetdescstring) { | |
connectionStr = entry.object.orclnetdescstring; | |
callback(null, connectionStr); | |
} | |
}); | |
// Docs say: "Note that the error event will only be for client/TCP errors, not LDAP error codes" | |
res.on('error', function(err) { | |
callback(err, null); | |
client.unbind(onUnbind); | |
}); | |
res.on('end', function (result) { | |
//TODO verify result.status === 0, which indicates success | |
client.unbind(onUnbind); | |
}); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment