Exemplo n.º 1
0
    def test_from_string_reference(self):
        ref = PolicyTypeReference.from_string_reference('action.concurrency')
        self.assertEqual(ref.resource_type, 'action')
        self.assertEqual(ref.name, 'concurrency')
        self.assertEqual(ref.ref, 'action.concurrency')

        ref = PolicyTypeReference.from_string_reference('action.concurrency.targeted')
        self.assertEqual(ref.resource_type, 'action')
        self.assertEqual(ref.name, 'concurrency.targeted')
        self.assertEqual(ref.ref, 'action.concurrency.targeted')

        self.assertRaises(InvalidReferenceError, PolicyTypeReference.from_string_reference, '.test')
        self.assertRaises(InvalidReferenceError, PolicyTypeReference.from_string_reference, '')
        self.assertRaises(InvalidReferenceError, PolicyTypeReference.from_string_reference, None)
Exemplo n.º 2
0
    def test_from_string_reference(self):
        ref = PolicyTypeReference.from_string_reference('action.concurrency')
        self.assertEqual(ref.resource_type, 'action')
        self.assertEqual(ref.name, 'concurrency')
        self.assertEqual(ref.ref, 'action.concurrency')

        ref = PolicyTypeReference.from_string_reference('action.concurrency.targeted')
        self.assertEqual(ref.resource_type, 'action')
        self.assertEqual(ref.name, 'concurrency.targeted')
        self.assertEqual(ref.ref, 'action.concurrency.targeted')

        self.assertRaises(InvalidReferenceError, PolicyTypeReference.from_string_reference, '.test')
        self.assertRaises(InvalidReferenceError, PolicyTypeReference.from_string_reference, '')
        self.assertRaises(InvalidReferenceError, PolicyTypeReference.from_string_reference, None)
Exemplo n.º 3
0
    def _get_all(self,
                 exclude_fields=None,
                 sort=None,
                 offset=0,
                 limit=None,
                 query_options=None,
                 from_model_kwargs=None,
                 raw_filters=None,
                 requester_user=None):

        resp = super(PolicyTypeController,
                     self)._get_all(exclude_fields=exclude_fields,
                                    sort=sort,
                                    offset=offset,
                                    limit=limit,
                                    query_options=query_options,
                                    from_model_kwargs=from_model_kwargs,
                                    raw_filters=raw_filters,
                                    requester_user=requester_user)

        if self.include_reference:
            result = resp.json
            for item in result:
                resource_type = item.get('resource_type', None)
                name = item.get('name', None)
                item['ref'] = PolicyTypeReference(resource_type=resource_type,
                                                  name=name).ref
            resp.json = result

        return resp
Exemplo n.º 4
0
 def _get_by_object(cls, object):
     name = getattr(object, "name", "")
     resource_type = getattr(object, "resource_type", "")
     ref = PolicyTypeReference.to_string_reference(
         resource_type=resource_type, name=name
     )
     return cls.get_by_ref(ref)
Exemplo n.º 5
0
    def test_to_string_reference(self):
        ref = PolicyTypeReference.to_string_reference(resource_type='action',
                                                      name='concurrency')
        self.assertEqual(ref, 'action.concurrency')

        self.assertRaises(ValueError,
                          PolicyTypeReference.to_string_reference,
                          resource_type='action.test',
                          name='concurrency')
        self.assertRaises(ValueError,
                          PolicyTypeReference.to_string_reference,
                          resource_type=None,
                          name='concurrency')
        self.assertRaises(ValueError,
                          PolicyTypeReference.to_string_reference,
                          resource_type='',
                          name='concurrency')
        self.assertRaises(ValueError,
                          PolicyTypeReference.to_string_reference,
                          resource_type='action',
                          name=None)
        self.assertRaises(ValueError,
                          PolicyTypeReference.to_string_reference,
                          resource_type='action',
                          name='')
        self.assertRaises(ValueError,
                          PolicyTypeReference.to_string_reference,
                          resource_type=None,
                          name=None)
        self.assertRaises(ValueError,
                          PolicyTypeReference.to_string_reference,
                          resource_type='',
                          name='')
Exemplo n.º 6
0
 def get_by_ref(cls, ref):
     if ref:
         ref_obj = PolicyTypeReference.from_string_reference(ref=ref)
         result = cls.query(name=ref_obj.name, resource_type=ref_obj.resource_type).first()
         return result
     else:
         return None
 def get_by_ref(cls, ref):
     if ref:
         ref_obj = PolicyTypeReference.from_string_reference(ref=ref)
         result = cls.query(name=ref_obj.name,
                            resource_type=ref_obj.resource_type).first()
         return result
     else:
         return None
