serialization.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # -*- coding: utf-8 -*-
  2. """
  3. celery.security.serialization
  4. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. Secure serializer.
  6. """
  7. from __future__ import absolute_import
  8. from __future__ import with_statement
  9. import base64
  10. from kombu.serialization import registry, encode, decode
  11. from kombu.utils.encoding import bytes_to_str, str_to_bytes
  12. from .certificate import Certificate, FSCertStore
  13. from .key import PrivateKey
  14. from .utils import reraise_errors
  15. def b64encode(s):
  16. return bytes_to_str(base64.b64encode(str_to_bytes(s)))
  17. def b64decode(s):
  18. return base64.b64decode(str_to_bytes(s))
  19. class SecureSerializer(object):
  20. def __init__(self, key=None, cert=None, cert_store=None,
  21. digest='sha1', serializer='json'):
  22. self._key = key
  23. self._cert = cert
  24. self._cert_store = cert_store
  25. self._digest = digest
  26. self._serializer = serializer
  27. def serialize(self, data):
  28. """serialize data structure into string"""
  29. assert self._key is not None
  30. assert self._cert is not None
  31. with reraise_errors('Unable to serialize: %r', (Exception, )):
  32. content_type, content_encoding, body = encode(
  33. data, serializer=self._serializer)
  34. # What we sign is the serialized body, not the body itself.
  35. # this way the receiver doesn't have to decode the contents
  36. # to verify the signature (and thus avoiding potential flaws
  37. # in the decoding step).
  38. return self._pack(body, content_type, content_encoding,
  39. signature=self._key.sign(body, self._digest),
  40. signer=self._cert.get_id())
  41. def deserialize(self, data):
  42. """deserialize data structure from string"""
  43. assert self._cert_store is not None
  44. with reraise_errors('Unable to deserialize: %r', (Exception, )):
  45. payload = self._unpack(data)
  46. signature, signer, body = (payload['signature'],
  47. payload['signer'],
  48. payload['body'])
  49. self._cert_store[signer].verify(body, signature, self._digest)
  50. return decode(body, payload['content_type'],
  51. payload['content_encoding'], force=True)
  52. def _pack(self, body, content_type, content_encoding, signer, signature,
  53. sep='\x00\x01'):
  54. return b64encode(sep.join([signer, signature,
  55. content_type, content_encoding, body]))
  56. def _unpack(self, payload, sep='\x00\x01',
  57. fields=('signer', 'signature', 'content_type',
  58. 'content_encoding', 'body')):
  59. return dict(zip(fields, b64decode(payload).split(sep)))
  60. def register_auth(key=None, cert=None, store=None, digest='sha1',
  61. serializer='json'):
  62. """register security serializer"""
  63. s = SecureSerializer(key and PrivateKey(key),
  64. cert and Certificate(cert),
  65. store and FSCertStore(store),
  66. digest=digest, serializer=serializer)
  67. registry.register('auth', s.serialize, s.deserialize,
  68. content_type='application/data',
  69. content_encoding='utf-8')