@Test public void postProcessQuotaManagerTest() throws Exception { QuotaManager quotaManager = Mockito.mock(QuotaManager.class); AmbrySecurityService ambrySecurityService = new AmbrySecurityService(new FrontendConfig(new VerifiableProperties(new Properties())), new FrontendMetrics(new MetricRegistry()), URL_SIGNING_SERVICE_FACTORY.getUrlSigningService(), quotaManager); Mockito.when(quotaManager.shouldThrottle(any())).thenReturn(false); for (int i = 0; i < 100; i++) { for (RestMethod restMethod : RestMethod.values()) { RestRequest restRequest = createRestRequest(restMethod, "/", null); ambrySecurityService.postProcessRequest(restRequest).get(); } } Mockito.when(quotaManager.shouldThrottle(any())).thenReturn(true); for (RestMethod restMethod : RestMethod.values()) { RestRequest restRequest = createRestRequest(restMethod, "/", null); try { ambrySecurityService.postProcessRequest(restRequest).get(); Assert.fail("Should have failed."); } catch (Exception e) { Assert.assertEquals("Exception should be TooManyRequests", RestServiceErrorCode.TooManyRequests, ((RestServiceException) e.getCause()).getErrorCode()); } } }
@Override public void postProcessRequest(RestRequest restRequest, Callback<Void> callback) { Exception exception = null; frontendMetrics.securityServicePostProcessRequestRate.mark(); long startTimeMs = System.currentTimeMillis(); if (!isOpen) { exception = new RestServiceException("SecurityService is closed", RestServiceErrorCode.ServiceUnavailable); } else if (restRequest == null || callback == null) { throw new IllegalArgumentException("RestRequest or Callback is null"); } else if (quotaManager.shouldThrottle(restRequest)) { exception = new RestServiceException("Too many requests", RestServiceErrorCode.TooManyRequests); } else if (restRequest.getRestMethod() == RestMethod.DELETE || restRequest.getRestMethod() == RestMethod.PUT) { try { accountAndContainerNamePreconditionCheck(restRequest); } catch (Exception e) { exception = e; } } else if (restRequest.getRestMethod() == RestMethod.GET) { RequestPath requestPath = getRequestPath(restRequest); String operationOrBlobId = requestPath.getOperationOrBlobId(true); if (!operationOrBlobId.isEmpty() && !OPERATIONS.contains(operationOrBlobId)) { try { validateSecurePathIfRequired(restRequest, requestPath.getPrefix(), frontendConfig.securePathPrefix); } catch (Exception e) { exception = e; } } } frontendMetrics.securityServicePostProcessRequestTimeInMs.update(System.currentTimeMillis() - startTimeMs); callback.onCompletion(null, exception); }
AmbrySecurityService implements SecurityService { @Override public void postProcessRequest(RestRequest restRequest, Callback<Void> callback) { Exception exception = null; frontendMetrics.securityServicePostProcessRequestRate.mark(); long startTimeMs = System.currentTimeMillis(); if (!isOpen) { exception = new RestServiceException("SecurityService is closed", RestServiceErrorCode.ServiceUnavailable); } else if (restRequest == null || callback == null) { throw new IllegalArgumentException("RestRequest or Callback is null"); } else if (quotaManager.shouldThrottle(restRequest)) { exception = new RestServiceException("Too many requests", RestServiceErrorCode.TooManyRequests); } else if (restRequest.getRestMethod() == RestMethod.DELETE || restRequest.getRestMethod() == RestMethod.PUT) { try { accountAndContainerNamePreconditionCheck(restRequest); } catch (Exception e) { exception = e; } } else if (restRequest.getRestMethod() == RestMethod.GET) { RequestPath requestPath = getRequestPath(restRequest); String operationOrBlobId = requestPath.getOperationOrBlobId(true); if (!operationOrBlobId.isEmpty() && !OPERATIONS.contains(operationOrBlobId)) { try { validateSecurePathIfRequired(restRequest, requestPath.getPrefix(), frontendConfig.securePathPrefix); } catch (Exception e) { exception = e; } } } frontendMetrics.securityServicePostProcessRequestTimeInMs.update(System.currentTimeMillis() - startTimeMs); callback.onCompletion(null, exception); } }
AmbrySecurityService implements SecurityService { @Override public void postProcessRequest(RestRequest restRequest, Callback<Void> callback) { Exception exception = null; frontendMetrics.securityServicePostProcessRequestRate.mark(); long startTimeMs = System.currentTimeMillis(); if (!isOpen) { exception = new RestServiceException("SecurityService is closed", RestServiceErrorCode.ServiceUnavailable); } else if (restRequest == null || callback == null) { throw new IllegalArgumentException("RestRequest or Callback is null"); } else if (quotaManager.shouldThrottle(restRequest)) { exception = new RestServiceException("Too many requests", RestServiceErrorCode.TooManyRequests); } else if (restRequest.getRestMethod() == RestMethod.DELETE || restRequest.getRestMethod() == RestMethod.PUT) { try { accountAndContainerNamePreconditionCheck(restRequest); } catch (Exception e) { exception = e; } } else if (restRequest.getRestMethod() == RestMethod.GET) { RequestPath requestPath = getRequestPath(restRequest); String operationOrBlobId = requestPath.getOperationOrBlobId(true); if (!operationOrBlobId.isEmpty() && !OPERATIONS.contains(operationOrBlobId)) { try { validateSecurePathIfRequired(restRequest, requestPath.getPrefix(), frontendConfig.securePathPrefix); } catch (Exception e) { exception = e; } } } frontendMetrics.securityServicePostProcessRequestTimeInMs.update(System.currentTimeMillis() - startTimeMs); callback.onCompletion(null, exception); } AmbrySecurityService(FrontendConfig frontendConfig, FrontendMetrics frontendMetrics,
      UrlSigningService urlSigningService, QuotaManager quotaManager); }
