summaryrefslogtreecommitdiff
path: root/grpc/tools/run_tests/xds_k8s_test_driver/tests/security_test.py
blob: 58604b75feec57cf2a29b3cff55effebf0b04204 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# Copyright 2020 gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import uuid

from absl import flags
from absl.testing import absltest

from framework import xds_k8s_testcase

logger = logging.getLogger(__name__)
flags.adopt_module_key_flags(xds_k8s_testcase)

# Type aliases
_XdsTestServer = xds_k8s_testcase.XdsTestServer
_XdsTestClient = xds_k8s_testcase.XdsTestClient
_SecurityMode = xds_k8s_testcase.SecurityXdsKubernetesTestCase.SecurityMode


class SecurityTest(xds_k8s_testcase.SecurityXdsKubernetesTestCase):

    def test_mtls(self):
        """mTLS test.

        Both client and server configured to use TLS and mTLS.
        """
        self.setupTrafficDirectorGrpc()
        self.setupSecurityPolicies(server_tls=True,
                                   server_mtls=True,
                                   client_tls=True,
                                   client_mtls=True)

        test_server: _XdsTestServer = self.startSecureTestServer()
        self.setupServerBackends()
        test_client: _XdsTestClient = self.startSecureTestClient(test_server)

        self.assertTestAppSecurity(_SecurityMode.MTLS, test_client, test_server)
        self.assertSuccessfulRpcs(test_client)
        logger.info('[SUCCESS] mTLS security mode confirmed.')

    def test_tls(self):
        """TLS test.

        Both client and server configured to use TLS and not use mTLS.
        """
        self.setupTrafficDirectorGrpc()
        self.setupSecurityPolicies(server_tls=True,
                                   server_mtls=False,
                                   client_tls=True,
                                   client_mtls=False)

        test_server: _XdsTestServer = self.startSecureTestServer()
        self.setupServerBackends()
        test_client: _XdsTestClient = self.startSecureTestClient(test_server)

        self.assertTestAppSecurity(_SecurityMode.TLS, test_client, test_server)
        self.assertSuccessfulRpcs(test_client)
        logger.info('[SUCCESS] TLS security mode confirmed.')

    def test_plaintext_fallback(self):
        """Plain-text fallback test.

        Control plane provides no security config so both client and server
        fallback to plaintext based on fallback-credentials.
        """
        self.setupTrafficDirectorGrpc()
        self.setupSecurityPolicies(server_tls=False,
                                   server_mtls=False,
                                   client_tls=False,
                                   client_mtls=False)

        test_server: _XdsTestServer = self.startSecureTestServer()
        self.setupServerBackends()
        test_client: _XdsTestClient = self.startSecureTestClient(test_server)

        self.assertTestAppSecurity(_SecurityMode.PLAINTEXT, test_client,
                                   test_server)
        self.assertSuccessfulRpcs(test_client)
        logger.info('[SUCCESS] Plaintext security mode confirmed.')

    def test_mtls_error(self):
        """Negative test: mTLS Error.

        Server expects client mTLS cert, but client configured only for TLS.

        Note: because this is a negative test we need to make sure the mTLS
        failure happens after receiving the correct configuration at the
        client. To ensure that we will perform the following steps in that
        sequence:

        - Creation of a backendService, and attaching the backend (NEG)
        - Creation of the Server mTLS Policy, and attaching to the ECS
        - Creation of the Client TLS Policy, and attaching to the backendService
        - Creation of the urlMap, targetProxy, and forwardingRule

        With this sequence we are sure that when the client receives the
        endpoints of the backendService the security-config would also have
        been received as confirmed by the TD team.
        """
        # Create backend service
        self.td.setup_backend_for_grpc()

        # Start server and attach its NEGs to the backend service, but
        # until they become healthy.
        test_server: _XdsTestServer = self.startSecureTestServer()
        self.setupServerBackends(wait_for_healthy_status=False)

        # Setup policies and attach them.
        self.setupSecurityPolicies(server_tls=True,
                                   server_mtls=True,
                                   client_tls=True,
                                   client_mtls=False)

        # Create the routing rule map.
        self.td.setup_routing_rule_map_for_grpc(self.server_xds_host,
                                                self.server_xds_port)
        # Now that TD setup is complete, Backend Service can be populated
        # with healthy backends (NEGs).
        self.td.wait_for_backends_healthy_status()

        # Start the client, but don't wait for it to report a healthy channel.
        test_client: _XdsTestClient = self.startSecureTestClient(
            test_server, wait_for_active_server_channel=False)

        self.assertClientCannotReachServerRepeatedly(test_client)
        logger.info(
            "[SUCCESS] Client's connectivity state is consistent with a mTLS "
            "error caused by not presenting mTLS certificate to the server.")

    def test_server_authz_error(self):
        """Negative test: AuthZ error.

        Client does not authorize server because of mismatched SAN name.
        The order of operations is the same as in `test_mtls_error`.
        """
        # Create backend service
        self.td.setup_backend_for_grpc()

        # Start server and attach its NEGs to the backend service, but
        # until they become healthy.
        test_server: _XdsTestServer = self.startSecureTestServer()
        self.setupServerBackends(wait_for_healthy_status=False)

        # Regular TLS setup, but with client policy configured using
        # intentionality incorrect server_namespace.
        self.td.setup_server_security(server_namespace=self.server_namespace,
                                      server_name=self.server_name,
                                      server_port=self.server_port,
                                      tls=True,
                                      mtls=False)
        incorrect_namespace = f'incorrect-namespace-{uuid.uuid4().hex}'
        self.td.setup_client_security(server_namespace=incorrect_namespace,
                                      server_name=self.server_name,
                                      tls=True,
                                      mtls=False)

        # Create the routing rule map.
        self.td.setup_routing_rule_map_for_grpc(self.server_xds_host,
                                                self.server_xds_port)
        # Now that TD setup is complete, Backend Service can be populated
        # with healthy backends (NEGs).
        self.td.wait_for_backends_healthy_status()

        # Start the client, but don't wait for it to report a healthy channel.
        test_client: _XdsTestClient = self.startSecureTestClient(
            test_server, wait_for_active_server_channel=False)

        self.assertClientCannotReachServerRepeatedly(test_client)
        logger.info("[SUCCESS] Client's connectivity state is consistent with "
                    "AuthZ error caused by server presenting incorrect SAN.")


if __name__ == '__main__':
    absltest.main()