Exemplo n.º 8
0
    def _get_by_ref(self, resource_ref):
        try:
            ref = PolicyTypeReference.from_string_reference(ref=resource_ref)
        except Exception:
            return None

        resource_db = self.access.query(name=ref.name, resource_type=ref.resource_type).first()
        return resource_db
Exemplo n.º 9
0
 def test_get_name(self):
     self.assertEqual(
         PolicyTypeReference.get_name("action.concurrency"), "concurrency"
     )
     self.assertRaises(InvalidReferenceError, PolicyTypeReference.get_name, ".abc")
     self.assertRaises(InvalidReferenceError, PolicyTypeReference.get_name, "abc")
     self.assertRaises(InvalidReferenceError, PolicyTypeReference.get_name, "")
     self.assertRaises(InvalidReferenceError, PolicyTypeReference.get_name, None)
Exemplo n.º 10
0
    def _get_by_ref(self, resource_ref):
        try:
            ref = PolicyTypeReference.from_string_reference(ref=resource_ref)
        except Exception:
            return None

        resource_db = self.access.query(name=ref.name, resource_type=ref.resource_type).first()
        return resource_db
Exemplo n.º 11
0
    def _get_one(self, ref_or_id):
        instance = self._get_by_ref_or_id(ref_or_id=ref_or_id)
        result = self.model.from_model(instance)

        if result and self.include_reference:
            resource_type = getattr(result, 'resource_type', None)
            name = getattr(result, 'name', None)
            result.ref = PolicyTypeReference(resource_type=resource_type, name=name).ref

        return result
Exemplo n.º 12
0
 def test_get_name(self):
     self.assertEqual(PolicyTypeReference.get_name('action.concurrency'),
                      'concurrency')
     self.assertRaises(InvalidReferenceError, PolicyTypeReference.get_name,
                       '.abc')
     self.assertRaises(InvalidReferenceError, PolicyTypeReference.get_name,
                       'abc')
     self.assertRaises(InvalidReferenceError, PolicyTypeReference.get_name,
                       '')
     self.assertRaises(InvalidReferenceError, PolicyTypeReference.get_name,
                       None)
Exemplo n.º 13
0
    def test_from_string_reference(self):
        ref = PolicyTypeReference.from_string_reference("action.concurrency")
        self.assertEqual(ref.resource_type, "action")
        self.assertEqual(ref.name, "concurrency")
        self.assertEqual(ref.ref, "action.concurrency")

        ref = PolicyTypeReference.from_string_reference("action.concurrency.targeted")
        self.assertEqual(ref.resource_type, "action")
        self.assertEqual(ref.name, "concurrency.targeted")
        self.assertEqual(ref.ref, "action.concurrency.targeted")

        self.assertRaises(
            InvalidReferenceError, PolicyTypeReference.from_string_reference, ".test"
        )
        self.assertRaises(
            InvalidReferenceError, PolicyTypeReference.from_string_reference, ""
        )
        self.assertRaises(
            InvalidReferenceError, PolicyTypeReference.from_string_reference, None
        )
Exemplo n.º 14
0
    def _get_by_ref_or_id(self, ref_or_id):
        if PolicyTypeReference.is_reference(ref_or_id):
            resource_db = self._get_by_ref(resource_ref=ref_or_id)
        else:
            resource_db = self._get_by_id(resource_id=ref_or_id)

        if not resource_db:
            msg = 'PolicyType with a reference of id "%s" not found.' % (ref_or_id)
            raise Exception(msg)

        return resource_db
Exemplo n.º 15
0
    def _get_by_ref_or_id(self, ref_or_id):
        if PolicyTypeReference.is_reference(ref_or_id):
            resource_db = self._get_by_ref(resource_ref=ref_or_id)
        else:
            resource_db = self._get_by_id(resource_id=ref_or_id)

        if not resource_db:
            msg = 'PolicyType with a reference of id "%s" not found.' % (ref_or_id)
            raise StackStormDBObjectNotFoundError(msg)

        return resource_db
