Updating the Register payloads to properly use protection masks

This change updates the Register 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 15:37:59 -04:00 committed by Peter Hamilton
parent bc4e4e38bd
commit ffe5d7a86c
2 changed files with 140 additions and 63 deletions

View File

@ -33,9 +33,9 @@ class RegisterRequestPayload(primitives.Struct):
object_type: The type of the object to register.
template_attribute: A group of attributes to set on the new object.
managed_object: The object to register.
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,
@ -64,9 +64,9 @@ class RegisterRequestPayload(primitives.Struct):
* secrets.SymmetricKey
* secrets.Template
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(RegisterRequestPayload, self).__init__(
@ -151,22 +151,25 @@ class RegisterRequestPayload(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):
@ -251,7 +254,7 @@ class RegisterRequestPayload(primitives.Struct):
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(
@ -366,9 +369,7 @@ class RegisterRequestPayload(primitives.Struct):
"template_attribute={}".format(repr(self.template_attribute)),
"managed_object={}".format(repr(self.managed_object)),
"protection_storage_masks={}".format(
"{}".format(
repr(self.protection_storage_masks)
) if self._protection_storage_masks else None
repr(self.protection_storage_masks)
)
])
return "RegisterRequestPayload({})".format(args)
@ -380,9 +381,7 @@ class RegisterRequestPayload(primitives.Struct):
'"template_attribute": {}'.format(self.template_attribute),
'"managed_object": {}'.format(self.managed_object),
'"protection_storage_masks": {}'.format(
"{}".format(
str(self.protection_storage_masks)
) if self._protection_storage_masks else None
str(self.protection_storage_masks)
)
]
)

View File

