Request extends BaseCommand { @Override public void cmdExecute(final Message clientMessage, final ServerConnection serverConnection, final SecurityService securityService, long start) throws IOException { Part regionNamePart = null, keyPart = null, valuePart = null; String regionName = null; Object callbackArg = null, key = null; CachedRegionHelper crHelper = serverConnection.getCachedRegionHelper(); CacheServerStats stats = serverConnection.getCacheServerStats(); String errMessage = null; serverConnection.setAsTrue(REQUIRES_RESPONSE); { long oldStart = start; start = DistributionStats.getStatTime(); stats.incReadGetRequestTime(start - oldStart); } int parts = clientMessage.getNumberOfParts(); regionNamePart = clientMessage.getPart(0); keyPart = clientMessage.getPart(1); if (parts > 2) { valuePart = clientMessage.getPart(2); try { callbackArg = valuePart.getObject(); } catch (Exception e) { writeException(clientMessage, e, false, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } } regionName = regionNamePart.getCachedString(); try { key = keyPart.getStringOrObject(); } catch (Exception e) { writeException(clientMessage, e, false, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } if (logger.isDebugEnabled()) { logger.debug("{}: Received get request ({} bytes) from {} for region {} key {} txId {}", serverConnection.getName(), clientMessage.getPayloadLength(), serverConnection.getSocketString(), regionName, key, clientMessage.getTransactionId()); } if (key == null || regionName == null) { if ((key == null) && (regionName == null)) { errMessage = "The input region name and key for the get request are null."; } else if (key == null) { errMessage = "The input key for the get request is null."; } else if (regionName == null) { errMessage = "The input region name for the get request is null."; } logger.warn("{}: {}", serverConnection.getName(), errMessage); writeErrorResponse(clientMessage, MessageType.REQUESTDATAERROR, errMessage, serverConnection); serverConnection.setAsTrue(RESPONDED); } else { Region region = serverConnection.getCache().getRegion(regionName); if (region == null) { String reason = String.format("%s was not found during get request", regionName); writeRegionDestroyedEx(clientMessage, regionName, reason, serverConnection); serverConnection.setAsTrue(RESPONDED); } else { GetOperationContext getContext = null; try { securityService.authorize(Resource.DATA, Operation.READ, regionName, key); AuthorizeRequest authzRequest = serverConnection.getAuthzRequest(); if (authzRequest != null) { getContext = authzRequest.getAuthorize(regionName, key, callbackArg); callbackArg = getContext.getCallbackArg(); } } catch (NotAuthorizedException ex) { writeException(clientMessage, ex, false, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } Object[] valueAndIsObject = new Object[3]; try { getValueAndIsObject(region, key, callbackArg, serverConnection, valueAndIsObject); } catch (Exception e) { writeException(clientMessage, e, false, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } Object data = valueAndIsObject[0]; boolean isObject = ((Boolean) valueAndIsObject[1]).booleanValue(); try { AuthorizeRequestPP postAuthzRequest = serverConnection.getPostAuthzRequest(); if (postAuthzRequest != null) { getContext = postAuthzRequest.getAuthorize(regionName, key, data, isObject, getContext); byte[] serializedValue = getContext.getSerializedValue(); if (serializedValue == null) { data = getContext.getObject(); } else { data = serializedValue; } isObject = getContext.isObject(); } } catch (NotAuthorizedException ex) { writeException(clientMessage, ex, false, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } { long oldStart = start; start = DistributionStats.getStatTime(); stats.incProcessGetTime(start - oldStart); } if (region instanceof PartitionedRegion) { PartitionedRegion pr = (PartitionedRegion) region; if (pr.getNetworkHopType() != PartitionedRegion.NETWORK_HOP_NONE) { writeResponseWithRefreshMetadata(data, callbackArg, clientMessage, isObject, serverConnection, pr, pr.getNetworkHopType()); pr.clearNetworkHopData(); } else { writeResponse(data, callbackArg, clientMessage, isObject, serverConnection); } } else { writeResponse(data, callbackArg, clientMessage, isObject, serverConnection); } serverConnection.setAsTrue(RESPONDED); if (logger.isDebugEnabled()) { logger.debug("{}: Wrote get response back to {} for region {} key {} value: {}", serverConnection.getName(), serverConnection.getSocketString(), regionName, key, data); } stats.incWriteGetResponseTime(DistributionStats.getStatTime() - start); } } } Request(); static Command getCommand(); @Override void cmdExecute(final Message clientMessage, final ServerConnection serverConnection,
      final SecurityService securityService, long start); void getValueAndIsObject(Region region, Object key, Object callbackArg,
      ServerConnection servConn, Object[] result);  }
@Test public void noSecurityShouldSucceed() throws Exception { when(this.securityService.isClientSecurityRequired()).thenReturn(false); this.request.cmdExecute(this.message, this.serverConnection, this.securityService, 0); verify(this.responseMessage).send(this.serverConnection); }
@Test public void integratedSecurityShouldSucceedIfAuthorized() throws Exception { when(this.securityService.isClientSecurityRequired()).thenReturn(true); when(this.securityService.isIntegratedSecurity()).thenReturn(true); this.request.cmdExecute(this.message, this.serverConnection, this.securityService, 0); verify(this.securityService).authorize(Resource.DATA, Operation.READ, REGION_NAME, KEY); verify(this.responseMessage).send(this.serverConnection); }
@Test public void integratedSecurityShouldFailIfNotAuthorized() throws Exception { when(this.securityService.isClientSecurityRequired()).thenReturn(true); when(this.securityService.isIntegratedSecurity()).thenReturn(true); doThrow(new NotAuthorizedException("")).when(this.securityService).authorize(Resource.DATA, Operation.READ, REGION_NAME, KEY); this.request.cmdExecute(this.message, this.serverConnection, this.securityService, 0); verify(this.securityService).authorize(Resource.DATA, Operation.READ, REGION_NAME, KEY); verify(this.errorResponseMessage).send(eq(this.serverConnection)); }
@Test public void oldSecurityShouldSucceedIfAuthorized() throws Exception { when(this.securityService.isClientSecurityRequired()).thenReturn(true); when(this.securityService.isIntegratedSecurity()).thenReturn(false); this.request.cmdExecute(this.message, this.serverConnection, this.securityService, 0); verify(this.authzRequest).getAuthorize(eq(REGION_NAME), eq(KEY), eq(CALLBACK_ARG)); verify(this.responseMessage).send(this.serverConnection); }
@Test public void oldSecurityShouldFailIfNotAuthorized() throws Exception { when(this.securityService.isClientSecurityRequired()).thenReturn(true); when(this.securityService.isIntegratedSecurity()).thenReturn(false); doThrow(new NotAuthorizedException("")).when(this.authzRequest).getAuthorize(eq(REGION_NAME), eq(KEY), eq(CALLBACK_ARG)); this.request.cmdExecute(this.message, this.serverConnection, this.securityService, 0); verify(this.authzRequest).getAuthorize(eq(REGION_NAME), eq(KEY), eq(CALLBACK_ARG)); verify(this.errorResponseMessage).send(eq(this.serverConnection)); }