项目作者: ketgo

项目描述 :
Python Attribute Based Access Control (ABAC)
高级语言: Python
项目地址: git://github.com/ketgo/py-abac.git
创建时间: 2019-09-19T06:54:43Z
项目社区:https://github.com/ketgo/py-abac

开源协议:Apache License 2.0

下载


py-ABAC

Attribute Based Access Control (ABAC) for python.

Build Status
codecov
Apache 2.0 licensed


Introduction

Py-ABAC is an attribute-based access control (ABAC) toolkit based on policies. ABAC gives you a fine-grained control on definition of the rules that restrict an access to resources and is generally considered a “next generation” authorization model. The design of py-ABAC stems from the XACML standard, and the ABAC python SDK Vakt.

See documentation for more details.

Install

PyABAC runs on Python >= 3.5. PyPy implementation is supported as well.

To install basic package run the following:

  1. pip install py-abac

With the basic package the in-memory policy storage backend can be used. For the other persistent backends run:

  1. # MongoDB backend
  2. pip install py-abac[mongo]
  3. # SQL backend
  4. pip install py-abac[sql]

Example Usage

A quick dive-in:

  1. from pymongo import MongoClient
  2. from py_abac import PDP, Policy, AccessRequest
  3. from py_abac.storage.mongo import MongoStorage
  4. # Policy definition in JSON
  5. policy_json = {
  6. "uid": "1",
  7. "description": "Max and Nina are allowed to create, delete, get any "
  8. "resources only if the client IP matches.",
  9. "effect": "allow",
  10. "rules": {
  11. "subject": [{"$.name": {"condition": "Equals", "value": "Max"}},
  12. {"$.name": {"condition": "Equals", "value": "Nina"}}],
  13. "resource": {"$.name": {"condition": "RegexMatch", "value": ".*"}},
  14. "action": [{"$.method": {"condition": "Equals", "value": "create"}},
  15. {"$.method": {"condition": "Equals", "value": "delete"}},
  16. {"$.method": {"condition": "Equals", "value": "get"}}],
  17. "context": {"$.ip": {"condition": "CIDR", "value": "127.0.0.1/32"}}
  18. },
  19. "targets": {},
  20. "priority": 0
  21. }
  22. # Parse JSON and create policy object
  23. policy = Policy.from_json(policy_json)
  24. # Setup policy storage
  25. client = MongoClient()
  26. storage = MongoStorage(client)
  27. # Add policy to storage
  28. storage.add(policy)
  29. # Create policy decision point
  30. pdp = PDP(storage)
  31. # A sample access request JSON
  32. request_json = {
  33. "subject": {
  34. "id": "",
  35. "attributes": {"name": "Max"}
  36. },
  37. "resource": {
  38. "id": "",
  39. "attributes": {"name": "myrn:example.com:resource:123"}
  40. },
  41. "action": {
  42. "id": "",
  43. "attributes": {"method": "get"}
  44. },
  45. "context": {
  46. "ip": "127.0.0.1"
  47. }
  48. }
  49. # Parse JSON and create access request object
  50. request = AccessRequest.from_json(request_json)
  51. # Check if access request is allowed. Evaluates to True since
  52. # Max is allowed to get any resource when client IP matches.
  53. assert pdp.is_allowed(request)

Documentation

Py-ABAC documentation can be found at https://py-abac.readthedocs.io

You can also build the documentation by running make html inside the docs folder.

Logging

py-ABAC follows a common logging pattern for libraries:

Its corresponding modules log all the events that happen but the log messages by default are handled by NullHandler. It’s up to the outer code/application to provide desired log handlers, filters, levels, etc.

For example:

  1. import logging
  2. root = logging.getLogger()
  3. root.setLevel(logging.INFO)
  4. root.addHandler(logging.StreamHandler())
  5. ... # here go all the py_abac calls.

Milestones

Most valuable features to be implemented in the order of importance:

  • Sphinx Documentation
  • Policy Obligations
  • In-Memory Storage
  • SQL Storage
  • Caching mechanism for Storage
  • File Storage

Acknowledgements

The conceptual and implementation design of py-ABAC stems from the XACML standard and the ABAC python SDK Vakt.

Back to top

Development

Py-ABAC requires a few backend databases like MongoDB, MySQL, etc for testing and development. For convenience
a docker-compose file is provided in the test folder to spawn up the required infrastructure. Just run:

  1. $ cd tests
  2. $ docker-compose up -d # this spawns up all the databases.
  3. $ cd .. # returns to the root repo folder

To hack py-ABAC run:

  1. $ pip install -e .[dev] # to install all dependencies
  2. $ pytest --cov=py_abac tests/ # to get coverage report
  3. $ pylint py_abac # to check code quality with PyLint
  4. $ bandit py_abac # to check code security with Bandit

Optionally you can use make to perform development tasks.

License

The source code is licensed under Apache License Version 2.0

Contributions

Pull requests and bug reports always welcomed! :)