Update the Locate payloads

This change updates the Locate payloads to the current payload
format, adding properties for different payload attributes and
adding comparison and string operators. Changes are also made to
the PyKMIP clients and the surrounding testing infrastructure to
reflect the payload changes. An official unit test suite for the
Locate payloads has also been included, which will eventually
replace the existing Locate message tests elsewhere in the test
suite.

This change prepares the Locate payloads for future updates to
support KMIP 2.0.
This commit is contained in:
Peter Hamilton 2019-03-08 10:51:55 -05:00 committed by Peter Hamilton
parent 438ec42574
commit 938a0a3b16
8 changed files with 2018 additions and 130 deletions

View File

@ -13,139 +13,505 @@
# License for the specific language governing permissions and limitations
# under the License.
from kmip.core import attributes
import six
from kmip.core import enums
from kmip.core.enums import Tags
from kmip.core.objects import Attribute
from kmip.core.primitives import Struct
from kmip.core.primitives import Enumeration
from kmip.core.primitives import Integer
from kmip.core.utils import BytearrayStream
from kmip.core import objects
from kmip.core import primitives
from kmip.core import utils
class LocateRequestPayload(Struct):
class LocateRequestPayload(primitives.Struct):
"""
A request payload for the Locate operation.
# 9.1.3.2.33
class ObjectGroupMember(Enumeration):
Attributes:
maximum_items: The maximum number of object identifiers to be returned.
offset_items: The number of object identifiers to skip when selecting
the object identifiers to return.
storage_status_mask: A bit mask specifying which types of stored
objects should be searched.
object_group_member: The object group member type for the searched
objects.
attributes: The attributes that should be used to filter and match
objects.
"""
def __init__(self, value=None):
super(LocateRequestPayload.ObjectGroupMember, self).__init__(
enums.ObjectGroupMember, value, Tags.OBJECT_GROUP_MEMBER)
def __init__(self,
maximum_items=None,
offset_items=None,
storage_status_mask=None,
object_group_member=None,
attributes=None):
"""
Construct a Locate request payload structure.
class MaximumItems(Integer):
def __init__(self, value=None):
super(LocateRequestPayload.MaximumItems, self).__init__(
value, Tags.MAXIMUM_ITEMS)
# 9.1.3.3.2
class StorageStatusMask(Enumeration):
def __init__(self, value=None):
super(LocateRequestPayload.StorageStatusMask, self).__init__(
enums.StorageStatusMask, value, Tags.STORAGE_STATUS_MASK)
def __init__(self, maximum_items=None, storage_status_mask=None,
object_group_member=None, attributes=None):
Args:
maximum_items (int): An integer specifying the maximum number of
object identifiers to be returned. Optional, defaults to None.
offset_items (int): An integer specifying the number of object
identifiers to skip when selecting the object identifiers to
return. Optional, defaults to None.
storage_status_mask (int, list): An integer bit mask or a
corresponding list of StorageStatusMask enumerations indicating
which types of stored objects should be searched. Optional,
defaults to None.
object_group_member (enum): An ObjectGroupMember enumeration
specifying the object group member type for the searched
objects. Optional, defaults to None.
attributes (list): A list of Attribute structures containing the
attribute values that should be used to filter and match
objects. Optional, defaults to None.
"""
super(LocateRequestPayload, self).__init__(enums.Tags.REQUEST_PAYLOAD)
self._maximum_items = None
self._offset_items = None
self._storage_status_mask = None
self._object_group_member = None
self._attributes = None
self.maximum_items = maximum_items
self.offset_items = offset_items
self.storage_status_mask = storage_status_mask
self.object_group_member = object_group_member
self.attributes = attributes or []
self.validate()
self.attributes = attributes
def read(self, istream, kmip_version=enums.KMIPVersion.KMIP_1_0):
@property
def maximum_items(self):
if self._maximum_items:
return self._maximum_items.value
else:
return None
@maximum_items.setter
def maximum_items(self, value):
if value is None:
self._maximum_items = None
elif isinstance(value, six.integer_types):
self._maximum_items = primitives.Integer(
value=value,
tag=enums.Tags.MAXIMUM_ITEMS
)
else:
raise TypeError("Maximum items must be an integer.")
@property
def offset_items(self):
if self._offset_items:
return self._offset_items.value
else:
return None
@offset_items.setter
def offset_items(self, value):
if value is None:
self._offset_items = None
elif isinstance(value, six.integer_types):
self._offset_items = primitives.Integer(
value=value,
tag=enums.Tags.OFFSET_ITEMS
)
else:
raise TypeError("Offset items must be an integer.")
@property
def storage_status_mask(self):
if self._storage_status_mask:
return self._storage_status_mask.value
else:
return None
@storage_status_mask.setter
def storage_status_mask(self, value):
if value is None:
self._storage_status_mask = None
elif isinstance(value, six.integer_types):
if enums.is_bit_mask(enums.StorageStatusMask, value):
self._storage_status_mask = primitives.Integer(
value=value,
tag=enums.Tags.STORAGE_STATUS_MASK
)
else:
raise TypeError(
"Storage status mask must be an integer representing a "
"valid StorageStatusMask bit mask."
)
else:
raise TypeError(
"Storage status mask must be an integer representing a valid "
"StorageStatusMask bit mask."
)
@property
def object_group_member(self):
if self._object_group_member:
return self._object_group_member.value
else:
return None
@object_group_member.setter
def object_group_member(self, value):
if value is None:
self._object_group_member = None
elif isinstance(value, enums.ObjectGroupMember):
self._object_group_member = primitives.Enumeration(
enums.ObjectGroupMember,
value=value,
tag=enums.Tags.OBJECT_GROUP_MEMBER
)
else:
raise TypeError(
"Object group member must be an ObjectGroupMember enumeration."
)
@property
def attributes(self):
if self._attributes:
return self._attributes
return []
@attributes.setter
def attributes(self, value):
if value is None:
self._attributes = []
elif isinstance(value, list):
for v in value:
if not isinstance(v, objects.Attribute):
raise TypeError(
"Attributes must be a list of Attribute structures."
)
self._attributes = value
else:
raise TypeError(
"Attributes must be a list of Attribute structures."
)
def read(self, input_buffer, kmip_version=enums.KMIPVersion.KMIP_1_0):
"""
Read the data encoding the Locate request payload and decode it into
its constituent parts.
Args:
input_buffer (stream): A data buffer containing encoded object
data, supporting a read method.
kmip_version (KMIPVersion): An enumeration defining the KMIP
version with which the object will be decoded. Optional,
defaults to KMIP 1.0.
"""
super(LocateRequestPayload, self).read(
istream,
input_buffer,
kmip_version=kmip_version
)
tstream = BytearrayStream(istream.read(self.length))
if self.is_tag_next(Tags.MAXIMUM_ITEMS, tstream):
self.maximum_items = LocateRequestPayload.MaximumItems()
self.maximum_items.read(tstream, kmip_version=kmip_version)
if self.is_tag_next(Tags.STORAGE_STATUS_MASK, tstream):
self.storage_status_mask = LocateRequestPayload.StorageStatusMask()
self.storage_status_mask.read(tstream, kmip_version=kmip_version)
if self.is_tag_next(Tags.OBJECT_GROUP_MEMBER, tstream):
self.object_group_member = LocateRequestPayload.ObjectGroupMember()
self.object_group_member.read(tstream, kmip_version=kmip_version)
while self.is_tag_next(Tags.ATTRIBUTE, tstream):
attr = Attribute()
attr.read(tstream, kmip_version=kmip_version)
self.attributes.append(attr)
local_buffer = utils.BytearrayStream(input_buffer.read(self.length))
self.validate()
if self.is_tag_next(enums.Tags.MAXIMUM_ITEMS, local_buffer):
self._maximum_items = primitives.Integer(
tag=enums.Tags.MAXIMUM_ITEMS
)
self._maximum_items.read(
local_buffer,
kmip_version=kmip_version
)
def write(self, ostream, kmip_version=enums.KMIPVersion.KMIP_1_0):
tstream = BytearrayStream()
if self.maximum_items is not None:
self.maximum_items.write(tstream, kmip_version=kmip_version)
if self.storage_status_mask is not None:
self.storage_status_mask.write(tstream, kmip_version=kmip_version)
if self.object_group_member is not None:
self.object_group_member.write(tstream, kmip_version=kmip_version)
if self.attributes is not None:
for a in self.attributes:
a.write(tstream, kmip_version=kmip_version)
if self.is_tag_next(enums.Tags.OFFSET_ITEMS, local_buffer):
self._offset_items = primitives.Integer(
tag=enums.Tags.OFFSET_ITEMS
)
self._offset_items.read(
local_buffer,
kmip_version=kmip_version
)
# Write the length and value of the request payload
self.length = tstream.length()
if self.is_tag_next(enums.Tags.STORAGE_STATUS_MASK, local_buffer):
self._storage_status_mask = primitives.Integer(
tag=enums.Tags.STORAGE_STATUS_MASK
)
self._storage_status_mask.read(
local_buffer,
kmip_version=kmip_version
)
if self.is_tag_next(enums.Tags.OBJECT_GROUP_MEMBER, local_buffer):
self._object_group_member = primitives.Enumeration(
enums.ObjectGroupMember,
tag=enums.Tags.OBJECT_GROUP_MEMBER
)
self._object_group_member.read(
local_buffer,
kmip_version=kmip_version
)
while self.is_tag_next(enums.Tags.ATTRIBUTE, local_buffer):
attribute = objects.Attribute()
attribute.read(local_buffer, kmip_version=kmip_version)
self._attributes.append(attribute)
def write(self, output_buffer, kmip_version=enums.KMIPVersion.KMIP_1_0):
"""
Write the data encoding the Locate request payload to a buffer.
Args:
output_buffer (stream): A data buffer in which to encode object
data, supporting a write method.
kmip_version (KMIPVersion): An enumeration defining the KMIP
version with which the object will be encoded. Optional,
defaults to KMIP 1.0.
"""
local_buffer = utils.BytearrayStream()
if self._maximum_items:
self._maximum_items.write(local_buffer, kmip_version=kmip_version)
if self._offset_items:
self._offset_items.write(local_buffer, kmip_version=kmip_version)
if self._storage_status_mask:
self._storage_status_mask.write(
local_buffer,
kmip_version=kmip_version
)
if self._object_group_member:
self._object_group_member.write(
local_buffer,
kmip_version=kmip_version
)
if self._attributes:
for attribute in self.attributes:
attribute.write(local_buffer, kmip_version=kmip_version)
self.length = local_buffer.length()
super(LocateRequestPayload, self).write(
ostream,
output_buffer,
kmip_version=kmip_version
)
ostream.write(tstream.buffer)
output_buffer.write(local_buffer.buffer)
def validate(self):
self._validate()
def __eq__(self, other):
if isinstance(other, LocateRequestPayload):
if self.maximum_items != other.maximum_items:
return False
elif self.offset_items != other.offset_items:
return False
elif self.storage_status_mask != other.storage_status_mask:
return False
elif self.object_group_member != other.object_group_member:
return False
elif self.attributes != other.attributes:
return False
else:
return True
else:
return NotImplemented
def _validate(self):
# TODO Finish implementation.
pass
def __ne__(self, other):
if isinstance(other, LocateRequestPayload):
return not (self == other)
else:
return NotImplemented
def __repr__(self):
args = ", ".join([
"maximum_items={}".format(self.maximum_items),
"offset_items={}".format(self.offset_items),
"storage_status_mask={}".format(self.storage_status_mask),
"object_group_member={}".format(self.object_group_member),
"attributes={}".format(
[repr(attribute) for attribute in self.attributes]
)
])
return "LocateRequestPayload({})".format(args)
def __str__(self):
value = ", ".join(
[
'"maximum_items": {}'.format(self.maximum_items),
'"offset_items": {}'.format(self.offset_items),
'"storage_status_mask": {}'.format(self.storage_status_mask),
'"object_group_member": {}'.format(self.object_group_member),
'"attributes": {}'.format(
[str(attribute) for attribute in self.attributes]
)
]
)
return '{' + value + '}'
class LocateResponsePayload(Struct):
class LocateResponsePayload(primitives.Struct):
"""
A response payload for the Locate operation.
def __init__(self, unique_identifiers=[]):
Attributes:
located_items: The number of matching objects found by the server.
unique_identifiers: The object identifiers for the matching objects.
"""
def __init__(self,
located_items=None,
unique_identifiers=None):
"""
Construct a Locate response payload structure.
Args:
located_items (int): An integer specifying the number of matching
objects found by the server. Note that this may not equal the
number of object identifiers returned in this payload.
Optional, defaults to None.
unique_identifiers (list): A list of strings specifying the object
identifiers for matching objects. Optional, defaults to None.
"""
super(LocateResponsePayload, self).__init__(
enums.Tags.RESPONSE_PAYLOAD)
self.unique_identifiers = unique_identifiers or []
self.validate()
def read(self, istream, kmip_version=enums.KMIPVersion.KMIP_1_0):
self._located_items = None
self._unique_identifiers = None
self.located_items = located_items
self.unique_identifiers = unique_identifiers
@property
def located_items(self):
if self._located_items:
return self._located_items.value
return None
@located_items.setter
def located_items(self, value):
if value is None:
self._located_items = None
elif isinstance(value, six.integer_types):
self._located_items = primitives.Integer(
value=value,
tag=enums.Tags.LOCATED_ITEMS
)
else:
raise TypeError("Located items must be an integer.")
@property
def unique_identifiers(self):
if self._unique_identifiers:
return [x.value for x in self._unique_identifiers]
return []
@unique_identifiers.setter
def unique_identifiers(self, value):
if value is None:
self._unique_identifiers = []
elif isinstance(value, list):
self._unique_identifiers = []
for v in value:
if not isinstance(v, six.string_types):
self._unique_identifiers = []
raise TypeError(
"Unique identifiers must be a list of strings."
)
self._unique_identifiers.append(
primitives.TextString(
value=v,
tag=enums.Tags.UNIQUE_IDENTIFIER
)
)
else:
raise TypeError("Unique identifiers must be a list of strings.")
def read(self, input_buffer, kmip_version=enums.KMIPVersion.KMIP_1_0):
"""
Read the data encoding the Locate response payload and decode it
into its constituent parts.
Args:
input_buffer (stream): A data buffer containing encoded object
data, supporting a read method.
kmip_version (KMIPVersion): An enumeration defining the KMIP
version with which the object will be decoded. Optional,
defaults to KMIP 1.0.
"""
super(LocateResponsePayload, self).read(
istream,
input_buffer,
kmip_version=kmip_version
)
tstream = BytearrayStream(istream.read(self.length))
local_buffer = utils.BytearrayStream(input_buffer.read(self.length))
while self.is_tag_next(Tags.UNIQUE_IDENTIFIER, tstream):
ui = attributes.UniqueIdentifier()
ui.read(tstream, kmip_version=kmip_version)
self.unique_identifiers.append(ui)
if self.is_tag_next(enums.Tags.LOCATED_ITEMS, local_buffer):
self._located_items = primitives.Integer(
tag=enums.Tags.LOCATED_ITEMS
)
self._located_items.read(
local_buffer,
kmip_version=kmip_version
)
self.is_oversized(tstream)
self.validate()
self._unique_identifiers = []
while self.is_tag_next(enums.Tags.UNIQUE_IDENTIFIER, local_buffer):
unique_identifier = primitives.TextString(
tag=enums.Tags.UNIQUE_IDENTIFIER
)
unique_identifier.read(local_buffer, kmip_version=kmip_version)
self._unique_identifiers.append(unique_identifier)
def write(self, ostream, kmip_version=enums.KMIPVersion.KMIP_1_0):
tstream = BytearrayStream()
self.is_oversized(local_buffer)
for ui in self.unique_identifiers:
ui.write(tstream, kmip_version=kmip_version)
def write(self, output_buffer, kmip_version=enums.KMIPVersion.KMIP_1_0):
"""
Write the data encoding the Locate response payload to a buffer.
# Write the length and value of the request payload
self.length = tstream.length()
Args:
output_buffer (stream): A data buffer in which to encode object
data, supporting a write method.
kmip_version (KMIPVersion): An enumeration defining the KMIP
version with which the object will be encoded. Optional,
defaults to KMIP 1.0.
"""
local_buffer = utils.BytearrayStream()
if self._located_items:
self._located_items.write(local_buffer, kmip_version=kmip_version)
if self._unique_identifiers:
for unique_identifier in self._unique_identifiers:
unique_identifier.write(
local_buffer,
kmip_version=kmip_version
)
self.length = local_buffer.length()
super(LocateResponsePayload, self).write(
ostream,
output_buffer,
kmip_version=kmip_version
)
ostream.write(tstream.buffer)
output_buffer.write(local_buffer.buffer)
def validate(self):
self.__validate()
def __eq__(self, other):
if isinstance(other, LocateResponsePayload):
if self.located_items != other.located_items:
return False
elif self.unique_identifiers != other.unique_identifiers:
return False
else:
return True
else:
return NotImplemented
def __validate(self):
# TODO Finish implementation.
pass
def __ne__(self, other):
if isinstance(other, LocateResponsePayload):
return not (self == other)
else:
return NotImplemented
def __repr__(self):
args = ", ".join([
"located_items={}".format(self.located_items),
"unique_identifiers={}".format(self.unique_identifiers)
])
return "LocateResponsePayload({})".format(args)
def __str__(self):
value = ", ".join(
[
'"located_items": {}'.format(self.located_items),
'"unique_identifiers": {}'.format(self.unique_identifiers),
]
)
return '{' + value + '}'

