policy validate cann't work when create a policy

Bug #1716812 reported by TingtingYu
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
senlin
Fix Released
Undecided
Unassigned

Bug Description

When I create a loadbalancer policy with an invalid subnet,but it's also create successfully.
Then I see the senlin code with policy create,"policy = self._validate_policy(ctx, req.spec, name=name)
" in the policy_create(),then see the _validate_policy(),I see it call policy.validate() with validate_props=False,but in policy.validate(),if validate_props=False, it can not to check the validity of params.
   def policy_create(self, ctx, req):
        """Create a policy with the given name and spec.

        :param ctx: An instance of the request context.
        :param req: An instance of the PolicyCreateRequestBody.
        :return: A dictionary containing the details of the policy object
                 created.
        """
        name = req.name

        if CONF.name_unique:
            if policy_obj.Policy.get_by_name(ctx, name):
                msg = _("A policy named '%(name)s' already exists."
                        ) % {"name": name}
                raise exception.BadRequest(msg=msg)

        policy = self._validate_policy(ctx, req.spec, name=name)

        LOG.info("Creating policy %(type)s '%(name)s'",
                 {'type': policy.type, 'name': policy.name})

        policy.store(ctx)
        LOG.info("Policy '%(name)s' is created: %(id)s.",
                 {'name': name, 'id': policy.id})
        return policy.to_dict()

    def _validate_policy(self, context, spec, name=None, validate_props=False):
        """Validate a policy.

        :param context: An instance of the request context.
        :param spec: A dictionary containing the spec for the policy.
        :param name: The name of the policy to be validated.
        :param validate_props: Whether to validate the value of property.
        :return: Validated policy object.
        """

        type_name, version = schema.get_spec_version(spec)
        type_str = "-".join([type_name, version])

        plugin = environment.global_env().get_policy(type_str)

        kwargs = {
            'user': context.user,
            'project': context.project,
            'domain': context.domain,
        }
        if name is None:
            name = 'validated_policy'
        policy = plugin(name, spec, **kwargs)

        try:
            policy.validate(context, validate_props=validate_props)
        except exception.InvalidSpec as ex:
            msg = six.text_type(ex)
            LOG.error("Failed in validating policy: %s", msg)
            raise exception.InvalidSpec(message=msg)

        return policy

    def validate(self, context, validate_props=False):
        super(LoadBalancingPolicy, self).validate(context, validate_props)

        if not validate_props:
            return True

        nc = self.network(context.user, context.project)

        # validate pool subnet
        name_or_id = self.pool_spec.get(self.POOL_SUBNET)
        try:
            nc.subnet_get(name_or_id)
        except exc.InternalError:
            msg = _("The specified %(key)s '%(value)s' could not be found."
                    ) % {'key': self.POOL_SUBNET, 'value': name_or_id}
            raise exc.InvalidSpec(message=msg)

        # validate VIP subnet
        name_or_id = self.vip_spec.get(self.VIP_SUBNET)
        try:
            nc.subnet_get(name_or_id)
        except exc.InternalError:
            msg = _("The specified %(key)s '%(value)s' could not be found."
                    ) % {'key': self.VIP_SUBNET, 'value': name_or_id}
            raise exc.InvalidSpec(message=msg)

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to senlin (master)

Reviewed: https://review.openstack.org/503502
Committed: https://git.openstack.org/cgit/openstack/senlin/commit/?id=914a46eff07aa44c7d802fe70b1a58a38bc514c6
Submitter: Jenkins
Branch: master

commit 914a46eff07aa44c7d802fe70b1a58a38bc514c6
Author: TingtingYu <yu_qearl@163.com>
Date: Wed Sep 13 10:09:43 2017 +0800

    fix the bug that policy validate cannot work when creatting a policy

    It can be created successfully when I create a policy with an
    invalid subnet. And I find the default value is False of the parameter
    validate_props,so that the policy validate() cannot validate deeply.

    To fix the bug, I change the validate_props from False to True in the
    code of policy_create().

    Change-Id: I727b3a0d74ff69d8cd797b8a5af8d6f40a8f5fb9
    Closes-Bug: 1716812

Changed in senlin:
status: New → Fix Released
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix included in openstack/senlin 5.0.0.0b2

This issue was fixed in the openstack/senlin 5.0.0.0b2 development milestone.

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix included in openstack/senlin 5.0.0.0b3

This issue was fixed in the openstack/senlin 5.0.0.0b3 development milestone.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.