Search in sources :

Example 1 with WebHook

use of opengrok.auth.plugin.util.WebHook in project OpenGrok by OpenGrok.

the class LdapFacade method lookup.

/**
 * Lookups the LDAP server for content.
 *
 * @param <T> return type
 * @param dn search base for the query
 * @param filter LDAP filter for the query
 * @param attributes returning LDAP attributes
 * @param mapper mapper class implementing @code{AttributeMapper} closed
 * @param fail current count of failures
 *
 * @return results transformed with mapper or {@code null} on failure
 * @throws LdapException LDAP exception
 */
private <T> LdapSearchResult<T> lookup(String dn, String filter, String[] attributes, AttributeMapper<T> mapper, int fail) throws LdapException {
    if (errorTimestamp > 0 && errorTimestamp + interval > System.currentTimeMillis()) {
        if (!reported) {
            reported = true;
            LOGGER.log(Level.SEVERE, "LDAP server pool is still broken");
        }
        throw new LdapException("LDAP server pool is still broken");
    }
    if (fail > servers.size() - 1) {
        // did the whole rotation
        LOGGER.log(Level.SEVERE, "Tried all LDAP servers in a pool but no server works");
        errorTimestamp = System.currentTimeMillis();
        reported = false;
        WebHook hook;
        if ((hook = webHooks.getFail()) != null) {
            hook.post();
        }
        throw new LdapException("Tried all LDAP servers in a pool but no server works");
    }
    if (!isConfigured()) {
        LOGGER.log(Level.SEVERE, "LDAP is not configured");
        throw new LdapException("LDAP is not configured");
    }
    NamingEnumeration<SearchResult> namingEnum = null;
    LdapServer server = null;
    try {
        server = servers.get(actualServer);
        controls.setReturningAttributes(attributes);
        for (namingEnum = server.search(dn, filter, controls); namingEnum.hasMore(); ) {
            SearchResult sr = namingEnum.next();
            reported = false;
            if (errorTimestamp > 0) {
                errorTimestamp = 0;
                WebHook hook;
                if ((hook = webHooks.getRecover()) != null) {
                    hook.post();
                }
            }
            return new LdapSearchResult<>(sr.getNameInNamespace(), processResult(sr, mapper));
        }
    } catch (NameNotFoundException ex) {
        LOGGER.log(Level.WARNING, String.format("The LDAP name for search '%s' was not found on server %s", getSearchDescription(dn, filter, attributes), server), ex);
        throw new LdapException("The LDAP name was not found.", ex);
    } catch (SizeLimitExceededException ex) {
        LOGGER.log(Level.SEVERE, String.format("The maximum size of the LDAP result has exceeded " + "on server %s", server), ex);
        closeActualServer();
        actualServer = getNextServer();
        return lookup(dn, filter, attributes, mapper, fail + 1);
    } catch (TimeLimitExceededException ex) {
        LOGGER.log(Level.SEVERE, String.format("Time limit for LDAP operation has exceeded on server %s", server), ex);
        closeActualServer();
        actualServer = getNextServer();
        return lookup(dn, filter, attributes, mapper, fail + 1);
    } catch (CommunicationException ex) {
        LOGGER.log(Level.WARNING, String.format("Communication error received on server %s, " + "reconnecting to next server.", server), ex);
        closeActualServer();
        actualServer = getNextServer();
        return lookup(dn, filter, attributes, mapper, fail + 1);
    } catch (NamingException ex) {
        LOGGER.log(Level.SEVERE, String.format("An arbitrary LDAP error occurred on server %s " + "when searching for '%s'", server, getSearchDescription(dn, filter, attributes)), ex);
        closeActualServer();
        actualServer = getNextServer();
        return lookup(dn, filter, attributes, mapper, fail + 1);
    } finally {
        if (namingEnum != null) {
            try {
                namingEnum.close();
            } catch (NamingException e) {
                LOGGER.log(Level.WARNING, "failed to close search result enumeration");
            }
        }
    }
    return null;
}
Also used : SizeLimitExceededException(javax.naming.SizeLimitExceededException) CommunicationException(javax.naming.CommunicationException) NameNotFoundException(javax.naming.NameNotFoundException) WebHook(opengrok.auth.plugin.util.WebHook) SearchResult(javax.naming.directory.SearchResult) NamingException(javax.naming.NamingException) TimeLimitExceededException(javax.naming.TimeLimitExceededException)

Example 2 with WebHook

use of opengrok.auth.plugin.util.WebHook in project OpenGrok by OpenGrok.

the class ConfigurationTest method testEncodeDecode.

@Test
void testEncodeDecode() {
    // Create an exception listener to detect errors while encoding and
    // decoding
    final LinkedList<Exception> exceptions = new LinkedList<>();
    ExceptionListener listener = exceptions::addLast;
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    XMLEncoder enc = new XMLEncoder(out);
    enc.setExceptionListener(listener);
    Configuration configuration1 = new Configuration();
    configuration1.setInterval(500);
    configuration1.setSearchTimeout(1000);
    configuration1.setConnectTimeout(42);
    configuration1.setCountLimit(10);
    configuration1.setServers(new ArrayList<>(List.of(new LdapServer("http://server.com"))));
    WebHooks webHooks = new WebHooks();
    WebHook hook = new WebHook();
    hook.setContent("foo");
    hook.setURI("http://localhost:8080/source/api/v1/messages");
    webHooks.setFail(hook);
    configuration1.setWebHooks(webHooks);
    enc.writeObject(configuration1);
    enc.close();
    // verify that the write didn't fail
    if (!exceptions.isEmpty()) {
        throw new AssertionError("Got " + exceptions.size() + " exception(s)", exceptions.getFirst());
    }
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    XMLDecoder dec = new XMLDecoder(in, null, listener);
    Configuration configuration2 = (Configuration) dec.readObject();
    assertNotNull(configuration2);
    assertEquals(configuration1.getXMLRepresentationAsString(), configuration2.getXMLRepresentationAsString());
    dec.close();
    // verify that the read didn't fail
    if (!exceptions.isEmpty()) {
        throw new AssertionError("Got " + exceptions.size() + " exception(s)", exceptions.getFirst());
    }
}
Also used : WebHook(opengrok.auth.plugin.util.WebHook) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) LinkedList(java.util.LinkedList) LdapServer(opengrok.auth.plugin.ldap.LdapServer) XMLEncoder(java.beans.XMLEncoder) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLDecoder(java.beans.XMLDecoder) ExceptionListener(java.beans.ExceptionListener) WebHooks(opengrok.auth.plugin.util.WebHooks) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

WebHook (opengrok.auth.plugin.util.WebHook)2 ExceptionListener (java.beans.ExceptionListener)1 XMLDecoder (java.beans.XMLDecoder)1 XMLEncoder (java.beans.XMLEncoder)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 LinkedList (java.util.LinkedList)1 CommunicationException (javax.naming.CommunicationException)1 NameNotFoundException (javax.naming.NameNotFoundException)1 NamingException (javax.naming.NamingException)1 SizeLimitExceededException (javax.naming.SizeLimitExceededException)1 TimeLimitExceededException (javax.naming.TimeLimitExceededException)1 SearchResult (javax.naming.directory.SearchResult)1 LdapServer (opengrok.auth.plugin.ldap.LdapServer)1 WebHooks (opengrok.auth.plugin.util.WebHooks)1 Test (org.junit.jupiter.api.Test)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1