geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jinmeil...@apache.org
Subject [08/50] [abbrv] incubator-geode git commit: Clear Region
Date Mon, 27 Jun 2016 21:20:58 GMT
Clear Region


Project: http://git-wip-us.apache.org/repos/asf/incubator-geode/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-geode/commit/dd1b04b0
Tree: http://git-wip-us.apache.org/repos/asf/incubator-geode/tree/dd1b04b0
Diff: http://git-wip-us.apache.org/repos/asf/incubator-geode/diff/dd1b04b0

Branch: refs/heads/feature/GEODE-1571
Commit: dd1b04b0a07e2d2ad61504498acc69f9133b1f72
Parents: 8076567
Author: Kevin J. Duling <kduling@pivotal.io>
Authored: Tue Jun 21 16:30:09 2016 -0700
Committer: Kevin J. Duling <kduling@pivotal.io>
Committed: Tue Jun 21 16:30:09 2016 -0700

----------------------------------------------------------------------
 .../cache/tier/sockets/command/ClearRegion.java | 139 +++++++++----------
 .../security/IntegratedClientAuthDUnitTest.java |  69 ++++++---
 2 files changed, 120 insertions(+), 88 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/dd1b04b0/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/ClearRegion.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/ClearRegion.java
b/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/ClearRegion.java
index f9531e7..093a322 100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/ClearRegion.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/ClearRegion.java
@@ -15,40 +15,45 @@
  * limitations under the License.
  */
 /**
- * 
+ *
  */
 package com.gemstone.gemfire.internal.cache.tier.sockets.command;
 
+import java.io.IOException;
+import java.nio.ByteBuffer;
+
+import com.gemstone.gemfire.cache.operations.RegionClearOperationContext;
+import com.gemstone.gemfire.distributed.internal.DistributionStats;
 import com.gemstone.gemfire.internal.cache.EventID;
 import com.gemstone.gemfire.internal.cache.LocalRegion;
 import com.gemstone.gemfire.internal.cache.tier.CachedRegionHelper;
 import com.gemstone.gemfire.internal.cache.tier.Command;
 import com.gemstone.gemfire.internal.cache.tier.MessageType;
-import com.gemstone.gemfire.internal.cache.tier.sockets.*;
+import com.gemstone.gemfire.internal.cache.tier.sockets.BaseCommand;
+import com.gemstone.gemfire.internal.cache.tier.sockets.CacheServerStats;
+import com.gemstone.gemfire.internal.cache.tier.sockets.Message;
+import com.gemstone.gemfire.internal.cache.tier.sockets.Part;
+import com.gemstone.gemfire.internal.cache.tier.sockets.ServerConnection;
 import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
 import com.gemstone.gemfire.internal.logging.log4j.LocalizedMessage;
 import com.gemstone.gemfire.internal.security.AuthorizeRequest;
