XACMLPdpServlet extends HttpServlet implements Runnable { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { OnapLoggingContext loggingContext = OnapLoggingUtils.getLoggingContextForRequest(request, baseLoggingContext); loggingContext.transactionStarted(); loggingContext.setServiceName("PDP.decide"); if ((loggingContext.getRequestId() == null) || ("".equals(loggingContext.getRequestId()))) { UUID requestID = UUID.randomUUID(); loggingContext.setRequestId(requestID.toString()); PolicyLogger.info("requestID not provided in call to XACMLPdpSrvlet (doPost) so we generated one"); } else { PolicyLogger.info("requestID was provided in call to XACMLPdpSrvlet (doPost)"); } loggingContext.metricStarted(); loggingContext.metricEnded(); PolicyLogger.metrics("Metric example posted here - 1 of 2"); loggingContext.metricStarted(); loggingContext.metricEnded(); PolicyLogger.metrics("Metric example posted here - 2 of 2"); monitor.pdpEvaluationAttempts(); try { im.startTransaction(); } catch (IntegrityMonitorException e) { String message = e.toString(); PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, message + e); loggingContext.transactionEnded(); PolicyLogger.audit("Transaction Failed - See Error.log"); try { response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, message); } catch (Exception e1) { logger.error("Exception occured while sending error in response" + e1); } return; } if (status.getLoadedRootPolicies().isEmpty()) { logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Request from PEP at " + request.getRequestURI() + " for service when PDP has No Root Policies loaded"); PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, "Request from PEP at " + request.getRequestURI() + " for service when PDP has No Root Policies loaded"); loggingContext.transactionEnded(); PolicyLogger.audit("Transaction Failed - See Error.log"); try { response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE); } catch (Exception e1) { logger.error("Exception occured while sending error in response" + e1); } im.endTransaction(); return; } XacmlRest.dumpRequest(request); response.setHeader("Cache-Control", "no-cache"); if (request.getContentType() == null) { logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Must specify a Content-Type"); PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "Must specify a Content-Type"); loggingContext.transactionEnded(); PolicyLogger.audit("Transaction Failed - See Error.log"); try { response.sendError(HttpServletResponse.SC_BAD_REQUEST, "no content-type given"); } catch (Exception e1) { logger.error("Exception occured while sending error in response" + e1); } im.endTransaction(); return; } try { if (request.getContentLength() > Integer .parseInt(XACMLProperties.getProperty("MAX_CONTENT_LENGTH", "32767"))) { String message = "Content-Length larger than server will accept."; logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE + message); PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, message); loggingContext.transactionEnded(); PolicyLogger.audit("Transaction Failed - See Error.log"); response.sendError(HttpServletResponse.SC_BAD_REQUEST, message); im.endTransaction(); return; } } catch (Exception e) { logger.error("Exception occured while getting max content length" + e); } if (request.getContentLength() <= 0) { String message = "Content-Length is negative"; logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE + message); PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, message); loggingContext.transactionEnded(); PolicyLogger.audit("Transaction Failed - See Error.log"); try { response.sendError(HttpServletResponse.SC_BAD_REQUEST, message); } catch (Exception e1) { logger.error("Exception occured while sending error in response" + e1); } im.endTransaction(); return; } ContentType contentType = null; try { contentType = ContentType.parse(request.getContentType()); } catch (Exception e) { String message = "Parsing Content-Type: " + request.getContentType() + ", error=" + e.getMessage(); logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE + message, e); loggingContext.transactionEnded(); PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, message); PolicyLogger.audit("Transaction Failed - See Error.log"); try { response.sendError(HttpServletResponse.SC_BAD_REQUEST, message); } catch (Exception e1) { logger.error("Exception occured while sending error in response" + e1); } im.endTransaction(); return; } String incomingRequestString = null; Request pdpRequest = null; if (contentType.getMimeType().equalsIgnoreCase(ContentType.APPLICATION_JSON.getMimeType()) || contentType.getMimeType().equalsIgnoreCase(ContentType.APPLICATION_XML.getMimeType()) || contentType.getMimeType().equalsIgnoreCase("application/xacml+xml")) { StringBuilder buffer = new StringBuilder(); BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(request.getInputStream())); } catch (IOException e) { logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error during reading input stream", e); return; } String line; try { while ((line = reader.readLine()) != null) { buffer.append(line); } } catch (Exception e) { logger.error("Exception Occured while reading line" + e); } incomingRequestString = buffer.toString(); logger.info(incomingRequestString); try { if (contentType.getMimeType().equalsIgnoreCase(ContentType.APPLICATION_JSON.getMimeType())) { pdpRequest = JSONRequest.load(incomingRequestString); } else if (contentType.getMimeType().equalsIgnoreCase(ContentType.APPLICATION_XML.getMimeType()) || "application/xacml+xml".equalsIgnoreCase(contentType.getMimeType())) { pdpRequest = DOMRequest.load(incomingRequestString); } } catch (Exception e) { logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Could not parse request", e); PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "Could not parse request"); loggingContext.transactionEnded(); PolicyLogger.audit("Transaction Failed - See Error.log"); try { response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage()); } catch (Exception e1) { logger.error("Exception occured while sending error in response" + e1); } im.endTransaction(); return; } } else { String message = "unsupported content type" + request.getContentType(); logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE + message); PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, message); loggingContext.transactionEnded(); PolicyLogger.audit("Transaction Failed - See Error.log"); try { response.sendError(HttpServletResponse.SC_BAD_REQUEST, message); } catch (Exception e1) { logger.error("Exception occured while sending error in response" + e1); } im.endTransaction(); return; } if (pdpRequest == null || pdpRequest.getRequestAttributes() == null || pdpRequest.getRequestAttributes().isEmpty()) { String message = "Zero Attributes found in the request"; logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE + message); PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, message); loggingContext.transactionEnded(); PolicyLogger.audit("Transaction Failed - See Error.log"); try { response.sendError(HttpServletResponse.SC_BAD_REQUEST, message); } catch (Exception e1) { logger.error("Exception occured while sending error in response" + e1); } im.endTransaction(); return; } try { if (!authorizeRequest(request)) { String message = "PEP not Authorized for making this Request!! \n Contact Administrator for this Scope. "; logger.error(XACMLErrorConstants.ERROR_PERMISSIONS + message); PolicyLogger.error(MessageCodes.ERROR_PERMISSIONS, message); loggingContext.transactionEnded(); PolicyLogger.audit("Transaction Failed - See Error.log"); response.sendError(HttpServletResponse.SC_FORBIDDEN, message); im.endTransaction(); return; } PDPEngine myEngine = null; synchronized (pdpEngineLock) { myEngine = XACMLPdpServlet.pdpEngine; } if (myEngine == null) { String message = "No engine loaded."; logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + message); PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, message); loggingContext.transactionEnded(); PolicyLogger.audit("Transaction Failed - See Error.log"); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, message); im.endTransaction(); return; } long lTimeStart; long lTimeEnd; Response pdpResponse = null; synchronized (pdpEngineLock) { myEngine = XACMLPdpServlet.pdpEngine; try { PolicyList.clearPolicyList(); lTimeStart = System.currentTimeMillis(); pdpResponse = myEngine.decide(pdpRequest); lTimeEnd = System.currentTimeMillis(); } catch (PDPException e) { String message = "Exception during decide: " + e.getMessage(); logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + message + e); PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, message); loggingContext.transactionEnded(); PolicyLogger.audit("Transaction Failed - See Error.log"); try { response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, message); } catch (Exception e1) { logger.error("Exception occured while sending error in response" + e1); } im.endTransaction(); return; } } monitor.computeLatency(lTimeEnd - lTimeStart); requestLogger.info(lTimeStart + "=" + incomingRequestString); for (String policy : PolicyList.getpolicyList()) { monitor.policyCountAdd(policy, 1); } logger.info("PolicyID triggered in Request: " + PolicyList.getpolicyList()); if (logger.isDebugEnabled()) { logger.debug("Request time: " + (lTimeEnd - lTimeStart) + "ms"); } if (pdpResponse == null) { requestLogger.info(lTimeStart + "=" + "{}"); try { throw new PDPException("Failed to get response from PDP engine."); } catch (Exception e1) { logger.error("Exception occured while throwing Exception" + e1); } } response.setContentType(contentType.getMimeType()); String outgoingResponseString = ""; try { if (contentType.getMimeType().equalsIgnoreCase(ContentType.APPLICATION_JSON.getMimeType())) { outgoingResponseString = JSONResponse.toString(pdpResponse, logger.isDebugEnabled()); if (logger.isDebugEnabled()) { logger.debug(outgoingResponseString); outgoingResponseString = JSONResponse.toString(pdpResponse, false); } } else if (contentType.getMimeType().equalsIgnoreCase(ContentType.APPLICATION_XML.getMimeType()) || contentType.getMimeType().equalsIgnoreCase("application/xacml+xml")) { outgoingResponseString = DOMResponse.toString(pdpResponse, logger.isDebugEnabled()); if (logger.isDebugEnabled()) { logger.debug(outgoingResponseString); outgoingResponseString = DOMResponse.toString(pdpResponse, false); } } if (outgoingResponseString.contains("NotApplicable") || outgoingResponseString.contains("Decision not a Permit")) { monitor.pdpEvaluationNA(); } if (outgoingResponseString.contains("Permit") && !outgoingResponseString.contains("Decision not a Permit")) { monitor.pdpEvaluationPermit(); } if (outgoingResponseString.contains("Deny")) { monitor.pdpEvaluationDeny(); } requestLogger.info(lTimeStart + "=" + outgoingResponseString); response.getWriter().print(outgoingResponseString); } catch (Exception e) { logger.error("Exception Occured" + e); } } catch (Exception e) { String message = "Exception executing request: " + e; logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + message, e); PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, e, message); loggingContext.transactionEnded(); PolicyLogger.audit("Transaction Failed - See Error.log"); try { response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, message); } catch (Exception e1) { logger.error("Exception occured while sending error in response" + e1); } return; } monitor.pdpEvaluationSuccess(); response.setStatus(HttpServletResponse.SC_OK); loggingContext.transactionEnded(); auditLogger.info("Success"); PolicyLogger.audit("Success"); } XACMLPdpServlet(); static int getNotificationDelay(); IntegrityMonitor getIm(); void setIm(IntegrityMonitor im); @Override void init(ServletConfig config); @Override void destroy(); @Override void run(); static PDPEngine getPDPEngine(); static Constructor<?> getCreateUpdatePolicyConstructor(); static Object getPDPEngineLock(); static final PdpRestMonitor monitor; }
@Test public void testDoPost() { LOGGER.info("XACMLPdpServletTest - testDoPost"); try { pdpServlet.init(servletConfig); pdpServlet.doPost(httpServletRequest, httpServletResponse); assertTrue(true); } catch (Exception e) { LOGGER.error(ERROR_TEXT + e); fail(); } }