def _is_session_valid()

in cookbooks/aws-parallelcluster-platform/files/dcv/pcluster_dcv_authenticator.py [0:0]


    def _is_session_valid(user, session_id):
        """
        Verify if the DCV session exists and the ownership.

        # We are using ps aux to retrieve the list of sessions
        # because currently DCV doesn't allow list-session to list all session even for non-root user.
        # TODO change this method if DCV updates his behaviour.
        """
        logger.info("Verifying Amazon DCV session validity..")
        # Remove the first and the last because they are the heading and empty, respectively
        # All commands and arguments in this subprocess call are built as literals
        processes = subprocess.check_output(["/bin/ps", "aux"]).decode("utf-8").split("\n")[1:-1]  # nosec B603

        # Check the filter is empty
        if not next(
            filter(lambda process: DCVAuthenticator.check_dcv_process(process, user, session_id), processes), None
        ):
            raise DCVAuthenticator.IncorrectRequestError("The given session does not exists")
        logger.info("The Amazon DCV session is valid.")