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. object_type: The type of the object to register.
template_attribute: A group of attributes to set on the new object. template_attribute: A group of attributes to set on the new object.
managed_object: The object to register. managed_object: The object to register.
protection_storage_masks: An integer representing all of the protection_storage_masks: A ProtectionStorageMasks structure
protection storage mask selections for the new object. Added in containing the storage masks permissible for the new object.
KMIP 2.0. Added in KMIP 2.0.
""" """
def __init__(self, def __init__(self,
@ -64,9 +64,9 @@ class RegisterRequestPayload(primitives.Struct):
* secrets.SymmetricKey * secrets.SymmetricKey
* secrets.Template * secrets.Template
Optional, defaults to None. Required for read/write. Optional, defaults to None. Required for read/write.
protection_storage_masks (int): An integer representing all of protection_storage_masks (structure): A ProtectionStorageMasks
the protection storage mask selections for the new object. structure containing the storage masks permissible for the new
Optional, defaults to None. Added in KMIP 2.0. object. Added in KMIP 2.0. Optional, defaults to None.
""" """
super(RegisterRequestPayload, self).__init__( super(RegisterRequestPayload, self).__init__(
@ -151,22 +151,25 @@ class RegisterRequestPayload(primitives.Struct):
@property @property
def protection_storage_masks(self): def protection_storage_masks(self):
if self._protection_storage_masks: return self._protection_storage_masks
return self._protection_storage_masks.value
return None
@protection_storage_masks.setter @protection_storage_masks.setter
def protection_storage_masks(self, value): def protection_storage_masks(self, value):
if value is None: if value is None:
self._protection_storage_masks = None self._protection_storage_masks = None
elif isinstance(value, six.integer_types): elif isinstance(value, objects.ProtectionStorageMasks):
self._protection_storage_masks = primitives.Integer( if value.tag == enums.Tags.PROTECTION_STORAGE_MASKS:
value=value, self._protection_storage_masks = value
tag=enums.Tags.PROTECTION_STORAGE_MASKS else:
) raise TypeError(
"The protection storage masks must be a "
"ProtectionStorageMasks structure with a "
"ProtectionStorageMasks tag."
)
else: else:
raise TypeError( 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): 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, enums.Tags.PROTECTION_STORAGE_MASKS,
local_buffer local_buffer
): ):
protection_storage_masks = primitives.Integer( protection_storage_masks = objects.ProtectionStorageMasks(
tag=enums.Tags.PROTECTION_STORAGE_MASKS tag=enums.Tags.PROTECTION_STORAGE_MASKS
) )
protection_storage_masks.read( protection_storage_masks.read(
@ -366,9 +369,7 @@ class RegisterRequestPayload(primitives.Struct):
"template_attribute={}".format(repr(self.template_attribute)), "template_attribute={}".format(repr(self.template_attribute)),
"managed_object={}".format(repr(self.managed_object)), "managed_object={}".format(repr(self.managed_object)),
"protection_storage_masks={}".format( "protection_storage_masks={}".format(
"{}".format( repr(self.protection_storage_masks)
repr(self.protection_storage_masks)
) if self._protection_storage_masks else None
) )
]) ])
return "RegisterRequestPayload({})".format(args) return "RegisterRequestPayload({})".format(args)
@ -380,9 +381,7 @@ class RegisterRequestPayload(primitives.Struct):
'"template_attribute": {}'.format(self.template_attribute), '"template_attribute": {}'.format(self.template_attribute),
'"managed_object": {}'.format(self.managed_object), '"managed_object": {}'.format(self.managed_object),
'"protection_storage_masks": {}'.format( '"protection_storage_masks": {}'.format(
"{}".format( str(self.protection_storage_masks)
str(self.protection_storage_masks)
) if self._protection_storage_masks else None
) )
] ]
) )

View File

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