security.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. """Kazoo Security"""
  2. from base64 import b64encode
  3. from collections import namedtuple
  4. import hashlib
  5. # Represents a Zookeeper ID and ACL object
  6. Id = namedtuple('Id', 'scheme id')
  7. class ACL(namedtuple('ACL', 'perms id')):
  8. """An ACL for a Zookeeper Node
  9. An ACL object is created by using an :class:`Id` object along with
  10. a :class:`Permissions` setting. For convenience,
  11. :meth:`make_digest_acl` should be used to create an ACL object with
  12. the desired scheme, id, and permissions.
  13. """
  14. @property
  15. def acl_list(self):
  16. perms = []
  17. if self.perms & Permissions.ALL == Permissions.ALL:
  18. perms.append('ALL')
  19. return perms
  20. if self.perms & Permissions.READ == Permissions.READ:
  21. perms.append('READ')
  22. if self.perms & Permissions.WRITE == Permissions.WRITE:
  23. perms.append('WRITE')
  24. if self.perms & Permissions.CREATE == Permissions.CREATE:
  25. perms.append('CREATE')
  26. if self.perms & Permissions.DELETE == Permissions.DELETE:
  27. perms.append('DELETE')
  28. if self.perms & Permissions.ADMIN == Permissions.ADMIN:
  29. perms.append('ADMIN')
  30. return perms
  31. def __repr__(self):
  32. return 'ACL(perms=%r, acl_list=%s, id=%r)' % (
  33. self.perms, self.acl_list, self.id)
  34. class Permissions(object):
  35. READ = 1
  36. WRITE = 2
  37. CREATE = 4
  38. DELETE = 8
  39. ADMIN = 16
  40. ALL = 31
  41. # Shortcuts for common Ids
  42. ANYONE_ID_UNSAFE = Id('world', 'anyone')
  43. AUTH_IDS = Id('auth', '')
  44. # Shortcuts for common ACLs
  45. OPEN_ACL_UNSAFE = [ACL(Permissions.ALL, ANYONE_ID_UNSAFE)]
  46. CREATOR_ALL_ACL = [ACL(Permissions.ALL, AUTH_IDS)]
  47. READ_ACL_UNSAFE = [ACL(Permissions.READ, ANYONE_ID_UNSAFE)]
  48. def make_digest_acl_credential(username, password):
  49. """Create a SHA1 digest credential"""
  50. credential = username.encode('utf-8') + b":" + password.encode('utf-8')
  51. cred_hash = b64encode(hashlib.sha1(credential).digest()).strip()
  52. return username + ":" + cred_hash.decode('utf-8')
  53. def make_acl(scheme, credential, read=False, write=False,
  54. create=False, delete=False, admin=False, all=False):
  55. """Given a scheme and credential, return an :class:`ACL` object
  56. appropriate for use with Kazoo.
  57. :param scheme: The scheme to use. I.e. `digest`.
  58. :param credential:
  59. A colon separated username, password. The password should be
  60. hashed with the `scheme` specified. The
  61. :meth:`make_digest_acl_credential` method will create and
  62. return a credential appropriate for use with the `digest`
  63. scheme.
  64. :param write: Write permission.
  65. :type write: bool
  66. :param create: Create permission.
  67. :type create: bool
  68. :param delete: Delete permission.
  69. :type delete: bool
  70. :param admin: Admin permission.
  71. :type admin: bool
  72. :param all: All permissions.
  73. :type all: bool
  74. :rtype: :class:`ACL`
  75. """
  76. if all:
  77. permissions = Permissions.ALL
  78. else:
  79. permissions = 0
  80. if read:
  81. permissions |= Permissions.READ
  82. if write:
  83. permissions |= Permissions.WRITE
  84. if create:
  85. permissions |= Permissions.CREATE
  86. if delete:
  87. permissions |= Permissions.DELETE
  88. if admin:
  89. permissions |= Permissions.ADMIN
  90. return ACL(permissions, Id(scheme, credential))
  91. def make_digest_acl(username, password, read=False, write=False,
  92. create=False, delete=False, admin=False, all=False):
  93. """Create a digest ACL for Zookeeper with the given permissions
  94. This method combines :meth:`make_digest_acl_credential` and
  95. :meth:`make_acl` to create an :class:`ACL` object appropriate for
  96. use with Kazoo's ACL methods.
  97. :param username: Username to use for the ACL.
  98. :param password: A plain-text password to hash.
  99. :param write: Write permission.
  100. :type write: bool
  101. :param create: Create permission.
  102. :type create: bool
  103. :param delete: Delete permission.
  104. :type delete: bool
  105. :param admin: Admin permission.
  106. :type admin: bool
  107. :param all: All permissions.
  108. :type all: bool
  109. :rtype: :class:`ACL`
  110. """
  111. cred = make_digest_acl_credential(username, password)
  112. return make_acl("digest", cred, read=read, write=write, create=create,
  113. delete=delete, admin=admin, all=all)