Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

69 рядки
2.6 KiB

  1. # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
  2. # Copyright (C) 2003-2007, 2009-2011 Nominum, Inc.
  3. #
  4. # Permission to use, copy, modify, and distribute this software and its
  5. # documentation for any purpose with or without fee is hereby granted,
  6. # provided that the above copyright notice and this permission notice
  7. # appear in all copies.
  8. #
  9. # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
  10. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
  12. # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  15. # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. """A place to store TSIG keys."""
  17. import base64
  18. from typing import Any, Dict
  19. import dns.name
  20. import dns.tsig
  21. def from_text(textring: Dict[str, Any]) -> Dict[dns.name.Name, dns.tsig.Key]:
  22. """Convert a dictionary containing (textual DNS name, base64 secret)
  23. pairs into a binary keyring which has (dns.name.Name, bytes) pairs, or
  24. a dictionary containing (textual DNS name, (algorithm, base64 secret))
  25. pairs into a binary keyring which has (dns.name.Name, dns.tsig.Key) pairs.
  26. @rtype: dict"""
  27. keyring = {}
  28. for name, value in textring.items():
  29. kname = dns.name.from_text(name)
  30. if isinstance(value, str):
  31. keyring[kname] = dns.tsig.Key(kname, value).secret
  32. else:
  33. (algorithm, secret) = value
  34. keyring[kname] = dns.tsig.Key(kname, secret, algorithm)
  35. return keyring
  36. def to_text(keyring: Dict[dns.name.Name, Any]) -> Dict[str, Any]:
  37. """Convert a dictionary containing (dns.name.Name, dns.tsig.Key) pairs
  38. into a text keyring which has (textual DNS name, (textual algorithm,
  39. base64 secret)) pairs, or a dictionary containing (dns.name.Name, bytes)
  40. pairs into a text keyring which has (textual DNS name, base64 secret) pairs.
  41. @rtype: dict"""
  42. textring = {}
  43. def b64encode(secret):
  44. return base64.encodebytes(secret).decode().rstrip()
  45. for name, key in keyring.items():
  46. tname = name.to_text()
  47. if isinstance(key, bytes):
  48. textring[tname] = b64encode(key)
  49. else:
  50. if isinstance(key.secret, bytes):
  51. text_secret = b64encode(key.secret)
  52. else:
  53. text_secret = str(key.secret)
  54. textring[tname] = (key.algorithm.to_text(), text_secret)
  55. return textring