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

jchen21 pushed a commit to branch support/1.14
in repository https://gitbox.apache.org/repos/asf/geode.git


The following commit(s) were added to refs/heads/support/1.14 by this push:
     new 52a0592  GEODE-9515: Skip setting MBeanServer when JMXConnectorServer 
will set it (#6770) (#6842)
52a0592 is described below

commit 52a0592ff6e61980896b5a437a225d4a0d799273
Author: Jianxia Chen <[email protected]>
AuthorDate: Fri Sep 3 12:41:34 2021 -0700

    GEODE-9515: Skip setting MBeanServer when JMXConnectorServer will set it 
(#6770) (#6842)
    
    (cherry picked from commit 280bd841427f244f507633826def2f845a9b10ef)
---
 .../internal/ManagementAgentIntegrationTest.java   | 123 +++++++++++++++++++++
 .../geode/management/internal/ManagementAgent.java |  27 +++--
 2 files changed, 142 insertions(+), 8 deletions(-)

diff --git 
a/geode-core/src/integrationTest/java/org/apache/geode/management/internal/ManagementAgentIntegrationTest.java
 
b/geode-core/src/integrationTest/java/org/apache/geode/management/internal/ManagementAgentIntegrationTest.java
new file mode 100644
index 0000000..15815a5
--- /dev/null
+++ 
b/geode-core/src/integrationTest/java/org/apache/geode/management/internal/ManagementAgentIntegrationTest.java
@@ -0,0 +1,123 @@
+/*
+ * 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.geode.management.internal;
+
+import static org.assertj.core.api.Assertions.assertThatCode;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.io.File;
+import java.io.IOException;
+import java.lang.management.ManagementFactory;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.management.MBeanServer;
+import javax.management.remote.JMXConnectorServer;
+import javax.management.remote.JMXServiceURL;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import org.apache.geode.distributed.internal.DistributionConfig;
+import org.apache.geode.internal.cache.InternalCache;
+import org.apache.geode.internal.security.SecurityService;
+
+public class ManagementAgentIntegrationTest {
+
+  // private ManagementAgent managementAgent = spy(ManagementAgent.class);
+  @Rule
+  public TemporaryFolder temporaryFolder = new TemporaryFolder();
+
+  @Test
+  public void testSetMBeanServer() throws IOException {
+    DistributionConfig distributionConfig = mock(DistributionConfig.class);
+    InternalCache internalCache = mock(InternalCache.class);
+    JmxRmiSerialFilter serialFilter = mock(JmxRmiSerialFilter.class);
+    SecurityService securityService = mock(SecurityService.class);
+    when(internalCache.getSecurityService()).thenReturn(securityService);
+    when(securityService.isIntegratedSecurity()).thenReturn(false);
+    File tempFile = temporaryFolder.newFile("testFile");
+    
when(distributionConfig.getJmxManagerAccessFile()).thenReturn(tempFile.getCanonicalPath());
+    ManagementAgent managementAgent =
+        new ManagementAgent(distributionConfig, internalCache, serialFilter);
+    MBeanServer mBeanServer = mock(MBeanServer.class);
+    JMXConnectorServer jmxConnectorServerWithMBeanServer = new 
JMXConnectorServer(mBeanServer) {
+      @Override
+      public void start() throws IOException {
+
+      }
+
+      @Override
+      public void stop() throws IOException {
+
+      }
+
+      @Override
+      public boolean isActive() {
+        return false;
+      }
+
+      @Override
+      public JMXServiceURL getAddress() {
+        return null;
+      }
+
+      @Override
+      public Map<String, ?> getAttributes() {
+        return null;
+      }
+    };
+    managementAgent.setJmxConnectorServer(jmxConnectorServerWithMBeanServer);
+    assertThatCode(() -> managementAgent
+        .setMBeanServerForwarder(ManagementFactory.getPlatformMBeanServer(), 
new HashMap<>()))
+            .doesNotThrowAnyException();
+    
managementAgent.setMBeanServerForwarder(ManagementFactory.getPlatformMBeanServer(),
+        new HashMap<>());
+
+    JMXConnectorServer jmxConnectorServerNullMBeanServer = new 
JMXConnectorServer() {
+      @Override
+      public void start() throws IOException {
+
+      }
+
+      @Override
+      public void stop() throws IOException {
+
+      }
+
+      @Override
+      public boolean isActive() {
+        return false;
+      }
+
+      @Override
+      public JMXServiceURL getAddress() {
+        return null;
+      }
+
+      @Override
+      public Map<String, ?> getAttributes() {
+        return null;
+      }
+    };
+
+    managementAgent.setJmxConnectorServer(jmxConnectorServerNullMBeanServer);
+    assertThatCode(() -> managementAgent
+        .setMBeanServerForwarder(ManagementFactory.getPlatformMBeanServer(), 
new HashMap<>()))
+            .doesNotThrowAnyException();
+  }
+}
diff --git 
a/geode-core/src/main/java/org/apache/geode/management/internal/ManagementAgent.java
 
b/geode-core/src/main/java/org/apache/geode/management/internal/ManagementAgent.java
index 3ad1102..5648ceb 100755
--- 
a/geode-core/src/main/java/org/apache/geode/management/internal/ManagementAgent.java
+++ 
b/geode-core/src/main/java/org/apache/geode/management/internal/ManagementAgent.java
@@ -52,6 +52,7 @@ import org.apache.commons.lang3.StringUtils;
 import org.apache.logging.log4j.Logger;
 
 import org.apache.geode.GemFireConfigException;
+import org.apache.geode.annotations.VisibleForTesting;
 import org.apache.geode.cache.internal.HttpService;
 import org.apache.geode.distributed.internal.DistributionConfig;
 import org.apache.geode.internal.GemFireVersion;
@@ -429,6 +430,19 @@ public class ManagementAgent {
           }
         };
 
+    setMBeanServerForwarder(mbs, env);
+    registerAccessControlMBean();
+    registerFileUploaderMBean();
+
+    jmxConnectorServer.start();
+    if (logger.isDebugEnabled()) {
+      logger.debug("Finished starting jmx manager agent.");
+    }
+  }
+
+  @VisibleForTesting
+  void setMBeanServerForwarder(MBeanServer mbs, HashMap<String, Object> env)
+      throws IOException {
     if (securityService.isIntegratedSecurity()) {
       shiroAuthenticator = new JMXShiroAuthenticator(this.securityService);
       env.put(JMXConnectorServer.AUTHENTICATOR, shiroAuthenticator);
@@ -449,20 +463,12 @@ public class ManagementAgent {
       if (accessFile != null && accessFile.length() > 0) {
         // Rewire the mbs hierarchy to set accessController
         ReadOpFileAccessController controller = new 
ReadOpFileAccessController(accessFile);
-        controller.setMBeanServer(mbs);
         jmxConnectorServer.setMBeanServerForwarder(controller);
       } else {
         // if no access control, do not allow mbean creation to prevent Mlet 
attack
         jmxConnectorServer.setMBeanServerForwarder(new 
BlockMBeanCreationController());
       }
     }
-    registerAccessControlMBean();
-    registerFileUploaderMBean();
-
-    jmxConnectorServer.start();
-    if (logger.isDebugEnabled()) {
-      logger.debug("Finished starting jmx manager agent.");
-    }
   }
 
   private void registerAccessControlMBean() {
@@ -508,6 +514,11 @@ public class ManagementAgent {
     return jmxConnectorServer;
   }
 
+  @VisibleForTesting
+  void setJmxConnectorServer(JMXConnectorServer jmxConnectorServer) {
+    this.jmxConnectorServer = jmxConnectorServer;
+  }
+
   public synchronized RemoteStreamExporter getRemoteStreamExporter() {
     if (remoteStreamExporter == null) {
       remoteStreamExporter =

Reply via email to