Exemplo n.º 16
0
    def _get_all(self, **kwargs):
        result = super(PolicyTypeController, self)._get_all(**kwargs)

        if self.include_reference:
            for item in result:
                resource_type = getattr(item, 'resource_type', None)
                name = getattr(item, 'name', None)
                item.ref = PolicyTypeReference(resource_type=resource_type,
                                               name=name).ref

        return result
Exemplo n.º 17
0
    def _get_by_ref_or_id(self, ref_or_id):
        if PolicyTypeReference.is_reference(ref_or_id):
            resource_db = self._get_by_ref(resource_ref=ref_or_id)
        else:
            resource_db = self._get_by_id(resource_id=ref_or_id)

        if not resource_db:
            msg = 'PolicyType with a reference of id "%s" not found.' % (ref_or_id)
            raise StackStormDBObjectNotFoundError(msg)

        return resource_db
Exemplo n.º 18
0
    def _get_by_ref_or_id(self, ref_or_id):
        if PolicyTypeReference.is_reference(ref_or_id):
            resource_db = self._get_by_ref(resource_ref=ref_or_id)
        else:
            resource_db = self._get_by_id(resource_id=ref_or_id)

        if not resource_db:
            msg = 'PolicyType with a reference of id "%s" not found.' % (
                ref_or_id)
            raise Exception(msg)

        return resource_db
Exemplo n.º 19
0
    def _get_all(self, **kwargs):
        resp = super(PolicyTypeController, self)._get_all(**kwargs)

        if self.include_reference:
            result = resp.json
            for item in result:
                resource_type = item.get('resource_type', None)
                name = item.get('name', None)
                item['ref'] = PolicyTypeReference(resource_type=resource_type, name=name).ref
            resp.json = result

        return resp
Exemplo n.º 20
0
    def _get_filters(self, **kwargs):
        filters = copy.deepcopy(kwargs)
        ref = filters.get('ref', None)

        if ref:
            try:
                ref_obj = PolicyTypeReference.from_string_reference(ref=ref)
            except InvalidReferenceError:
                raise

            filters['name'] = ref_obj.name
            filters['resource_type'] = ref_obj.resource_type
            del filters['ref']

        return filters
Exemplo n.º 21
0
    def _get_filters(self, **kwargs):
        filters = copy.deepcopy(kwargs)
        ref = filters.get('ref', None)

        if ref:
            try:
                ref_obj = PolicyTypeReference.from_string_reference(ref=ref)
            except InvalidReferenceError:
                raise

            filters['name'] = ref_obj.name
            filters['resource_type'] = ref_obj.resource_type
            del filters['ref']

        return filters
Exemplo n.º 22
0
    def test_to_string_reference(self):
        ref = PolicyTypeReference.to_string_reference(
            resource_type="action", name="concurrency"
        )
        self.assertEqual(ref, "action.concurrency")

        self.assertRaises(
            ValueError,
            PolicyTypeReference.to_string_reference,
            resource_type="action.test",
            name="concurrency",
        )
        self.assertRaises(
            ValueError,
            PolicyTypeReference.to_string_reference,
            resource_type=None,
            name="concurrency",
        )
        self.assertRaises(
            ValueError,
            PolicyTypeReference.to_string_reference,
            resource_type="",
            name="concurrency",
        )
        self.assertRaises(
            ValueError,
            PolicyTypeReference.to_string_reference,
            resource_type="action",
            name=None,
        )
        self.assertRaises(
            ValueError,
            PolicyTypeReference.to_string_reference,
            resource_type="action",
            name="",
        )
        self.assertRaises(
            ValueError,
            PolicyTypeReference.to_string_reference,
            resource_type=None,
            name=None,
        )
        self.assertRaises(
            ValueError,
            PolicyTypeReference.to_string_reference,
            resource_type="",
            name="",
        )
Exemplo n.º 23
0
    def _get_one(self, ref_or_id):
        LOG.info('GET %s with ref_or_id=%s', pecan.request.path, ref_or_id)

        instance = self._get_by_ref_or_id(ref_or_id=ref_or_id)
        result = self.model.from_model(instance)

        if result and self.include_reference:
            resource_type = getattr(result, 'resource_type', None)
            name = getattr(result, 'name', None)
            result.ref = PolicyTypeReference(resource_type=resource_type,
                                             name=name).ref

        LOG.debug('GET %s with ref_or_id=%s, client_result=%s',
                  pecan.request.path, ref_or_id, result)

        return result
