test_cert_info.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # coding: utf-8
  2. # Python libs
  3. from __future__ import absolute_import
  4. import logging
  5. # Salt libs
  6. import salt.beacons.cert_info as cert_info
  7. from tests.support.mixins import LoaderModuleMockMixin
  8. from tests.support.mock import mock_open, patch
  9. # Salt testing libs
  10. from tests.support.unit import TestCase
  11. log = logging.getLogger(__name__)
  12. _TEST_CERT = """
  13. -----BEGIN CERTIFICATE-----
  14. MIIC/jCCAeagAwIBAgIJAIQMfu6ShHvfMA0GCSqGSIb3DQEBCwUAMCQxIjAgBgNV
  15. BAMMGXNhbHR0ZXN0LTAxLmV4YW1wbGUubG9jYWwwHhcNMTkwNjAzMjA1OTIyWhcN
  16. MjkwNTMxMjA1OTIyWjAkMSIwIAYDVQQDDBlzYWx0dGVzdC0wMS5leGFtcGxlLmxv
  17. Y2FsMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAv5UxxKGsOO8n2hUk
  18. KjL8r2Rjt0om4wwdXUu0R1fQUlaSO0g+vk0wHHaovoVcEU6uZlhDPw1qZ4C+cp9Z
  19. rDzSfwI2Njg813I5jzTBgox+3pJ+82vgXZ14xpqZ+f0ACMo4uRPjBkyQpHqYiDJ3
  20. VockZSxm5s7RT05xDnedDfPgu1WAvzQovWO6slCs+Hlp8sh6QAy/hIwOZ0hT8y3J
  21. NV6PSPqK7BEypOPak36+ogtiuPxxat4da74SUVS8Ffupnr40BjqVqEXBvfIIHiQt
  22. 3r5gpjoBjrWX2ccgQlHQP8gFaToFxWLSSYVT6E8Oj5UEywpmvPDRjJsJ5epscblT
  23. oFyVXQIDAQABozMwMTAJBgNVHRMEAjAAMCQGA1UdEQQdMBuCGXNhbHR0ZXN0LTAx
  24. LmV4YW1wbGUubG9jYWwwDQYJKoZIhvcNAQELBQADggEBABPqQlkaZDV5dPwNO/s2
  25. PBT/19LroOwQ+fBJgZpbGha5/ZaSr+jcYZf2jAicPajWGlY/rXAdBSuxpmUYCC12
  26. 23tI4stwGyB8Quuoyg2Z+5LQJSDA1LxNJ1kxQfDUnS3tVQa0wJVtq8W9wNryNONL
  27. noaQaDcdbGx3V15W+Bx0as5NfIWqz1uVi4MGGxI6hMBuDD7E7M+k1db8EaS+tI4u
  28. seZBENjwjJA6zZmTXvYyzV5OBP4JyOhYuG9aqr7e6/yjPBEtZv0TJ9KMMbcywvE9
  29. 9FF+l4Y+wgKR/icrpDEpPlC4wYn64sy5vk7EGVagnVyhkjLJ52rn4trzyPox8FmO
  30. 2Zw=
  31. -----END CERTIFICATE-----
  32. """
  33. class CertInfoBeaconTestCase(TestCase, LoaderModuleMockMixin):
  34. """
  35. Test case for salt.beacons.cert_info
  36. """
  37. def setup_loader_modules(self):
  38. return {cert_info: {"__context__": {}, "__salt__": {}}}
  39. def test_non_list_config(self):
  40. config = {}
  41. ret = cert_info.validate(config)
  42. self.assertEqual(
  43. ret, (False, "Configuration for cert_info beacon must be a list.")
  44. )
  45. def test_empty_config(self):
  46. config = [{}]
  47. ret = cert_info.validate(config)
  48. self.assertEqual(
  49. ret,
  50. (False, "Configuration for cert_info beacon must contain files option."),
  51. )
  52. def test_cert_information(self):
  53. with patch("salt.utils.files.fopen", mock_open(read_data=_TEST_CERT)):
  54. config = [{"files": ["/etc/pki/tls/certs/mycert.pem"], "notify_days": -1}]
  55. ret = cert_info.validate(config)
  56. self.assertEqual(ret, (True, "Valid beacon configuration"))
  57. _expected_return = [
  58. {
  59. "certificates": [
  60. {
  61. "cert_path": "/etc/pki/tls/certs/mycert.pem",
  62. "extensions": [
  63. {
  64. "ext_data": "CA:FALSE",
  65. "ext_name": "basicConstraints",
  66. },
  67. {
  68. "ext_data": "DNS:salttest-01.example.local",
  69. "ext_name": "subjectAltName",
  70. },
  71. ],
  72. "has_expired": False,
  73. "issuer": 'CN="salttest-01.example.local"',
  74. "issuer_dict": {"CN": "salttest-01.example.local"},
  75. "notAfter": "2029-05-31 20:59:22Z",
  76. "notAfter_raw": "20290531205922Z",
  77. "notBefore": "2019-06-03 20:59:22Z",
  78. "notBefore_raw": "20190603205922Z",
  79. "serial_number": 9515119675852487647,
  80. "signature_algorithm": "sha256WithRSAEncryption",
  81. "subject": 'CN="salttest-01.example.local"',
  82. "subject_dict": {"CN": "salttest-01.example.local"},
  83. "version": 2,
  84. }
  85. ]
  86. }
  87. ]
  88. ret = cert_info.beacon(config)
  89. self.assertEqual(ret, _expected_return)