From 829097ddaa118c0283c7e2191f9afeb241fa195b Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Mon, 14 Jan 2013 16:20:11 +0100 Subject: o Adding a tool to generate Person's from a LDAP directory. --- .../io/trygvis/esper/testing/PersonGenerator.java | 121 +++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100755 src/main/java/io/trygvis/esper/testing/PersonGenerator.java (limited to 'src/main') diff --git a/src/main/java/io/trygvis/esper/testing/PersonGenerator.java b/src/main/java/io/trygvis/esper/testing/PersonGenerator.java new file mode 100755 index 0000000..28d8438 --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/PersonGenerator.java @@ -0,0 +1,121 @@ +package io.trygvis.esper.testing; + +import com.jolbox.bonecp.*; +import io.trygvis.esper.testing.core.db.*; +import io.trygvis.esper.testing.jenkins.*; +import io.trygvis.esper.testing.util.sql.*; +import org.slf4j.*; + +import java.sql.*; +import java.util.*; +import javax.naming.*; +import javax.naming.directory.*; + +public class PersonGenerator { + public static void main(String[] args) throws Exception { + Config config = Config.loadFromDisk("person-generator"); + + final Logger logger = LoggerFactory.getLogger(config.appName); + + String ldapHost = System.getProperty("ldap.host", "localhost"); + int ldapPort = Integer.parseInt(System.getProperty("ldap.port", "386")); + String baseDn = System.getProperty("ldap.base"); + + if(baseDn == null) { + System.err.println("Missing system property: ldap.base"); + return; + } + + Properties properties = new Properties(); + properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); + properties.put(Context.PROVIDER_URL, "ldap://" + ldapHost + ":" + ldapPort); + properties.put(Context.REFERRAL, "ignore"); + properties.put(Context.SECURITY_AUTHENTICATION, "none"); + + InitialDirContext context = new InitialDirContext(properties); + SearchControls searchCtls = new SearchControls(); + searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); + String returnedAtts[] = {"uid", "mail", "displayName"}; + searchCtls.setReturningAttributes(returnedAtts); + NamingEnumeration answer = context.search(baseDn, "(&(uid=*)(mail=*)(displayName=*))", searchCtls); + + BoneCPDataSource dataSource = config.createBoneCp(); + final Daos daos = new Daos(dataSource.getConnection()); + PersonDao personDao = daos.personDao; + List servers = daos.jenkinsDao.selectServers(false); + + while (answer.hasMore()) { + SearchResult next = (SearchResult) answer.next(); + String uid = next.getAttributes().get("uid").get().toString(); + String mail = next.getAttributes().get("mail").get().toString(); + String displayName = next.getAttributes().get("displayName").get().toString(); + + logger.info("LDAP: uid = {}, mail = {}, displayName = {}", uid, mail, displayName); + + SqlOption personO = personDao.selectPersonByMail(mail); + + Uuid personUuid; + if(personO.isNone()) { + personUuid = personDao.insertPerson(mail, displayName); + logger.info("New person: uuid={}", personUuid); + } + else { + personUuid = personO.get().uuid; + logger.info("Existing person: uuid={}", personUuid); + } + + claimUid(logger, daos, servers, uid, personUuid); + + // Add the username from their email addresses as aliases too + int i = mail.indexOf('@'); + + if (i == -1) { + continue; + } + + String alternate = mail.substring(0, i); + + if(alternate.equals(uid)) { + continue; + } + + claimUid(logger, daos, servers, alternate, personUuid); + } + +// System.out.println("ROLLBACK"); +// daos.rollback(); + logger.info("COMMIT"); + daos.commit(); + logger.info("Closing SQL connection"); + daos.close(); + + logger.info("Closing pool"); + dataSource.close(); + System.exit(0); + } + + private static void claimUid(final Logger logger, final Daos daos, List servers, String uid, Uuid personUuid) throws SQLException { + for (final JenkinsServerDto server : servers) { + final String url = server.userUrl(uid).toASCIIString(); + + UUID jenkinsUserUuid = daos.jenkinsDao.selectUserByAbsoluteUrl(server.uuid, url).map(new SqlF() { + public UUID apply(JenkinsUserDto jenkinsUserDto) throws SQLException { + return jenkinsUserDto.uuid; + } + }).getOrElse(new SqlP0() { + public UUID apply() throws SQLException { + logger.info("New Jenkins user for server {}", server.url); + return daos.jenkinsDao.insertUser(server.uuid, url); + } + }); + + if(!daos.personDao.hasPersonJenkinsUser(personUuid, jenkinsUserUuid)) { + logger.info("Person claims jenkins user: " + url); + daos.personDao.insertPersonJenkinsUser(personUuid, jenkinsUserUuid); + } + else { + logger.info("Person already had a claim for: " + url); + } + } + } +} -- cgit v1.2.3