Beispiel #1
0
    def validate(audit_template):
        available_goals = objects.Goal.list(AuditTemplatePostType._ctx)
        available_goal_uuids_map = {g.uuid: g for g in available_goals}
        available_goal_names_map = {g.name: g for g in available_goals}
        if audit_template.goal in available_goal_uuids_map:
            goal = available_goal_uuids_map[audit_template.goal]
        elif audit_template.goal in available_goal_names_map:
            goal = available_goal_names_map[audit_template.goal]
        else:
            raise exception.InvalidGoal(goal=audit_template.goal)

        common_utils.Draft4Validator(
            default.DefaultScope.DEFAULT_SCHEMA).validate(audit_template.scope)

        include_host_aggregates = False
        exclude_host_aggregates = False
        for rule in audit_template.scope:
            if 'host_aggregates' in rule:
                include_host_aggregates = True
            elif 'exclude' in rule:
                for resource in rule['exclude']:
                    if 'host_aggregates' in resource:
                        exclude_host_aggregates = True
        if include_host_aggregates and exclude_host_aggregates:
            raise exception.Invalid(
                message=_("host_aggregates can't be "
                          "included and excluded together"))

        if audit_template.strategy:
            available_strategies = objects.Strategy.list(
                AuditTemplatePostType._ctx)
            available_strategies_map = {
                s.uuid: s
                for s in available_strategies
            }
            if audit_template.strategy not in available_strategies_map:
                raise exception.InvalidStrategy(
                    strategy=audit_template.strategy)

            strategy = available_strategies_map[audit_template.strategy]
            # Check that the strategy we indicate is actually related to the
            # specified goal
            if strategy.goal_id != goal.id:
                choices = [
                    "'%s' (%s)" % (s.uuid, s.name)
                    for s in available_strategies
                ]
                raise exception.InvalidStrategy(
                    message=_("'%(strategy)s' strategy does relate to the "
                              "'%(goal)s' goal. Possible choices: %(choices)s")
                    % dict(strategy=strategy.name,
                           goal=goal.name,
                           choices=", ".join(choices)))
            audit_template.strategy = strategy.uuid

        # We force the UUID so that we do not need to query the DB with the
        # name afterwards
        audit_template.goal = goal.uuid

        return audit_template
Beispiel #2
0
 def _validate_goal(patch):
     serialized_patch = {'path': patch.path, 'op': patch.op}
     if patch.value is not wsme.Unset:
         serialized_patch['value'] = patch.value
     new_goal = patch.value
     if new_goal and new_goal not in cfg.CONF.watcher_goals.goals.keys():
         raise exception.InvalidGoal(goal=new_goal)
Beispiel #3
0
    def _validate_goal(patch):
        patch.path = "/goal_id"
        goal = patch.value

        if goal:
            available_goals = objects.Goal.list(AuditTemplatePatchType._ctx)
            available_goal_uuids_map = {g.uuid: g for g in available_goals}
            available_goal_names_map = {g.name: g for g in available_goals}
            if goal in available_goal_uuids_map:
                patch.value = available_goal_uuids_map[goal].id
            elif goal in available_goal_names_map:
                patch.value = available_goal_names_map[goal].id
            else:
                raise exception.InvalidGoal(goal=goal)
Beispiel #4
0
    def create(self, context=None):
        """Create a :class:`AuditTemplate` record in the DB.

        :param context: Security context. NOTE: This should only
                        be used internally by the indirection_api.
                        Unfortunately, RPC requires context as the first
                        argument, even though we don't use it.
                        A context should be set when instantiating the
                        object, e.g.: AuditTemplate(context)
        """

        values = self.obj_get_changes()
        goal = values['goal']
        if goal not in cfg.CONF.watcher_goals.goals.keys():
            raise exception.InvalidGoal(goal=goal)
        db_audit_template = self.dbapi.create_audit_template(values)
        self._from_db_object(self, db_audit_template)
Beispiel #5
0
 def validate(audit_template):
     if audit_template.goal not in cfg.CONF.watcher_goals.goals.keys():
         raise exception.InvalidGoal(audit_template.goal)
     return audit_template