def testGet_UpdateChangedPolicy(self): policy_obj_1 = bit9_db.Bit9Policy( id='1', name='bar', enforcement_level=constants.BIT9_ENFORCEMENT_LEVEL.LOCKDOWN) policy_obj_1.put() old_policy_dt = policy_obj_1.updated_dt policy_obj_2 = bit9_db.Bit9Policy( id='2', name='baz', enforcement_level=constants.BIT9_ENFORCEMENT_LEVEL.LOCKDOWN) policy_obj_2.put() old_other_policy_dt = policy_obj_2.updated_dt policy1 = api.Policy(id=1, name='foo', enforcement_level=30) policy2 = api.Policy(id=2, name='baz', enforcement_level=20) self._PatchApiRequests([policy1, policy2]) self.testapp.get('/') self.assertEqual(2, bit9_db.Bit9Policy.query().count()) # First policy should have has its name updated from 'bar' to 'foo'. updated_policy = bit9_db.Bit9Policy.get_by_id('1') self.assertEqual('foo', updated_policy.name) self.assertEqual(constants.BIT9_ENFORCEMENT_LEVEL.BLOCK_AND_ASK, updated_policy.enforcement_level) self.assertNotEqual(old_policy_dt, updated_policy.updated_dt) # Second policy should be unchanged. other_updated_policy = bit9_db.Bit9Policy.get_by_id('2') self.assertEqual(old_other_policy_dt, other_updated_policy.updated_dt)
def testGet_IgnoreBadEnforcementLevel(self): policy_obj = bit9_models.Bit9Policy( id='1', name='foo', enforcement_level=constants.BIT9_ENFORCEMENT_LEVEL.LOCKDOWN) policy_obj.put() # Updated to an unknown enforcement level. policy = api.Policy(id=1, name='bar', enforcement_level=25) self._PatchApiRequests([policy]) self.testapp.get('/') # Policy name should _not_ be updated. updated_policy = bit9_models.Bit9Policy.get_by_id('1') self.assertEqual('foo', updated_policy.name)
def CreateBit9Policy(**kwargs): """Creates a Bit9Policy. Args: **kwargs: Dictionary of any policy properties to customize. Returns: The newly created host. """ defaults = { 'id': RandomDigits(16), 'name': RandomLetters(16), 'enforcement_level': constants.BIT9_ENFORCEMENT_LEVEL.LOCKDOWN } defaults.update(kwargs.copy()) new_policy = bit9.Bit9Policy(**defaults) new_policy.put() return new_policy
def get(self): policies_future = bit9.Bit9Policy.query().fetch_async() active_policies = (api.Policy.query().filter( api.Policy.total_computers > 0).execute(utils.CONTEXT)) local_policies = { policy.key.id(): policy for policy in policies_future.get_result() } policies_to_update = [] for policy in active_policies: try: level = constants.BIT9_ENFORCEMENT_LEVEL.MAP_FROM_INTEGRAL_LEVEL[ policy.enforcement_level] except KeyError: logging.warning('Unknown enforcement level "%s". Skipping...', policy.enforcement_level) continue local_policy = local_policies.get(str(policy.id)) if local_policy is None: new_policy = bit9.Bit9Policy(id=str(policy.id), name=policy.name, enforcement_level=level) policies_to_update.append(new_policy) else: dirty = False if local_policy.name != policy.name: local_policy.name = policy.name dirty = True if local_policy.enforcement_level != level: local_policy.enforcement_level = level dirty = True if dirty: policies_to_update.append(local_policy) if policies_to_update: logging.info('Updating %s policies', len(policies_to_update)) ndb.put_multi(policies_to_update)