Exemple #1
0
 def _ReadReply(self):
   try:
     replylen_str = self._ReadExactly(sutils.SIZE_PACKER.size)
     if not replylen_str:
       raise HTTPDataStoreError("Could not read reply from data server.")
     replylen = sutils.SIZE_PACKER.unpack(replylen_str)[0]
     reply = self._ReadExactly(replylen)
     response = rdfvalue.DataStoreResponse(reply)
     CheckResponseStatus(response)
     return response
   except (socket.error, socket.timeout):
     logging.warning("Cannot read reply from server %s:%d", self.Address(),
                     self.Port())
     return None
Exemple #2
0
    def Wrapper(self, request):
        """Wrap the function can catch exceptions, converting them to status."""
        failed = True
        response = rdfvalue.DataStoreResponse()
        response.status = rdfvalue.DataStoreResponse.Status.OK

        try:
            f(self, request, response)
            failed = False
        except access_control.UnauthorizedAccess as e:
            # Attach a copy of the request to the response so the caller can tell why
            # we failed the request.
            response.Clear()
            response.request = request

            response.status = rdfvalue.DataStoreResponse.Status.AUTHORIZATION_DENIED
            if e.subject:
                response.failed_subject = utils.SmartUnicode(e.subject)

            response.status_desc = utils.SmartUnicode(e)

        except data_store.Error as e:
            # Attach a copy of the request to the response so the caller can tell why
            # we failed the request.
            response.Clear()
            response.request = request

            response.status = rdfvalue.DataStoreResponse.Status.DATA_STORE_ERROR
            response.status_desc = utils.SmartUnicode(e)

        except access_control.ExpiryError as e:
            # Attach a copy of the request to the response so the caller can tell why
            # we failed the request.
            response.Clear()
            response.request = request

            response.status = rdfvalue.DataStoreResponse.Status.TIMEOUT_ERROR
            response.status_desc = utils.SmartUnicode(e)

        if failed:
            # Limit the size of the error report since it can be quite large.
            logging.info("Failed: %s", utils.SmartStr(response)[:1000])
        serialized_response = response.SerializeToString()
        return serialized_response
Exemple #3
0
    def HandleClient(self, sock, permissions):
        """Handles new client requests readable from 'read'."""
        # Use a long timeout here.
        sock.settimeout(self.CLIENT_TIMEOUT_TIME)
        cmdlen_str = self._ReadExactlyFailAfterFirst(sock,
                                                     sutils.SIZE_PACKER.size)
        if not cmdlen_str:
            return ""
        cmdlen = sutils.SIZE_PACKER.unpack(cmdlen_str)[0]
        # Full request must be here.
        sock.settimeout(self.READ_TIMEOUT)
        try:
            cmd_str = self._ReadExactly(sock, cmdlen)
        except (socket.timeout, socket.error):
            return ""
        cmd = rdfvalue.DataStoreCommand(cmd_str)

        request = cmd.request
        op = cmd.command

        cmdinfo = CMDTABLE.get(op)
        if not cmdinfo:
            logging.error("Unrecognized command %d", op)
            return ""
        method, perm = cmdinfo
        if perm in permissions:
            response = method(request)
        else:
            resp = rdfvalue.DataStoreResponse()
            resp.request = cmd.request
            resp.status = rdfvalue.DataStoreResponse.Status.AUTHORIZATION_DENIED
            resp.status_desc = (
                "Operation not allowed: required %s but only have "
                "%s permissions" % (perm, permissions))
            response = resp.SerializeToString()

        replybody = sutils.SIZE_PACKER.pack(len(response)) + response

        return replybody