Ejemplo n.º 1
0
  def _insert_blockable(self, blockable_id):
    blockable_type = self.request.get('type')
    model_class = getattr(
        model_mapping.BlockableTypeModelMap, blockable_type, None)
    if not model_class:
      self.abort(httplib.BAD_REQUEST, explanation='Model class not found')
    elif model_class.get_by_id(blockable_id):
      self.abort(httplib.CONFLICT, explanation='Blockable already exists')
    else:
      flag = (self.request.get('flagged') == 'true')
      blockable = model_class.get_or_insert(
          blockable_id,
          file_name=self.request.get('fileName'),
          publisher=self.request.get('publisher'),
          flagged=flag,
          id_type=constants.ID_TYPE.SHA256)

      # If one was provided, create a note to accompany the blockable.
      note_text = self.request.get('notes')
      if note_text:
        note_key = base_db.Note.GenerateKey(note_text, blockable.key)
        note = base_db.Note(
            key=note_key, message=note_text, author=self.user.key.id())
        note.put()

        blockable.notes.append(note.key)

      blockable.put()
      self.respond_json(blockable)
Ejemplo n.º 2
0
def _PersistBanNote(file_catalog):
    """Creates a Note entity containing a ban description if needed."""

    tuples = [(file_catalog.certificate_state, 'certificate'),
              (file_catalog.file_state, 'file'),
              (file_catalog.publisher_state, 'publisher')]

    ban_strings = sorted([
        'Banned by %s' % string for state, string in tuples
        if state == bit9_constants.APPROVAL_STATE.BANNED
    ])

    if ban_strings:
        full_message = '\n'.join(ban_strings)

        blockable_key = ndb.Key(bit9.Bit9Binary, file_catalog.sha256)
        note_key = base.Note.GenerateKey(full_message, blockable_key)

        if note_key.get() is None:
            logging.info('Persisting new ban Note for %s: %s',
                         file_catalog.sha256, ', '.join(ban_strings))
            note = base.Note(key=note_key, message=full_message)
            return note.put_async()

    return datastore_utils.GetNoOpFuture()
Ejemplo n.º 3
0
  def _insert_blockable(self, blockable_id, timestamp):

    blockable_type = self.request.get('type')

    model_class_map = {
        constants.BLOCKABLE_TYPE.SANTA_BINARY:
            santa_models.SantaBlockable,
        constants.BLOCKABLE_TYPE.SANTA_CERTIFICATE:
            santa_models.SantaCertificate}
    model_class = model_class_map.get(blockable_type, None)

    if not model_class:
      self.abort(
          httplib.BAD_REQUEST,
          explanation='No Model class found for "%s"' % blockable_type)

    elif model_class.get_by_id(blockable_id):
      self.abort(
          httplib.CONFLICT,
          explanation='Blockable "%s" already exists' % blockable_id)

    else:
      flag = (self.request.get('flagged') == 'true')

      logging.info('Creating new %s %s', model_class.__name__, blockable_id)
      blockable = model_class.get_or_insert(
          blockable_id,
          file_name=self.request.get('fileName'),
          publisher=self.request.get('publisher'),
          flagged=flag,
          id_type=constants.ID_TYPE.SHA256)
      blockable.InsertBigQueryRow(
          constants.BLOCK_ACTION.FIRST_SEEN, timestamp=timestamp)

      # If one was provided, create a note to accompany the blockable.
      note_text = self.request.get('notes')
      if note_text:
        note_key = base_models.Note.GenerateKey(note_text, blockable.key)
        note = base_models.Note(
            key=note_key, message=note_text, author=self.user.key.id())
        note.put()

        blockable.notes.append(note.key)

      blockable.put()
      return blockable