Codebase list python-castellan / 31d467a
Add created property to Managed Objects Adds the property 'created' to managed objects in Castellan. The property is None until the secret has been stored. Change-Id: I83e79cd3dbc07b90f4526a36aaf4ee76e902e228 “Fernando authored 8 years ago Fernando Diaz committed 8 years ago
15 changed file(s) with 179 addition(s) and 102 deletion(s). Raw diff Collapse all Expand all
1818 This module defines the ManagedObject class. The ManagedObject class
1919 is the base class to represent all objects managed by the key manager.
2020 """
21
2221 import abc
2322
2423 import six
2827 class ManagedObject(object):
2928 """Base class to represent all managed objects."""
3029
31 def __init__(self, name=None):
32 """Managed Object has a name, defaulted to None."""
30 def __init__(self, name=None, created=None):
31 """Managed Object
32
33 :param name: the name of the managed object.
34 :param created: the time a managed object was created.
35 """
3336 self._name = name
37
38 # If None or POSIX times
39 if not created or type(created) == int:
40 self._created = created
41 else:
42 raise ValueError('created must be of long type, actual type %s' %
43 type(created))
3444
3545 @property
3646 def name(self):
3949 Returns the object's name or None if this object does not have one.
4050 """
4151 return self._name
52
53 @property
54 def created(self):
55 """Returns the POSIX time(long) of the object that was created.
56
57 Returns the POSIX time(long) of the object that was created or None if
58 the object does not have one, meaning it has not been persisted.
59 """
60 return self._created
4261
4362 @abc.abstractproperty
4463 def format(self):
2424 class OpaqueData(managed_object.ManagedObject):
2525 """This class represents opaque data."""
2626
27 def __init__(self, data, name=None):
27 def __init__(self, data, name=None, created=None):
2828 """Create a new OpaqueData object.
2929
3030 Expected type for data is a bytestring.
3131 """
3232 self._data = data
33 super(OpaqueData, self).__init__(name=name)
33 super(OpaqueData, self).__init__(name=name, created=created)
3434
3535 @property
3636 def format(self):
4343
4444 def __eq__(self, other):
4545 if isinstance(other, OpaqueData):
46 return (self._data == other._data and
47 self._name == other._name)
46 return (self._data == other._data)
4847 else:
4948 return False
5049
2424 class Passphrase(managed_object.ManagedObject):
2525 """This class represents a passphrase."""
2626
27 def __init__(self, passphrase, name=None):
27 def __init__(self, passphrase, name=None, created=None):
2828 """Create a new Passphrase object.
2929
3030 The expected type for the passphrase is a bytestring.
3131 """
3232 self._passphrase = passphrase
33 super(Passphrase, self).__init__(name=name)
33 super(Passphrase, self).__init__(name=name, created=created)
3434
3535 @property
3636 def format(self):
4343
4444 def __eq__(self, other):
4545 if isinstance(other, Passphrase):
46 return (self._passphrase == other._passphrase and
47 self._name == other._name)
46 return (self._passphrase == other._passphrase)
4847 else:
4948 return False
5049
2424 class PrivateKey(key.Key):
2525 """This class represents private keys."""
2626
27 def __init__(self, algorithm, bit_length, key, name=None):
27 def __init__(self, algorithm, bit_length, key,
28 name=None, created=None):
2829 """Create a new PrivateKey object.
2930
3031 The arguments specify the algorithm and bit length for the asymmetric
3334 self._alg = algorithm
3435 self._bit_length = bit_length
3536 self._key = key
36 super(PrivateKey, self).__init__(name=name)
37 super(PrivateKey, self).__init__(name=name, created=created)
3738
3839 @property
3940 def algorithm(self):
5859 if isinstance(other, PrivateKey):
5960 return (self._alg == other._alg and
6061 self._bit_length == other._bit_length and
61 self._key == other._key and
62 self._name == other._name)
62 self._key == other._key)
6363 else:
6464 return False
6565
2424 class PublicKey(key.Key):
2525 """This class represents public keys."""
2626
27 def __init__(self, algorithm, bit_length, key, name=None):
27 def __init__(self, algorithm, bit_length, key,
28 name=None, created=None):
2829 """Create a new PublicKey object.
2930
3031 The arguments specify the algorithm and bit length for the asymmetric
3435 self._alg = algorithm
3536 self._bit_length = bit_length
3637 self._key = key
37 super(PublicKey, self).__init__(name=name)
38 super(PublicKey, self).__init__(name=name, created=created)
3839
3940 @property
4041 def algorithm(self):
5960 if isinstance(other, PublicKey):
6061 return (self._alg == other._alg and
6162 self._bit_length == other._bit_length and
62 self._key == other._key and
63 self._name == other._name)
63 self._key == other._key)
6464 else:
6565 return False
6666
2424 class SymmetricKey(key.Key):
2525 """This class represents symmetric keys."""
2626
27 def __init__(self, algorithm, bit_length, key, name=None):
27 def __init__(self, algorithm, bit_length, key,
28 name=None, created=None):
2829 """Create a new SymmetricKey object.
2930
3031 The arguments specify the algorithm and bit length for the symmetric
3334 self._alg = algorithm
3435 self._bit_length = bit_length
3536 self._key = key
36 super(SymmetricKey, self).__init__(name=name)
37 super(SymmetricKey, self).__init__(name=name, created=created)
3738
3839 @property
3940 def algorithm(self):
5859 if isinstance(other, SymmetricKey):
5960 return (self._alg == other._alg and
6061 self._bit_length == other._bit_length and
61 self._key == other._key and
62 self._name == other._name)
62 self._key == other._key)
6363 else:
6464 return False
6565
2424 class X509(certificate.Certificate):
2525 """This class represents X.509 certificates."""
2626
27 def __init__(self, data, name=None):
27 def __init__(self, data, name=None, created=None):
2828 """Create a new X509 object.
2929
3030 The data should be in a bytestring.
3131 """
3232 self._data = data
33 super(X509, self).__init__(name=name)
33 super(X509, self).__init__(name=name, created=created)
3434
3535 @property
3636 def format(self):
4343
4444 def __eq__(self, other):
4545 if isinstance(other, X509):
46 return (self._data == other._data and
47 self._name == other._name)
46 return (self._data == other._data)
4847 else:
4948 return False
5049
1515 """
1616 Key manager implementation for Barbican
1717 """
18 import calendar
1819 import time
1920
2021 from cryptography.hazmat import backends
3940
4041 from barbicanclient import client as barbican_client
4142 from barbicanclient import exceptions as barbican_exceptions
43 from oslo_utils import timeutils
4244 from six.moves import urllib
45
4346
4447 barbican_opts = [
4548 cfg.StrOpt('barbican_endpoint',
420423
421424 secret_data = self._get_secret_data(secret)
422425
426 # convert created ISO8601 in Barbican to POSIX
427 if secret.created:
428 time_stamp = timeutils.parse_isotime(
429 str(secret.created)).timetuple()
430 created = calendar.timegm(time_stamp)
431
423432 if issubclass(secret_type, key_base_class.Key):
424433 return secret_type(secret.algorithm,
425434 secret.bit_length,
426435 secret_data,
427 secret.name)
436 secret.name,
437 created)
428438 else:
429439 return secret_type(secret_data,
430 secret.name)
440 secret.name,
441 created)
431442
432443 def _get_secret(self, context, object_id):
433444 """Returns the metadata of the secret.
1515 """
1616 Test cases for the barbican key manager.
1717 """
18 import calendar
1819
1920 from barbicanclient import exceptions as barbican_exceptions
2021 import mock
2122 from oslo_config import cfg
23 from oslo_utils import timeutils
2224
2325 from castellan.common import exception
2426 from castellan.common.objects import symmetric_key as sym_key
186188 original_secret_metadata.bit_length = mock.sentinel.bit
187189 original_secret_metadata.secret_type = 'symmetric'
188190
191 created = timeutils.parse_isotime('2015-10-20 18:51:17+00:00')
192 original_secret_metadata.created = created
193 created_formatted = timeutils.parse_isotime(str(created))
194 created_posix = calendar.timegm(created_formatted.timetuple())
195
189196 key_name = 'my key'
190197 original_secret_metadata.name = key_name
191198
198205 self.get.assert_called_once_with(self.secret_ref)
199206 self.assertEqual(key_name, key.name)
200207 self.assertEqual(original_secret_data, key.get_encoded())
208 self.assertEqual(created_posix, key.created)
201209
202210 def test_get_null_context(self):
203211 self.key_mgr._barbican_client = None
1515 """
1616 Test cases for the opaque data class.
1717 """
18
1918 from castellan.common.objects import opaque_data
2019 from castellan.tests import base
2120
2322 class OpaqueDataTestCase(base.TestCase):
2423
2524 def _create_data(self):
26 return opaque_data.OpaqueData(self.data, self.name)
25 return opaque_data.OpaqueData(self.data,
26 self.name,
27 self.created)
2728
2829 def setUp(self):
2930 self.data = bytes(b"secret opaque data")
3031 self.name = 'my opaque'
32 self.created = 1448088699
3133 self.opaque_data = self._create_data()
3234
3335 super(OpaqueDataTestCase, self).setUp()
4143 def test_get_name(self):
4244 self.assertEqual(self.name, self.opaque_data.name)
4345
46 def test_get_created(self):
47 self.assertEqual(self.created, self.opaque_data.created)
48
49 def test_get_created_none(self):
50 created = None
51 data = opaque_data.OpaqueData(self.data,
52 self.name,
53 created)
54
55 self.assertEqual(created, data.created)
56
4457 def test___eq__(self):
4558 self.assertTrue(self.opaque_data == self.opaque_data)
4659 self.assertTrue(self.opaque_data is self.opaque_data)
4861 self.assertFalse(self.opaque_data is None)
4962 self.assertFalse(None == self.opaque_data)
5063
51 other_opaque_data = opaque_data.OpaqueData(self.data, self.name)
64 other_opaque_data = opaque_data.OpaqueData(self.data)
5265 self.assertTrue(self.opaque_data == other_opaque_data)
5366 self.assertFalse(self.opaque_data is other_opaque_data)
5467
5972 def test___ne___data(self):
6073 other_opaque = opaque_data.OpaqueData(b'other data', self.name)
6174 self.assertTrue(self.opaque_data != other_opaque)
62
63 def test___ne___name(self):
64 other_opaque = opaque_data.OpaqueData(self.data, "other opaque")
65 self.assertTrue(self.opaque_data != other_opaque)
1515 """
1616 Test cases for the passphrase class.
1717 """
18
1918 from castellan.common.objects import passphrase
2019 from castellan.tests import base
2120
2423
2524 def _create_passphrase(self):
2625 return passphrase.Passphrase(self.passphrase_data,
27 self.name)
26 self.name,
27 self.created)
2828
2929 def setUp(self):
3030 self.passphrase_data = bytes(b"secret passphrase")
3131 self.name = 'my phrase'
32 self.created = 1448088699
3233 self.passphrase = self._create_passphrase()
3334
3435 super(PassphraseTestCase, self).setUp()
4243 def test_get_name(self):
4344 self.assertEqual(self.name, self.passphrase.name)
4445
46 def test_get_created(self):
47 self.assertEqual(self.created, self.passphrase.created)
48
49 def test_get_created_none(self):
50 created = None
51 phrase = passphrase.Passphrase(self.passphrase_data,
52 self.name,
53 created)
54
55 self.assertEqual(created, phrase.created)
56
4557 def test___eq__(self):
4658 self.assertTrue(self.passphrase == self.passphrase)
4759 self.assertTrue(self.passphrase is self.passphrase)
4961 self.assertFalse(self.passphrase is None)
5062 self.assertFalse(None == self.passphrase)
5163
52 other_passphrase = passphrase.Passphrase(self.passphrase_data,
53 self.name)
64 other_passphrase = passphrase.Passphrase(self.passphrase_data)
5465 self.assertTrue(self.passphrase == other_passphrase)
5566 self.assertFalse(self.passphrase is other_passphrase)
5667
6172 def test___ne___data(self):
6273 other_phrase = passphrase.Passphrase(b"other passphrase", self.name)
6374 self.assertTrue(self.passphrase != other_phrase)
64
65 def test___ne__name(self):
66 other_phrase = passphrase.Passphrase(self.passphrase_data,
67 "other phrase")
68 self.assertTrue(self.passphrase != other_phrase)
1515 """
1616 Test cases for the private key class.
1717 """
18
1918 from castellan.common.objects import private_key
2019 from castellan.tests import base
2120 from castellan.tests import utils
2524
2625 def _create_key(self):
2726 return private_key.PrivateKey(self.algorithm,
28 self.length,
27 self.bit_length,
2928 self.encoded,
30 self.name)
29 self.name,
30 self.created)
3131
3232 def setUp(self):
3333 self.algorithm = 'RSA'
34 self.length = 2048
34 self.bit_length = 2048
3535 self.encoded = bytes(utils.get_private_key_der())
3636 self.name = 'my key'
37 self.created = 1448088699
3738
3839 super(PrivateKeyTestCase, self).setUp()
3940
4041 def test_get_algorithm(self):
4142 self.assertEqual(self.algorithm, self.key.algorithm)
4243
43 def test_get_length(self):
44 self.assertEqual(self.length, self.key.bit_length)
44 def test_get_bit_length(self):
45 self.assertEqual(self.bit_length, self.key.bit_length)
4546
4647 def test_get_name(self):
4748 self.assertEqual(self.name, self.key.name)
5253 def test_get_encoded(self):
5354 self.assertEqual(self.encoded, self.key.get_encoded())
5455
56 def test_get_created(self):
57 self.assertEqual(self.created, self.key.created)
58
59 def test_get_created_none(self):
60 created = None
61 key = private_key.PrivateKey(self.algorithm,
62 self.bit_length,
63 self.encoded,
64 self.name,
65 created)
66
67 self.assertEqual(created, key.created)
68
5569 def test___eq__(self):
5670 self.assertTrue(self.key == self.key)
5771 self.assertTrue(self.key is self.key)
6074 self.assertFalse(None == self.key)
6175
6276 other_key = private_key.PrivateKey(self.algorithm,
63 self.length,
64 self.encoded,
65 self.name)
77 self.bit_length,
78 self.encoded)
6679 self.assertTrue(self.key == other_key)
6780 self.assertFalse(self.key is other_key)
6881
7285
7386 def test___ne___algorithm(self):
7487 other_key = private_key.PrivateKey('DSA',
75 self.length,
88 self.bit_length,
7689 self.encoded,
7790 self.name)
7891 self.assertTrue(self.key != other_key)
7992
80 def test___ne___length(self):
93 def test___ne___bit_length(self):
8194 other_key = private_key.PrivateKey(self.algorithm,
8295 4096,
8396 self.encoded,
87100 def test___ne___encoded(self):
88101 different_encoded = bytes(utils.get_private_key_der()) + b'\x00'
89102 other_key = private_key.PrivateKey(self.algorithm,
90 self.length,
103 self.bit_length,
91104 different_encoded,
92105 self.name)
93106 self.assertTrue(self.key != other_key)
94
95 def test___ne___name(self):
96 other_key = private_key.PrivateKey(self.algorithm,
97 self.length,
98 self.encoded,
99 'other key')
100 self.assertTrue(self.key != other_key)
1515 """
1616 Test cases for the public key class.
1717 """
18
1918 from castellan.common.objects import public_key
2019 from castellan.tests import base
2120 from castellan.tests import utils
2524
2625 def _create_key(self):
2726 return public_key.PublicKey(self.algorithm,
28 self.length,
27 self.bit_length,
2928 self.encoded,
30 self.name)
29 self.name,
30 self.created)
3131
3232 def setUp(self):
3333 self.algorithm = 'RSA'
34 self.length = 2048
34 self.bit_length = 2048
3535 self.encoded = bytes(utils.get_public_key_der())
3636 self.name = 'my key'
37 self.created = 1448088699
3738
3839 super(PublicKeyTestCase, self).setUp()
3940
4041 def test_get_algorithm(self):
4142 self.assertEqual(self.algorithm, self.key.algorithm)
4243
43 def test_get_length(self):
44 self.assertEqual(self.length, self.key.bit_length)
44 def test_get_bit_length(self):
45 self.assertEqual(self.bit_length, self.key.bit_length)
4546
4647 def test_get_name(self):
4748 self.assertEqual(self.name, self.key.name)
5253 def test_get_encoded(self):
5354 self.assertEqual(self.encoded, self.key.get_encoded())
5455
56 def test_get_created(self):
57 self.assertEqual(self.created, self.key.created)
58
59 def test_get_created_none(self):
60 created = None
61 key = public_key.PublicKey(self.algorithm,
62 self.bit_length,
63 self.encoded,
64 self.name,
65 created)
66
67 self.assertEqual(created, key.created)
68
5569 def test___eq__(self):
5670 self.assertTrue(self.key == self.key)
5771 self.assertTrue(self.key is self.key)
6074 self.assertFalse(None == self.key)
6175
6276 other_key = public_key.PublicKey(self.algorithm,
63 self.length,
64 self.encoded,
65 self.name)
77 self.bit_length,
78 self.encoded)
6679 self.assertTrue(self.key == other_key)
6780 self.assertFalse(self.key is other_key)
6881
7285
7386 def test___ne___algorithm(self):
7487 other_key = public_key.PublicKey('DSA',
75 self.length,
88 self.bit_length,
7689 self.encoded,
7790 self.name)
7891 self.assertTrue(self.key != other_key)
7992
80 def test___ne___length(self):
93 def test___ne___bit_length(self):
8194 other_key = public_key.PublicKey(self.algorithm,
8295 4096,
8396 self.encoded,
87100 def test___ne___encoded(self):
88101 different_encoded = bytes(utils.get_public_key_der()) + b'\x00'
89102 other_key = public_key.PublicKey(self.algorithm,
90 self.length,
103 self.bit_length,
91104 different_encoded,
92105 self.name)
93106 self.assertTrue(self.key != other_key)
94
95 def test___ne__name(self):
96 other_key = public_key.PublicKey(self.algorithm,
97 self.length,
98 self.encoded,
99 'other key')
100 self.assertTrue(self.key != other_key)
1515 """
1616 Test cases for the symmetric key class.
1717 """
18
1918 from castellan.common.objects import symmetric_key as sym_key
2019 from castellan.tests import base
2120
2625 return sym_key.SymmetricKey(self.algorithm,
2726 self.bit_length,
2827 self.encoded,
29 self.name)
28 self.name,
29 self.created)
3030
3131 def setUp(self):
3232 self.algorithm = 'AES'
3333 self.encoded = bytes(b'0' * 64)
3434 self.bit_length = len(self.encoded) * 8
3535 self.name = 'my key'
36 self.created = 1448088699
3637
3738 super(SymmetricKeyTestCase, self).setUp()
3839
5152 def test_get_bit_length(self):
5253 self.assertEqual(self.bit_length, self.key.bit_length)
5354
55 def test_get_created(self):
56 self.assertEqual(self.created, self.key.created)
57
58 def test_get_created_none(self):
59 created = None
60 key = sym_key.SymmetricKey(self.algorithm,
61 self.bit_length,
62 self.encoded,
63 self.name,
64 created)
65
66 self.assertEqual(created, key.created)
67
5468 def test___eq__(self):
5569 self.assertTrue(self.key == self.key)
5670 self.assertTrue(self.key is self.key)
6074
6175 other_key = sym_key.SymmetricKey(self.algorithm,
6276 self.bit_length,
63 self.encoded,
64 self.name)
77 self.encoded)
6578 self.assertTrue(self.key == other_key)
6679 self.assertFalse(self.key is other_key)
6780
7689 self.name)
7790 self.assertTrue(self.key != other_key)
7891
79 def test___ne___length(self):
92 def test___ne___bit_length(self):
8093 other_key = sym_key.SymmetricKey(self.algorithm,
8194 self.bit_length * 2,
8295 self.encoded,
90103 different_encoded,
91104 self.name)
92105 self.assertTrue(self.key != other_key)
93
94 def test___ne___name(self):
95 other_key = sym_key.SymmetricKey(self.algorithm,
96 self.bit_length,
97 self.encoded,
98 'other key')
99 self.assertTrue(self.key != other_key)
1515 """
1616 Test cases for the X.509 certificate class.
1717 """
18
1918 from castellan.common.objects import x_509
2019 from castellan.tests import base
2120 from castellan.tests import utils
2423 class X509TestCase(base.CertificateTestCase):
2524
2625 def _create_cert(self):
27 return x_509.X509(self.data, self.name)
26 return x_509.X509(self.data,
27 self.name,
28 self.created)
2829
2930 def setUp(self):
3031 self.data = utils.get_certificate_der()
3132 self.name = 'my cert'
33 self.created = 1448088699
3234
3335 super(X509TestCase, self).setUp()
3436
4143 def test_get_encoded(self):
4244 self.assertEqual(self.data, self.cert.get_encoded())
4345
46 def test_get_created(self):
47 self.assertEqual(self.created, self.cert.created)
48
49 def test_get_created_none(self):
50 created = None
51 cert = x_509.X509(self.data,
52 self.name,
53 created)
54
55 self.assertEqual(created, cert.created)
56
4457 def test___eq__(self):
4558 self.assertTrue(self.cert == self.cert)
4659 self.assertTrue(self.cert is self.cert)
4861 self.assertFalse(self.cert is None)
4962 self.assertFalse(None == self.cert)
5063
51 other_x_509 = x_509.X509(self.data, self.name)
64 other_x_509 = x_509.X509(self.data)
5265 self.assertTrue(self.cert == other_x_509)
5366 self.assertFalse(self.cert is other_x_509)
5467
5972 def test___ne___data(self):
6073 other_x509 = x_509.X509(b'\x00\x00\x00', self.name)
6174 self.assertTrue(self.cert != other_x509)
62
63 def test___ne__name(self):
64 other_x509 = x_509.X509(self.data, "other x509")
65 self.assertTrue(self.cert != other_x509)