View File

@ -707,8 +707,7 @@ class ProxyKmipClient(object):
status = result.result_status.value
if status == enums.ResultStatus.SUCCESS:
uids = [uuid.value for uuid in result.uuids]
return uids
return result.uuids
else:
reason = result.result_reason.value
message = result.result_message.value

View File

@ -1476,30 +1476,21 @@ class KMIPProxy(object):
return result
def _locate(self, maximum_items=None, storage_status_mask=None,
object_group_member=None, attributes=[], credential=None):
object_group_member=None, attributes=None, credential=None):
operation = Operation(OperationEnum.LOCATE)
mxi = None
ssmask = None
objgrp = None
payload = payloads.LocateRequestPayload(
maximum_items=maximum_items,
storage_status_mask=storage_status_mask,
object_group_member=object_group_member,
attributes=attributes
)
if maximum_items is not None:
mxi = payloads.LocateRequestPayload.MaximumItems(maximum_items)
if storage_status_mask is not None:
m = storage_status_mask
ssmask = payloads.LocateRequestPayload.StorageStatusMask(m)
if object_group_member is not None:
o = object_group_member
objgrp = payloads.LocateRequestPayload.ObjectGroupMember(o)
payload = payloads.LocateRequestPayload(maximum_items=mxi,
storage_status_mask=ssmask,
object_group_member=objgrp,
attributes=attributes)
batch_item = messages.RequestBatchItem(operation=operation,
request_payload=payload)
batch_item = messages.RequestBatchItem(
operation=operation,
request_payload=payload
)
message = self._build_request_message(credential, [batch_item])
self._send_message(message)