Exemplo n.º 24
0
    def test_to_string_reference(self):
        ref = PolicyTypeReference.to_string_reference(resource_type='action', name='concurrency')
        self.assertEqual(ref, 'action.concurrency')

        self.assertRaises(ValueError, PolicyTypeReference.to_string_reference,
                          resource_type='action.test', name='concurrency')
        self.assertRaises(ValueError, PolicyTypeReference.to_string_reference,
                          resource_type=None, name='concurrency')
        self.assertRaises(ValueError, PolicyTypeReference.to_string_reference,
                          resource_type='', name='concurrency')
        self.assertRaises(ValueError, PolicyTypeReference.to_string_reference,
                          resource_type='action', name=None)
        self.assertRaises(ValueError, PolicyTypeReference.to_string_reference,
                          resource_type='action', name='')
        self.assertRaises(ValueError, PolicyTypeReference.to_string_reference,
                          resource_type=None, name=None)
        self.assertRaises(ValueError, PolicyTypeReference.to_string_reference,
                          resource_type='', name='')
Exemplo n.º 25
0
    def _get_one(self, ref_or_id, requester_user):
        instance = self._get_by_ref_or_id(ref_or_id=ref_or_id)

        permission_type = PermissionType.POLICY_TYPE_VIEW
        rbac_utils.assert_user_has_resource_db_permission(
            user_db=requester_user,
            resource_db=instance,
            permission_type=permission_type)

        result = self.model.from_model(instance)

        if result and self.include_reference:
            resource_type = getattr(result, 'resource_type', None)
            name = getattr(result, 'name', None)
            result.ref = PolicyTypeReference(resource_type=resource_type,
                                             name=name).ref

        return result
Exemplo n.º 26
0
    def _get_one(self, ref_or_id):
        LOG.info('GET %s with ref_or_id=%s', pecan.request.path, ref_or_id)

        try:
            instance = self._get_by_ref_or_id(ref_or_id=ref_or_id)
        except Exception as e:
            LOG.exception(e.message)
            abort(http_client.NOT_FOUND, e.message)
            return

        result = self.model.from_model(instance)
        if result and self.include_reference:
            resource_type = getattr(result, 'resource_type', None)
            name = getattr(result, 'name', None)
            result.ref = PolicyTypeReference(resource_type=resource_type,
                                             name=name).ref

        LOG.debug('GET %s with ref_or_id=%s, client_result=%s',
                  pecan.request.path, ref_or_id, result)

        return result
Exemplo n.º 27
0
 def test_validate_resource_type(self):
     self.assertEqual(PolicyTypeReference.validate_resource_type('action'),
                      'action')
     self.assertRaises(ValueError,
                       PolicyTypeReference.validate_resource_type,
                       'action.test')
Exemplo n.º 28
0
 def test_get_name(self):
     self.assertEqual(PolicyTypeReference.get_name('action.concurrency'), 'concurrency')
     self.assertRaises(InvalidReferenceError, PolicyTypeReference.get_name, '.abc')
     self.assertRaises(InvalidReferenceError, PolicyTypeReference.get_name, 'abc')
     self.assertRaises(InvalidReferenceError, PolicyTypeReference.get_name, '')
     self.assertRaises(InvalidReferenceError, PolicyTypeReference.get_name, None)
Exemplo n.º 29
0
 def test_validate_resource_type(self):
     self.assertEqual(PolicyTypeReference.validate_resource_type('action'), 'action')
     self.assertRaises(ValueError, PolicyTypeReference.validate_resource_type, 'action.test')
Exemplo n.º 30
0
 def test_is_reference(self):
     self.assertTrue(PolicyTypeReference.is_reference('action.concurrency'))
     self.assertFalse(PolicyTypeReference.is_reference('concurrency'))
     self.assertFalse(PolicyTypeReference.is_reference(''))
     self.assertFalse(PolicyTypeReference.is_reference(None))
Exemplo n.º 31
0
 def test_is_reference(self):
     self.assertTrue(PolicyTypeReference.is_reference('action.concurrency'))
     self.assertFalse(PolicyTypeReference.is_reference('concurrency'))
     self.assertFalse(PolicyTypeReference.is_reference(''))
     self.assertFalse(PolicyTypeReference.is_reference(None))
Exemplo n.º 32
0
 def _get_by_object(cls, object):
     name = getattr(object, 'name', '')
     resource_type = getattr(object, 'resource_type', '')
     ref = PolicyTypeReference.to_string_reference(resource_type=resource_type, name=name)
     return cls.get_by_ref(ref)