Ejemplo n.º 1
0
 def _ReadReply(self):
     try:
         replylen_str = self._ReadExactly(sutils.SIZE_PACKER.size)
         replylen = sutils.SIZE_PACKER.unpack(replylen_str)[0]
         reply = self._ReadExactly(replylen)
         response = rdf_data_store.DataStoreResponse(reply)
         CheckResponseStatus(response)
         return response
     except (socket.error, socket.timeout, IOError) as e:
         logging.warning("Cannot read reply from server %s:%d : %s",
                         self.Address(), self.Port(), e)
         return None
Ejemplo n.º 2
0
  def Wrapper(self, request):
    """Wrap the function can catch exceptions, converting them to status."""
    failed = True
    response = rdf_data_store.DataStoreResponse()
    response.status = rdf_data_store.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 = (
          rdf_data_store.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 = rdf_data_store.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 = rdf_data_store.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
Ejemplo n.º 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 = rdf_data_server.DataStoreCommand(cmd_str)

        request = cmd.request
        op = cmd.command

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

        return sutils.SIZE_PACKER.pack(len(response)) + response