-import com.gemstone.gemfire.cache.operations.RegionClearOperationContext;
-import com.gemstone.gemfire.distributed.internal.DistributionStats;
-import java.io.IOException;
-import java.nio.ByteBuffer;
+import com.gemstone.gemfire.internal.security.GeodeSecurityUtil;
 
 
 public class ClearRegion extends BaseCommand {
 
   private final static ClearRegion singleton = new ClearRegion();
 
-  public static Command getCommand() {
-    return singleton;
+  private ClearRegion() {
   }
 
-  private ClearRegion() {
+  public static Command getCommand() {
+    return singleton;
   }
 
   @Override
-  public void cmdExecute(Message msg, ServerConnection servConn, long start)
-      throws IOException, InterruptedException {
-    Part regionNamePart = null,  callbackArgPart = null;
+  public void cmdExecute(Message msg, ServerConnection servConn, long start) throws IOException,
InterruptedException {
+    Part regionNamePart = null, callbackArgPart = null;
     String regionName = null;
     Object callbackArg = null;
     Part eventPart = null;
@@ -64,13 +69,12 @@ public class ClearRegion extends BaseCommand {
     // Retrieve the data from the message parts
     regionNamePart = msg.getPart(0);
     eventPart = msg.getPart(1);
-//    callbackArgPart = null; (redundant assignment)
+    //    callbackArgPart = null; (redundant assignment)
     if (msg.getNumberOfParts() > 2) {
       callbackArgPart = msg.getPart(2);
       try {
         callbackArg = callbackArgPart.getObject();
-      }
-      catch (Exception e) {
+      } catch (Exception e) {
         writeException(msg, e, false, servConn);
         servConn.setAsTrue(RESPONDED);
         return;
@@ -78,73 +82,66 @@ public class ClearRegion extends BaseCommand {
     }
     regionName = regionNamePart.getString();
     if (logger.isDebugEnabled()) {
-      logger.debug(servConn.getName() + ": Received clear region request (" + msg.getPayloadLength()
+ " bytes) from " + servConn.getSocketString() + " for region " + regionName);
+      logger.debug(servConn.getName() + ": Received clear region request (" + msg.getPayloadLength()
+ " bytes) from " + servConn
+        .getSocketString() + " for region " + regionName);
     }
 
     // Process the clear region request
     if (regionName == null) {
-      logger.warn(LocalizedMessage.create(LocalizedStrings.ClearRegion_0_THE_INPUT_REGION_NAME_FOR_THE_CLEAR_REGION_REQUEST_IS_NULL,
servConn.getName()));
+      logger.warn(LocalizedMessage.create(LocalizedStrings.ClearRegion_0_THE_INPUT_REGION_NAME_FOR_THE_CLEAR_REGION_REQUEST_IS_NULL,
servConn
+        .getName()));
       String errMessage = LocalizedStrings.ClearRegion_THE_INPUT_REGION_NAME_FOR_THE_CLEAR_REGION_REQUEST_IS_NULL.toLocalizedString();
 
-      writeErrorResponse(msg, MessageType.CLEAR_REGION_DATA_ERROR, errMessage,
-          servConn);
+      writeErrorResponse(msg, MessageType.CLEAR_REGION_DATA_ERROR, errMessage, servConn);
       servConn.setAsTrue(RESPONDED);
+      return;
     }
-    else {
-      LocalRegion region = (LocalRegion)crHelper.getRegion(regionName);
-      if (region == null) {
-        String reason = LocalizedStrings.ClearRegion_WAS_NOT_FOUND_DURING_CLEAR_REGION_REGUEST.toLocalizedString();
-        writeRegionDestroyedEx(msg, regionName, reason, servConn);
-        servConn.setAsTrue(RESPONDED);
-      }
-      else {
-        // Clear the region
-
-        ByteBuffer eventIdPartsBuffer = ByteBuffer.wrap(eventPart
-            .getSerializedForm());
-        long threadId = EventID
-            .readEventIdPartsFromOptmizedByteArray(eventIdPartsBuffer);
-        long sequenceId = EventID
-            .readEventIdPartsFromOptmizedByteArray(eventIdPartsBuffer);
-        EventID eventId = new EventID(servConn.getEventMemberIDByteArray(),
-            threadId, sequenceId);
-
-        try {
-          AuthorizeRequest authzRequest = servConn.getAuthzRequest();
-          if (authzRequest != null) {
-            RegionClearOperationContext clearContext = authzRequest
-                .clearAuthorize(regionName, callbackArg);
-            callbackArg = clearContext.getCallbackArg();
-          }
-          region.basicBridgeClear(callbackArg, servConn.getProxyID(),
-              true /* boolean from cache Client */, eventId);
-        }
-        catch (Exception e) {
-          // If an interrupted exception is thrown , rethrow it
-          checkForInterrupt(servConn, e);
-
-          // If an exception occurs during the clear, preserve the connection
-          writeException(msg, e, false, servConn);
-          servConn.setAsTrue(RESPONDED);
-          return;
-        }
-
-        // Update the statistics and write the reply
-        {
-          long oldStart = start;
-          start = DistributionStats.getStatTime();
-          stats.incProcessClearRegionTime(start - oldStart);
-        }
-        writeReply(msg, servConn);
-        servConn.setAsTrue(RESPONDED);
-        if (logger.isDebugEnabled()) {
-          logger.debug(servConn.getName() + ": Sent clear region response for region " +
regionName);
-        }
-        stats.incWriteClearRegionResponseTime(DistributionStats.getStatTime()
-            - start);
+
+    LocalRegion region = (LocalRegion) crHelper.getRegion(regionName);
+    if (region == null) {
+      String reason = LocalizedStrings.ClearRegion_WAS_NOT_FOUND_DURING_CLEAR_REGION_REGUEST.toLocalizedString();
+      writeRegionDestroyedEx(msg, regionName, reason, servConn);
+      servConn.setAsTrue(RESPONDED);
+      return;
+    }
+    // Clear the region
+    GeodeSecurityUtil.authorizeRegionWrite(regionName);
+
+    ByteBuffer eventIdPartsBuffer = ByteBuffer.wrap(eventPart.getSerializedForm());
+    long threadId = EventID.readEventIdPartsFromOptmizedByteArray(eventIdPartsBuffer);
+    long sequenceId = EventID.readEventIdPartsFromOptmizedByteArray(eventIdPartsBuffer);
+    EventID eventId = new EventID(servConn.getEventMemberIDByteArray(), threadId, sequenceId);
+
+    try {
+      AuthorizeRequest authzRequest = servConn.getAuthzRequest();
+      if (authzRequest != null) {
+        RegionClearOperationContext clearContext = authzRequest.clearAuthorize(regionName,
callbackArg);
+        callbackArg = clearContext.getCallbackArg();
       }
+      region.basicBridgeClear(callbackArg, servConn.getProxyID(), true /* boolean from cache
Client */, eventId);
+    } catch (Exception e) {
+      // If an interrupted exception is thrown , rethrow it
+      checkForInterrupt(servConn, e);
+
+      // If an exception occurs during the clear, preserve the connection
+      writeException(msg, e, false, servConn);
+      servConn.setAsTrue(RESPONDED);
+      return;
     }
 
+    // Update the statistics and write the reply
+    {
+      long oldStart = start;
+      start = DistributionStats.getStatTime();
+      stats.incProcessClearRegionTime(start - oldStart);
+    }
+    writeReply(msg, servConn);
+    servConn.setAsTrue(RESPONDED);
+    if (logger.isDebugEnabled()) {
+      logger.debug(servConn.getName() + ": Sent clear region response for region " + regionName);
+    }
+    stats.incWriteClearRegionResponseTime(DistributionStats.getStatTime() - start);
   }
 
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/dd1b04b0/geode-core/src/test/java/com/gemstone/gemfire/security/IntegratedClientAuthDUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/security/IntegratedClientAuthDUnitTest.java
b/geode-core/src/test/java/com/gemstone/gemfire/security/IntegratedClientAuthDUnitTest.java
index 4e1f84c..a408c81 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/security/IntegratedClientAuthDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/security/IntegratedClientAuthDUnitTest.java
@@ -176,22 +176,7 @@ public class IntegratedClientAuthDUnitTest extends JUnit4DistributedTestCase
{
   public void testDestroyInvalidate() throws InterruptedException {
 
     // First, load up 5 keys to work with
-    SerializableRunnable putKeys = new SerializableRunnable() {
-      @Override
-      public void run() {
-        Cache cache = getCache();
-        Region region = cache.getRegion(SecurityTestUtils.REGION_NAME);
-        assertNotNull(region);
-        region.clear();
-        for (int i = 0; i < 5; i++) {
-          String key = "key" + i;
-          String value = "value" + i;
-          region.put(key, value);
-        }
-        assertEquals(5, region.size());
-      }
-    };
-    server1.invoke(putKeys);
+    server1.invoke(generate5Keys());
 
     // Delete one key and invalidate another key with an authorized user.
     AsyncInvocation ai1 = client1.invokeAsync(() -> {
@@ -236,8 +221,58 @@ public class IntegratedClientAuthDUnitTest extends JUnit4DistributedTestCase
{
     ai2.checkException();
   }
 
-  public static void assertNotAuthorized(ThrowingCallable shouldRaiseThrowable, String permString){
+  public static void assertNotAuthorized(ThrowingCallable shouldRaiseThrowable, String permString)
{
     assertThatThrownBy(shouldRaiseThrowable).hasMessageContaining(permString);
   }
 
+  private static SerializableRunnable generate5Keys() {
+    SerializableRunnable putKeys = new SerializableRunnable() {
+      @Override
+      public void run() {
+        Cache cache = getCache();
+        Region region = cache.getRegion(SecurityTestUtils.REGION_NAME);
+        assertNotNull(region);
+        region.clear();
+        for (int i = 0; i < 5; i++) {
+          String key = "key" + i;
+          String value = "value" + i;
+          region.put(key, value);
+        }
+        assertEquals(5, region.size());
+      }
+    };
+    return putKeys;
+  }
+
+  @Test
+  public void testRegionClear() throws InterruptedException {
+
+    // First, load up 5 keys to work with
+    server1.invoke(generate5Keys());
+
+    // Verify that an unauthorized user can't clear the region
+    SerializableRunnable clearUnauthorized = new SerializableRunnable() {
+      @Override
+      public void run() {
+        Cache cache = SecurityTestUtils.createCacheClient("stranger", "1234567", serverPort,
SecurityTestUtils.NO_EXCEPTION);
+        final Region region = cache.getRegion(SecurityTestUtils.REGION_NAME);
+        assertNotAuthorized(() -> region.clear(), "DATA:WRITE:AuthRegion");
+        cache.close();
+      }
+    };
+    client1.invoke(clearUnauthorized);
+
+    // Verify that an authorized user can clear the region
+    SerializableRunnable clearAuthorized = new SerializableRunnable() {
+      @Override
+      public void run() {
+        Cache cache = SecurityTestUtils.createCacheClient("authRegionUser", "1234567", serverPort,
SecurityTestUtils.NO_EXCEPTION);
+        final Region region = cache.getRegion(SecurityTestUtils.REGION_NAME);
+        assertNotAuthorized(() -> region.clear(), "DATA:WRITE:AuthRegion");
+        cache.close();
+      }
+    };
+    client1.invoke(clearUnauthorized);
+  }
+
 }


Mime
View raw message