Updating the Create payloads to properly use protection masks

This change updates the Create payloads to properly use the new
ProtectionStorageMasks structure. Unit tests have been updated to
reflect this change.
This commit is contained in:
Peter Hamilton 2019-05-10 14:52:29 -04:00 committed by Peter Hamilton
parent 7280ccb1eb
commit bc4e4e38bd
2 changed files with 142 additions and 63 deletions

View File

@ -29,9 +29,9 @@ class CreateRequestPayload(primitives.Struct):
Attributes:
object_type: The type of the object to create.
template_attribute: A group of attributes to set on the new object.
protection_storage_masks: An integer representing all of the
protection storage mask selections for the new object. Added in
KMIP 2.0.
protection_storage_masks: A ProtectionStorageMasks structure
containing the storage masks permissible for the new object.
Added in KMIP 2.0.
"""
def __init__(self,
@ -48,9 +48,9 @@ class CreateRequestPayload(primitives.Struct):
template_attribute (TemplateAttribute): A TemplateAttribute
structure containing a set of attributes to set on the new
object. Optional, defaults to None. Required for read/write.
protection_storage_masks (int): An integer representing all of
the protection storage mask selections for the new object.
Optional, defaults to None. Added in KMIP 2.0.
protection_storage_masks (structure): A ProtectionStorageMasks
structure containing the storage masks permissible for the new
object. Added in KMIP 2.0. Optional, defaults to None.
"""
super(CreateRequestPayload, self).__init__(
tag=enums.Tags.REQUEST_PAYLOAD
@ -103,22 +103,25 @@ class CreateRequestPayload(primitives.Struct):
@property
def protection_storage_masks(self):
if self._protection_storage_masks:
return self._protection_storage_masks.value
return None
return self._protection_storage_masks
@protection_storage_masks.setter
def protection_storage_masks(self, value):
if value is None:
self._protection_storage_masks = None
elif isinstance(value, six.integer_types):
self._protection_storage_masks = primitives.Integer(
value=value,
tag=enums.Tags.PROTECTION_STORAGE_MASKS
elif isinstance(value, objects.ProtectionStorageMasks):
if value.tag == enums.Tags.PROTECTION_STORAGE_MASKS:
self._protection_storage_masks = value
else:
raise TypeError(
"The protection storage masks must be a "
"ProtectionStorageMasks structure with a "
"ProtectionStorageMasks tag."
)
else:
raise TypeError(
"The protection storage masks must be an integer."
"The protection storage masks must be a "
"ProtectionStorageMasks structure."
)
def read(self, input_buffer, kmip_version=enums.KMIPVersion.KMIP_1_0):
@ -187,11 +190,12 @@ class CreateRequestPayload(primitives.Struct):
"attributes structure."
)
if kmip_version >= enums.KMIPVersion.KMIP_2_0:
if self.is_tag_next(
enums.Tags.PROTECTION_STORAGE_MASKS,
local_buffer
):
protection_storage_masks = primitives.Integer(
protection_storage_masks = objects.ProtectionStorageMasks(
tag=enums.Tags.PROTECTION_STORAGE_MASKS
)
protection_storage_masks.read(
@ -255,6 +259,7 @@ class CreateRequestPayload(primitives.Struct):
"attribute field."
)
if kmip_version >= enums.KMIPVersion.KMIP_2_0:
if self._protection_storage_masks:
self._protection_storage_masks.write(
local_buffer,
@ -293,9 +298,7 @@ class CreateRequestPayload(primitives.Struct):
"object_type={}".format(self.object_type),
"template_attribute={}".format(repr(self.template_attribute)),
"protection_storage_masks={}".format(
"{}".format(
repr(self.protection_storage_masks)
) if self._protection_storage_masks else None
)
])
return "CreateRequestPayload({})".format(args)
@ -306,9 +309,7 @@ class CreateRequestPayload(primitives.Struct):
'"object_type": {}'.format(self.object_type),
'"template_attribute": {}'.format(self.template_attribute),
'"protection_storage_masks": {}'.format(
"{}".format(
str(self.protection_storage_masks)
) if self._protection_storage_masks else None
)
]
)

View File

