[java-shib-attribute] branch main updated: IDP-2040 - LDAP connection validator has NPE when connection fails

Scott Cantor cantor.2 at osu.edu
Wed Nov 16 00:03:46 UTC 2022


This is an automated email from the git hooks/post-receive script.

scantor pushed a commit to branch main
in repository java-shib-attribute.

View the commit online:
http://git.shibboleth.net/view/?p=java-shib-attribute.git;a=commit;h=f54e9ea9139e09c781138e0f99ff24d4570a5e70

The following commit(s) were added to refs/heads/main by this push:
     new f54e9ea91 IDP-2040 - LDAP connection validator has NPE when connection fails
f54e9ea91 is described below

commit f54e9ea9139e09c781138e0f99ff24d4570a5e70
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Tue Nov 15 19:03:43 2022 -0500

    IDP-2040 - LDAP connection validator has NPE when connection fails
    
    https://shibboleth.atlassian.net/browse/IDP-2040
---
 .../dc/ldap/impl/ConnectionFactoryValidator.java   | 53 +++++++++++-----------
 1 file changed, 27 insertions(+), 26 deletions(-)

diff --git a/shib-attribute-resolver-impl/src/main/java/net/shibboleth/idp/attribute/resolver/dc/ldap/impl/ConnectionFactoryValidator.java b/shib-attribute-resolver-impl/src/main/java/net/shibboleth/idp/attribute/resolver/dc/ldap/impl/ConnectionFactoryValidator.java
index 9d2364e8a..4c37eec95 100644
--- a/shib-attribute-resolver-impl/src/main/java/net/shibboleth/idp/attribute/resolver/dc/ldap/impl/ConnectionFactoryValidator.java
+++ b/shib-attribute-resolver-impl/src/main/java/net/shibboleth/idp/attribute/resolver/dc/ldap/impl/ConnectionFactoryValidator.java
@@ -18,6 +18,7 @@
 package net.shibboleth.idp.attribute.resolver.dc.ldap.impl;
 
 import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
 
 import org.ldaptive.Connection;
 import org.ldaptive.ConnectionFactory;
@@ -27,21 +28,19 @@ import org.slf4j.LoggerFactory;
 
 import net.shibboleth.idp.attribute.resolver.dc.ValidationException;
 import net.shibboleth.idp.attribute.resolver.dc.Validator;
-import net.shibboleth.shared.component.AbstractInitializableComponent;
-import net.shibboleth.shared.component.ComponentInitializationException;
 import net.shibboleth.shared.logic.Constraint;
 
 /**
  * Validator implementation that invokes {@link Connection#open()} to determine if the ConnectionFactory is properly
  * configured.
  */
-public class ConnectionFactoryValidator extends AbstractInitializableComponent implements Validator {
+public class ConnectionFactoryValidator implements Validator {
 
     /** Class logger. */
-    private final Logger log = LoggerFactory.getLogger(ConnectionFactoryValidator.class);
+    @Nonnull private final Logger log = LoggerFactory.getLogger(ConnectionFactoryValidator.class);
 
     /** Connection factory to validate. */
-    @Nonnull private ConnectionFactory connectionFactory;
+    @Nullable private ConnectionFactory connectionFactory;
     
     /** Whether validate should throw, default value is <code>true</code>. */
     private boolean throwOnValidateError;
@@ -52,13 +51,6 @@ public class ConnectionFactoryValidator extends AbstractInitializableComponent i
      */
     public ConnectionFactoryValidator() {  
     }
-
-    /** {@inheritDoc} */
-    @Override
-    protected void doInitialize() throws ComponentInitializationException {
-        Constraint.isNotNull(connectionFactory, "Connection factory must be non-null");
-        super.doInitialize();
-    }
     
     /**
      * Sets the connection factory.
@@ -66,7 +58,6 @@ public class ConnectionFactoryValidator extends AbstractInitializableComponent i
      * @param factory the connection factory
      */
     public void setConnectionFactory(@Nonnull final ConnectionFactory factory) {
-        checkSetterPreconditions();
         connectionFactory = Constraint.isNotNull(factory, "Connection factory must be non-null");
     }
 
@@ -76,13 +67,12 @@ public class ConnectionFactoryValidator extends AbstractInitializableComponent i
      *
      * @return connection factory
      */
-    @Nonnull public ConnectionFactory getConnectionFactory() {
+    @Nullable public ConnectionFactory getConnectionFactory() {
         return connectionFactory;
     }
 
     /** {@inheritDoc} */
     public void setThrowValidateError(final boolean what) {
-        checkSetterPreconditions();
         throwOnValidateError = what;
     }
 
@@ -93,19 +83,30 @@ public class ConnectionFactoryValidator extends AbstractInitializableComponent i
 
     /** {@inheritDoc} */
     @Override public void validate() throws ValidationException {
-        try (Connection connection = connectionFactory.getConnection()) {
-            if (connection == null) {
-                log.error("Unable to retrieve connections from configured connection factory");
+        
+        if (connectionFactory == null) {
+            log.error("No connection factory installed");
+            if (isThrowValidateError()) {
+                throw new ValidationException("Connection factory is not set");
+            }
+        } else {
+            assert connectionFactory != null;
+            try (final Connection connection = connectionFactory.getConnection()) {
+                if (connection == null) {
+                    log.error("Unable to retrieve connections from configured connection factory");
+                    if (isThrowValidateError()) {
+                        throw new LdapException("Unable to retrieve connection from connection factory");
+                    }
+                } else {
+                    connection.open();
+                }
+            } catch (final LdapException e) {
+                log.error("Connection factory validation failed", e);
                 if (isThrowValidateError()) {
-                    throw new LdapException("Unable to retrieve connection from connection factory");
+                    throw new ValidationException(e);
                 }
             }
-            connection.open();
-        } catch (final LdapException e) {
-            log.error("Connection factory validation failed", e);
-            if (isThrowValidateError()) {
-                throw new ValidationException(e);
-            }
         }
     }
-}
+
+}
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list