summaryrefslogtreecommitdiff
path: root/nova/tests/unit/console/securityproxy/test_rfb.py
blob: 3eb8ba6acf0e7e6dc5de4e0035531f8ef7b6a658 (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# Copyright (c) 2014-2016 Red Hat, Inc
# All Rights Reserved.
#
#    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 the Console Security Proxy Framework."""

import mock

from nova.console.rfb import auth
from nova.console.rfb import authnone
from nova.console.securityproxy import rfb
from nova import exception
from nova import test


class RFBSecurityProxyTestCase(test.NoDBTestCase):
    """Test case for the base RFBSecurityProxy."""

    def setUp(self):
        super(RFBSecurityProxyTestCase, self).setUp()
        self.manager = mock.Mock()
        self.tenant_sock = mock.Mock()
        self.compute_sock = mock.Mock()

        self.tenant_sock.recv.side_effect = []
        self.compute_sock.recv.side_effect = []

        self.expected_manager_calls = []
        self.expected_tenant_calls = []
        self.expected_compute_calls = []

        self.proxy = rfb.RFBSecurityProxy()

    def _assert_expected_calls(self):
        self.assertEqual(self.expected_manager_calls,
                         self.manager.mock_calls)
        self.assertEqual(self.expected_tenant_calls,
                         self.tenant_sock.mock_calls)
        self.assertEqual(self.expected_compute_calls,
                         self.compute_sock.mock_calls)

    def _version_handshake(self):
        full_version_str = "RFB 003.008\n"

        self._expect_compute_recv(auth.VERSION_LENGTH, full_version_str)
        self._expect_compute_send(full_version_str)

        self._expect_tenant_send(full_version_str)
        self._expect_tenant_recv(auth.VERSION_LENGTH, full_version_str)

    def _to_binary(self, val):
        if not isinstance(val, bytes):
            val = bytes(val, 'utf-8')
        return val

    def _expect_tenant_send(self, val):
        val = self._to_binary(val)
        self.expected_tenant_calls.append(mock.call.sendall(val))

    def _expect_compute_send(self, val):
        val = self._to_binary(val)
        self.expected_compute_calls.append(mock.call.sendall(val))

    def _expect_tenant_recv(self, amt, ret_val):
        ret_val = self._to_binary(ret_val)
        self.expected_tenant_calls.append(mock.call.recv(amt))
        self.tenant_sock.recv.side_effect = (
            list(self.tenant_sock.recv.side_effect) + [ret_val])

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

    def test_fail(self):
        """Validate behavior for invalid initial message from tenant.

        The spec defines the sequence that should be used in the handshaking
        process. Anything outside of this is invalid.
        """
        self._expect_tenant_send("\x00\x00\x00\x01\x00\x00\x00\x04blah")

        self.proxy._fail(self.tenant_sock, None, 'blah')

        self._assert_expected_calls()

    def test_fail_server_message(self):
        """Validate behavior for invalid initial message from server.

        The spec defines the sequence that should be used in the handshaking
        process. Anything outside of this is invalid.
        """
        self._expect_tenant_send("\x00\x00\x00\x01\x00\x00\x00\x04blah")
        self._expect_compute_send("\x00")

        self.proxy._fail(self.tenant_sock, self.compute_sock, 'blah')

        self._assert_expected_calls()

    def test_parse_version(self):
        """Validate behavior of version parser."""
        res = self.proxy._parse_version("RFB 012.034\n")
        self.assertEqual(12.34, res)

    def test_fails_on_compute_version(self):
        """Validate behavior for unsupported compute RFB version.

        We only support RFB protocol version 3.8.
        """
        for full_version_str in ["RFB 003.007\n", "RFB 003.009\n"]:
            self._expect_compute_recv(auth.VERSION_LENGTH, full_version_str)

            ex = self.assertRaises(exception.SecurityProxyNegotiationFailed,
                                   self.proxy.connect,
                                   self.tenant_sock,
                                   self.compute_sock)
            self.assertIn('version 3.8, but server', str(ex))
            self._assert_expected_calls()

    def test_fails_on_tenant_version(self):
        """Validate behavior for unsupported tenant RFB version.

        We only support RFB protocol version 3.8.
        """
        full_version_str = "RFB 003.008\n"

        for full_version_str_invalid in ["RFB 003.007\n", "RFB 003.009\n"]:
            self._expect_compute_recv(auth.VERSION_LENGTH, full_version_str)
            self._expect_compute_send(full_version_str)

            self._expect_tenant_send(full_version_str)
            self._expect_tenant_recv(auth.VERSION_LENGTH,
                                     full_version_str_invalid)

            ex = self.assertRaises(exception.SecurityProxyNegotiationFailed,
                                   self.proxy.connect,
                                   self.tenant_sock,
                                   self.compute_sock)
            self.assertIn('version 3.8, but tenant', str(ex))
            self._assert_expected_calls()

    def test_fails_on_sec_type_cnt_zero(self):
        """Validate behavior if a server returns 0 supported security types.

        This indicates a random issue and the cause of that issues should be
        decoded and reported in the exception.
        """
        self.proxy._fail = mock.Mock()

        self._version_handshake()

        self._expect_compute_recv(1, "\x00")
        self._expect_compute_recv(4, "\x00\x00\x00\x06")
        self._expect_compute_recv(6, "cheese")
        self._expect_tenant_send("\x00\x00\x00\x00\x06cheese")

        ex = self.assertRaises(exception.SecurityProxyNegotiationFailed,
                               self.proxy.connect,
                               self.tenant_sock,
                               self.compute_sock)
        self.assertIn('cheese', str(ex))

        self._assert_expected_calls()

    @mock.patch.object(authnone.RFBAuthSchemeNone, "security_handshake")
    def test_full_run(self, mock_handshake):
        """Validate correct behavior."""
        new_sock = mock.MagicMock()
        mock_handshake.return_value = new_sock

        self._version_handshake()

        self._expect_compute_recv(1, "\x02")
        self._expect_compute_recv(2, "\x01\x02")

        self._expect_tenant_send("\x01\x01")
        self._expect_tenant_recv(1, "\x01")

        self._expect_compute_send("\x01")

        self.assertEqual(new_sock, self.proxy.connect(
            self.tenant_sock, self.compute_sock))

        mock_handshake.assert_called_once_with(self.compute_sock)
        self._assert_expected_calls()

    def test_client_auth_invalid_fails(self):
        """Validate behavior if no security types are supported."""
        self.proxy._fail = self.manager.proxy._fail
        self.proxy.security_handshake = self.manager.proxy.security_handshake

        self._version_handshake()

        self._expect_compute_recv(1, "\x02")
        self._expect_compute_recv(2, "\x01\x02")

        self._expect_tenant_send("\x01\x01")
        self._expect_tenant_recv(1, "\x02")

        self.expected_manager_calls.append(
            mock.call.proxy._fail(
                self.tenant_sock, self.compute_sock,
                "Only the security type 1 (NONE) is supported",
            )
        )

        self.assertRaises(exception.SecurityProxyNegotiationFailed,
                          self.proxy.connect,
                          self.tenant_sock,
                          self.compute_sock)
        self._assert_expected_calls()

    def test_exception_in_choose_security_type_fails(self):
        """Validate behavior if a given security type isn't supported."""
        self.proxy._fail = self.manager.proxy._fail
        self.proxy.security_handshake = self.manager.proxy.security_handshake

        self._version_handshake()

        self._expect_compute_recv(1, "\x02")
        self._expect_compute_recv(2, "\x02\x05")

        self._expect_tenant_send("\x01\x01")
        self._expect_tenant_recv(1, "\x01")

        self.expected_manager_calls.extend([
            mock.call.proxy._fail(
                self.tenant_sock, self.compute_sock,
                'Unable to negotiate security with server')])

        self.assertRaises(exception.SecurityProxyNegotiationFailed,
                          self.proxy.connect,
                          self.tenant_sock,
                          self.compute_sock)

        self._assert_expected_calls()

    @mock.patch.object(authnone.RFBAuthSchemeNone, "security_handshake")
    def test_exception_security_handshake_fails(self, mock_auth):
        """Validate behavior if the security handshake fails for any reason."""
        self.proxy._fail = self.manager.proxy._fail

        self._version_handshake()

        self._expect_compute_recv(1, "\x02")
        self._expect_compute_recv(2, "\x01\x02")

        self._expect_tenant_send("\x01\x01")
        self._expect_tenant_recv(1, "\x01")

        self._expect_compute_send("\x01")

        ex = exception.RFBAuthHandshakeFailed(reason="crackers")
        mock_auth.side_effect = ex

        self.expected_manager_calls.extend([
            mock.call.proxy._fail(self.tenant_sock, None,
                                  'Unable to negotiate security with server')])

        self.assertRaises(exception.SecurityProxyNegotiationFailed,
                          self.proxy.connect,
                          self.tenant_sock,
                          self.compute_sock)

        mock_auth.assert_called_once_with(self.compute_sock)
        self._assert_expected_calls()