summaryrefslogtreecommitdiff
path: root/nova/tests/unit/console/rfb/test_authvencrypt.py
blob: de9bccb44aa878671e94ac979385b40858c0ede3 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# Copyright (c) 2016 Red Hat, Inc
#
# 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 ssl
import struct
from unittest import mock


from nova.console.rfb import auth
from nova.console.rfb import authvencrypt
from nova import exception
from nova import test


class RFBAuthSchemeVeNCryptTestCase(test.NoDBTestCase):

    def setUp(self):
        super(RFBAuthSchemeVeNCryptTestCase, self).setUp()

        self.scheme = authvencrypt.RFBAuthSchemeVeNCrypt()
        self.compute_sock = mock.MagicMock()

        self.compute_sock.recv.side_effect = []

        self.expected_calls = []

        self.flags(vencrypt_ca_certs="/certs/ca.pem", group="vnc")

    def _expect_send(self, val):
        self.expected_calls.append(mock.call.sendall(val))

    def _expect_recv(self, amt, ret_val):
        self.expected_calls.append(mock.call.recv(amt))
        self.compute_sock.recv.side_effect = (
            list(self.compute_sock.recv.side_effect) + [ret_val])

    @mock.patch.object(ssl, "wrap_socket", return_value="wrapped")
    def test_security_handshake_with_x509(self, mock_socket):
        self.flags(vencrypt_client_key='/certs/keyfile',
                   vencrypt_client_cert='/certs/cert.pem',
                   group="vnc")

        self._expect_recv(1, "\x00")
        self._expect_recv(1, "\x02")

        self._expect_send(b"\x00\x02")
        self._expect_recv(1, "\x00")

        self._expect_recv(1, "\x02")
        subtypes_raw = [authvencrypt.AuthVeNCryptSubtype.X509NONE,
                        authvencrypt.AuthVeNCryptSubtype.X509VNC]
        subtypes = struct.pack('!2I', *subtypes_raw)
        self._expect_recv(8, subtypes)

        self._expect_send(struct.pack('!I', subtypes_raw[0]))

        self._expect_recv(1, "\x01")

        self.assertEqual("wrapped", self.scheme.security_handshake(
            self.compute_sock))

        mock_socket.assert_called_once_with(
            self.compute_sock,
            keyfile='/certs/keyfile',
            certfile='/certs/cert.pem',
            server_side=False,
            cert_reqs=ssl.CERT_REQUIRED,
            ca_certs='/certs/ca.pem')

        self.assertEqual(self.expected_calls, self.compute_sock.mock_calls)

    @mock.patch.object(ssl, "wrap_socket", return_value="wrapped")
    def test_security_handshake_without_x509(self, mock_socket):
        self._expect_recv(1, "\x00")
        self._expect_recv(1, "\x02")

        self._expect_send(b"\x00\x02")
        self._expect_recv(1, "\x00")

        self._expect_recv(1, "\x02")
        subtypes_raw = [authvencrypt.AuthVeNCryptSubtype.X509NONE,
                        authvencrypt.AuthVeNCryptSubtype.X509VNC]
        subtypes = struct.pack('!2I', *subtypes_raw)
        self._expect_recv(8, subtypes)

        self._expect_send(struct.pack('!I', subtypes_raw[0]))

        self._expect_recv(1, "\x01")

        self.assertEqual("wrapped", self.scheme.security_handshake(
            self.compute_sock))
        mock_socket.assert_called_once_with(
            self.compute_sock,
            keyfile=None,
            certfile=None,
            server_side=False,
            cert_reqs=ssl.CERT_REQUIRED,
            ca_certs='/certs/ca.pem'
        )

        self.assertEqual(self.expected_calls, self.compute_sock.mock_calls)

    def _test_security_handshake_fails(self):
        self.assertRaises(exception.RFBAuthHandshakeFailed,
                          self.scheme.security_handshake,
                          self.compute_sock)
        self.assertEqual(self.expected_calls, self.compute_sock.mock_calls)

    def test_security_handshake_fails_on_low_version(self):
        self._expect_recv(1, "\x00")
        self._expect_recv(1, "\x01")

        self._test_security_handshake_fails()

    def test_security_handshake_fails_on_cant_use_version(self):
        self._expect_recv(1, "\x00")
        self._expect_recv(1, "\x02")

        self._expect_send(b"\x00\x02")
        self._expect_recv(1, "\x01")

        self._test_security_handshake_fails()

    def test_security_handshake_fails_on_missing_subauth(self):
        self._expect_recv(1, "\x00")
        self._expect_recv(1, "\x02")

        self._expect_send(b"\x00\x02")
        self._expect_recv(1, "\x00")

        self._expect_recv(1, "\x01")
        subtypes_raw = [authvencrypt.AuthVeNCryptSubtype.X509VNC]
        subtypes = struct.pack('!I', *subtypes_raw)
        self._expect_recv(4, subtypes)

        self._test_security_handshake_fails()

    def test_security_handshake_fails_on_auth_not_accepted(self):
        self._expect_recv(1, "\x00")
        self._expect_recv(1, "\x02")

        self._expect_send(b"\x00\x02")
        self._expect_recv(1, "\x00")

        self._expect_recv(1, "\x02")
        subtypes_raw = [authvencrypt.AuthVeNCryptSubtype.X509NONE,
                        authvencrypt.AuthVeNCryptSubtype.X509VNC]
        subtypes = struct.pack('!2I', *subtypes_raw)
        self._expect_recv(8, subtypes)

        self._expect_send(struct.pack('!I', subtypes_raw[0]))

        self._expect_recv(1, "\x00")

        self._test_security_handshake_fails()

    @mock.patch.object(ssl, "wrap_socket")
    def test_security_handshake_fails_on_ssl_failure(self, mock_socket):
        self._expect_recv(1, "\x00")
        self._expect_recv(1, "\x02")

        self._expect_send(b"\x00\x02")
        self._expect_recv(1, "\x00")

        self._expect_recv(1, "\x02")
        subtypes_raw = [authvencrypt.AuthVeNCryptSubtype.X509NONE,
                        authvencrypt.AuthVeNCryptSubtype.X509VNC]
        subtypes = struct.pack('!2I', *subtypes_raw)
        self._expect_recv(8, subtypes)

        self._expect_send(struct.pack('!I', subtypes_raw[0]))

        self._expect_recv(1, "\x01")

        mock_socket.side_effect = ssl.SSLError("cheese")

        self._test_security_handshake_fails()

        mock_socket.assert_called_once_with(
            self.compute_sock,
            keyfile=None,
            certfile=None,
            server_side=False,
            cert_reqs=ssl.CERT_REQUIRED,
            ca_certs='/certs/ca.pem'
        )

    def test_types(self):
        scheme = authvencrypt.RFBAuthSchemeVeNCrypt()

        self.assertEqual(auth.AuthType.VENCRYPT,
                         scheme.security_type())