This file is indexed.

/usr/lib/python3/dist-packages/castellan/tests/unit/key_manager/test_migration_key_manager.py is in python3-castellan 0.17.0-0ubuntu1.

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
# Copyright 2017 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.

"""
Test cases for the migration key manager.
"""

import binascii
import mock

from oslo_config import cfg

from castellan.common import exception
from castellan.common.objects import symmetric_key as key
from castellan import key_manager
from castellan.key_manager import not_implemented_key_manager
from castellan.tests.unit.key_manager import test_key_manager

CONF = cfg.CONF


class ConfKeyManager(not_implemented_key_manager.NotImplementedKeyManager):
    pass


class MigrationKeyManagerTestCase(test_key_manager.KeyManagerTestCase):

    def _create_key_manager(self):
        self.fixed_key = '1' * 64
        try:
            self.conf.register_opt(cfg.StrOpt('fixed_key'),
                                   group='key_manager')
        except cfg.DuplicateOptError:
            pass
        self.conf.set_override('fixed_key',
                               self.fixed_key,
                               group='key_manager')
        return key_manager.API(self.conf)

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

        # Create fake context (actual contents doesn't matter).
        self.ctxt = mock.Mock()

        fixed_key_bytes = bytes(binascii.unhexlify(self.fixed_key))
        fixed_key_length = len(fixed_key_bytes) * 8
        self.fixed_key_secret = key.SymmetricKey('AES',
                                                 fixed_key_length,
                                                 fixed_key_bytes)
        self.fixed_key_id = '00000000-0000-0000-0000-000000000000'
        self.other_key_id = "d152fa13-2b41-42ca-a934-6c21566c0f40"

    def test_get_fixed_key(self):
        self.assertEqual('MigrationKeyManager', type(self.key_mgr).__name__)
        secret = self.key_mgr.get(self.ctxt, self.fixed_key_id)
        self.assertEqual(self.fixed_key_secret, secret)

    def test_get_fixed_key_fail_bad_context(self):
        self.assertRaises(exception.Forbidden,
                          self.key_mgr.get,
                          context=None,
                          managed_object_id=self.fixed_key_id)

    def test_delete_fixed_key(self):
        self.key_mgr.delete(self.ctxt, self.fixed_key_id)
        # Delete looks like it succeeded, but nothing actually happened.
        secret = self.key_mgr.get(self.ctxt, self.fixed_key_id)
        self.assertEqual(self.fixed_key_secret, secret)

    def test_delete_fixed_key_fail_bad_context(self):
        self.assertRaises(exception.Forbidden,
                          self.key_mgr.delete,
                          context=None,
                          managed_object_id=self.fixed_key_id)

    def test_get_other_key(self):
        # Request to get other_key_id should be passed on to the backend,
        # who will throw an error because we don't have a valid context.
        self.assertRaises(exception.KeyManagerError,
                          self.key_mgr.get,
                          context=self.ctxt,
                          managed_object_id=self.other_key_id)

    def test_delete_other_key(self):
        # Request to delete other_key_id should be passed on to the backend,
        # who will throw an error because we don't have a valid context.
        self.assertRaises(exception.KeyManagerError,
                          self.key_mgr.delete,
                          context=self.ctxt,
                          managed_object_id=self.other_key_id)

    def test_no_fixed_key(self):
        conf = self.conf
        conf.set_override('fixed_key', None, group='key_manager')
        key_mgr = key_manager.API(conf)
        self.assertNotEqual('MigrationKeyManager', type(key_mgr).__name__)
        self.assertRaises(exception.KeyManagerError,
                          key_mgr.get,
                          context=self.ctxt,
                          managed_object_id=self.fixed_key_id)

    def test_using_conf_key_manager(self):
        conf = self.conf
        ckm_backend = 'castellan.tests.unit.key_manager.' \
                      'test_migration_key_manager.ConfKeyManager'
        conf.set_override('backend', ckm_backend, group='key_manager')
        key_mgr = key_manager.API(conf)
        self.assertNotEqual('MigrationKeyManager', type(key_mgr).__name__)
        self.assertRaises(NotImplementedError,
                          key_mgr.get,
                          context=self.ctxt,
                          managed_object_id=self.fixed_key_id)