@ -76,15 +76,17 @@ class TestCreateRequestPayload(testtools.TestCase):
# Cryptographic Algorithm - AES
# Cryptographic Length - 128
# Cryptographic Usage Mask - Encrypt | Decrypt
# Protection Storage Masks - Software | Hardware
# Protection Storage Masks
# Protection Storage Mask - Software | Hardware
self.full_encoding_with_attributes = utils.BytearrayStream(
b'\x42\x00\x79\x01\x00\x00\x00\x58'
b'\x42\x00\x79\x01\x00\x00\x00\x60'
b'\x42\x00\x57\x05\x00\x00\x00\x04\x00\x00\x00\x02\x00\x00\x00\x00'
b'\x42\x01\x25\x01\x00\x00\x00\x30'
b'\x42\x00\x28\x05\x00\x00\x00\x04\x00\x00\x00\x03\x00\x00\x00\x00'
b'\x42\x00\x2A\x02\x00\x00\x00\x04\x00\x00\x00\x80\x00\x00\x00\x00'
b'\x42\x00\x2C\x02\x00\x00\x00\x04\x00\x00\x00\x0C\x00\x00\x00\x00'
b'\x42\x01\x5F\x02\x00\x00\x00\x04\x00\x00\x00\x03\x00\x00\x00\x00'
b'\x42\x01\x5F\x01\x00\x00\x00\x10'
b'\x42\x01\x5E\x02\x00\x00\x00\x04\x00\x00\x00\x03\x00\x00\x00\x00'
)
# Encoding obtained from the KMIP 1.1 testing document,
@ -194,7 +196,20 @@ class TestCreateRequestPayload(testtools.TestCase):
kwargs = {"protection_storage_masks": "invalid"}
self.assertRaisesRegex(
TypeError,
"The protection storage masks must be an integer.",
"The protection storage masks must be a ProtectionStorageMasks "
"structure.",
payloads.CreateRequestPayload,
**kwargs
)
kwargs = {
"protection_storage_masks": objects.ProtectionStorageMasks(
tag=enums.Tags.COMMON_PROTECTION_STORAGE_MASKS
)
}
self.assertRaisesRegex(
TypeError,
"The protection storage masks must be a ProtectionStorageMasks "
"structure with a ProtectionStorageMasks tag.",
payloads.CreateRequestPayload,
**kwargs
)
@ -206,7 +221,22 @@ class TestCreateRequestPayload(testtools.TestCase):
)
self.assertRaisesRegex(
TypeError,
"The protection storage masks must be an integer.",
"The protection storage masks must be a ProtectionStorageMasks "
"structure.",
setattr,
*args
)
args = (
payloads.CreateRequestPayload(),
"protection_storage_masks",
objects.ProtectionStorageMasks(
tag=enums.Tags.COMMON_PROTECTION_STORAGE_MASKS
)
)
self.assertRaisesRegex(
TypeError,
"The protection storage masks must be a ProtectionStorageMasks "
"structure with a ProtectionStorageMasks tag.",
setattr,
*args
)
@ -325,7 +355,10 @@ class TestCreateRequestPayload(testtools.TestCase):
),
payload.template_attribute
)
self.assertEqual(3, payload.protection_storage_masks)
self.assertEqual(
objects.ProtectionStorageMasks(protection_storage_masks=[3]),
payload.protection_storage_masks
)
def test_read_missing_object_type(self):
"""
@ -479,10 +512,14 @@ class TestCreateRequestPayload(testtools.TestCase):
)
]
),
protection_storage_masks=(
protection_storage_masks=objects.ProtectionStorageMasks(
protection_storage_masks=[
(
enums.ProtectionStorageMask.SOFTWARE.value |
enums.ProtectionStorageMask.HARDWARE.value
)
]
)
)
stream = utils.BytearrayStream()
@ -602,16 +639,21 @@ class TestCreateRequestPayload(testtools.TestCase):
)
]
),
protection_storage_masks=(
protection_storage_masks=objects.ProtectionStorageMasks(
protection_storage_masks=[
(
enums.ProtectionStorageMask.SOFTWARE.value |
enums.ProtectionStorageMask.HARDWARE.value
)
]
)
)
self.assertEqual(
"CreateRequestPayload("
"object_type=ObjectType.SYMMETRIC_KEY, "
"template_attribute=Struct(), "
"protection_storage_masks=3)",
"protection_storage_masks=ProtectionStorageMasks("
"protection_storage_masks=[3]))",
repr(payload)
)
@ -644,16 +686,20 @@ class TestCreateRequestPayload(testtools.TestCase):
)
]
),
protection_storage_masks=(
protection_storage_masks=objects.ProtectionStorageMasks(
protection_storage_masks=[
(
enums.ProtectionStorageMask.SOFTWARE.value |
enums.ProtectionStorageMask.HARDWARE.value
)
]
)
)
self.assertEqual(
'{'
'"object_type": ObjectType.SYMMETRIC_KEY, '
'"template_attribute": Struct(), '
'"protection_storage_masks": 3'
'"protection_storage_masks": {"protection_storage_masks": [3]}'
'}',
str(payload)
)
@ -706,10 +752,14 @@ class TestCreateRequestPayload(testtools.TestCase):
)
]
),
protection_storage_masks=(
protection_storage_masks=objects.ProtectionStorageMasks(
protection_storage_masks=[
(
enums.ProtectionStorageMask.SOFTWARE.value |
enums.ProtectionStorageMask.HARDWARE.value
)
]
)
)
b = payloads.CreateRequestPayload(
object_type=enums.ObjectType.SYMMETRIC_KEY,
@ -748,10 +798,14 @@ class TestCreateRequestPayload(testtools.TestCase):
)
]
),
protection_storage_masks=(
protection_storage_masks=objects.ProtectionStorageMasks(
protection_storage_masks=[
(
enums.ProtectionStorageMask.SOFTWARE.value |
enums.ProtectionStorageMask.HARDWARE.value
)
]
)
)
self.assertTrue(a == b)
@ -818,16 +872,24 @@ class TestCreateRequestPayload(testtools.TestCase):
request payloads with different protection storage masks.
"""
a = payloads.CreateRequestPayload(
protection_storage_masks=(
protection_storage_masks=objects.ProtectionStorageMasks(
protection_storage_masks=[
(
enums.ProtectionStorageMask.SOFTWARE.value |
enums.ProtectionStorageMask.HARDWARE.value
)
]
)
)
b = payloads.CreateRequestPayload(
protection_storage_masks=(
protection_storage_masks=objects.ProtectionStorageMasks(
protection_storage_masks=[
(
enums.ProtectionStorageMask.ON_SYSTEM.value |
enums.ProtectionStorageMask.OFF_SYSTEM.value
)
]
)
)
self.assertFalse(a == b)
@ -892,10 +954,14 @@ class TestCreateRequestPayload(testtools.TestCase):
)
]
),
protection_storage_masks=(
protection_storage_masks=objects.ProtectionStorageMasks(
protection_storage_masks=[
(
enums.ProtectionStorageMask.SOFTWARE.value |
enums.ProtectionStorageMask.HARDWARE.value
)
]
)
)
b = payloads.CreateRequestPayload(
object_type=enums.ObjectType.SYMMETRIC_KEY,
@ -934,10 +1000,14 @@ class TestCreateRequestPayload(testtools.TestCase):
)
]
),
protection_storage_masks=(
protection_storage_masks=objects.ProtectionStorageMasks(
protection_storage_masks=[
(
enums.ProtectionStorageMask.SOFTWARE.value |
enums.ProtectionStorageMask.HARDWARE.value
)
]
)
)
self.assertFalse(a != b)
@ -1004,16 +1074,24 @@ class TestCreateRequestPayload(testtools.TestCase):
Create request payloads with different protection storage masks.
"""
a = payloads.CreateRequestPayload(
protection_storage_masks=(
protection_storage_masks=objects.ProtectionStorageMasks(
protection_storage_masks=[
(
enums.ProtectionStorageMask.SOFTWARE.value |
enums.ProtectionStorageMask.HARDWARE.value
)
]
)
)
b = payloads.CreateRequestPayload(
protection_storage_masks=(
protection_storage_masks=objects.ProtectionStorageMasks(
protection_storage_masks=[
(
enums.ProtectionStorageMask.ON_SYSTEM.value |
enums.ProtectionStorageMask.OFF_SYSTEM.value
)
]
)
)
self.assertTrue(a != b)