aboutsummaryrefslogtreecommitdiff
path: root/src/python/grpcio_tests/tests/unit/_xds_credentials_test.py
blob: 6d8b2b6a04199f10e89d803c7615568084e8e7ca (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
# Copyright 2021 The 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.
"""Tests xDS server and channel credentials."""

from concurrent import futures
import contextlib
import logging
import unittest

import grpc
import grpc.experimental

from tests.unit import resources
from tests.unit import test_common


class _GenericHandler(grpc.GenericRpcHandler):
    def service(self, handler_call_details):
        return grpc.unary_unary_rpc_method_handler(
            lambda request, unused_context: request
        )


@contextlib.contextmanager
def xds_channel_server_without_xds(server_fallback_creds):
    server = grpc.server(futures.ThreadPoolExecutor())
    server.add_generic_rpc_handlers((_GenericHandler(),))
    server_server_fallback_creds = grpc.ssl_server_credentials(
        ((resources.private_key(), resources.certificate_chain()),)
    )
    server_creds = grpc.xds_server_credentials(server_fallback_creds)
    port = server.add_secure_port("localhost:0", server_creds)
    server.start()
    try:
        yield "localhost:{}".format(port)
    finally:
        server.stop(None)


class XdsCredentialsTest(unittest.TestCase):
    def test_xds_creds_fallback_ssl(self):
        # Since there is no xDS server, the fallback credentials will be used.
        # In this case, SSL credentials.
        server_fallback_creds = grpc.ssl_server_credentials(
            ((resources.private_key(), resources.certificate_chain()),)
        )
        with xds_channel_server_without_xds(
            server_fallback_creds
        ) as server_address:
            override_options = (
                ("grpc.ssl_target_name_override", "foo.test.google.fr"),
            )
            channel_fallback_creds = grpc.ssl_channel_credentials(
                root_certificates=resources.test_root_certificates(),
                private_key=resources.private_key(),
                certificate_chain=resources.certificate_chain(),
            )
            channel_creds = grpc.xds_channel_credentials(channel_fallback_creds)
            with grpc.secure_channel(
                server_address, channel_creds, options=override_options
            ) as channel:
                request = b"abc"
                response = channel.unary_unary(
                    "/test/method",
                    _registered_method=True,
                )(request, wait_for_ready=True)
                self.assertEqual(response, request)

    def test_xds_creds_fallback_insecure(self):
        # Since there is no xDS server, the fallback credentials will be used.
        # In this case, insecure.
        server_fallback_creds = grpc.insecure_server_credentials()
        with xds_channel_server_without_xds(
            server_fallback_creds
        ) as server_address:
            channel_fallback_creds = (
                grpc.experimental.insecure_channel_credentials()
            )
            channel_creds = grpc.xds_channel_credentials(channel_fallback_creds)
            with grpc.secure_channel(server_address, channel_creds) as channel:
                request = b"abc"
                response = channel.unary_unary(
                    "/test/method",
                    _registered_method=True,
                )(request, wait_for_ready=True)
                self.assertEqual(response, request)

    def test_start_xds_server(self):
        server = grpc.server(futures.ThreadPoolExecutor(), xds=True)
        server.add_generic_rpc_handlers((_GenericHandler(),))
        server_fallback_creds = grpc.insecure_server_credentials()
        server_creds = grpc.xds_server_credentials(server_fallback_creds)
        port = server.add_secure_port("localhost:0", server_creds)
        server.start()
        server.stop(None)
        # No exceptions thrown. A more comprehensive suite of tests will be
        # provided by the interop tests.


if __name__ == "__main__":
    logging.basicConfig()
    unittest.main()