View File

@ -1563,9 +1563,9 @@ class KmipEngine(object):
managed_objects = managed_objects_filtered
unique_identifiers = [attributes.UniqueIdentifier(
str(managed_object.unique_identifier))
for managed_object in managed_objects]
unique_identifiers = [
str(x.unique_identifier) for x in managed_objects
]
response_payload = payloads.LocateResponsePayload(
unique_identifiers=unique_identifiers

File diff suppressed because it is too large Load Diff

View File

@ -1946,7 +1946,7 @@ class TestResponseMessage(TestCase):
operation = contents.Operation(enums.Operation.LOCATE)
result_status = contents.ResultStatus(enums.ResultStatus.SUCCESS)
uuid = attr.UniqueIdentifier('49a1ca88-6bea-4fb2-b450-7e58802c3038')
uuid = "49a1ca88-6bea-4fb2-b450-7e58802c3038"
resp_pl = payloads.LocateResponsePayload(unique_identifiers=[uuid])

View File

@ -2700,8 +2700,7 @@ class TestProxyKmipClient(testtools.TestCase):
uuid0 = 'aaaaaaaa-1111-2222-3333-ffffffffffff'
uuid1 = 'bbbbbbbb-4444-5555-6666-gggggggggggg'
unique_identifiers = [attr.UniqueIdentifier(uuid0),
attr.UniqueIdentifier(uuid1)]
unique_identifiers = [uuid0, uuid1]
result = results.LocateResult(
contents.ResultStatus(enums.ResultStatus.SUCCESS),

View File

@ -4289,7 +4289,7 @@ class TestKmipEngine(testtools.TestCase):
)
self.assertEqual(
id_a,
response_payload.unique_identifiers[0].value
response_payload.unique_identifiers[0]
)
# Add the second obj and test the locate
@ -4315,11 +4315,11 @@ class TestKmipEngine(testtools.TestCase):
)
self.assertIn(
id_a,
[uid.value for uid in response_payload.unique_identifiers]
response_payload.unique_identifiers
)
self.assertIn(
id_b,
[uid.value for uid in response_payload.unique_identifiers]
response_payload.unique_identifiers
)
def test_locate_with_name(self):
@ -4381,11 +4381,11 @@ class TestKmipEngine(testtools.TestCase):
)
self.assertIn(
id_a,
[uid.value for uid in response_payload.unique_identifiers]
response_payload.unique_identifiers
)
self.assertIn(
id_b,
[uid.value for uid in response_payload.unique_identifiers]
response_payload.unique_identifiers
)
# Locate the obj with name 'name1'
@ -4415,7 +4415,7 @@ class TestKmipEngine(testtools.TestCase):
)
self.assertIn(
id_c,
[uid.value for uid in response_payload.unique_identifiers]
response_payload.unique_identifiers
)
def test_get(self):