AmbrySecurityService implements SecurityService { @Override public void postProcessRequest(RestRequest restRequest, Callback<Void> callback) { Exception exception = null; frontendMetrics.securityServicePostProcessRequestRate.mark(); long startTimeMs = System.currentTimeMillis(); if (!isOpen) { exception = new RestServiceException("SecurityService is closed", RestServiceErrorCode.ServiceUnavailable); } else if (restRequest == null || callback == null) { throw new IllegalArgumentException("RestRequest or Callback is null"); } else if (quotaManager.shouldThrottle(restRequest)) { exception = new RestServiceException("Too many requests", RestServiceErrorCode.TooManyRequests); } else if (restRequest.getRestMethod() == RestMethod.DELETE || restRequest.getRestMethod() == RestMethod.PUT) { try { accountAndContainerNamePreconditionCheck(restRequest); } catch (Exception e) { exception = e; } } else if (restRequest.getRestMethod() == RestMethod.GET) { RequestPath requestPath = getRequestPath(restRequest); String operationOrBlobId = requestPath.getOperationOrBlobId(true); if (!operationOrBlobId.isEmpty() && !OPERATIONS.contains(operationOrBlobId)) { try { validateSecurePathIfRequired(restRequest, requestPath.getPrefix(), frontendConfig.securePathPrefix); } catch (Exception e) { exception = e; } } } frontendMetrics.securityServicePostProcessRequestTimeInMs.update(System.currentTimeMillis() - startTimeMs); callback.onCompletion(null, exception); } AmbrySecurityService(FrontendConfig frontendConfig, FrontendMetrics frontendMetrics,
      UrlSigningService urlSigningService, QuotaManager quotaManager); @Override void preProcessRequest(RestRequest restRequest, Callback<Void> callback); @Override void processRequest(RestRequest restRequest, Callback<Void> callback); @Override void postProcessRequest(RestRequest restRequest, Callback<Void> callback); @Override void processResponse(RestRequest restRequest, RestResponseChannel responseChannel, BlobInfo blobInfo,
      Callback<Void> callback); @Override void close(); }
AmbrySecurityService implements SecurityService { @Override public void postProcessRequest(RestRequest restRequest, Callback<Void> callback) { Exception exception = null; frontendMetrics.securityServicePostProcessRequestRate.mark(); long startTimeMs = System.currentTimeMillis(); if (!isOpen) { exception = new RestServiceException("SecurityService is closed", RestServiceErrorCode.ServiceUnavailable); } else if (restRequest == null || callback == null) { throw new IllegalArgumentException("RestRequest or Callback is null"); } else if (quotaManager.shouldThrottle(restRequest)) { exception = new RestServiceException("Too many requests", RestServiceErrorCode.TooManyRequests); } else if (restRequest.getRestMethod() == RestMethod.DELETE || restRequest.getRestMethod() == RestMethod.PUT) { try { accountAndContainerNamePreconditionCheck(restRequest); } catch (Exception e) { exception = e; } } else if (restRequest.getRestMethod() == RestMethod.GET) { RequestPath requestPath = getRequestPath(restRequest); String operationOrBlobId = requestPath.getOperationOrBlobId(true); if (!operationOrBlobId.isEmpty() && !OPERATIONS.contains(operationOrBlobId)) { try { validateSecurePathIfRequired(restRequest, requestPath.getPrefix(), frontendConfig.securePathPrefix); } catch (Exception e) { exception = e; } } } frontendMetrics.securityServicePostProcessRequestTimeInMs.update(System.currentTimeMillis() - startTimeMs); callback.onCompletion(null, exception); } AmbrySecurityService(FrontendConfig frontendConfig, FrontendMetrics frontendMetrics,
      UrlSigningService urlSigningService, QuotaManager quotaManager); @Override void preProcessRequest(RestRequest restRequest, Callback<Void> callback); @Override void processRequest(RestRequest restRequest, Callback<Void> callback); @Override void postProcessRequest(RestRequest restRequest, Callback<Void> callback); @Override void processResponse(RestRequest restRequest, RestResponseChannel responseChannel, BlobInfo blobInfo,
      Callback<Void> callback); @Override void close();  }