def testNewHost(self): users = test_utils.RandomStrings(2) host = bit9_test_utils.CreateComputer( users='{0}\\{1},{0}\\{2}'.format(settings.AD_DOMAIN, *users)) occurred_dt = datetime.datetime.utcnow() self.assertEntityCount(bit9_models.Bit9Host, 0) sync._PersistBit9Host(host, occurred_dt).wait() self.assertEntityCount(bit9_models.Bit9Host, 1) self.assertBigQueryInsertions( [constants.BIGQUERY_TABLE.HOST] + [constants.BIGQUERY_TABLE.USER] * 2)
def testUpdateHostUsers_NoIncomingUsers(self): old_users = test_utils.RandomStrings(2) policy_key = ndb.Key(bit9_models.Bit9Policy, '22222') test_utils.CreateBit9Host( id='12345', users=old_users, policy_key=policy_key) host = bit9_test_utils.CreateComputer( id=12345, policy_id=22222, users='') occurred_dt = datetime.datetime.utcnow() sync._PersistBit9Host(host, occurred_dt).wait() # Verify that the users weren't updated in Datastore. host = bit9_models.Bit9Host.get_by_id('12345') self.assertSameElements(old_users, host.users)
def testNewHost(self): users = test_utils.RandomStrings(2) host = bit9_test_utils.CreateComputer( users='{0}\\{1},{0}\\{2}'.format(settings.AD_DOMAIN, *users)) occurred_dt = datetime.datetime.utcnow() self.assertEntityCount(bit9_db.Bit9Host, 0) self.assertEntityCount(bigquery_db.HostRow, 0) self.assertEntityCount(bigquery_db.UserRow, 0) sync._PersistBit9Host(host, occurred_dt).wait() self.assertEntityCount(bit9_db.Bit9Host, 1) self.assertTaskCount(constants.TASK_QUEUE.BQ_PERSISTENCE, 3) self.DrainTaskQueue(constants.TASK_QUEUE.BQ_PERSISTENCE) self.assertEntityCount(bigquery_db.HostRow, 1) self.assertEntityCount(bigquery_db.UserRow, 2)
def testUpdatePolicyKey(self): users = test_utils.RandomStrings(2) old_policy_key = ndb.Key(bit9_models.Bit9Policy, '22222') test_utils.CreateBit9Host( id='11111', policy_key=old_policy_key, users=users) host = bit9_test_utils.CreateComputer( id=11111, policy_id=33333, 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('11111') new_policy_key = ndb.Key(bit9_models.Bit9Policy, '33333') self.assertEqual(new_policy_key, bit9_host.policy_key) self.assertBigQueryInsertions([constants.BIGQUERY_TABLE.HOST])
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()
def testUpdateLastEventDt(self): now_dt = datetime.datetime.utcnow() earlier_dt = now_dt - datetime.timedelta(days=7) users = test_utils.RandomStrings(2) policy_key = ndb.Key(bit9_models.Bit9Policy, '100') test_utils.CreateBit9Host( id='12345', last_event_dt=earlier_dt, users=users, policy_key=policy_key) host = bit9_test_utils.CreateComputer( id=12345, policy_id=100, users='{0}\\{1},{0}\\{2}'.format(settings.AD_DOMAIN, *users)) sync._PersistBit9Host(host, now_dt).wait() bit9_host = bit9_models.Bit9Host.get_by_id('12345') self.assertEqual(now_dt, bit9_host.last_event_dt) self.assertNoBigQueryInsertions()
def testUpdateHostname(self): users = test_utils.RandomStrings(2) policy_key = ndb.Key(bit9_db.Bit9Policy, '100') test_utils.CreateBit9Host( id='12345', hostname=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_db.Bit9Host.get_by_id('12345') self.assertEqual(utils.ExpandHostname('hostname2'), bit9_host.hostname) self.assertTaskCount(constants.TASK_QUEUE.BQ_PERSISTENCE, 0)
def testUpdateHostUsers_WindowManager(self): test_utils.CreateBit9Host( id='12345', users=['a_real_person'], policy_key=ndb.Key(bit9_models.Bit9Policy, '22222')) host = bit9_test_utils.CreateComputer( id=12345, policy_id=22222, users=r'Window Manager\DWM-999') occurred_dt = datetime.datetime.utcnow() sync._PersistBit9Host(host, occurred_dt).wait() # Verify that the user didn't get updated. host = bit9_models.Bit9Host.get_by_id('12345') self.assertSameElements(['a_real_person'], host.users) # Verify no BigQuery row persistence. self.assertNoBigQueryInsertions()
def testUpdateHostUsers(self): old_users = test_utils.RandomStrings(2) new_users = test_utils.RandomStrings(2) policy_key = ndb.Key(bit9_db.Bit9Policy, '22222') test_utils.CreateBit9Host( id='12345', users=old_users, policy_key=policy_key) host = bit9_test_utils.CreateComputer( id=12345, policy_id=22222, users='{0}\\{1},{0}\\{2}'.format(settings.AD_DOMAIN, *new_users)) occurred_dt = datetime.datetime.utcnow() sync._PersistBit9Host(host, occurred_dt).wait() bit9_db.Bit9Host.get_by_id('12345') self.assertTaskCount(constants.TASK_QUEUE.BQ_PERSISTENCE, 3) self.DrainTaskQueue(constants.TASK_QUEUE.BQ_PERSISTENCE) self.assertEntityCount(bigquery_db.HostRow, 1) self.assertEntityCount(bigquery_db.UserRow, 2)
def testUpdatePolicyKey(self): users = test_utils.RandomStrings(2) old_policy_key = ndb.Key(bit9_db.Bit9Policy, '22222') test_utils.CreateBit9Host( id='11111', policy_key=old_policy_key, users=users) host = bit9_test_utils.CreateComputer( id=11111, policy_id=33333, users='{0}\\{1},{0}\\{2}'.format(settings.AD_DOMAIN, *users)) occurred_dt = datetime.datetime.utcnow() sync._PersistBit9Host(host, occurred_dt).wait() bit9_host = bit9_db.Bit9Host.get_by_id('11111') new_policy_key = ndb.Key(bit9_db.Bit9Policy, '33333') self.assertEqual(new_policy_key, bit9_host.policy_key) self.assertTaskCount(constants.TASK_QUEUE.BQ_PERSISTENCE, 1) self.RunDeferredTasks(constants.TASK_QUEUE.BQ_PERSISTENCE) self.assertEntityCount(bigquery_db.HostRow, 1)
def testCopyLocalRules_NoPreviousHosts(self): old_user = test_utils.CreateUser(email=user_map.UsernameToEmail('foo')) new_user = test_utils.CreateUser(email=user_map.UsernameToEmail('bar')) test_utils.CreateBit9Host( id='12345', users=[old_user.nickname], policy_key=ndb.Key(bit9_db.Bit9Policy, '22222')) host = bit9_test_utils.CreateComputer( id=12345, policy_id=22222, users='{0}\\{1},{0}\\{2}'.format( settings.AD_DOMAIN, old_user.nickname, new_user.nickname)) occurred_dt = datetime.datetime.utcnow() sync._PersistBit9Host(host, occurred_dt).wait() self.assertEntityCount(bit9_db.Bit9Rule, 0) self.assertEntityCount(bit9_db.RuleChangeSet, 0) self.assertTaskCount(constants.TASK_QUEUE.BQ_PERSISTENCE, 1) self.DrainTaskQueue(constants.TASK_QUEUE.BQ_PERSISTENCE) self.assertEntityCount(bigquery_db.HostRow, 1)
def testUpdateHostUsers_People(self): old_users = test_utils.RandomStrings(2) new_users = test_utils.RandomStrings(2) policy_key = ndb.Key(bit9_models.Bit9Policy, '22222') test_utils.CreateBit9Host( id='12345', users=old_users, policy_key=policy_key) host = bit9_test_utils.CreateComputer( id=12345, policy_id=22222, users='{0}\\{1},{0}\\{2}'.format(settings.AD_DOMAIN, *new_users)) occurred_dt = datetime.datetime.utcnow() sync._PersistBit9Host(host, occurred_dt).wait() # Verify that the users were updated in Datastore. host = bit9_models.Bit9Host.get_by_id('12345') self.assertSameElements(new_users, host.users) # Verify all BigQuery row persistence. self.assertBigQueryInsertions( [constants.BIGQUERY_TABLE.HOST] + [constants.BIGQUERY_TABLE.USER] * 2)
def testCopyLocalRules_Success(self): old_user = test_utils.CreateUser(email=user_map.UsernameToEmail('foo')) new_user = test_utils.CreateUser(email=user_map.UsernameToEmail('bar')) policy_key = ndb.Key(bit9_db.Bit9Policy, '22222') host1 = test_utils.CreateBit9Host( id='12345', users=[old_user.nickname], policy_key=policy_key) test_utils.CreateBit9Host( id='67890', users=[new_user.nickname], policy_key=policy_key) blockable1 = test_utils.CreateBit9Binary() test_utils.CreateBit9Rule( blockable1.key, host_id=host1.key.id(), user_key=old_user.key) blockable2 = test_utils.CreateBit9Binary() test_utils.CreateBit9Rule( blockable2.key, host_id=host1.key.id(), user_key=old_user.key) host = bit9_test_utils.CreateComputer( id=67890, policy_id=22222, users='{0}\\{1},{0}\\{2}'.format( settings.AD_DOMAIN, old_user.nickname, new_user.nickname)) occurred_dt = datetime.datetime.utcnow() sync._PersistBit9Host(host, occurred_dt).wait() self.assertEntityCount(bit9_db.Bit9Rule, 4) # 2 New + 2 Old self.assertEntityCount(bit9_db.RuleChangeSet, 2) rules_for_host1 = bit9_db.Bit9Rule.query( bit9_db.Bit9Rule.host_id == host1.key.id()).fetch() self.assertEqual(2, len(rules_for_host1)) self.assertSameElements( [blockable1.key, blockable2.key], [rule.key.parent() for rule in rules_for_host1]) self.assertTaskCount(constants.TASK_QUEUE.BQ_PERSISTENCE, 1) self.DrainTaskQueue(constants.TASK_QUEUE.BQ_PERSISTENCE) self.assertEntityCount(bigquery_db.HostRow, 1)