This file is indexed.

/usr/lib/python2.7/dist-packages/magnum/tests/unit/common/cert_manager/test_local.py is in python-magnum 3.1.1-5.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
279
280
281
282
283
284
# Copyright 2014 Rackspace US, 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 os

import mock
from oslo_config import cfg
from oslo_config import fixture as oslo_fixture

from magnum.common.cert_manager import cert_manager
from magnum.common.cert_manager import local_cert_manager
from magnum.common import exception
from magnum.tests import base


class TestLocalCert(base.BaseTestCase):

    def setUp(self):
        self.certificate = "My Certificate"
        self.intermediates = "My Intermediates"
        self.private_key = "My Private Key"
        self.private_key_passphrase = "My Private Key Passphrase"

        super(TestLocalCert, self).setUp()

    def test_local_cert(self):
        # Create a cert
        cert = local_cert_manager.Cert(
            certificate=self.certificate,
            intermediates=self.intermediates,
            private_key=self.private_key,
            private_key_passphrase=self.private_key_passphrase
        )

        # Validate the cert functions
        self.assertEqual(self.certificate, cert.get_certificate())
        self.assertEqual(self.intermediates, cert.get_intermediates())
        self.assertEqual(self.private_key, cert.get_private_key())
        self.assertEqual(self.private_key_passphrase,
                         cert.get_private_key_passphrase())


