Skip to content

Commit

Permalink
Merge pull request #2 from joshmoore/all-users
Browse files Browse the repository at this point in the history
Print all users if no arguments are passed
  • Loading branch information
chris-allan authored Apr 28, 2021
2 parents ede6557 + c03d1f0 commit 48a97e3
Show file tree
Hide file tree
Showing 5 changed files with 322 additions and 98 deletions.
24 changes: 13 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Command line tool for working with OMERO and LDAP
Requirements
============

* OMERO 5.4.x+
* OMERO 5.6.x+
* Java 8+

Workflow
Expand All @@ -17,21 +17,23 @@ User lookup

```
$ omero-ldaptool --help
Usage: <main class> [--debug] [--help] <config> <username>
<config> LDAP configuration properties file
<username> Username to search for
--debug Set logging level to DEBUG
--help Display this help and exit
Usage: <main class> [--help] [--log-level=<logLevel>] (--all |
--user=<username>) <config>
<config> LDAP configuration properties file
--all Print all users
--help Display this help and exit
--log-level=<logLevel>
Change logging level; valid values are OFF, ERROR,
WARN, INFO, DEBUG, TRACE and ALL. (default: WARN)
--user=<username> Username to search
```

The format of "config" is a standard Java properties file which should at a
minimum include the `omero.db.*` and `omero.ldap.*` configuration
options from your OMERO server.
options from your OMERO server:

Additional non-standard options for socket timeout testing:

* `omero.ldap.connect_timeout` (in milliseconds; sets `com.sun.jndi.ldap.connect.timeout` on the Spring LDAP default context source)
* `omero.ldap.read_timeout` (in milliseconds; sets `com.sun.jndi.ldap.read.timeout` on the Spring LDAP default context source)
* https://docs.openmicroscopy.org/omero/5.6.3/sysadmins/server-ldap.html
* https://docs.openmicroscopy.org/omero/5.6.3/sysadmins/config.html#ldap

Development Installation
========================
Expand Down
47 changes: 11 additions & 36 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,55 +25,30 @@ repositories {

configurations.all {
resolutionStrategy.cacheChangingModulesFor 0, 'minutes'
exclude group: 'OME'
exclude group: 'antlr'
exclude group: 'asm'
exclude group: 'backport-util-concurrent'
exclude group: 'batik'
exclude group: 'cglib'
exclude group: 'checkstyle'
exclude group: 'com.drewnoakes'
exclude group: 'com.jamonapi'
exclude group: 'com.mortennobel'
exclude group: 'com.zeroc', module: 'freeze'
exclude group: 'com.zeroc', module: 'icefreeze'
exclude group: 'com.zeroc', module: 'icegrid'
exclude group: 'com.zeroc', module: 'icestorm'
exclude group: 'commons-beanutils'
exclude group: 'commons-codec'
exclude group: 'commons-collections'
exclude group: 'commons-io'
exclude group: 'commons-lang'
exclude group: 'commons-pool'
exclude group: 'dom4j'
exclude group: 'edu.ucar'
exclude group: 'freemarker'
exclude group: 'geronimo-spec'
exclude group: 'gnu.getopt'
exclude group: 'hsqldb'
exclude group: 'javassist'
exclude group: 'javax.jts'
exclude group: 'jmock'
exclude group: 'net.sourceforge.findbugs'
exclude group: 'org.apache.ant'
exclude group: 'org.apache.lucene'
exclude group: 'org.apache.httpcomponents'
exclude group: 'net.sf.ehcache'
exclude group: 'org.apache.pdfbox'
exclude group: 'org.apache.xmlgraphics'
exclude group: 'org.ini4j'
exclude group: 'org.uncommons'
exclude group: 'org.javassist'
exclude group: 'org.quartz-scheduler'
exclude group: 'org.subethamail'
exclude group: 'pdfbox'
exclude group: 'quartz'
exclude group: 'xerces'
exclude group: 'xalan'
exclude group: 'xml-apis'
exclude group: 'zeroc', module: 'ice-db'
}

dependencies {
implementation ('omero:blitz:5.4.10-ice36-b105') {
exclude group: 'org.testng', module: 'testng'
}
implementation 'org.openmicroscopy:omero-blitz:5.5.8'
implementation 'org.springframework.security:spring-security-ldap:4.2.4.RELEASE'
implementation 'ch.qos.logback:logback-classic:1.1.7'
implementation 'ch.qos.logback:logback-core:1.1.7'
implementation 'info.picocli:picocli:3.9.3'
implementation 'org.slf4j:jcl-over-slf4j:1.7.22'
implementation 'info.picocli:picocli:4.6.1'
testImplementation 'org.testng:testng:6.10'
}

Expand Down
140 changes: 90 additions & 50 deletions src/main/java/com/glencoesoftware/ldaptool/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.StringJoiner;
import java.util.concurrent.Callable;

import picocli.CommandLine;
import picocli.CommandLine.ArgGroup;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

Expand All @@ -57,34 +59,36 @@ public class Main implements Callable<Integer>
)
boolean help;

@Option(names = "--debug", description = "Set logging level to DEBUG")
boolean debug;
@Option(
names = "--log-level",
description = "Change logging level; valid values are " +
"OFF, ERROR, WARN, INFO, DEBUG, TRACE and ALL. " +
"(default: ${DEFAULT-VALUE})"
)
String logLevel = "WARN";

@ArgGroup(exclusive = true, multiplicity = "1")
SearchFor searchFor;

