Ejemplo n.º 1
0
    def testPackage(self):
        blockable = test_utils.CreateSantaBlockable()
        bundle = test_utils.CreateSantaBundle(bundle_binaries=[blockable])
        rule = test_utils.CreateSantaRule(
            bundle.key, rule_type=constants.RULE_TYPE.PACKAGE)

        self.assertSameElements([blockable.key.id()],
                                model_utils.GetBundleBinaryIdsForRule(rule))
Ejemplo n.º 2
0
    def testNotPackage(self):
        blockable = test_utils.CreateSantaBlockable()
        rule = test_utils.CreateSantaRule(blockable.key,
                                          rule_type=constants.RULE_TYPE.BINARY)

        self.assertListEqual([], model_utils.GetBundleBinaryIdsForRule(rule))
Ejemplo n.º 3
0
    def post(self, uuid):
        # Prepare the query
        cursor = self.parsed_json.get(_RULE_DOWNLOAD.CURSOR)

        if self.host.rule_sync_dt is None:
            logging.info('%s clean rule sync',
                         'Continuing' if cursor else 'Starting')

        # pylint:disable=g-explicit-bool-comparison, singleton-comparison
        query = rule_models.SantaRule.query(
            rule_models.SantaRule.in_effect == True,
            rule_models.SantaRule.updated_dt >= self.host.rule_sync_dt,
            rule_models.SantaRule.host_id.IN(['', uuid])).order(
                rule_models.SantaRule.updated_dt, rule_models.SantaRule.key)
        # pylint:enable=g-explicit-bool-comparison, singleton-comparison

        # Fetch
        rules, next_cursor, more = query.fetch_page(
            settings.SANTA_RULE_BATCH_SIZE,
            start_cursor=datastore_query.Cursor(urlsafe=cursor))

        # Process the received rules.
        response_rules = []
        for rule in rules:
            epoch = datetime.datetime.utcfromtimestamp(0)
            creation_timestamp = (rule.updated_dt - epoch).total_seconds()
            rule_dict = {
                _RULE_DOWNLOAD.SHA256: rule.key.parent().id(),
                _RULE_DOWNLOAD.RULE_TYPE: rule.rule_type,
                _RULE_DOWNLOAD.POLICY: rule.policy,
                _RULE_DOWNLOAD.CUSTOM_MSG: rule.custom_msg,
                _RULE_DOWNLOAD.CREATION_TIME: creation_timestamp
            }

            if rule.rule_type == constants.RULE_TYPE.PACKAGE:
                # For Bundles, each binary member should have a separate rule generated
                # with a policy type matching that of the PACKAGE rule.
                binary_ids = model_utils.GetBundleBinaryIdsForRule(rule)
                binary_count = len(binary_ids)
                logging.info('Syncing %s bundle rules', binary_ids)
                for id_ in binary_ids:
                    dict_ = rule_dict.copy()
                    dict_.update({
                        _RULE_DOWNLOAD.SHA256:
                        id_,
                        _RULE_DOWNLOAD.RULE_TYPE:
                        constants.RULE_TYPE.BINARY,
                        _RULE_DOWNLOAD.FILE_BUNDLE_BINARY_COUNT:
                        binary_count,
                        _RULE_DOWNLOAD.FILE_BUNDLE_HASH:
                        rule.key.parent().id()
                    })
                    response_rules.append(dict_)
            else:
                response_rules.append(rule_dict)

        # Prepare the response, include the cursor if there are more rules.
        response = {_RULE_DOWNLOAD.RULES: response_rules}
        if more:
            response[_RULE_DOWNLOAD.CURSOR] = next_cursor.urlsafe()

        self.respond_json(response)