Ejemplo n.º 1
0
  def testUpdateHostname(self):
    users = test_utils.RandomStrings(2)
    policy_key = ndb.Key(bit9_models.Bit9Policy, '100')

    test_utils.CreateBit9Host(
        id='12345', hostname=bit9_utils.ExpandHostname('hostname1'),
        users=users, policy_key=policy_key)

    host = bit9_test_utils.CreateComputer(
        id=12345,
        name='hostname2',
        policy_id=100,
        users='{0}\\{1},{0}\\{2}'.format(settings.AD_DOMAIN, *users))
    occurred_dt = datetime.datetime.utcnow()

    sync._PersistBit9Host(host, occurred_dt).wait()

    bit9_host = bit9_models.Bit9Host.get_by_id('12345')
    self.assertEqual(bit9_utils.ExpandHostname('hostname2'), bit9_host.hostname)
    self.assertNoBigQueryInsertions()
Ejemplo n.º 2
0
 def testAlreadyFullyQualified(self):
     hostname = 'im-a-computer.' + settings.AD_HOSTNAME.lower()
     self.assertEqual(hostname, utils.ExpandHostname(hostname))
Ejemplo n.º 3
0
def _PersistBit9Host(computer, occurred_dt):
    """Creates a Bit9Host from the Event protobuf if one does not already exist.

  NOTE: This function could be transactional but, at least for now, host puts in
  multiple requests don't really need to be processed in a fixed order.
  last_event_dt is the only frequently modified property and there's currently
  no need for it to be perfectly accurate.

  Args:
    computer: api.Computer object associated with the event.
    occurred_dt: datetime object corresponding to the time of the event.

  Returns:
    ndb.Future that resolves when the host is updated.
  """
    host_id = str(computer.id)
    policy = computer.policy_id
    policy_key = (ndb.Key(bit9.Bit9Policy, str(policy))
                  if policy is not None else None)
    hostname = bit9_utils.ExpandHostname(
        bit9_utils.StripDownLevelDomain(computer.name))
    policy_entity = policy_key.get()
    mode = (policy_entity.enforcement_level
            if policy_entity is not None else constants.HOST_MODE.UNKNOWN)

    # Grab the corresponding Bit9Host.
    bit9_host = yield bit9.Bit9Host.get_by_id_async(host_id)

    existing_users = set(bit9_host.users if bit9_host is not None else [])
    extracted_users = list(bit9_utils.ExtractHostUsers(computer.users))

    # Ignore any 'Desktop Window Manager' users, otherwise a user can temporarily
    # become disassociated with their machine. If they vote for something to be
    # locally whitelisted during such a period, they won't get a rule for it.
    incoming_users = set()
    for extracted_user in extracted_users:
        if r'Window Manager\DWM-' in extracted_user:
            logging.warning('Ignoring user "%s"', extracted_user)
        else:
            incoming_users.add(extracted_user)

    # If there are incoming users, either because it was only all 'Desktop Window
    # Manager' entries, or because Bit9 didn't report any users for whatever
    # reason, then just stick with the existing users, otherwise we'll
    # disassociate the machine from the user.
    if not incoming_users:
        incoming_users = existing_users

    # Perform initialization for users new to this host.
    new_users = incoming_users - existing_users
    for new_user in new_users:

        # Create User if we haven't seen this user before.
        email = user_map.UsernameToEmail(new_user)
        user = user_models.User.GetOrInsert(email_addr=email)

        # Copy the user's local rules over from a pre-existing host.
        yield _CopyLocalRules(user.key, host_id)

    # List of all row action that need to be persisted.
    row_actions = []

    # Doesn't exist? Guess we better fix that.
    if bit9_host is None:
        logging.info('Creating new Bit9Host')
        bit9_host = bit9.Bit9Host(id=host_id,
                                  hostname=hostname,
                                  last_event_dt=occurred_dt,
                                  policy_key=policy_key,
                                  users=sorted(list(incoming_users)))

        row_actions.append(constants.HOST_ACTION.FIRST_SEEN)

    else:
        changed = False

        if not bit9_host.last_event_dt or bit9_host.last_event_dt < occurred_dt:
            bit9_host.last_event_dt = occurred_dt
            changed = True

        if bit9_host.hostname != hostname:
            logging.info('Hostname for %s changed from %s to %s', host_id,
                         bit9_host.hostname, hostname)
            bit9_host.hostname = hostname
            changed = True

        if bit9_host.policy_key != policy_key:
            bit9_host.policy_key = policy_key
            changed = True
            row_actions.append(constants.HOST_ACTION.MODE_CHANGE)

        if existing_users != incoming_users:
            existing_users_list = sorted(list(existing_users))
            incoming_users_list = sorted(list(incoming_users))
            logging.info('Users for %s changed from %s to %s', host_id,
                         existing_users_list, incoming_users_list)
            bit9_host.users = incoming_users_list
            changed = True
            row_actions.append(constants.HOST_ACTION.USERS_CHANGE)

        if not changed:
            raise ndb.Return()

    logging.info('Attempting to put Bit9Host...')
    yield bit9_host.put_async()

    for action in row_actions:
        tables.HOST.InsertRow(device_id=host_id,
                              timestamp=(bit9_host.recorded_dt if action
                                         == constants.HOST_ACTION.FIRST_SEEN
                                         else bit9_host.last_event_dt),
                              action=action,
                              hostname=hostname,
                              platform=constants.PLATFORM.WINDOWS,
                              users=sorted(list(incoming_users)),
                              mode=mode)
Ejemplo n.º 4
0
 def testSuccess(self):
     partial_hostname = 'im-a-computer'
     expected = partial_hostname + '.' + settings.AD_HOSTNAME.lower()
     self.assertEqual(expected, utils.ExpandHostname(partial_hostname))