security_test.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. # Copyright 2020 gRPC authors.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import logging
  15. from absl import flags
  16. from absl.testing import absltest
  17. from framework import xds_k8s_testcase
  18. from framework.helpers import rand
  19. from framework.helpers import skips
  20. logger = logging.getLogger(__name__)
  21. flags.adopt_module_key_flags(xds_k8s_testcase)
  22. # Type aliases
  23. _XdsTestServer = xds_k8s_testcase.XdsTestServer
  24. _XdsTestClient = xds_k8s_testcase.XdsTestClient
  25. _SecurityMode = xds_k8s_testcase.SecurityXdsKubernetesTestCase.SecurityMode
  26. class SecurityTest(xds_k8s_testcase.SecurityXdsKubernetesTestCase):
  27. @staticmethod
  28. def isSupported(config: skips.TestConfig) -> bool:
  29. if config.client_lang in ['cpp', 'python', 'go']:
  30. return config.version_ge('v1.41.x')
  31. return False
  32. def test_mtls(self):
  33. """mTLS test.
  34. Both client and server configured to use TLS and mTLS.
  35. """
  36. self.setupTrafficDirectorGrpc()
  37. self.setupSecurityPolicies(server_tls=True,
  38. server_mtls=True,
  39. client_tls=True,
  40. client_mtls=True)
  41. test_server: _XdsTestServer = self.startSecureTestServer()
  42. self.setupServerBackends()
  43. test_client: _XdsTestClient = self.startSecureTestClient(test_server)
  44. self.assertTestAppSecurity(_SecurityMode.MTLS, test_client, test_server)
  45. self.assertSuccessfulRpcs(test_client)
  46. logger.info('[SUCCESS] mTLS security mode confirmed.')
  47. def test_tls(self):
  48. """TLS test.
  49. Both client and server configured to use TLS and not use mTLS.
  50. """
  51. self.setupTrafficDirectorGrpc()
  52. self.setupSecurityPolicies(server_tls=True,
  53. server_mtls=False,
  54. client_tls=True,
  55. client_mtls=False)
  56. test_server: _XdsTestServer = self.startSecureTestServer()
  57. self.setupServerBackends()
  58. test_client: _XdsTestClient = self.startSecureTestClient(test_server)
  59. self.assertTestAppSecurity(_SecurityMode.TLS, test_client, test_server)
  60. self.assertSuccessfulRpcs(test_client)
  61. logger.info('[SUCCESS] TLS security mode confirmed.')
  62. def test_plaintext_fallback(self):
  63. """Plain-text fallback test.
  64. Control plane provides no security config so both client and server
  65. fallback to plaintext based on fallback-credentials.
  66. """
  67. self.setupTrafficDirectorGrpc()
  68. self.setupSecurityPolicies(server_tls=False,
  69. server_mtls=False,
  70. client_tls=False,
  71. client_mtls=False)
  72. test_server: _XdsTestServer = self.startSecureTestServer()
  73. self.setupServerBackends()
  74. test_client: _XdsTestClient = self.startSecureTestClient(test_server)
  75. self.assertTestAppSecurity(_SecurityMode.PLAINTEXT, test_client,
  76. test_server)
  77. self.assertSuccessfulRpcs(test_client)
  78. logger.info('[SUCCESS] Plaintext security mode confirmed.')
  79. def test_mtls_error(self):
  80. """Negative test: mTLS Error.
  81. Server expects client mTLS cert, but client configured only for TLS.
  82. Note: because this is a negative test we need to make sure the mTLS
  83. failure happens after receiving the correct configuration at the
  84. client. To ensure that we will perform the following steps in that
  85. sequence:
  86. - Creation of a backendService, and attaching the backend (NEG)
  87. - Creation of the Server mTLS Policy, and attaching to the ECS
  88. - Creation of the Client TLS Policy, and attaching to the backendService
  89. - Creation of the urlMap, targetProxy, and forwardingRule
  90. With this sequence we are sure that when the client receives the
  91. endpoints of the backendService the security-config would also have
  92. been received as confirmed by the TD team.
  93. """
  94. # Create backend service
  95. self.td.setup_backend_for_grpc(
  96. health_check_port=self.server_maintenance_port)
  97. # Start server and attach its NEGs to the backend service, but
  98. # until they become healthy.
  99. test_server: _XdsTestServer = self.startSecureTestServer()
  100. self.setupServerBackends(wait_for_healthy_status=False)
  101. # Setup policies and attach them.
  102. self.setupSecurityPolicies(server_tls=True,
  103. server_mtls=True,
  104. client_tls=True,
  105. client_mtls=False)
  106. # Create the routing rule map.
  107. self.td.setup_routing_rule_map_for_grpc(self.server_xds_host,
  108. self.server_xds_port)
  109. # Now that TD setup is complete, Backend Service can be populated
  110. # with healthy backends (NEGs).
  111. self.td.wait_for_backends_healthy_status()
  112. # Start the client, but don't wait for it to report a healthy channel.
  113. test_client: _XdsTestClient = self.startSecureTestClient(
  114. test_server, wait_for_active_server_channel=False)
  115. self.assertClientCannotReachServerRepeatedly(test_client)
  116. logger.info(
  117. "[SUCCESS] Client's connectivity state is consistent with a mTLS "
  118. "error caused by not presenting mTLS certificate to the server.")
  119. def test_server_authz_error(self):
  120. """Negative test: AuthZ error.
  121. Client does not authorize server because of mismatched SAN name.
  122. The order of operations is the same as in `test_mtls_error`.
  123. """
  124. # Create backend service
  125. self.td.setup_backend_for_grpc(
  126. health_check_port=self.server_maintenance_port)
  127. # Start server and attach its NEGs to the backend service, but
  128. # until they become healthy.
  129. test_server: _XdsTestServer = self.startSecureTestServer()
  130. self.setupServerBackends(wait_for_healthy_status=False)
  131. # Regular TLS setup, but with client policy configured using
  132. # intentionality incorrect server_namespace.
  133. self.td.setup_server_security(server_namespace=self.server_namespace,
  134. server_name=self.server_name,
  135. server_port=self.server_port,
  136. tls=True,
  137. mtls=False)
  138. incorrect_namespace = f'incorrect-namespace-{rand.rand_string()}'
  139. self.td.setup_client_security(server_namespace=incorrect_namespace,
  140. server_name=self.server_name,
  141. tls=True,
  142. mtls=False)
  143. # Create the routing rule map.
  144. self.td.setup_routing_rule_map_for_grpc(self.server_xds_host,
  145. self.server_xds_port)
  146. # Now that TD setup is complete, Backend Service can be populated
  147. # with healthy backends (NEGs).
  148. self.td.wait_for_backends_healthy_status()
  149. # Start the client, but don't wait for it to report a healthy channel.
  150. test_client: _XdsTestClient = self.startSecureTestClient(
  151. test_server, wait_for_active_server_channel=False)
  152. self.assertClientCannotReachServerRepeatedly(test_client)
  153. logger.info("[SUCCESS] Client's connectivity state is consistent with "
  154. "AuthZ error caused by server presenting incorrect SAN.")
  155. if __name__ == '__main__':
  156. absltest.main()