static class SearchFor {
@Option(names = "--all", description = "Print all users")
boolean all;

@Option(names = "--user", description = "Username to search")
String username;
}

@Parameters(
index = "0",
description = "LDAP configuration properties file"
)
File config;

@Parameters(
index = "1",
description = "Username to search for"
)
String username;
// Non-CLI fields
LdapImpl ldapImpl;
LdapTemplate ldapTemplate;

Main()
{
ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger)
LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
if (debug)
{
root.setLevel(Level.DEBUG);
}
else
{
root.setLevel(Level.INFO);
}
}
Main() { }

public static void main(String[] args)
{
Expand All @@ -111,59 +115,95 @@ public GroupLoader newGroupLoader(

@Override
public Integer call() throws Exception {
ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger)
LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
root.setLevel(Level.toLevel(logLevel));

log.info("Loading LDAP configuration from: {}",
config.getAbsolutePath());
config.getAbsolutePath());
try (FileInputStream v = new FileInputStream(config)) {
Properties properties = System.getProperties();
properties.load(v);
log.info("Properties: {}", properties);
System.setProperties(properties);
}

OmeroContext context = new OmeroContext(new String [] {
OmeroContext context = new OmeroContext(new String[]{
"classpath:ome/config.xml",
"classpath:ome/services/datalayer.xml",
"classpath*:beanRefContext.xml"});

LdapImpl ldapImpl =
(LdapImpl) context.getBean("internal-ome.api.ILdap");
LdapTemplate ldapTemplate =
(LdapTemplate) context.getBean("ldapTemplate");
ldapImpl = (LdapImpl) context.getBean("internal-ome.api.ILdap");
ldapTemplate = (LdapTemplate) context.getBean("ldapTemplate");
String referral = context.getProperty("omero.ldap.referral");
Field ignorePartialResultException =
LdapTemplate.class.getDeclaredField("ignorePartialResultException");
LdapTemplate.class.getDeclaredField("ignorePartialResultException");
ignorePartialResultException.setAccessible(true);
log.info("Ignoring partial result exceptions? {}",
ignorePartialResultException.get(ldapTemplate));
log.info("Referral set to: '{}'", referral);
String dn = ldapImpl.findDN(username);
log.info("Found DN: {}", dn);
Experimenter experimenter = ldapImpl.findExperimenter(username);
log.info(
"Experimenter field mappings id={} email={} firstName={} " +
"lastName={} institution={} ldap={} middleName={} omeName={}",
experimenter.getId(), experimenter.getEmail(),
experimenter.getFirstName(), experimenter.getLastName(),
experimenter.getInstitution(), experimenter.getLdap(),
experimenter.getMiddleName(), experimenter.getOmeName()
);

System.out.println("---");
if (searchFor.all) {
lookupAllUsers(ldapImpl, ldapTemplate);
} else {
try {
Experimenter experimenter = ldapImpl.findExperimenter(searchFor.username);
lookupUser(ldapImpl, ldapTemplate, experimenter);
} catch (ome.conditions.ApiUsageException api) {
System.err.println("no such user: " + searchFor.username);
return 1;
}
}
return 0;
}

public void lookupAllUsers(LdapImpl ldapImpl, LdapTemplate ldapTemplate) throws Exception {
List<Experimenter> users = ldapImpl.searchAll();
for (Experimenter user : users) {
lookupUser(ldapImpl, ldapTemplate, user);
}
}

public void lookupUser(LdapImpl ldapImpl, LdapTemplate template, Experimenter user) throws Exception {

String dn = (String) user.retrieve("LDAP_DN");

// This class needs updating in omero-server to make it also return strings
GroupLoader groupLoader = newGroupLoader(
ldapImpl, username, new DistinguishedName(dn));
ldapImpl, user.getOmeName(), new DistinguishedName(dn));
Field groups = LdapImpl.GroupLoader.class.getDeclaredField("groups");
groups.setAccessible(true);
List<Long> groupIds = (List<Long>) groups.get(groupLoader);
List<Long> ownedGroupIds = groupLoader.getOwnedGroups();
log.info(
"Would be member of Group IDs={}",
Arrays.toString(groupIds.toArray())
);
log.info(
"Would be owner of Group IDs={}",
Arrays.toString(ownedGroupIds.toArray())
);
printString("- dn", dn);
printString(" omeName", user.getOmeName());
printString(" firstName", user.getFirstName());
printString(" middleName", user.getMiddleName());
printString(" lastName", user.getLastName());
printString(" email", user.getEmail());
printString(" institution", user.getInstitution());
printGroup("owner", groupLoader.getOwnedGroups());
printGroup("member", (List<Long>) groups.get(groupLoader));

return 0;
}

private void printString(String key, String value) {
if (value == null) {
return;
}
value = '"' + value + '"';
System.out.println(String.format("%s: %s", key, value));
}

private void printGroup(String key, List<Long> groups) {
if (groups == null || groups.size() == 0) {
return;
}

StringJoiner joiner = new StringJoiner(", ");
for (Long id : groups) {
joiner.add(id.toString());
}
System.out.println(String.format(" %s: [%s]", key, joiner.toString()));
}

}
2 changes: 1 addition & 1 deletion src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<logger name="ome.security.auth" level="DEBUG"/>

<root level="info">
<root level="warn">
<appender-ref ref="STDOUT" />
</root>
</configuration>
Loading

0 comments on commit 48a97e3

Please sign in to comment.