Updating the server to handle errors during attribute lookup

This change updates the server to better handle exceptions thrown
while looking up the value of different attributes on managed
objects. Some managed objects in PyKMIP don't currently support
attributes they are supposed to. This can cause lookup errors
when using GetAttributes. This change suppresses any exceptions
and simply returns None for the missing attribute value. The
server unit tests have been updated to account for this change.
This commit is contained in:
Peter Hamilton 2016-12-07 09:13:38 -05:00
parent bd76dc280b
commit e2841f1ee5
2 changed files with 46 additions and 4 deletions

View File

@ -649,10 +649,13 @@ class KmipEngine(object):
attribute_name,
object_type
):
attribute_value = self._get_attribute_from_managed_object(
managed_object,
attribute_name
)
try:
attribute_value = self._get_attribute_from_managed_object(
managed_object,
attribute_name
)
except Exception:
attribute_value = None
if attribute_value is not None:
if self._attribute_policy.is_attribute_multivalued(

View File

@ -1417,6 +1417,45 @@ class TestKmipEngine(testtools.TestCase):
)
self.assertIn(attribute, result)
def test_get_attributes_from_managed_object_with_missing_attribute(self):
"""
Test that any exceptions are suppressed when attempting to retrieve
non-existent attributes from managed objects.
"""
e = engine.KmipEngine()
e._data_store = self.engine
e._data_store_session_factory = self.session_factory
e._data_session = e._data_store_session_factory()
e._logger = mock.MagicMock()
symmetric_key = pie_objects.SymmetricKey(
enums.CryptographicAlgorithm.AES,
0,
b'',
masks=[enums.CryptographicUsageMask.ENCRYPT,
enums.CryptographicUsageMask.DECRYPT]
)
symmetric_key.names = ['Name 1', 'Name 2']
e._data_session.add(symmetric_key)
e._data_session.commit()
e._data_session = e._data_store_session_factory()
e._get_attribute_from_managed_object = mock.Mock()
e._get_attribute_from_managed_object.side_effect = Exception
result = e._get_attributes_from_managed_object(
symmetric_key,
['Unique Identifier',
'Name',
'Cryptographic Algorithm',
'Cryptographic Length',
'Cryptographic Usage Mask',
'invalid']
)
self.assertEqual(0, len(result))
def test_get_attribute_from_managed_object(self):
"""
Test that an attribute can be retrieved from a given managed object.