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



我有一个用python编写的web应用程序(django REST框架(,现在我想在我的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,
effect=vakt.ALLOW_ACCESS,
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实现的完整列表,您可以在维基百科上查看此列表。

最新更新