Skip to content

Commit

Permalink
Add logging for test LdapServer actions (opensearch-project#3933)
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Nied <[email protected]>
  • Loading branch information
peternied authored Jan 11, 2024
1 parent 37d3ca2 commit b905999
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
40 changes: 25 additions & 15 deletions src/test/java/com/amazon/dlic/auth/ldap/srv/LdapServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Handler;
import java.util.logging.LogRecord;

import com.google.common.io.CharStreams;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand Down Expand Up @@ -153,9 +156,8 @@ private synchronized int configureAndStartServer(String... ldifFiles) throws Exc
config.setEnforceAttributeSyntaxCompliance(false);
config.setEnforceSingleStructuralObjectClass(false);

// config.setLDAPDebugLogHandler(DEBUG_HANDLER);
// config.setAccessLogHandler(DEBUG_HANDLER);
// config.addAdditionalBindCredentials(configuration.getBindDn(), configuration.getPassword());
config.setLDAPDebugLogHandler(new ServerLogger());
config.setAccessLogHandler(new ServerLogger());

server = new InMemoryDirectoryServer(config);

Expand Down Expand Up @@ -214,25 +216,33 @@ private int loadLdifFiles(String... ldifFiles) throws Exception {
return ldifLoadCount;
}

/* private static class DebugHandler extends Handler {
private final static Logger LOG = LogManager.getLogger(DebugHandler.class);
private static class ServerLogger extends Handler {
final Logger logger = LogManager.getLogger(ServerLogger.class);

@Override
public void publish(LogRecord logRecord) {
//LOG.debug(ToStringBuilder.reflectionToString(logRecord, ToStringStyle.MULTI_LINE_STYLE));
public void publish(final LogRecord logRecord) {
logger.log(toLog4jLevel(logRecord.getLevel()), logRecord.getMessage(), logRecord.getThrown());
}

@Override
public void flush() {
}
public void flush() {}

@Override
public void close() throws SecurityException {
public void close() throws SecurityException {}

private Level toLog4jLevel(java.util.logging.Level javaLevel) {
switch (javaLevel.getName()) {
case "SEVERE":
return Level.ERROR;
case "WARNING":
return Level.WARN;
case "INFO":
return Level.INFO;
case "CONFIG":
return Level.DEBUG;
default:
return Level.TRACE;
}
}
}
private static final DebugHandler DEBUG_HANDLER = new DebugHandler();
*/
}
6 changes: 6 additions & 0 deletions src/test/resources/log4j2-test.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ rootLogger.level = warn
rootLogger.appenderRef.console.ref = console
rootLogger.appenderRef.file.ref = LOGFILE

# For troubleshooting com.amazon.dlic.auth.ldap.* test cases
logger.ldapServerLogger.name = com.amazon.dlic.auth.ldap.srv.LdapServer.ServerLogger
logger.ldapServerLogger.level = info
logger.ldapAuthBackend.name = com.amazon.dlic.auth.ldap.backend.LDAPAuthorizationBackend
logger.ldapAuthBackend.level = debug

#logger.resolver.name = org.opensearch.security.resolver
#logger.resolver.level = trace

Expand Down

0 comments on commit b905999

Please sign in to comment.