You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@geode.apache.org by GitBox <gi...@apache.org> on 2022/01/07 22:34:45 UTC

[GitHub] [geode] Bill commented on a change in pull request #7217: GEODE-9758: Add internal serial filter API

Bill commented on a change in pull request #7217:
URL: https://github.com/apache/geode/pull/7217#discussion_r780497191



##########
File path: geode-core/src/distributedTest/java/org/apache/geode/cache/ValidateSerializableObjectsDistributedTest.java
##########
@@ -0,0 +1,180 @@
+/*
+ * 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.cache;
+
+import static java.util.Arrays.asList;
+import static org.apache.geode.cache.RegionShortcut.REPLICATE;
+import static org.apache.geode.distributed.ConfigurationProperties.ENABLE_CLUSTER_CONFIGURATION;
+import static org.apache.geode.distributed.ConfigurationProperties.LOCATORS;
+import static org.apache.geode.distributed.ConfigurationProperties.VALIDATE_SERIALIZABLE_OBJECTS;
+import static org.apache.geode.test.dunit.IgnoredException.addIgnoredException;
+import static org.apache.geode.test.dunit.VM.getVM;
+import static org.apache.geode.test.dunit.rules.DistributedRule.getLocatorPort;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.catchThrowable;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InvalidClassException;
+import java.io.NotSerializableException;
+import java.io.Serializable;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+
+import org.apache.geode.InternalGemFireException;
+import org.apache.geode.SerializationException;
+import org.apache.geode.cache.util.CacheListenerAdapter;
+import org.apache.geode.distributed.ServerLauncher;
+import org.apache.geode.test.dunit.VM;
+import org.apache.geode.test.dunit.rules.DistributedReference;
+import org.apache.geode.test.dunit.rules.DistributedRule;
+import org.apache.geode.test.junit.rules.serializable.SerializableTemporaryFolder;
+
+@SuppressWarnings("serial")
+public class ValidateSerializableObjectsDistributedTest implements Serializable {
+
+  private VM server1;
+  private VM server2;
+
+  private File server1Dir;
+  private File server2Dir;
+  private int locatorPort;
+
+  @Rule
+  public DistributedRule distributedRule = new DistributedRule();
+  @Rule
+  public DistributedReference<ServerLauncher> server = new DistributedReference<>();
+  @Rule
+  public SerializableTemporaryFolder temporaryFolder = new SerializableTemporaryFolder();
+
+  @Before
+  public void setUp() throws IOException {
+    server1 = getVM(0);
+    server2 = getVM(1);
+
+    server1Dir = temporaryFolder.newFolder("server1");
+    server2Dir = temporaryFolder.newFolder("server2");
+
+    locatorPort = getLocatorPort();
+
+    server1.invoke(() -> {
+      server.set(startServer("server1", server1Dir));
+    });
+    server2.invoke(() -> {
+      server.set(startServer("server2", server2Dir));
+    });
+
+    asList(server1, server2).forEach(vm -> vm.invoke(() -> {
+      server.get().getCache()
+          .createRegionFactory(REPLICATE)
+          .addCacheListener(new CacheListenerAdapter<Object, Object>() {
+            @Override
+            public void afterCreate(EntryEvent<Object, Object> event) {
+              // cache listener afterCreate causes all creates to deserialize the value which causes
+              // the tests to pass if serialization filter is configured
+              assertThat(event.getNewValue()).isNotNull();
+            }
+          })
+          .create("region");
+    }));
+
+  }
+
+  @Test
+  public void stringIsAllowed() {
+    server1.invoke(() -> {
+      Region<Object, Object> region = server.get().getCache().getRegion("region");
+      region.put("key", "value");
+    });
+  }
+
+  @Test
+  public void primitiveIsAllowed() {
+    server1.invoke(() -> {
+      Region<Object, Object> region = server.get().getCache().getRegion("region");
+      region.put(1, 1);
+    });
+  }
+
+  @Test
+  public void nonSerializableThrowsNotSerializableException() {
+    server1.invoke(() -> {
+      Region<Object, Object> region = server.get().getCache().getRegion("region");
+      Throwable thrown = catchThrowable(() -> {
+        region.put(new Object(), new Object());
+      });
+      assertThat(thrown).hasCauseExactlyInstanceOf(NotSerializableException.class);
+    });
+  }
+
+  @Test
+  public void nonAllowedIsNotPropagatedToOtherServer() {
+    addIgnoredException(InvalidClassException.class);
+    addIgnoredException(SerializationException.class);
+
+    server1.invoke(() -> {
+      Region<Object, Object> region = server.get().getCache().getRegion("region");
+      region.put("key", new SerializableClass());
+    });
+
+    server2.invoke(() -> {
+      Region<Object, Object> region = server.get().getCache().getRegion("region");
+      Throwable thrown = catchThrowable(() -> {
+        region.get("key");
+      });
+      assertThat(thrown)
+          .isInstanceOf(SerializationException.class)
+          .hasCauseInstanceOf(InvalidClassException.class);
+    });
+  }
+
+  @Test
+  public void nonAllowedDoesNotThrow() {
+    addIgnoredException(InvalidClassException.class);
+    addIgnoredException(IOException.class);
+
+    server1.invoke(() -> {
+      Region<Object, Object> region = server.get().getCache().getRegion("region");
+      Throwable thrown = catchThrowable(() -> {
+        region.put(new SerializableClass(), new SerializableClass());
+      });
+      assertThat(thrown).isInstanceOf(InternalGemFireException.class);

Review comment:
       The name of this test seems at odds with the fact that it requires the `put()` to throw. Please explain or rename.

##########
File path: geode-core/src/distributedTest/java/org/apache/geode/internal/serialization/filter/LocatorLauncherGlobalSerialFilterDistributedTest.java
##########
@@ -0,0 +1,159 @@
+/*
+ * 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.internal.serialization.filter;
+
+import static org.apache.commons.lang3.JavaVersion.JAVA_1_8;
+import static org.apache.commons.lang3.JavaVersion.JAVA_9;
+import static org.apache.commons.lang3.SerializationUtils.serialize;
+import static org.apache.commons.lang3.SystemUtils.isJavaVersionAtLeast;
+import static org.apache.commons.lang3.SystemUtils.isJavaVersionAtMost;
+import static org.apache.geode.distributed.ConfigurationProperties.ENABLE_CLUSTER_CONFIGURATION;
+import static org.apache.geode.distributed.ConfigurationProperties.HTTP_SERVICE_PORT;
+import static org.apache.geode.distributed.ConfigurationProperties.JMX_MANAGER;
+import static org.apache.geode.distributed.ConfigurationProperties.LOCATORS;
+import static org.apache.geode.distributed.ConfigurationProperties.SERIALIZABLE_OBJECT_FILTER;
+import static org.apache.geode.distributed.ConfigurationProperties.VALIDATE_SERIALIZABLE_OBJECTS;
+import static org.apache.geode.test.dunit.IgnoredException.addIgnoredException;
+import static org.apache.geode.test.dunit.VM.getVM;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.catchThrowable;
+import static org.assertj.core.api.Assumptions.assumeThat;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InvalidClassException;
+import java.io.ObjectInput;
+import java.io.ObjectInputStream;
+import java.io.Serializable;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+
+import org.apache.geode.distributed.LocatorLauncher;
+import org.apache.geode.internal.AvailablePortHelper;
+import org.apache.geode.test.dunit.VM;
+import org.apache.geode.test.dunit.rules.DistributedReference;
+import org.apache.geode.test.dunit.rules.DistributedRule;
+import org.apache.geode.test.junit.rules.serializable.SerializableTemporaryFolder;
+
+@SuppressWarnings("serial")
+public class LocatorLauncherGlobalSerialFilterDistributedTest implements Serializable {
+
+  private VM locatorVM;
+  private File locatorDir;
+  private int locatorPort;
+
+  @Rule
+  public DistributedRule distributedRule = new DistributedRule();
+  @Rule
+  public DistributedReference<LocatorLauncher> locator = new DistributedReference<>();
+  @Rule
+  public SerializableTemporaryFolder temporaryFolder = new SerializableTemporaryFolder();
+
+  @Before
+  public void setUp() throws IOException {
+    locatorVM = getVM(0).bounce();
+
+    locatorDir = temporaryFolder.newFolder("locator");
+    locatorPort = AvailablePortHelper.getRandomAvailableTCPPort();
+
+    locatorVM.invoke(() -> {
+      locator.set(startLocator("locator", locatorDir, locatorPort));
+    });
+  }
+
+  @Test
+  public void stringIsAllowed() {
+    locatorVM.invoke(() -> {
+      Serializable object = "hello";
+      try (ObjectInput inputStream = new ObjectInputStream(byteArrayInputStream(object))) {
+        assertThat(inputStream.readObject()).isEqualTo(object);
+      }
+    });
+  }
+
+  @Test
+  public void primitiveIsAllowed() {
+    locatorVM.invoke(() -> {
+      Integer integerObject = 1;
+      try (ObjectInput inputStream = new ObjectInputStream(byteArrayInputStream(integerObject))) {
+        assertThat(inputStream.readObject()).isEqualTo(integerObject);
+      }
+    });
+  }
+
+  @Test
+  public void nonAllowed_doesNotThrow_onJava8() {
+    assumeThat(isJavaVersionAtMost(JAVA_1_8)).isTrue();
+
+    addIgnoredException(InvalidClassException.class);
+
+    locatorVM.invoke(() -> {
+      Throwable thrown = catchThrowable(() -> {
+        Serializable object = new SerializableClass("hello");
+        try (ObjectInput inputStream = new ObjectInputStream(byteArrayInputStream(object))) {
+          assertThat(inputStream.readObject()).isEqualTo(object);
+        }
+      });
+      assertThat(thrown).isNull();

Review comment:
       I believe this `invoke()` block is explicitly asserting that no exception was thrown, rather than letting the exception escape the block, because the latter might make it harder to understand test failures.
   
   If that's true then it seems equally important for `stringIsAllowed()` and `primitiveIsAllowed()` to do the same thing. Please make the test methods consistent in this regard.

##########
File path: geode-core/src/main/java/org/apache/geode/distributed/LocatorLauncher.java
##########
@@ -729,6 +736,15 @@ public LocatorState start() {
               bindAddress, true, getDistributedSystemProperties(),
               getHostnameForClients(),
               Paths.get(workingDirectory));
+
+          if (serializationFilterConfigured) {
+            locator.getCache().getInternalDistributedSystem().getConfig()
+                .setValidateSerializableObjects(true);

Review comment:
       Looks like there's a little window here where the serialization filter setting is not respected. The locator has already been started before `setValidateSerializableObjects(true)` is called on the `DistributionConfig`.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@geode.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org