diff --git a/kmip/services/kmip_client.py b/kmip/services/kmip_client.py index 1de2a774..7f72adf7 100644 --- a/kmip/services/kmip_client.py +++ b/kmip/services/kmip_client.py @@ -44,6 +44,7 @@ from kmip.core.factories.credentials import CredentialFactory from kmip.core import objects +from kmip.core import primitives from kmip.core.messages.contents import Authentication from kmip.core.messages.contents import BatchCount @@ -363,7 +364,11 @@ def send_request_payload(self, operation, payload, credential=None): ) batch_item = messages.RequestBatchItem( - operation=operation, + operation=primitives.Enumeration( + enums.Operation, + operation, + tag=enums.Tags.OPERATION + ), request_payload=payload ) @@ -403,7 +408,7 @@ def send_request_payload(self, operation, payload, credential=None): elif batch_item.operation.value == enums.Operation.SET_ATTRIBUTE: if not isinstance( batch_item.response_payload, - payloads.SetAttributeRequestPayload + payloads.SetAttributeResponsePayload ): raise exceptions.InvalidMessage( "Invalid response payload received for the SetAttribute " @@ -412,7 +417,7 @@ def send_request_payload(self, operation, payload, credential=None): elif batch_item.operation.value == enums.Operation.MODIFY_ATTRIBUTE: if not isinstance( batch_item.response_payload, - payloads.ModifyAttributeRequestPayload + payloads.ModifyAttributeResponsePayload ): raise exceptions.InvalidMessage( "Invalid response payload received for the " diff --git a/kmip/tests/integration/services/test_integration.py b/kmip/tests/integration/services/test_integration.py index 3acd3e9c..901cc3c2 100644 --- a/kmip/tests/integration/services/test_integration.py +++ b/kmip/tests/integration/services/test_integration.py @@ -40,6 +40,7 @@ from kmip.core.factories.credentials import CredentialFactory from kmip.core.factories.secrets import SecretFactory +from kmip.core.messages import payloads from kmip.core.misc import KeyFormatType from kmip.core.objects import Attribute @@ -1827,3 +1828,93 @@ def test_split_key_register_get_destroy(self): ResultReason.ITEM_NOT_FOUND, result.result_reason.value ) + + def test_modify_delete_attribute(self): + """ + Test that the KMIPProxy client can be used to set, modify, and delete + attributes for an object stored by the server. + """ + key_name = "Integration Test - Set-Modify-Delete-Attribute Key" + result = self._create_symmetric_key(key_name=key_name) + key_uuid = result.uuid + + self.assertEqual(ResultStatus.SUCCESS, result.result_status.value) + self.assertEqual(ObjectType.SYMMETRIC_KEY, result.object_type) + self.assertIsInstance(key_uuid, str) + + # Get the "Name" attribute for the key + result = self.client.get_attributes( + uuid=key_uuid, + attribute_names=["Name"] + ) + self.assertEqual(ResultStatus.SUCCESS, result.result_status.value) + self.assertEqual(1, len(result.attributes)) + self.assertEqual( + "Name", + result.attributes[0].attribute_name.value + ) + self.assertEqual(0, result.attributes[0].attribute_index.value) + self.assertEqual( + "Integration Test - Set-Modify-Delete-Attribute Key", + result.attributes[0].attribute_value.name_value.value + ) + + # Modify the "Name" attribute for the key. + response_payload = self.client.send_request_payload( + enums.Operation.MODIFY_ATTRIBUTE, + payloads.ModifyAttributeRequestPayload( + unique_identifier=key_uuid, + attribute=self.attr_factory.create_attribute( + enums.AttributeType.NAME, + "New Name", + index=0 + ) + ) + ) + self.assertEqual(key_uuid, response_payload.unique_identifier) + self.assertEqual( + "Name", + response_payload.attribute.attribute_name.value + ) + self.assertEqual(0, response_payload.attribute.attribute_index.value) + self.assertEqual( + "New Name", + response_payload.attribute.attribute_value.name_value.value + ) + + # Get the "Name" attribute for the key to verify it was modified + result = self.client.get_attributes( + uuid=key_uuid, + attribute_names=["Name"] + ) + self.assertEqual(ResultStatus.SUCCESS, result.result_status.value) + self.assertEqual(1, len(result.attributes)) + self.assertEqual( + "Name", + result.attributes[0].attribute_name.value + ) + self.assertEqual(0, result.attributes[0].attribute_index.value) + self.assertEqual( + "New Name", + result.attributes[0].attribute_value.name_value.value + ) + + # Delete the "Name" attribute for the key. + response_payload = self.client.send_request_payload( + enums.Operation.DELETE_ATTRIBUTE, + payloads.DeleteAttributeRequestPayload( + unique_identifier=key_uuid, + attribute_name="Name", + attribute_index=0 + ) + ) + self.assertEqual(key_uuid, response_payload.unique_identifier) + self.assertEqual( + "Name", + response_payload.attribute.attribute_name.value + ) + self.assertEqual(0, response_payload.attribute.attribute_index.value) + self.assertEqual( + "New Name", + response_payload.attribute.attribute_value.name_value.value + ) diff --git a/kmip/tests/integration/services/test_proxykmipclient.py b/kmip/tests/integration/services/test_proxykmipclient.py index 5d22a973..aeecc03e 100644 --- a/kmip/tests/integration/services/test_proxykmipclient.py +++ b/kmip/tests/integration/services/test_proxykmipclient.py @@ -1349,3 +1349,70 @@ def test_split_key_register_get_destroy(self): exceptions.KmipOperationFailure, self.client.get, uid) self.assertRaises( exceptions.KmipOperationFailure, self.client.destroy, uid) + + def test_modify_delete_attribute(self): + """ + Test that the ProxyKmipClient can modify and delete an attribute. + """ + key_id = self.client.create( + enums.CryptographicAlgorithm.IDEA, + 128, + name="Symmetric Key" + ) + + self.assertIsInstance(key_id, str) + + # Get the "Name" attribute for the key. + result_id, result_attributes = self.client.get_attributes( + uid=key_id, + attribute_names=["Name"] + ) + self.assertEqual(1, len(result_attributes)) + self.assertEqual("Name", result_attributes[0].attribute_name.value) + self.assertEqual( + "Symmetric Key", + result_attributes[0].attribute_value.name_value.value + ) + + # Modify the "Name" attribute for the key. + response_id, response_attr = self.client.modify_attribute( + unique_identifier=key_id, + attribute=self.attribute_factory.create_attribute( + enums.AttributeType.NAME, + "Modified Name", + index=0 + ) + ) + self.assertEqual(key_id, response_id) + self.assertEqual("Name", response_attr.attribute_name.value) + self.assertEqual(0, response_attr.attribute_index.value) + self.assertEqual( + "Modified Name", + response_attr.attribute_value.name_value.value + ) + + # Get the "Name" attribute for the key to verify it was modified. + result_id, result_attributes = self.client.get_attributes( + uid=key_id, + attribute_names=["Name"] + ) + self.assertEqual(1, len(result_attributes)) + self.assertEqual("Name", result_attributes[0].attribute_name.value) + self.assertEqual( + "Modified Name", + result_attributes[0].attribute_value.name_value.value + ) + + # Delete the "Name" attribute for the key. + response_id, response_attr = self.client.delete_attribute( + unique_identifier=key_id, + attribute_name="Name", + attribute_index=0 + ) + self.assertEqual(key_id, response_id) + self.assertEqual("Name", response_attr.attribute_name.value) + self.assertEqual(0, response_attr.attribute_index.value) + self.assertEqual( + "Modified Name", + response_attr.attribute_value.name_value.value + )