@ -156,9 +156,10 @@ class TestRegisterRequestPayload(testtools.TestCase):
# Certificate
# Certificate Type - X.509
# Certificate Value - See comment for the full encoding.
# 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\x03\x70'
b'\x42\x00\x79\x01\x00\x00\x03\x78'
b'\x42\x00\x57\x05\x00\x00\x00\x04\x00\x00\x00\x01\x00\x00\x00\x00'
b'\x42\x01\x25\x01\x00\x00\x00\x10'
b'\x42\x00\x2C\x02\x00\x00\x00\x04\x00\x00\x00\x03\x00\x00\x00\x00'
@ -166,7 +167,8 @@ class TestRegisterRequestPayload(testtools.TestCase):
b'\x42\x00\x1D\x05\x00\x00\x00\x04\x00\x00\x00\x01\x00\x00\x00\x00'
b'\x42\x00\x1E\x08\x00\x00\x03\x16' + self.certificate_value +
b'\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, Section 13.2.2.
@ -323,7 +325,20 @@ class TestRegisterRequestPayload(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.RegisterRequestPayload,
**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.RegisterRequestPayload,
**kwargs
)
@ -335,7 +350,22 @@ class TestRegisterRequestPayload(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.RegisterRequestPayload(),
"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
)
@ -422,7 +452,10 @@ class TestRegisterRequestPayload(testtools.TestCase):
),
payload.managed_object
)
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):
"""
@ -551,9 +584,13 @@ class TestRegisterRequestPayload(testtools.TestCase):
certificate_type=enums.CertificateType.X_509,
certificate_value=self.certificate_value
),
protection_storage_masks=(
enums.ProtectionStorageMask.SOFTWARE.value |
enums.ProtectionStorageMask.HARDWARE.value
protection_storage_masks=objects.ProtectionStorageMasks(
protection_storage_masks=[
(
enums.ProtectionStorageMask.SOFTWARE.value |
enums.ProtectionStorageMask.HARDWARE.value
)
]
)
)
@ -716,9 +753,13 @@ class TestRegisterRequestPayload(testtools.TestCase):
)
)
),
protection_storage_masks=(
enums.ProtectionStorageMask.SOFTWARE.value |
enums.ProtectionStorageMask.HARDWARE.value
protection_storage_masks=objects.ProtectionStorageMasks(
protection_storage_masks=[
(
enums.ProtectionStorageMask.SOFTWARE.value |
enums.ProtectionStorageMask.HARDWARE.value
)
]
)
)
self.assertEqual(
@ -726,7 +767,8 @@ class TestRegisterRequestPayload(testtools.TestCase):
"object_type=ObjectType.SECRET_DATA, "
"template_attribute=Struct(), "
"managed_object=Struct(), "
"protection_storage_masks=3)",
"protection_storage_masks=ProtectionStorageMasks("
"protection_storage_masks=[3]))",
repr(payload)
)
@ -769,9 +811,13 @@ class TestRegisterRequestPayload(testtools.TestCase):
)
)
),
protection_storage_masks=(
enums.ProtectionStorageMask.SOFTWARE.value |
enums.ProtectionStorageMask.HARDWARE.value
protection_storage_masks=objects.ProtectionStorageMasks(
protection_storage_masks=[
(
enums.ProtectionStorageMask.SOFTWARE.value |
enums.ProtectionStorageMask.HARDWARE.value
)
]
)
)
self.assertEqual(
@ -779,7 +825,7 @@ class TestRegisterRequestPayload(testtools.TestCase):
'"object_type": ObjectType.SECRET_DATA, '
'"template_attribute": Struct(), '
'"managed_object": Struct(), '
'"protection_storage_masks": 3'
'"protection_storage_masks": {"protection_storage_masks": [3]}'
'}',
str(payload)
)
@ -815,9 +861,13 @@ class TestRegisterRequestPayload(testtools.TestCase):
certificate_type=enums.CertificateType.X_509,
certificate_value=self.certificate_value
),
protection_storage_masks=(
enums.ProtectionStorageMask.SOFTWARE.value |
enums.ProtectionStorageMask.HARDWARE.value
protection_storage_masks=objects.ProtectionStorageMasks(
protection_storage_masks=[
(
enums.ProtectionStorageMask.SOFTWARE.value |
enums.ProtectionStorageMask.HARDWARE.value
)
]
)
)
b = payloads.RegisterRequestPayload(
@ -840,9 +890,13 @@ class TestRegisterRequestPayload(testtools.TestCase):
certificate_type=enums.CertificateType.X_509,
certificate_value=self.certificate_value
),
protection_storage_masks=(
enums.ProtectionStorageMask.SOFTWARE.value |
enums.ProtectionStorageMask.HARDWARE.value
protection_storage_masks=objects.ProtectionStorageMasks(
protection_storage_masks=[
(
enums.ProtectionStorageMask.SOFTWARE.value |
enums.ProtectionStorageMask.HARDWARE.value
)
]
)
)
@ -930,15 +984,23 @@ class TestRegisterRequestPayload(testtools.TestCase):
request payloads with different protection storage masks.
"""
a = payloads.RegisterRequestPayload(
protection_storage_masks=(
enums.ProtectionStorageMask.SOFTWARE.value |
enums.ProtectionStorageMask.HARDWARE.value
protection_storage_masks=objects.ProtectionStorageMasks(
protection_storage_masks=[
(
enums.ProtectionStorageMask.SOFTWARE.value |
enums.ProtectionStorageMask.HARDWARE.value
)
]
)
)
b = payloads.RegisterRequestPayload(
protection_storage_masks=(
enums.ProtectionStorageMask.ON_SYSTEM.value |
enums.ProtectionStorageMask.OFF_SYSTEM.value
protection_storage_masks=objects.ProtectionStorageMasks(
protection_storage_masks=[
(
enums.ProtectionStorageMask.ON_SYSTEM.value |
enums.ProtectionStorageMask.OFF_SYSTEM.value
)
]
)
)
@ -987,9 +1049,13 @@ class TestRegisterRequestPayload(testtools.TestCase):
certificate_type=enums.CertificateType.X_509,
certificate_value=self.certificate_value
),
protection_storage_masks=(
enums.ProtectionStorageMask.SOFTWARE.value |
enums.ProtectionStorageMask.HARDWARE.value
protection_storage_masks=objects.ProtectionStorageMasks(
protection_storage_masks=[
(
enums.ProtectionStorageMask.SOFTWARE.value |
enums.ProtectionStorageMask.HARDWARE.value
)
]
)
)
b = payloads.RegisterRequestPayload(
@ -1012,9 +1078,13 @@ class TestRegisterRequestPayload(testtools.TestCase):
certificate_type=enums.CertificateType.X_509,
certificate_value=self.certificate_value
),
protection_storage_masks=(
enums.ProtectionStorageMask.SOFTWARE.value |
enums.ProtectionStorageMask.HARDWARE.value
protection_storage_masks=objects.ProtectionStorageMasks(
protection_storage_masks=[
(
enums.ProtectionStorageMask.SOFTWARE.value |
enums.ProtectionStorageMask.HARDWARE.value
)
]
)
)
@ -1102,15 +1172,23 @@ class TestRegisterRequestPayload(testtools.TestCase):
Register request payloads with different protection storage masks.
"""
a = payloads.RegisterRequestPayload(
protection_storage_masks=(
enums.ProtectionStorageMask.SOFTWARE.value |
enums.ProtectionStorageMask.HARDWARE.value
protection_storage_masks=objects.ProtectionStorageMasks(
protection_storage_masks=[
(
enums.ProtectionStorageMask.SOFTWARE.value |
enums.ProtectionStorageMask.HARDWARE.value
)
]
)
)
b = payloads.RegisterRequestPayload(
protection_storage_masks=(
enums.ProtectionStorageMask.ON_SYSTEM.value |
enums.ProtectionStorageMask.OFF_SYSTEM.value
protection_storage_masks=objects.ProtectionStorageMasks(
protection_storage_masks=[
(
enums.ProtectionStorageMask.ON_SYSTEM.value |
enums.ProtectionStorageMask.OFF_SYSTEM.value
)
]
)
)