Marat
Marat

Reputation: 161

Searching LDAP from Jenkins scripted pipeline

we store a GitHub account in one of the AD User attribute. When receiving a Pull Request webhook from GitHub I want to find a user based on GitHub Account and notify user about tests results. How to do that?

Upvotes: 1

Views: 1013

Answers (1)

daggett
daggett

Reputation: 28634

if I understand you correctly you want to take email attribute value by another user attribute. can't test the following code but it should give you an idea how to do that.

import javax.naming.directory.InitialDirContext
import javax.naming.directory.DirContext

Properties ldapProps = [
    'java.naming.factory.initial'        :'com.sun.jndi.ldap.LdapCtxFactory',
    'java.naming.security.authentication':'simple',
    'java.naming.provider.url'           :'ldap://ldap-host:389',
    'java.naming.security.principal'     :'ldap-access-username',
    'java.naming.security.credentials'   :'ldap-access-password',
] as Properties


String user          = 'user-to-search'
String attr          = 'mail'           //attribute name for email could be different in your ldap
String ldapFilter    = "CN=${user}"     //put instead of `CN` git attribute
String[] attrValues  = []               //array because could be several values for one attribute

DirContext ldapCtx = new InitialDirContext(ldapProps)
ldapCtx.search("", ldapFilter, null).each{ldapUser->
    def ldapAttr = ldapUser.getAttributes().get(attr)
    attrValues = ldapAttr?.getAll()?.collect{it.toString()}
}
ldapCtx.close()
println "found values: $attrValues"

Upvotes: 1

Related Questions