Package list python-castellan / 35c2a99
Merge "Add ID to managed objects" Jenkins authored 4 years ago Gerrit Code Review committed 4 years ago
12 changed file(s) with 73 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
2727 class ManagedObject(object):
2828 """Base class to represent all managed objects."""
2929
30 def __init__(self, name=None, created=None):
30 def __init__(self, name=None, created=None, id=None):
3131 """Managed Object
3232
3333 :param name: the name of the managed object.
3434 :param created: the time a managed object was created.
35 :param id: the ID of the object, generated after storing the object.
3536 """
3637 self._name = name
3738
4142 else:
4243 raise ValueError('created must be of long type, actual type %s' %
4344 type(created))
45
46 self._id = id
47
48 @property
49 def id(self):
50 """Returns the ID of the managed object.
51
52 Returns the ID of the managed object or None if this object does not
53 have one. If the ID is None, the object has not been persisted yet.
54 """
55 return self._id
4456
4557 @property
4658 def name(self):
2424 class OpaqueData(managed_object.ManagedObject):
2525 """This class represents opaque data."""
2626
27 def __init__(self, data, name=None, created=None):
27 def __init__(self, data, name=None, created=None, id=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, created=created)
33 super(OpaqueData, self).__init__(name=name, created=created, id=id)
3434
3535 @property
3636 def format(self):
2424 class Passphrase(managed_object.ManagedObject):
2525 """This class represents a passphrase."""
2626
27 def __init__(self, passphrase, name=None, created=None):
27 def __init__(self, passphrase, name=None, created=None, id=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, created=created)
33 super(Passphrase, self).__init__(name=name, created=created, id=id)
3434
3535 @property
3636 def format(self):
2525 """This class represents private keys."""
2626
2727 def __init__(self, algorithm, bit_length, key,
28 name=None, created=None):
28 name=None, created=None, id=None):
2929 """Create a new PrivateKey object.
3030
3131 The arguments specify the algorithm and bit length for the asymmetric
3434 self._alg = algorithm
3535 self._bit_length = bit_length
3636 self._key = key
37 super(PrivateKey, self).__init__(name=name, created=created)
37 super(PrivateKey, self).__init__(name=name, created=created, id=id)
3838
3939 @property
4040 def algorithm(self):
2525 """This class represents public keys."""
2626
2727 def __init__(self, algorithm, bit_length, key,
28 name=None, created=None):
28 name=None, created=None, id=None):
2929 """Create a new PublicKey object.
3030
3131 The arguments specify the algorithm and bit length for the asymmetric
3535 self._alg = algorithm
3636 self._bit_length = bit_length
3737 self._key = key
38 super(PublicKey, self).__init__(name=name, created=created)
38 super(PublicKey, self).__init__(name=name, created=created, id=id)
3939
4040 @property
4141 def algorithm(self):
2525 """This class represents symmetric keys."""
2626
2727 def __init__(self, algorithm, bit_length, key,
28 name=None, created=None):
28 name=None, created=None, id=None):
2929 """Create a new SymmetricKey object.
3030
3131 The arguments specify the algorithm and bit length for the symmetric
3434 self._alg = algorithm
3535 self._bit_length = bit_length
3636 self._key = key
37 super(SymmetricKey, self).__init__(name=name, created=created)
37 super(SymmetricKey, self).__init__(name=name, created=created, id=id)
3838
3939 @property
4040 def algorithm(self):
2424 class X509(certificate.Certificate):
2525 """This class represents X.509 certificates."""
2626
27 def __init__(self, data, name=None, created=None):
27 def __init__(self, data, name=None, created=None, id=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, created=created)
33 super(X509, self).__init__(name=name, created=created, id=id)
3434
3535 @property
3636 def format(self):
483483 else:
484484 secret_data = self._get_secret_data(secret)
485485
486 if secret.secret_ref:
487 object_id = self._retrieve_secret_uuid(secret.secret_ref)
488 else:
489 object_id = None
490
486491 # convert created ISO8601 in Barbican to POSIX
487492 if secret.created:
488493 time_stamp = timeutils.parse_isotime(
494499 secret.bit_length,
495500 secret_data,
496501 secret.name,
497 created)
502 created,
503 object_id)
498504 else:
499505 return secret_type(secret_data,
500506 secret.name,
501 created)
507 created,
508 object_id)
502509
503510 def _get_secret(self, context, object_id):
504511 """Returns the metadata of the secret.
136136 self.assertEqual(managed_object.get_encoded(),
137137 retrieved_object.get_encoded())
138138 self.assertFalse(managed_object.is_metadata_only())
139 self.assertFalse(retrieved_object.is_metadata_only())
140 self.assertIsNotNone(retrieved_object.id)
139141
140142 @utils.parameterized_dataset({
141143 'symmetric_key': [_get_test_symmetric_key()],
154156 metadata_only=True)
155157 self.assertFalse(managed_object.is_metadata_only())
156158 self.assertTrue(retrieved_object.is_metadata_only())
159 self.assertIsNotNone(retrieved_object.id)
157160
158161 @utils.parameterized_dataset({
159162 'symmetric_key': [_get_test_symmetric_key()],
170173 retrieved_object = self.key_mgr.get(self.ctxt, uuid)
171174 self.assertEqual(managed_object.get_encoded(),
172175 retrieved_object.get_encoded())
176 self.assertIsNotNone(retrieved_object.id)
173177
174178 @utils.parameterized_dataset({
175179 'symmetric_key': [_get_test_symmetric_key()],
188192 # check if the object we created is in the list
189193 retrieved_objects = self.key_mgr.list(self.ctxt)
190194 self.assertTrue(managed_object in retrieved_objects)
191 for obj in retrieved_objects:
192 self.assertFalse(obj.is_metadata_only())
195 for retrieved_object in retrieved_objects:
196 self.assertFalse(retrieved_object.is_metadata_only())
197 self.assertIsNotNone(retrieved_object.id)
193198
194199 @utils.parameterized_dataset({
195200 'symmetric_key': [_get_test_symmetric_key()],
210215 # check if the object we created is in the list
211216 retrieved_objects = self.key_mgr.list(self.ctxt, metadata_only=True)
212217 self.assertTrue(expected_obj in retrieved_objects)
213 for obj in retrieved_objects:
214 self.assertTrue(obj.is_metadata_only())
218 for retrieved_object in retrieved_objects:
219 self.assertTrue(retrieved_object.is_metadata_only())
220 self.assertIsNotNone(retrieved_object.id)
215221
216222 @utils.parameterized_dataset({
217223 'query_by_object_type': {
232238 retrieved_objects = self.key_mgr.list(self.ctxt, **query_dict)
233239 for retrieved_object in retrieved_objects:
234240 self.assertEqual(type(object_1), type(retrieved_object))
241 self.assertIsNotNone(retrieved_object.id)
235242 self.assertTrue(object_1 in retrieved_objects)
162162 raise exception.Forbidden()
163163
164164 key_id = self._generate_key_id()
165 managed_object._id = key_id
165166 self.keys[key_id] = managed_object
166167
167168 return key_id
206206 original_secret_metadata.bit_length = mock.sentinel.bit
207207 original_secret_metadata.secret_type = 'symmetric'
208208
209 key_id = "43ed09c3-e551-4c24-b612-e619abe9b534"
210 key_ref = ("http://localhost:9311/v1/secrets/" + key_id)
211 original_secret_metadata.secret_ref = key_ref
212
209213 created = timeutils.parse_isotime('2015-10-20 18:51:17+00:00')
210214 original_secret_metadata.created = created
211215 created_formatted = timeutils.parse_isotime(str(created))
221225 key = self.key_mgr.get(self.ctxt, self.key_id)
222226
223227 self.get.assert_called_once_with(self.secret_ref)
228 self.assertEqual(key_id, key.id)
224229 self.assertEqual(key_name, key.name)
225230 self.assertEqual(original_secret_data, key.get_encoded())
226231 self.assertEqual(created_posix, key.created)
377382 original_secret_metadata.bit_length = mock.sentinel.bit
378383 original_secret_metadata.secret_type = 'symmetric'
379384
385 key_id = "43ed09c3-e551-4c24-b612-e619abe9b534"
386 key_ref = ("http://localhost:9311/v1/secrets/" + key_id)
387 original_secret_metadata.secret_ref = key_ref
388
380389 created = timeutils.parse_isotime('2015-10-20 18:51:17+00:00')
381390 original_secret_metadata.created = created
382391 created_formatted = timeutils.parse_isotime(str(created))
397406 key = key_list[0]
398407
399408 self.list.assert_called_once()
409 self.assertEqual(key_id, key.id)
400410 self.assertEqual(key_name, key.name)
401411 self.assertEqual(original_secret_data, key.get_encoded())
402412 self.assertEqual(created_posix, key.created)
6969 key_id = self.key_mgr.create_key(self.context, length=length)
7070 key = self.key_mgr.get(self.context, key_id)
7171 self.assertEqual(length / 8, len(key.get_encoded()))
72 self.assertIsNotNone(key.id)
7273
7374 def test_create_key_with_name(self):
7475 name = 'my key'
7576 key_id = self.key_mgr.create_key(self.context, name=name)
7677 key = self.key_mgr.get(self.context, key_id)
7778 self.assertEqual(name, key.name)
79 self.assertIsNotNone(key.id)
7880
7981 def test_create_key_with_algorithm(self):
8082 algorithm = 'DES'
8183 key_id = self.key_mgr.create_key(self.context, algorithm=algorithm)
8284 key = self.key_mgr.get(self.context, key_id)
8385 self.assertEqual(algorithm, key.algorithm)
86 self.assertIsNotNone(key.id)
8487
8588 def test_create_key_null_context(self):
8689 self.assertRaises(exception.Forbidden,
9396 self.context, 'RSA', length, name=name)
9497
9598 private_key = self.key_mgr.get(self.context, private_key_uuid)
99 self.assertIsNotNone(private_key.id)
96100 public_key = self.key_mgr.get(self.context, public_key_uuid)
101 self.assertIsNotNone(public_key.id)
97102
98103 crypto_private_key = get_cryptography_private_key(private_key)
99104 crypto_public_key = get_cryptography_public_key(public_key)
152157 actual_key = self.key_mgr.get(self.context, key_id)
153158 self.assertEqual(_key, actual_key)
154159
160 self.assertIsNotNone(actual_key.id)
161
155162 def test_store_key_and_get_metadata(self):
156163 secret_key = bytes(b'0' * 64)
157164 _key = sym_key.SymmetricKey('AES', 64 * 8, secret_key)
163170 self.assertIsNone(actual_key.get_encoded())
164171 self.assertTrue(actual_key.is_metadata_only())
165172
173 self.assertIsNotNone(actual_key.id)
174
166175 def test_store_key_and_get_metadata_and_get_key(self):
167176 secret_key = bytes(b'0' * 64)
168177 _key = sym_key.SymmetricKey('AES', 64 * 8, secret_key)
180189 self.assertIsNotNone(actual_key.get_encoded())
181190 self.assertFalse(actual_key.is_metadata_only())
182191
192 self.assertIsNotNone(actual_key.id)
193
183194 def test_store_null_context(self):
184195 self.assertRaises(exception.Forbidden,
185196 self.key_mgr.store, None, None)
219230 self.assertEqual(2, len(keys))
220231 self.assertTrue(key1 in keys)
221232 self.assertTrue(key2 in keys)
233
234 for key in keys:
235 self.assertIsNotNone(key.id)
222236
223237 def test_list_keys_metadata_only(self):
224238 key1 = sym_key.SymmetricKey('AES', 64 * 8, bytes(b'0' * 64))
232246 for key in keys:
233247 self.assertTrue(key.is_metadata_only())
234248 self.assertTrue(key.bit_length in bit_length_list)
249
250 for key in keys:
251 self.assertIsNotNone(key.id)