class TestLocalManager(base.BaseTestCase):

    def setUp(self):
        self.certificate = "My Certificate"
        self.intermediates = "My Intermediates"
        self.private_key = "My Private Key"
        self.private_key_passphrase = "My Private Key Passphrase"

        def _mock_isfile(path):
            _, ext = os.path.splitext(path)
            if self.intermediates is None and ext == '.int':
                return False
            if self.private_key_passphrase is None and ext == '.pass':
                return False
            return True
        isfile_patcher = mock.patch('os.path.isfile')
        self.mock_isfile = isfile_patcher.start()
        self.addCleanup(isfile_patcher.stop)
        self.mock_isfile.side_effect = _mock_isfile

        conf = oslo_fixture.Config(cfg.CONF)
        conf.config(group="certificates", storage_path="/tmp/")

        super(TestLocalManager, self).setUp()

    def _open_calls(self, cert_id, mode='w'):
        open_calls = []
        unexpected_calls = []
        for ext in ['crt', 'key', 'int', 'pass']:
            args = [os.path.join('/tmp/{0}.{1}'.format(cert_id, ext))]
            if mode:
                args.append(mode)

            call = mock.call(*args)
            if ext == 'int' and not self.intermediates:
                unexpected_calls.append(call)
            elif ext == 'pass' and not self.private_key_passphrase:
                unexpected_calls.append(call)
            else:
                open_calls.append(call)
        return open_calls, unexpected_calls

    def _write_calls(self):
        write_calls = [
            mock.call(self.certificate),
            mock.call(self.private_key),
        ]
        if self.intermediates:
            write_calls.append(mock.call(self.intermediates))
        if self.private_key_passphrase:
            write_calls.append(mock.call(self.private_key_passphrase))
        return write_calls

    def _store_cert(self):
        file_mock = mock.mock_open()
        # Attempt to store the cert
        with mock.patch('six.moves.builtins.open', file_mock, create=True):
            cert_id = local_cert_manager.CertManager.store_cert(
                certificate=self.certificate,
                intermediates=self.intermediates,
                private_key=self.private_key,
                private_key_passphrase=self.private_key_passphrase
            )

        # Check that something came back
        self.assertIsNotNone(cert_id)

        # Verify the correct files were opened
        open_calls, unexpected_calls = self._open_calls(cert_id)
        file_mock.assert_has_calls(open_calls, any_order=True)
        for unexpected_call in unexpected_calls:
            self.assertNotIn(unexpected_call, file_mock.mock_calls)

        # Verify the writes were made
        file_mock().write.assert_has_calls(self._write_calls(), any_order=True)

        return cert_id

    def _get_cert(self, cert_id):
        file_mock = mock.mock_open()
        # Attempt to retrieve the cert
        with mock.patch('six.moves.builtins.open', file_mock, create=True):
            data = local_cert_manager.CertManager.get_cert(cert_id)

        # Verify the correct files were opened
        open_calls, unexpected_calls = self._open_calls(cert_id, 'r')
        file_mock.assert_has_calls(open_calls, any_order=True)
        for unexpected_call in unexpected_calls:
            self.assertNotIn(unexpected_call, file_mock.mock_calls)

        # The returned data should be a Cert object
        self.assertIsInstance(data, cert_manager.Cert)

        return data

    def _get_cert_with_fail(self, cert_id, failed='crt'):
        def fake_open(path, mode):
            if path == os.path.join('/tmp/{0}.{1}'.format(cert_id, failed)):
                raise IOError()
            return mock.DEFAULT

        file_mock = mock.mock_open()
        file_mock.side_effect = fake_open
        # Attempt to retrieve the cert
        with mock.patch('six.moves.builtins.open', file_mock, create=True):
            self.assertRaises(
                exception.CertificateStorageException,
                local_cert_manager.CertManager.get_cert,
                cert_id
            )

    def _delete_cert(self, cert_id):
        remove_mock = mock.Mock()
        # Delete the cert
        with mock.patch('os.remove', remove_mock):
            local_cert_manager.CertManager.delete_cert(cert_id)

        open_calls, unexpected_calls = self._open_calls(cert_id, mode=None)
        # Verify the correct files were removed
        remove_mock.assert_has_calls(open_calls, any_order=True)
        for unexpected_call in unexpected_calls:
            self.assertNotIn(unexpected_call, remove_mock.mock_calls)

    def _delete_cert_with_fail(self, cert_id):
        remove_mock = mock.Mock()
        remove_mock.side_effect = IOError
        # Delete the cert
        with mock.patch('os.remove', remove_mock):
            self.assertRaises(
                exception.CertificateStorageException,
                local_cert_manager.CertManager.delete_cert,
                cert_id
            )

    def test_store_cert(self):
        self._store_cert()

    @mock.patch('six.moves.builtins.open', create=True)
    def test_store_cert_with_io_error(self, file_mock):
        file_mock.side_effect = IOError

        self.assertRaises(
            exception.CertificateStorageException,
            local_cert_manager.CertManager.store_cert,
            certificate=self.certificate,
            intermediates=self.intermediates,
            private_key=self.private_key,
            private_key_passphrase=self.private_key_passphrase
        )

    def test_get_cert(self):
        # Store a cert
        cert_id = self._store_cert()

        # Get the cert
        self._get_cert(cert_id)

    def test_get_cert_with_loading_cert_fail(self):
        # Store a cert
        cert_id = self._store_cert()

        self._get_cert_with_fail(cert_id, failed='crt')

    def test_get_cert_with_loading_private_key_fail(self):
        # Store a cert
        cert_id = self._store_cert()

        self._get_cert_with_fail(cert_id, failed='key')

    def test_get_cert_with_loading_intermediates_fail(self):
        # Store a cert
        cert_id = self._store_cert()

        self._get_cert_with_fail(cert_id, failed='int')

    def test_get_cert_with_loading_pkp_fail(self):
        # Store a cert
        cert_id = self._store_cert()

        self._get_cert_with_fail(cert_id, failed='pass')

    def test_get_cert_without_intermediate(self):
        self.intermediates = None
        # Store a cert
        cert_id = self._store_cert()

        # Get the cert
        self._get_cert(cert_id)

    def test_get_cert_without_pkp(self):
        self.private_key_passphrase = None
        # Store a cert
        cert_id = self._store_cert()

        # Get the cert
        self._get_cert(cert_id)

    def test_delete_cert(self):
        # Store a cert
        cert_id = self._store_cert()

        # Verify the cert exists
        self._get_cert(cert_id)

        # Delete the cert
        self._delete_cert(cert_id)

    def test_delete_cert_with_fail(self):
        # Store a cert
        cert_id = self._store_cert()

        # Verify the cert exists
        self._get_cert(cert_id)

        # Delete the cert with fail
        self._delete_cert_with_fail(cert_id)

    def test_delete_cert_without_intermediate(self):
        self.intermediates = None
        # Store a cert
        cert_id = self._store_cert()

        # Delete the cert with fail
        self._delete_cert_with_fail(cert_id)

    def test_delete_cert_without_pkp(self):
        self.private_key_passphrase = None
        # Store a cert
        cert_id = self._store_cert()

        # Delete the cert with fail
        self._delete_cert_with_fail(cert_id)