1
0

test_tls.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Wayne Werner <wwerner@saltstack.com>
  4. """
  5. # Import the future
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import os
  8. import tempfile
  9. # Salt Libs
  10. import salt.modules.cmdmod as cmd
  11. import salt.modules.file as file
  12. import salt.modules.tls as tls
  13. import salt.utils.files as files
  14. import salt.utils.stringutils as stringutils
  15. # Testing libs
  16. from tests.support.case import ModuleCase
  17. from tests.support.mixins import LoaderModuleMockMixin
  18. from tests.support.mock import MagicMock
  19. from tests.support.runtests import RUNTIME_VARS
  20. class TLSModuleTest(ModuleCase, LoaderModuleMockMixin):
  21. """
  22. Tests for salt.modules.tls
  23. """
  24. def setup_loader_modules(self):
  25. opts = {
  26. "cachedir": os.path.join(RUNTIME_VARS.TMP, "cache"),
  27. "test": True,
  28. }
  29. return {
  30. tls: {
  31. "__salt__": {
  32. "config.option": MagicMock(return_value=self.tempdir),
  33. "cmd.retcode": cmd.retcode,
  34. "pillar.get": MagicMock(return_value=False),
  35. "file.replace": file.replace,
  36. },
  37. "__opts__": opts,
  38. },
  39. file: {
  40. "__utils__": {
  41. "files.is_text": files.is_text,
  42. "stringutils.get_diff": stringutils.get_diff,
  43. },
  44. "__opts__": opts,
  45. },
  46. }
  47. @classmethod
  48. def setUpClass(cls):
  49. cls.ca_name = "roscivs"
  50. cls.tempdir = tempfile.mkdtemp(dir=RUNTIME_VARS.TMP)
  51. def test_ca_exists_should_be_False_before_ca_is_created(self):
  52. self.assertFalse(tls.ca_exists(self.ca_name))
  53. def test_ca_exists_should_be_True_after_ca_is_created(self):
  54. tls.create_ca(self.ca_name)
  55. self.assertTrue(tls.ca_exists(self.ca_name))
  56. def test_creating_csr_should_fail_with_no_ca(self):
  57. expected_message = (
  58. 'Certificate for CA named "bad_ca" does not exist,'
  59. " please create it first."
  60. )
  61. self.assertEqual(tls.create_csr(ca_name="bad_ca"), expected_message)
  62. def test_with_existing_ca_signing_csr_should_produce_valid_cert(self):
  63. print("Revoked should not be here")
  64. empty_crl_filename = os.path.join(self.tempdir, "empty.crl")
  65. tls.create_ca(self.ca_name)
  66. tls.create_csr(
  67. ca_name=self.ca_name, CN="testing.localhost",
  68. )
  69. tls.create_ca_signed_cert(
  70. ca_name=self.ca_name, CN="testing.localhost",
  71. )
  72. tls.create_empty_crl(
  73. ca_name=self.ca_name, crl_file=empty_crl_filename,
  74. )
  75. ret = tls.validate(
  76. cert=os.path.join(
  77. self.tempdir, self.ca_name, "certs", "testing.localhost.crt",
  78. ),
  79. ca_name=self.ca_name,
  80. crl_file=empty_crl_filename,
  81. )
  82. print("not there")
  83. self.assertTrue(ret["valid"], ret.get("error"))
  84. def test_revoked_cert_should_return_False_from_validate(self):
  85. revoked_crl_filename = os.path.join(self.tempdir, "revoked.crl")
  86. tls.create_ca(self.ca_name)
  87. tls.create_csr(
  88. ca_name=self.ca_name, CN="testing.bad.localhost",
  89. )
  90. tls.create_ca_signed_cert(
  91. ca_name=self.ca_name, CN="testing.bad.localhost",
  92. )
  93. tls.create_empty_crl(
  94. ca_name=self.ca_name, crl_file=revoked_crl_filename,
  95. )
  96. tls.revoke_cert(
  97. ca_name=self.ca_name,
  98. CN="testing.bad.localhost",
  99. crl_file=revoked_crl_filename,
  100. )
  101. self.assertFalse(
  102. tls.validate(
  103. cert=os.path.join(
  104. self.tempdir, self.ca_name, "certs", "testing.bad.localhost.crt",
  105. ),
  106. ca_name=self.ca_name,
  107. crl_file=revoked_crl_filename,
  108. )["valid"]
  109. )
  110. def test_validating_revoked_cert_with_no_crl_file_should_return_False(self):
  111. revoked_crl_filename = None
  112. tls.create_ca(self.ca_name)
  113. tls.create_csr(
  114. ca_name=self.ca_name, CN="testing.bad.localhost",
  115. )
  116. tls.create_ca_signed_cert(
  117. ca_name=self.ca_name, CN="testing.bad.localhost",
  118. )
  119. tls.create_empty_crl(
  120. ca_name=self.ca_name, crl_file=revoked_crl_filename,
  121. )
  122. tls.revoke_cert(
  123. ca_name=self.ca_name,
  124. CN="testing.bad.localhost",
  125. crl_file=revoked_crl_filename,
  126. )
  127. self.assertFalse(
  128. tls.validate(
  129. cert=os.path.join(
  130. self.tempdir, self.ca_name, "certs", "testing.bad.localhost.crt",
  131. ),
  132. ca_name=self.ca_name,
  133. crl_file=revoked_crl_filename,
  134. )["valid"]
  135. )