Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update payloads for the Activate operation #314

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion kmip/core/messages/payloads/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,12 @@
# License for the specific language governing permissions and limitations
# under the License.

__all__ = ['create', 'destroy', 'get', 'locate', 'register']
from kmip.core.messages.payloads.activate import (
ActivateRequestPayload, ActivateResponsePayload
)


__all__ = [
'ActivateRequestPayload',
'ActivateResponsePayload'
]
251 changes: 163 additions & 88 deletions kmip/core/messages/payloads/activate.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,151 +13,226 @@
# License for the specific language governing permissions and limitations
# under the License.

from kmip.core import attributes
from kmip.core import enums

from kmip.core.primitives import Struct
import six

from kmip.core.utils import BytearrayStream
from kmip.core import enums
from kmip.core import primitives
from kmip.core import utils


class ActivateRequestPayload(Struct):
class ActivateRequestPayload(primitives.Struct):
"""
A request payload for the Activate operation.

The payload contains a UUID of a cryptographic object that that server
should activate. See Section 4.19 of the KMIP 1.1 specification for more
information.

Attributes:
unique_identifier: The UUID of a managed cryptographic object
unique_identifier: The unique ID of the managed object to activate
on the server.
"""
def __init__(self,
unique_identifier=None):
"""
Construct a ActivateRequestPayload object.
Construct an Activate request payload struct.

Args:
unique_identifier (UniqueIdentifier): The UUID of a managed
cryptographic object.
unique_identifier (string): The ID of the managed object (e.g., a
symmetric key) to activate. Optional, defaults to None.
"""
super(ActivateRequestPayload, self).__init__(
tag=enums.Tags.REQUEST_PAYLOAD)

self._unique_identifier = None

self.unique_identifier = unique_identifier
self.validate()

def read(self, istream):
@property
def unique_identifier(self):
if self._unique_identifier:
return self._unique_identifier.value
else:
return None

@unique_identifier.setter
def unique_identifier(self, value):
if value is None:
self._unique_identifier = None
elif isinstance(value, six.string_types):
self._unique_identifier = primitives.TextString(
value=value,
tag=enums.Tags.UNIQUE_IDENTIFIER
)
else:
raise TypeError("Unique identifier must be a string.")

def read(self, input_stream):
"""
Read the data encoding the ActivateRequestPayload object and decode it
Read the data encoding the Activate request payload and decode it
into its constituent parts.
Args:
istream (Stream): A data stream containing encoded object data,
supporting a read method; usually a BytearrayStream object.
input_stream (stream): A data stream containing encoded object
data, supporting a read method; usually a BytearrayStream
object.
"""
super(ActivateRequestPayload, self).read(istream)
tstream = BytearrayStream(istream.read(self.length))
super(ActivateRequestPayload, self).read(input_stream)
local_stream = utils.BytearrayStream(input_stream.read(self.length))

self.unique_identifier = attributes.UniqueIdentifier()
self.unique_identifier.read(tstream)
if self.is_tag_next(enums.Tags.UNIQUE_IDENTIFIER, local_stream):
self._unique_identifier = primitives.TextString(
tag=enums.Tags.UNIQUE_IDENTIFIER
)
self._unique_identifier.read(local_stream)

self.is_oversized(tstream)
self.validate()
self.is_oversized(local_stream)

def write(self, ostream):
def write(self, output_stream):
"""
Write the data encoding the ActivateRequestPayload object to a stream.
Write the data encoding the Activate request payload to a stream.

Args:
ostream (Stream): A data stream in which to encode object data,
supporting a write method; usually a BytearrayStream object.
output_stream (stream): A data stream in which to encode object
data, supporting a write method; usually a BytearrayStream
object.
"""
tstream = BytearrayStream()
local_stream = utils.BytearrayStream()

# Write the contents of the request payload
if self.unique_identifier is not None:
self.unique_identifier.write(tstream)
if self._unique_identifier is not None:
self._unique_identifier.write(local_stream)

# Write the length and value of the request payload
self.length = tstream.length()
super(ActivateRequestPayload, self).write(ostream)
ostream.write(tstream.buffer)
self.length = local_stream.length()
super(ActivateRequestPayload, self).write(output_stream)
output_stream.write(local_stream.buffer)

def validate(self):
"""
Error check the attributes of the ActivateRequestPayload object.
"""
if self.unique_identifier is not None:
if not isinstance(self.unique_identifier,
attributes.UniqueIdentifier):
msg = "invalid unique identifier"
raise TypeError(msg)
def __eq__(self, other):
if isinstance(other, ActivateRequestPayload):
if self.unique_identifier != other.unique_identifier:
return False
else:
return True
else:
return NotImplemented

def __ne__(self, other):
if isinstance(other, ActivateRequestPayload):
return not (self == other)
else:
return NotImplemented

def __repr__(self):
arg = "unique_identifier='{0}'".format(self.unique_identifier)
return "ActivateRequestPayload({0})".format(arg)

def __str__(self):
return str({'unique_identifier': self.unique_identifier})

class ActivateResponsePayload(Struct):

