如何在基于python的Web应用程序中实现基于属性的访问控制?

问题描述

我有一个用python(django REST框架)编写的Web应用程序,现在我想在Web应用程序上实现基于属性的访问控制(ABAC)进行授权,如何在此应用程序上实现ABAC策略(我可以使用XACML)策略(如何在python Web应用程序上实现xacml)或是否有其他方法可以在python上编写ABAC策略以及如何在我的Web应用程序上实现) 我可以使用py-ABAC以及如何使用它吗?

import vakt
from vakt.rules import Eq,Any,StartsWith,And,Greater,Less

policy = vakt.Policy(
    123456,actions=[Eq('fork'),Eq('clone')],resources=[StartsWith('repos/Google',ci=True)],subjects=[{'name': Any(),'stars': And(Greater(50),Less(999))}],effect=vakt.ALLOW_ACCESS,context={'referer': Eq('https://github.com')},description="""
    Allow to fork or clone any Google repository for
    users that have > 50 and < 999 stars and came from Github
    """
)
storage = vakt.MemoryStorage()
storage.add(policy)
guard = vakt.Guard(storage,vakt.RulesChecker())

inq = vakt.Inquiry(action='fork',resource='repos/google/tensorflow',subject={'name': 'larry','stars': 80},context={'referer': 'https://github.com'})

assert guard.is_allowed(inq)
Or if you prefer Amazon IAM Policies style:

import vakt
from vakt.rules import CIDR

policy = vakt.Policy(
    123457,subjects=[r'<[a-zA-Z]+ M[a-z]+>'],resources=['library:books:<.+>','office:magazines:<.+>'],actions=['<read|get>'],context={
        'ip': CIDR('192.168.0.0/24'),},description="""
    Allow all readers of the book library whose surnames start with M get and read any book or magazine,but only when they connect from local library's computer
    """,)
storage = vakt.MemoryStorage()
storage.add(policy)
guard = vakt.Guard(storage,vakt.RegexChecker())

inq = vakt.Inquiry(action='read',resource='library:books:Hobbit',subject='Jim Morrison',context={'ip': '192.168.0.220'})

assert guard.is_allowed(inq)

Thanks in advance!  

解决方法

我没有使用Py-ABAC的经验,但是通常XACML是用XACML(一种基于XML的语言)编写的,或者是使用GUI或编译成XACML的语言(如ALFA)编写的。

然后,您的Python Web应用程序将使用REST或SOAP(最好是REST)调用策略决策点(PDP)。您可以使用HTTP库(例如请求)。

示例JSON:

{"Request":{"AccessSubject":
{"Attribute":
[ {"AttributeId":"user.name","Value":"alice"} ]
},"Resource":
{"Attribute":
[ {"AttributeId":"resource.objectType","Value":"insurance claim"} ]
},"Action":
{"Attribute":
[ {"AttributeId":"action-id","Value":"view"}]
}
}
}

我是否提到(至少对我来说)创建自己的授权引擎(PDP)并不是完全明智的选择?有些产品已经完成了外部授权的实现...

使用WSO2或AuthzForce之类的开源产品,或购买Axiomatics之类的产品(完整披露:我以前在这里工作)。

有关XACML实现的完整列表,可以在Wikipedia上查看此list

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...