package eu.openaire.urls_worker.controllers; import eu.openaire.urls_worker.components.AssignmentsHandler; import eu.openaire.urls_worker.util.UriBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; @RestController @RequestMapping("") public class GeneralController { private static final Logger logger = LoggerFactory.getLogger(GeneralController.class); private final String controllerIp; private final String shutdownOrCancelCode; public GeneralController(@Value("${info.controllerIp}") String controllerIp, @Value("${info.shutdownOrCancelCode}") String shutdownOrCancelCode) { this.controllerIp = controllerIp; this.shutdownOrCancelCode = shutdownOrCancelCode; } @GetMapping("isAlive") public ResponseEntity isWorkerAlive() { logger.info("Received an \"isAlive\" request."); return ResponseEntity.ok().build(); } public static boolean shouldShutdownWorker = false; @GetMapping("shutdownWorker/{shutdownCode}") public ResponseEntity shutdownWorkerGracefully(@PathVariable String shutdownCode, HttpServletRequest request) { String initMsg = "Received a \"shutdownWorker\" request."; ResponseEntity responseEntity = passSecurityChecks(request, shutdownCode, initMsg); if ( responseEntity != null ) return responseEntity; String finalMsg = ""; if ( shouldShutdownWorker ) finalMsg = " The worker has already received a \"shutdownWorker\" (which was not canceled afterwards)."; else shouldShutdownWorker = true; finalMsg += " The worker will shutdown, after finishing current work."; logger.info(initMsg + finalMsg); return ResponseEntity.ok().body(finalMsg + "\n"); } @GetMapping("cancelShutdownWorker/{cancelCode}") public ResponseEntity cancelShutdownWorkerGracefully(@PathVariable String cancelCode, HttpServletRequest request) { String initMsg = "Received a \"cancelShutdownWorker\" request."; ResponseEntity responseEntity = passSecurityChecks(request, cancelCode, initMsg); if ( responseEntity != null ) return responseEntity; shouldShutdownWorker = false; String finalMsg = " Any previous \"shutdownWorker\"-request is canceled. The \"maxAssignmentsBatchesToHandleBeforeShutdown\" will still be honored (if it's set)."; logger.info(initMsg + finalMsg); return ResponseEntity.ok().body(finalMsg + "\n"); } @GetMapping("getHandledAssignmentsCounts") public ResponseEntity getHandledAssignmentsCounts() { return ResponseEntity.ok(AssignmentsHandler.assignmentsNumsHandled); } public ResponseEntity passSecurityChecks(HttpServletRequest request, String code, String initMsg) { if ( request == null ) { logger.error(initMsg + " The \"HttpServletRequest\" is null!"); return ResponseEntity.internalServerError().build(); } String remoteAddr = request.getHeader("X-FORWARDED-FOR"); if ( remoteAddr == null || "".equals(remoteAddr) ) remoteAddr = request.getRemoteAddr(); if ( ! (remoteAddr.equals("127.0.0.1") || remoteAddr.equals(UriBuilder.ip) || remoteAddr.equals(controllerIp)) ) { logger.error(initMsg + " The request came from another IP: " + remoteAddr + " | while this worker has the IP: " + UriBuilder.ip); return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); } if ( !code.equals(this.shutdownOrCancelCode) ) { String errorMsg = initMsg + " But, it contains an invalid code: " + code; logger.error(errorMsg); return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(errorMsg); } return null; // The checks are passing. } }