class ActivateResponsePayload(primitives.Struct):
"""
A response payload for the Activate operation.

The payload contains the server response to the initial Activate request.
See Section 4.19 of the KMIP 1.1 specification for more information.

Attributes:
unique_identifier: The UUID of a managed cryptographic object.
unique_identifier: The unique ID of the managed object that was
activated on the server.
"""
def __init__(self,
unique_identifier=None):
"""
Construct a ActivateResponsePayload object.
Construct an Activate response payload struct.

Args:
unique_identifier (UniqueIdentifier): The UUID of a managed
cryptographic object.
unique_identifier (string): The ID of the managed object (e.g., a
symmetric key) that was activated. Optional, defaults to None.
Required for read/write.
"""
super(ActivateResponsePayload, self).__init__(
tag=enums.Tags.RESPONSE_PAYLOAD)
if unique_identifier is None:
self.unique_identifier = attributes.UniqueIdentifier()

self._unique_identifier = None

self.unique_identifier = unique_identifier

@property
def unique_identifier(self):
if self._unique_identifier:
return self._unique_identifier.value
else:
self.unique_identifier = unique_identifier
self.validate()
return None

@unique_identifier.setter
def unique_identifier(self, value):
if value is None:
self._unique_identifier = None
elif isinstance(value, six.string_types):
self._unique_identifier = primitives.TextString(
value=value,
tag=enums.Tags.UNIQUE_IDENTIFIER
)
else:
raise TypeError("Unique identifier must be a string.")

def read(self, istream):
def read(self, input_stream):
"""
Read the data encoding the ActivateResponsePayload object and decode it
Read the data encoding the Activate response payload and decode it
into its constituent parts.

Args:
istream (Stream): A data stream containing encoded object data,
supporting a read method; usually a BytearrayStream object.
"""
super(ActivateResponsePayload, self).read(istream)
tstream = BytearrayStream(istream.read(self.length))

self.unique_identifier = attributes.UniqueIdentifier()
self.unique_identifier.read(tstream)
input_stream (stream): A data stream containing encoded object
data, supporting a read method; usually a BytearrayStream
object.
"""
super(ActivateResponsePayload, self).read(input_stream)
local_stream = utils.BytearrayStream(input_stream.read(self.length))

if self.is_tag_next(enums.Tags.UNIQUE_IDENTIFIER, local_stream):
self._unique_identifier = primitives.TextString(
tag=enums.Tags.UNIQUE_IDENTIFIER
)
self._unique_identifier.read(local_stream)
else:
raise ValueError(
"Parsed payload encoding is missing the unique identifier "
"field."
)

self.is_oversized(tstream)
self.validate()
self.is_oversized(local_stream)

def write(self, ostream):
def write(self, output_stream):
"""
Write the data encoding the ActivateResponsePayload object to a stream.
Write the data encoding the Activate response payload to a stream.

Args:
ostream (Stream): A data stream in which to encode object data,
supporting a write method; usually a BytearrayStream object.
output_stream (stream): A data stream in which to encode object
data, supporting a write method; usually a BytearrayStream
object.
"""
tstream = BytearrayStream()
local_stream = utils.BytearrayStream()

if self.unique_identifier:
self._unique_identifier.write(local_stream)
else:
raise ValueError(
"Payload is missing the unique identifier field."
)
self.length = local_stream.length()
super(ActivateResponsePayload, self).write(output_stream)
output_stream.write(local_stream.buffer)

def __eq__(self, other):
if isinstance(other, ActivateResponsePayload):
if self.unique_identifier != other.unique_identifier:
return False
else:
return True
else:
return NotImplemented

# Write the contents of the response payload
self.unique_identifier.write(tstream)
def __ne__(self, other):
if isinstance(other, ActivateResponsePayload):
return not (self == other)
else:
return NotImplemented

# Write the length and value of the request payload
self.length = tstream.length()
super(ActivateResponsePayload, self).write(ostream)
ostream.write(tstream.buffer)
def __repr__(self):
arg = "unique_identifier='{0}'".format(self.unique_identifier)
return "ActivateResponsePayload({0})".format(arg)

def validate(self):
"""
Error check the attributes of the ActivateRequestPayload object.
"""
if not isinstance(self.unique_identifier, attributes.UniqueIdentifier):
msg = "invalid unique identifier"
raise TypeError(msg)
def __str__(self):
return str({'unique_identifier': self.unique_identifier})
8 changes: 3 additions & 5 deletions kmip/services/kmip_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -884,11 +884,9 @@ def _get(self,
def _activate(self, unique_identifier=None, credential=None):
operation = Operation(OperationEnum.ACTIVATE)

uuid = None
if unique_identifier is not None:
uuid = attr.UniqueIdentifier(unique_identifier)

payload = activate.ActivateRequestPayload(unique_identifier=uuid)
payload = activate.ActivateRequestPayload(
unique_identifier=unique_identifier
)

batch_item = messages.RequestBatchItem(operation=operation,
request_payload=payload)
Expand Down
4 changes: 2 additions & 2 deletions kmip/services/server/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -1775,7 +1775,7 @@ def _process_activate(self, payload):
self._logger.info("Processing operation: Activate")

if payload.unique_identifier:
unique_identifier = payload.unique_identifier.value
unique_identifier = payload.unique_identifier
else:
unique_identifier = self._id_placeholder

Expand All @@ -1802,7 +1802,7 @@ def _process_activate(self, payload):
self._data_session.commit()

response_payload = activate.ActivateResponsePayload(
unique_identifier=attributes.UniqueIdentifier(unique_identifier)
unique_identifier=unique_identifier
)

return response_payload
Expand Down
Loading