From 54f3688a14bcc442b270765e21732b77d18b2a72 Mon Sep 17 00:00:00 2001 From: Peter Hamilton Date: Mon, 25 Feb 2019 14:38:03 -0500 Subject: [PATCH] Block Attributes encoding and decoding on non KMIP 2.0 calls This change adds a check to the read and write methods of the new Attributes object that raises a new VersionNotSupported exception if KMIP 2.0 is not the version used for encoding and decoding. The Attributes object is not defined for older versions of KMIP and therefore cannot be correctly encoded or decoded in those use cases. --- kmip/core/exceptions.py | 6 +++ kmip/core/objects.py | 18 +++++++++ kmip/tests/unit/core/objects/test_objects.py | 42 ++++++++++++++++++++ 3 files changed, 66 insertions(+) diff --git a/kmip/core/exceptions.py b/kmip/core/exceptions.py index 9c1e952..b16623a 100644 --- a/kmip/core/exceptions.py +++ b/kmip/core/exceptions.py @@ -324,6 +324,12 @@ class ShutdownError(Exception): """ +class VersionNotSupported(Exception): + """ + An error generated when an unsupported KMIP version is referenced. + """ + + class StreamNotEmptyError(Exception): def __init__(self, cls, extra): super(StreamNotEmptyError, self).__init__() diff --git a/kmip/core/objects.py b/kmip/core/objects.py index 718499d..76ac378 100644 --- a/kmip/core/objects.py +++ b/kmip/core/objects.py @@ -258,7 +258,16 @@ class Attributes(primitives.Struct): Raises: AttributeNotSupported: Raised if an unsupported attribute is encountered while decoding. + VersionNotSupported: Raised when a KMIP version is provided that + does not support the Attributes object. """ + if kmip_version < enums.KMIPVersion.KMIP_2_0: + raise exceptions.VersionNotSupported( + "KMIP {} does not support the Attributes object.".format( + kmip_version.value + ) + ) + super(Attributes, self).read(input_stream, kmip_version=kmip_version) local_stream = BytearrayStream(input_stream.read(self.length)) @@ -297,7 +306,16 @@ class Attributes(primitives.Struct): Raises: AttributeNotSupported: Raised if an unsupported attribute is found in the attribute list while encoding. + VersionNotSupported: Raised when a KMIP version is provided that + does not support the Attributes object. """ + if kmip_version < enums.KMIPVersion.KMIP_2_0: + raise exceptions.VersionNotSupported( + "KMIP {} does not support the Attributes object.".format( + kmip_version.value + ) + ) + local_stream = BytearrayStream() for attribute in self._attributes: diff --git a/kmip/tests/unit/core/objects/test_objects.py b/kmip/tests/unit/core/objects/test_objects.py index e705b34..1a98a5a 100644 --- a/kmip/tests/unit/core/objects/test_objects.py +++ b/kmip/tests/unit/core/objects/test_objects.py @@ -400,6 +400,24 @@ class TestAttributes(TestCase): self.assertIsInstance(attr_2, primitives.Integer) self.assertEqual(128, attr_2.value) + def test_read_version_not_supported(self): + """ + Test that a VersionNotSupported error is raised when an unsupported + KMIP version is provided while reading in an Attributes structure from + a data stream. The Attributes structure is only supported in KMIP 2.0+. + """ + attrs = objects.Attributes() + + args = (self.full_encoding, ) + kwargs = {"kmip_version": enums.KMIPVersion.KMIP_1_2} + self.assertRaisesRegex( + exceptions.VersionNotSupported, + "KMIP 1.2 does not support the Attributes object.", + attrs.read, + *args, + **kwargs + ) + def test_write(self): """ Test that an Attributes structure can be correctly written to a data @@ -489,6 +507,30 @@ class TestAttributes(TestCase): self.assertEqual(len(self.alt_encoding), len(stream)) self.assertEqual(str(self.alt_encoding), str(stream)) + def test_write_version_not_supported(self): + """ + Test that a VersionNotSupported error is raised when an unsupported + KMIP version is provided while writing an Attributes structure to a + data stream. The Attributes structure is only supported in KMIP 2.0+. + """ + attrs = objects.Attributes(attributes=[ + primitives.TextString( + "default", + tag=enums.Tags.OPERATION_POLICY_NAME + ) + ]) + + stream = utils.BytearrayStream() + args = (stream, ) + kwargs = {"kmip_version": enums.KMIPVersion.KMIP_1_1} + self.assertRaisesRegex( + exceptions.VersionNotSupported, + "KMIP 1.1 does not support the Attributes object.", + attrs.write, + *args, + **kwargs + ) + def test_repr(self): """ Test that repr can be applied to an Attributes structure.