You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@zeppelin.apache.org by pd...@apache.org on 2025/07/17 06:56:52 UTC

(zeppelin) branch branch-0.12 updated: [ZEPPELIN-6242] Refactor initAuth in Neo4jConnectionManager for safer enum parsing and default handling

This is an automated email from the ASF dual-hosted git repository.

pdallig pushed a commit to branch branch-0.12
in repository https://gitbox.apache.org/repos/asf/zeppelin.git


The following commit(s) were added to refs/heads/branch-0.12 by this push:
     new 19b1b08e43 [ZEPPELIN-6242] Refactor initAuth in Neo4jConnectionManager for safer enum parsing and default handling
19b1b08e43 is described below

commit 19b1b08e432cac29dac67eda4ca85edcaf104476
Author: eunhwa99 <68...@users.noreply.github.com>
AuthorDate: Thu Jul 17 15:56:24 2025 +0900

    [ZEPPELIN-6242] Refactor initAuth in Neo4jConnectionManager for safer enum parsing and default handling
    
    ### What is this PR for?
    This PR improves the Neo4j authentication initialization logic by providing a default authentication type of `NONE` when the `neo4j.auth.type` property is absent.
    It enhances enum parsing with explicit try-catch handling to deliver clear, user-friendly error messages for unsupported or invalid authentication types.
    
    Additionally, a defensive default branch is not removed in the switch statement to satisfy the compiler and guard against unforeseen enum values.
    
    ### What type of PR is it?
    Refactoring
    
    ### Todos
    * [x] Provide default `NONE` value when `neo4j.auth.type` is missing
    * [x] Wrap enum parsing in try-catch with explicit error messages
    * [X] Add unit test for the modified code.
    
    ### What is the Jira issue?
    * [ZEPPELIN-6242](https://issues.apache.org/jira/browse/ZEPPELIN-6242)
    
    ### How should this be tested?
    * Add new unit test for the modified code.
       * Tests for missing auth type defaulting to NONE, invalid auth types throwing clear exceptions, and successful BASIC/NONE authentications should be confirmed
    
    ### Screenshots (if appropriate)
    N/A
    
    ### Questions:
    * Does the license files need to update? No
    * Is there breaking changes for older versions? No
    * Does this needs documentation? No
    
    Closes #4975 from eunhwa99/ZEPPELIN-6242.
    
    Signed-off-by: Philipp Dallig <ph...@gmail.com>
    (cherry picked from commit 86e94f3fe6acdc24b8c73c82ec5558843aa8c15a)
    Signed-off-by: Philipp Dallig <ph...@gmail.com>
---
 .../graph/neo4j/Neo4jConnectionManager.java        | 15 +++-
 .../graph/neo4j/Neo4jConnectionManagerTest.java    | 95 ++++++++++++++++++++++
 2 files changed, 107 insertions(+), 3 deletions(-)

diff --git a/neo4j/src/main/java/org/apache/zeppelin/graph/neo4j/Neo4jConnectionManager.java b/neo4j/src/main/java/org/apache/zeppelin/graph/neo4j/Neo4jConnectionManager.java
index 4bda1a91da..8f289175fa 100644
--- a/neo4j/src/main/java/org/apache/zeppelin/graph/neo4j/Neo4jConnectionManager.java
+++ b/neo4j/src/main/java/org/apache/zeppelin/graph/neo4j/Neo4jConnectionManager.java
@@ -85,8 +85,16 @@ public class Neo4jConnectionManager {
   }
 
   private AuthToken initAuth(Properties properties) {
-    String authType = properties.getProperty(NEO4J_AUTH_TYPE);
-    switch (Neo4jAuthType.valueOf(authType.toUpperCase())) {
+    String authType = properties.getProperty(NEO4J_AUTH_TYPE, "NONE").toUpperCase();
+
+    Neo4jAuthType neo4jAuthType;
+    try {
+      neo4jAuthType = Neo4jAuthType.valueOf(authType);
+    } catch (IllegalArgumentException e) {
+      throw new IllegalArgumentException("Unsupported Neo4j authentication type: " + authType, e);
+    }
+
+    switch (neo4jAuthType) {
       case BASIC:
         String username = properties.getProperty(NEO4J_AUTH_USER);
         String password = properties.getProperty(NEO4J_AUTH_PASSWORD);
@@ -95,7 +103,7 @@ public class Neo4jConnectionManager {
         LOGGER.debug("Creating NONE authentication");
         return AuthTokens.none();
       default:
-        throw new RuntimeException("Neo4j authentication type not supported");
+        throw new RuntimeException("Unexpected Neo4j authentication type");
     }
   }
 
@@ -154,3 +162,4 @@ public class Neo4jConnectionManager {
     return keys;
   }
 }
+
diff --git a/neo4j/src/test/java/org/apache/zeppelin/graph/neo4j/Neo4jConnectionManagerTest.java b/neo4j/src/test/java/org/apache/zeppelin/graph/neo4j/Neo4jConnectionManagerTest.java
new file mode 100644
index 0000000000..1f7320b795
--- /dev/null
+++ b/neo4j/src/test/java/org/apache/zeppelin/graph/neo4j/Neo4jConnectionManagerTest.java
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zeppelin.graph.neo4j;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.util.Properties;
+import org.junit.jupiter.api.Test;
+
+class Neo4jConnectionManagerTest {
+
+  private static final String NEO4J_AUTH_TYPE = "neo4j.auth.type";
+  private static final String NEO4J_AUTH_USER = "neo4j.auth.user";
+  private static final String NEO4J_AUTH_PASSWORD = "neo4j.auth.password";
+
+
+  @Test
+  void testCreateNeo4jConnectionManagerSuccess() {
+    Properties props = new Properties();
+    props.setProperty(NEO4J_AUTH_TYPE, "BASIC");
+    props.setProperty(NEO4J_AUTH_USER, "admin");
+    props.setProperty(NEO4J_AUTH_PASSWORD, "secret");
+
+    Neo4jConnectionManager neo4jConnectionManager = new Neo4jConnectionManager(props);
+
+    assertNotNull(neo4jConnectionManager);
+  }
+
+  @Test
+  void testCreateNeo4jConnectionManagerDefaultNoneAuthWhenMissingTypeSuccess() {
+    Properties props = new Properties();
+    props.setProperty(NEO4J_AUTH_USER, "admin");
+    props.setProperty(NEO4J_AUTH_PASSWORD, "secret");
+
+    Neo4jConnectionManager neo4jConnectionManager = new Neo4jConnectionManager(props);
+
+    assertNotNull(neo4jConnectionManager);
+  }
+
+  @Test
+  void testCreateNeo4jConnectionAuthTypeCaseInsensitiveSuccess() {
+    Properties props = new Properties();
+    props.setProperty(NEO4J_AUTH_TYPE, "basic"); // lowercase intentionally
+    props.setProperty(NEO4J_AUTH_USER, "user");
+    props.setProperty(NEO4J_AUTH_PASSWORD, "pw");
+
+    Neo4jConnectionManager neo4jConnectionManager = new Neo4jConnectionManager(props);
+
+    assertNotNull(neo4jConnectionManager);
+  }
+
+  @Test
+  void testCreateNeo4jConnectionManagerInvalidAuthTypeFail() {
+    Properties props = new Properties();
+    props.setProperty(NEO4J_AUTH_TYPE, "INVALID");
+    props.setProperty(NEO4J_AUTH_USER, "admin");
+    props.setProperty(NEO4J_AUTH_PASSWORD, "secret");
+
+    Exception exception = assertThrows(IllegalArgumentException.class, () ->
+        new Neo4jConnectionManager(props)
+    );
+
+    assertTrue(exception.getMessage().contains("Unsupported Neo4j authentication type: INVALID"));
+  }
+
+  @Test
+  void testCreateNeo4jConnectionWithMissingUsernameFail() {
+    Properties props = new Properties();
+    props.setProperty(NEO4J_AUTH_TYPE, "BASIC");
+    props.setProperty(NEO4J_AUTH_PASSWORD, "secret");
+
+    Exception exception = assertThrows(NullPointerException.class, () ->
+        new Neo4jConnectionManager(props)
+    );
+
+    assertTrue(exception.getMessage().contains("Username can't be null"));
+  }
+}