2014-08-11 22:34:30 +02:00
|
|
|
# Copyright (c) 2014 The Johns Hopkins University/Applied Physics Laboratory
|
|
|
|
# All Rights Reserved.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License. You may obtain
|
|
|
|
# a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
# License for the specific language governing permissions and limitations
|
|
|
|
# under the License.
|
|
|
|
|
|
|
|
from kmip.services.results import CreateResult
|
2015-01-09 19:11:05 +01:00
|
|
|
from kmip.services.results import CreateKeyPairResult
|
2014-08-11 22:34:30 +02:00
|
|
|
from kmip.services.results import DestroyResult
|
2015-01-23 21:26:28 +01:00
|
|
|
from kmip.services.results import DiscoverVersionsResult
|
2015-01-21 17:41:08 +01:00
|
|
|
from kmip.services.results import GetResult
|
2014-09-04 20:42:20 +02:00
|
|
|
from kmip.services.results import LocateResult
|
2015-02-23 23:18:05 +01:00
|
|
|
from kmip.services.results import QueryResult
|
2015-01-21 17:41:08 +01:00
|
|
|
from kmip.services.results import RegisterResult
|
|
|
|
from kmip.services.results import RekeyKeyPairResult
|
2014-08-11 22:34:30 +02:00
|
|
|
|
|
|
|
from kmip.core import attributes as attr
|
|
|
|
|
2015-04-09 16:45:59 +02:00
|
|
|
from kmip.core.enums import AuthenticationSuite
|
|
|
|
from kmip.core.enums import ConformanceClause
|
2014-10-24 04:28:20 +02:00
|
|
|
from kmip.core.enums import CredentialType
|
2015-04-09 16:45:59 +02:00
|
|
|
from kmip.core.enums import Operation as OperationEnum
|
2014-10-24 04:28:20 +02:00
|
|
|
|
|
|
|
from kmip.core.factories.credentials import CredentialFactory
|
2014-08-11 22:34:30 +02:00
|
|
|
|
|
|
|
from kmip.core import objects
|
|
|
|
from kmip.core.server import KMIP
|
|
|
|
|
|
|
|
from kmip.core.messages.contents import Authentication
|
|
|
|
from kmip.core.messages.contents import BatchCount
|
|
|
|
from kmip.core.messages.contents import Operation
|
2015-01-21 17:41:08 +01:00
|
|
|
from kmip.core.messages.contents import ProtocolVersion
|
2014-08-11 22:34:30 +02:00
|
|
|
|
|
|
|
from kmip.core.messages import messages
|
2014-11-06 16:59:33 +01:00
|
|
|
|
|
|
|
from kmip.core.messages.payloads import create
|
2015-01-09 19:11:05 +01:00
|
|
|
from kmip.core.messages.payloads import create_key_pair
|
2015-01-21 17:41:08 +01:00
|
|
|
from kmip.core.messages.payloads import destroy
|
2015-01-23 21:26:28 +01:00
|
|
|
from kmip.core.messages.payloads import discover_versions
|
2014-11-06 16:59:33 +01:00
|
|
|
from kmip.core.messages.payloads import get
|
|
|
|
from kmip.core.messages.payloads import locate
|
2015-02-23 23:18:05 +01:00
|
|
|
from kmip.core.messages.payloads import query
|
2015-01-21 17:41:08 +01:00
|
|
|
from kmip.core.messages.payloads import rekey_key_pair
|
|
|
|
from kmip.core.messages.payloads import register
|
2014-08-11 22:34:30 +02:00
|
|
|
|
|
|
|
from kmip.services.kmip_protocol import KMIPProtocol
|
|
|
|
|
2014-09-17 09:24:58 +02:00
|
|
|
from kmip.core.config_helper import ConfigHelper
|
|
|
|
|
2014-08-11 22:34:30 +02:00
|
|
|
from kmip.core.utils import BytearrayStream
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import logging.config
|
2014-08-28 20:04:23 +02:00
|
|
|
import os
|
|
|
|
import socket
|
|
|
|
import ssl
|
|
|
|
|
|
|
|
FILE_PATH = os.path.dirname(os.path.abspath(__file__))
|
2014-09-17 09:24:58 +02:00
|
|
|
CONFIG_FILE = os.path.normpath(os.path.join(FILE_PATH, '../kmipconfig.ini'))
|
2014-08-11 22:34:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
class KMIPProxy(KMIP):
|
|
|
|
|
2014-09-17 09:24:58 +02:00
|
|
|
def __init__(self, host=None, port=None, keyfile=None, certfile=None,
|
|
|
|
cert_reqs=None, ssl_version=None, ca_certs=None,
|
|
|
|
do_handshake_on_connect=None,
|
2014-10-24 04:28:20 +02:00
|
|
|
suppress_ragged_eofs=None,
|
2015-02-27 16:48:07 +01:00
|
|
|
username=None, password=None, config='client'):
|
2015-06-02 17:16:42 +02:00
|
|
|
super(KMIPProxy, self).__init__()
|
2014-08-11 22:34:30 +02:00
|
|
|
self.logger = logging.getLogger(__name__)
|
2014-10-24 04:28:20 +02:00
|
|
|
self.credential_factory = CredentialFactory()
|
2015-02-27 16:48:07 +01:00
|
|
|
self.config = config
|
2014-08-28 20:04:23 +02:00
|
|
|
|
2014-09-17 09:24:58 +02:00
|
|
|
self._set_variables(host, port, keyfile, certfile,
|
|
|
|
cert_reqs, ssl_version, ca_certs,
|
2014-10-24 04:28:20 +02:00
|
|
|
do_handshake_on_connect, suppress_ragged_eofs,
|
|
|
|
username, password)
|
2015-01-09 19:11:05 +01:00
|
|
|
self.batch_items = []
|
|
|
|
|
2015-04-09 16:45:59 +02:00
|
|
|
self.conformance_clauses = [
|
|
|
|
ConformanceClause.DISCOVER_VERSIONS]
|
|
|
|
|
|
|
|
self.authentication_suites = [
|
|
|
|
AuthenticationSuite.BASIC,
|
|
|
|
AuthenticationSuite.TLS12]
|
|
|
|
|
|
|
|
def get_supported_conformance_clauses(self):
|
|
|
|
"""
|
|
|
|
Get the list of conformance clauses supported by the client.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
list: A shallow copy of the list of supported conformance clauses.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
>>> client.get_supported_conformance_clauses()
|
|
|
|
[<ConformanceClause.DISCOVER_VERSIONS: 1>]
|
|
|
|
"""
|
|
|
|
return self.conformance_clauses[:]
|
|
|
|
|
|
|
|
def get_supported_authentication_suites(self):
|
|
|
|
"""
|
|
|
|
Get the list of authentication suites supported by the client.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
list: A shallow copy of the list of supported authentication
|
|
|
|
suites.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
>>> client.get_supported_authentication_suites()
|
|
|
|
[<AuthenticationSuite.BASIC: 1>, <AuthenticationSuite.TLS12: 2>]
|
|
|
|
"""
|
|
|
|
return self.authentication_suites[:]
|
|
|
|
|
|
|
|
def is_conformance_clause_supported(self, conformance_clause):
|
|
|
|
"""
|
|
|
|
Check if a ConformanceClause is supported by the client.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
conformance_clause (ConformanceClause): A ConformanceClause
|
|
|
|
enumeration to check against the list of supported
|
|
|
|
ConformanceClauses.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
bool: True if the ConformanceClause is supported, False otherwise.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
>>> clause = ConformanceClause.DISCOVER_VERSIONS
|
|
|
|
>>> client.is_conformance_clause_supported(clause)
|
|
|
|
True
|
|
|
|
>>> clause = ConformanceClause.BASELINE
|
|
|
|
>>> client.is_conformance_clause_supported(clause)
|
|
|
|
False
|
|
|
|
"""
|
|
|
|
return conformance_clause in self.conformance_clauses
|
|
|
|
|
|
|
|
def is_authentication_suite_supported(self, authentication_suite):
|
|
|
|
"""
|
|
|
|
Check if an AuthenticationSuite is supported by the client.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
authentication_suite (AuthenticationSuite): An AuthenticationSuite
|
|
|
|
enumeration to check against the list of supported
|
|
|
|
AuthenticationSuites.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
bool: True if the AuthenticationSuite is supported, False
|
|
|
|
otherwise.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
>>> suite = AuthenticationSuite.BASIC
|
|
|
|
>>> client.is_authentication_suite_supported(suite)
|
|
|
|
True
|
|
|
|
>>> suite = AuthenticationSuite.TLS12
|
|
|
|
>>> client.is_authentication_suite_supported(suite)
|
|
|
|
False
|
|
|
|
"""
|
|
|
|
return authentication_suite in self.authentication_suites
|
|
|
|
|
|
|
|
def is_profile_supported(self, conformance_clause, authentication_suite):
|
|
|
|
"""
|
|
|
|
Check if a profile is supported by the client.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
conformance_clause (ConformanceClause):
|
|
|
|
authentication_suite (AuthenticationSuite):
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
bool: True if the profile is supported, False otherwise.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
>>> client.is_profile_supported(
|
|
|
|
... ConformanceClause.DISCOVER_VERSIONS,
|
|
|
|
... AuthenticationSuite.BASIC)
|
|
|
|
True
|
|
|
|
"""
|
|
|
|
return (self.is_conformance_clause_supported(conformance_clause) and
|
|
|
|
self.is_authentication_suite_supported(authentication_suite))
|
|
|
|
|
2015-01-09 19:11:05 +01:00
|
|
|
def open(self):
|
2014-08-28 20:04:23 +02:00
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
2015-01-19 15:37:32 +01:00
|
|
|
|
|
|
|
self.logger.debug("KMIPProxy keyfile: {0}".format(self.keyfile))
|
|
|
|
self.logger.debug("KMIPProxy certfile: {0}".format(self.certfile))
|
|
|
|
self.logger.debug(
|
|
|
|
"KMIPProxy cert_reqs: {0} (CERT_REQUIRED: {1})".format(
|
|
|
|
self.cert_reqs, ssl.CERT_REQUIRED))
|
|
|
|
self.logger.debug(
|
|
|
|
"KMIPProxy ssl_version: {0} (PROTOCOL_SSLv23: {1})".format(
|
|
|
|
self.ssl_version, ssl.PROTOCOL_SSLv23))
|
|
|
|
self.logger.debug("KMIPProxy ca_certs: {0}".format(self.ca_certs))
|
|
|
|
self.logger.debug("KMIPProxy do_handshake_on_connect: {0}".format(
|
|
|
|
self.do_handshake_on_connect))
|
|
|
|
self.logger.debug("KMIPProxy suppress_ragged_eofs: {0}".format(
|
|
|
|
self.suppress_ragged_eofs))
|
|
|
|
|
2014-09-17 09:24:58 +02:00
|
|
|
self.socket = ssl.wrap_socket(
|
|
|
|
sock,
|
|
|
|
keyfile=self.keyfile,
|
|
|
|
certfile=self.certfile,
|
|
|
|
cert_reqs=self.cert_reqs,
|
|
|
|
ssl_version=self.ssl_version,
|
|
|
|
ca_certs=self.ca_certs,
|
|
|
|
do_handshake_on_connect=self.do_handshake_on_connect,
|
|
|
|
suppress_ragged_eofs=self.suppress_ragged_eofs)
|
2014-08-28 20:04:23 +02:00
|
|
|
self.protocol = KMIPProtocol(self.socket)
|
2014-08-11 22:34:30 +02:00
|
|
|
|
2014-09-17 09:24:58 +02:00
|
|
|
self.socket.connect((self.host, self.port))
|
2014-08-11 22:34:30 +02:00
|
|
|
|
|
|
|
def close(self):
|
2014-08-28 20:04:23 +02:00
|
|
|
self.socket.shutdown(socket.SHUT_RDWR)
|
2014-08-11 22:34:30 +02:00
|
|
|
|
|
|
|
def create(self, object_type, template_attribute, credential=None):
|
|
|
|
object_type = attr.ObjectType(object_type)
|
|
|
|
return self._create(object_type=object_type,
|
|
|
|
template_attribute=template_attribute,
|
|
|
|
credential=credential)
|
|
|
|
|
2015-01-09 19:11:05 +01:00
|
|
|
def create_key_pair(self, batch=False, common_template_attribute=None,
|
|
|
|
private_key_template_attribute=None,
|
|
|
|
public_key_template_attribute=None, credential=None):
|
|
|
|
batch_item = self._build_create_key_pair_batch_item(
|
|
|
|
common_template_attribute, private_key_template_attribute,
|
|
|
|
public_key_template_attribute)
|
|
|
|
|
|
|
|
if batch:
|
|
|
|
self.batch_items.append(batch_item)
|
|
|
|
else:
|
|
|
|
request = self._build_request_message(credential, [batch_item])
|
|
|
|
response = self._send_and_receive_message(request)
|
|
|
|
results = self._process_batch_items(response)
|
|
|
|
return results[0]
|
|
|
|
|
2014-08-11 22:34:30 +02:00
|
|
|
def get(self, uuid=None, key_format_type=None, key_compression_type=None,
|
|
|
|
key_wrapping_specification=None, credential=None):
|
2015-03-13 20:01:52 +01:00
|
|
|
return self._get(
|
|
|
|
unique_identifier=uuid,
|
|
|
|
key_format_type=key_format_type,
|
|
|
|
key_compression_type=key_compression_type,
|
|
|
|
key_wrapping_specification=key_wrapping_specification,
|
|
|
|
credential=credential)
|
2014-08-11 22:34:30 +02:00
|
|
|
|
|
|
|
def destroy(self, uuid, credential=None):
|
|
|
|
return self._destroy(unique_identifier=uuid,
|
|
|
|
credential=credential)
|
|
|
|
|
|
|
|
def register(self, object_type, template_attribute, secret,
|
|
|
|
credential=None):
|
|
|
|
object_type = attr.ObjectType(object_type)
|
|
|
|
return self._register(object_type=object_type,
|
|
|
|
template_attribute=template_attribute,
|
|
|
|
secret=secret,
|
|
|
|
credential=credential)
|
|
|
|
|
2015-01-21 17:41:08 +01:00
|
|
|
def rekey_key_pair(self, batch=False, private_key_uuid=None, offset=None,
|
|
|
|
common_template_attribute=None,
|
|
|
|
private_key_template_attribute=None,
|
|
|
|
public_key_template_attribute=None, credential=None):
|
|
|
|
batch_item = self._build_rekey_key_pair_batch_item(
|
|
|
|
private_key_uuid, offset, common_template_attribute,
|
|
|
|
private_key_template_attribute, public_key_template_attribute)
|
|
|
|
|
|
|
|
if batch:
|
|
|
|
self.batch_items.append(batch_item)
|
|
|
|
else:
|
|
|
|
request = self._build_request_message(credential, [batch_item])
|
|
|
|
response = self._send_and_receive_message(request)
|
|
|
|
results = self._process_batch_items(response)
|
|
|
|
return results[0]
|
|
|
|
|
2014-09-04 20:42:20 +02:00
|
|
|
def locate(self, maximum_items=None, storage_status_mask=None,
|
|
|
|
object_group_member=None, attributes=None, credential=None):
|
|
|
|
return self._locate(maximum_items=maximum_items,
|
|
|
|
storage_status_mask=storage_status_mask,
|
|
|
|
object_group_member=object_group_member,
|
|
|
|
attributes=attributes, credential=credential)
|
|
|
|
|
2015-02-23 23:18:05 +01:00
|
|
|
def query(self, batch=False, query_functions=None, credential=None):
|
|
|
|
"""
|
|
|
|
Send a Query request to the server.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
batch (boolean): A flag indicating if the operation should be sent
|
|
|
|
with a batch of additional operations. Defaults to False.
|
|
|
|
query_functions (list): A list of QueryFunction enumerations
|
|
|
|
indicating what information the client wants from the server.
|
|
|
|
Optional, defaults to None.
|
|
|
|
credential (Credential): A Credential object containing
|
|
|
|
authentication information for the server. Optional, defaults
|
|
|
|
to None.
|
|
|
|
"""
|
|
|
|
batch_item = self._build_query_batch_item(query_functions)
|
|
|
|
|
|
|
|
# TODO (peter-hamilton): Replace this with official client batch mode.
|
|
|
|
if batch:
|
|
|
|
self.batch_items.append(batch_item)
|
|
|
|
else:
|
|
|
|
request = self._build_request_message(credential, [batch_item])
|
|
|
|
response = self._send_and_receive_message(request)
|
|
|
|
results = self._process_batch_items(response)
|
|
|
|
return results[0]
|
|
|
|
|
2015-01-23 21:26:28 +01:00
|
|
|
def discover_versions(self, batch=False, protocol_versions=None,
|
|
|
|
credential=None):
|
|
|
|
batch_item = self._build_discover_versions_batch_item(
|
|
|
|
protocol_versions)
|
|
|
|
|
|
|
|
if batch:
|
|
|
|
self.batch_items.append(batch_item)
|
|
|
|
else:
|
|
|
|
request = self._build_request_message(credential, [batch_item])
|
|
|
|
response = self._send_and_receive_message(request)
|
|
|
|
results = self._process_batch_items(response)
|
|
|
|
return results[0]
|
|
|
|
|
2014-08-11 22:34:30 +02:00
|
|
|
def _create(self,
|
|
|
|
object_type=None,
|
|
|
|
template_attribute=None,
|
|
|
|
credential=None):
|
|
|
|
operation = Operation(OperationEnum.CREATE)
|
|
|
|
|
|
|
|
if object_type is None:
|
|
|
|
raise ValueError('object_type cannot be None')
|
|
|
|
|
2014-11-06 16:59:33 +01:00
|
|
|
req_pl = create.CreateRequestPayload(
|
2014-08-11 22:34:30 +02:00
|
|
|
object_type=object_type,
|
|
|
|
template_attribute=template_attribute)
|
|
|
|
batch_item = messages.RequestBatchItem(operation=operation,
|
|
|
|
request_payload=req_pl)
|
|
|
|
|
|
|
|
message = self._build_request_message(credential, [batch_item])
|
|
|
|
self._send_message(message)
|
|
|
|
message = messages.ResponseMessage()
|
|
|
|
data = self._receive_message()
|
|
|
|
message.read(data)
|
|
|
|
batch_items = message.batch_items
|
|
|
|
batch_item = batch_items[0]
|
|
|
|
payload = batch_item.response_payload
|
|
|
|
|
|
|
|
if payload is None:
|
|
|
|
payload_unique_identifier = None
|
|
|
|
payload_template_attribute = None
|
|
|
|
payload_object_type = None
|
|
|
|
else:
|
|
|
|
payload_unique_identifier = payload.unique_identifier
|
|
|
|
payload_template_attribute = payload.template_attribute
|
|
|
|
payload_object_type = payload.object_type
|
|
|
|
|
|
|
|
result = CreateResult(batch_item.result_status,
|
|
|
|
batch_item.result_reason,
|
|
|
|
batch_item.result_message,
|
|
|
|
payload_object_type,
|
|
|
|
payload_unique_identifier,
|
|
|
|
payload_template_attribute)
|
|
|
|
return result
|
|
|
|
|
2015-01-09 19:11:05 +01:00
|
|
|
def _build_create_key_pair_batch_item(self, common_template_attribute=None,
|
|
|
|
private_key_template_attribute=None,
|
|
|
|
public_key_template_attribute=None):
|
|
|
|
operation = Operation(OperationEnum.CREATE_KEY_PAIR)
|
|
|
|
payload = create_key_pair.CreateKeyPairRequestPayload(
|
|
|
|
common_template_attribute=common_template_attribute,
|
|
|
|
private_key_template_attribute=private_key_template_attribute,
|
|
|
|
public_key_template_attribute=public_key_template_attribute)
|
|
|
|
batch_item = messages.RequestBatchItem(
|
|
|
|
operation=operation, request_payload=payload)
|
|
|
|
return batch_item
|
|
|
|
|
2015-01-21 17:41:08 +01:00
|
|
|
def _build_rekey_key_pair_batch_item(self,
|
|
|
|
private_key_uuid=None, offset=None,
|
|
|
|
common_template_attribute=None,
|
|
|
|
private_key_template_attribute=None,
|
|
|
|
public_key_template_attribute=None):
|
|
|
|
operation = Operation(OperationEnum.REKEY_KEY_PAIR)
|
|
|
|
payload = rekey_key_pair.RekeyKeyPairRequestPayload(
|
|
|
|
private_key_uuid, offset,
|
|
|
|
common_template_attribute=common_template_attribute,
|
|
|
|
private_key_template_attribute=private_key_template_attribute,
|
|
|
|
public_key_template_attribute=public_key_template_attribute)
|
|
|
|
batch_item = messages.RequestBatchItem(
|
|
|
|
operation=operation, request_payload=payload)
|
|
|
|
return batch_item
|
|
|
|
|
2015-02-23 23:18:05 +01:00
|
|
|
def _build_query_batch_item(self, query_functions=None):
|
|
|
|
operation = Operation(OperationEnum.QUERY)
|
|
|
|
payload = query.QueryRequestPayload(query_functions)
|
|
|
|
batch_item = messages.RequestBatchItem(
|
|
|
|
operation=operation, request_payload=payload)
|
|
|
|
return batch_item
|
|
|
|
|
2015-01-23 21:26:28 +01:00
|
|
|
def _build_discover_versions_batch_item(self, protocol_versions=None):
|
|
|
|
operation = Operation(OperationEnum.DISCOVER_VERSIONS)
|
|
|
|
|
|
|
|
payload = discover_versions.DiscoverVersionsRequestPayload(
|
|
|
|
protocol_versions)
|
|
|
|
|
|
|
|
batch_item = messages.RequestBatchItem(
|
|
|
|
operation=operation, request_payload=payload)
|
|
|
|
return batch_item
|
|
|
|
|
2015-01-09 19:11:05 +01:00
|
|
|
def _process_batch_items(self, response):
|
|
|
|
results = []
|
|
|
|
for batch_item in response.batch_items:
|
|
|
|
operation = batch_item.operation.enum
|
|
|
|
processor = self._get_batch_item_processor(operation)
|
|
|
|
result = processor(batch_item)
|
|
|
|
results.append(result)
|
|
|
|
return results
|
|
|
|
|
|
|
|
def _get_batch_item_processor(self, operation):
|
|
|
|
if operation == OperationEnum.CREATE_KEY_PAIR:
|
|
|
|
return self._process_create_key_pair_batch_item
|
2015-01-21 17:41:08 +01:00
|
|
|
elif operation == OperationEnum.REKEY_KEY_PAIR:
|
|
|
|
return self._process_rekey_key_pair_batch_item
|
2015-02-23 23:18:05 +01:00
|
|
|
elif operation == OperationEnum.QUERY:
|
|
|
|
return self._process_query_batch_item
|
2015-01-23 21:26:28 +01:00
|
|
|
elif operation == OperationEnum.DISCOVER_VERSIONS:
|
|
|
|
return self._process_discover_versions_batch_item
|
2015-01-09 19:11:05 +01:00
|
|
|
else:
|
2015-01-21 17:41:08 +01:00
|
|
|
raise ValueError("no processor for operation: {0}".format(
|
|
|
|
operation))
|
2015-01-09 19:11:05 +01:00
|
|
|
|
2015-01-21 17:41:08 +01:00
|
|
|
def _process_key_pair_batch_item(self, batch_item, result):
|
2015-01-09 19:11:05 +01:00
|
|
|
payload = batch_item.response_payload
|
|
|
|
|
|
|
|
payload_private_key_uuid = None
|
|
|
|
payload_public_key_uuid = None
|
|
|
|
payload_private_key_template_attribute = None
|
|
|
|
payload_public_key_template_attribute = None
|
|
|
|
|
|
|
|
if payload is not None:
|
|
|
|
payload_private_key_uuid = payload.private_key_uuid
|
|
|
|
payload_public_key_uuid = payload.public_key_uuid
|
|
|
|
payload_private_key_template_attribute = \
|
|
|
|
payload.private_key_template_attribute
|
|
|
|
payload_public_key_template_attribute = \
|
|
|
|
payload.public_key_template_attribute
|
|
|
|
|
2015-01-21 17:41:08 +01:00
|
|
|
return result(batch_item.result_status, batch_item.result_reason,
|
|
|
|
batch_item.result_message, payload_private_key_uuid,
|
|
|
|
payload_public_key_uuid,
|
|
|
|
payload_private_key_template_attribute,
|
|
|
|
payload_public_key_template_attribute)
|
|
|
|
|
|
|
|
def _process_create_key_pair_batch_item(self, batch_item):
|
|
|
|
return self._process_key_pair_batch_item(
|
|
|
|
batch_item, CreateKeyPairResult)
|
|
|
|
|
|
|
|
def _process_rekey_key_pair_batch_item(self, batch_item):
|
|
|
|
return self._process_key_pair_batch_item(
|
|
|
|
batch_item, RekeyKeyPairResult)
|
2015-01-09 19:11:05 +01:00
|
|
|
|
2015-02-23 23:18:05 +01:00
|
|
|
def _process_query_batch_item(self, batch_item):
|
|
|
|
payload = batch_item.response_payload
|
|
|
|
|
|
|
|
operations = None
|
|
|
|
object_types = None
|
|
|
|
vendor_identification = None
|
|
|
|
server_information = None
|
|
|
|
application_namespaces = None
|
|
|
|
extension_information = None
|
|
|
|
|
|
|
|
if payload is not None:
|
|
|
|
operations = payload.operations
|
|
|
|
object_types = payload.object_types
|
|
|
|
vendor_identification = payload.vendor_identification
|
|
|
|
server_information = payload.server_information
|
|
|
|
application_namespaces = payload.application_namespaces
|
|
|
|
extension_information = payload.extension_information
|
|
|
|
|
|
|
|
return QueryResult(
|
|
|
|
batch_item.result_status,
|
|
|
|
batch_item.result_reason,
|
|
|
|
batch_item.result_message,
|
|
|
|
operations,
|
|
|
|
object_types,
|
|
|
|
vendor_identification,
|
|
|
|
server_information,
|
|
|
|
application_namespaces,
|
|
|
|
extension_information)
|
|
|
|
|
2015-01-23 21:26:28 +01:00
|
|
|
def _process_discover_versions_batch_item(self, batch_item):
|
|
|
|
payload = batch_item.response_payload
|
|
|
|
|
|
|
|
result = DiscoverVersionsResult(
|
|
|
|
batch_item.result_status, batch_item.result_reason,
|
|
|
|
batch_item.result_message, payload.protocol_versions)
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
2014-08-11 22:34:30 +02:00
|
|
|
def _get(self,
|
|
|
|
unique_identifier=None,
|
|
|
|
key_format_type=None,
|
|
|
|
key_compression_type=None,
|
|
|
|
key_wrapping_specification=None,
|
|
|
|
credential=None):
|
|
|
|
operation = Operation(OperationEnum.GET)
|
|
|
|
|
|
|
|
uuid = None
|
|
|
|
kft = None
|
|
|
|
kct = None
|
|
|
|
kws = None
|
|
|
|
|
|
|
|
if unique_identifier is not None:
|
|
|
|
uuid = attr.UniqueIdentifier(unique_identifier)
|
|
|
|
if key_format_type is not None:
|
2015-03-13 20:01:52 +01:00
|
|
|
kft = get.GetRequestPayload.KeyFormatType(key_format_type.enum)
|
2014-08-11 22:34:30 +02:00
|
|
|
if key_compression_type is not None:
|
|
|
|
kct = key_compression_type
|
2014-11-06 16:59:33 +01:00
|
|
|
kct = get.GetRequestPayload.KeyCompressionType(kct)
|
2014-08-11 22:34:30 +02:00
|
|
|
if key_wrapping_specification is not None:
|
|
|
|
kws = objects.KeyWrappingSpecification(key_wrapping_specification)
|
|
|
|
|
2014-11-06 16:59:33 +01:00
|
|
|
req_pl = get.GetRequestPayload(unique_identifier=uuid,
|
|
|
|
key_format_type=kft,
|
|
|
|
key_compression_type=kct,
|
|
|
|
key_wrapping_specification=kws)
|
2014-08-11 22:34:30 +02:00
|
|
|
|
|
|
|
batch_item = messages.RequestBatchItem(operation=operation,
|
|
|
|
request_payload=req_pl)
|
|
|
|
message = self._build_request_message(credential, [batch_item])
|
|
|
|
self._send_message(message)
|
|
|
|
message = messages.ResponseMessage()
|
|
|
|
data = self._receive_message()
|
|
|
|
message.read(data)
|
|
|
|
batch_items = message.batch_items
|
|
|
|
batch_item = batch_items[0]
|
|
|
|
payload = batch_item.response_payload
|
|
|
|
|
|
|
|
if payload is None:
|
|
|
|
payload_unique_identifier = None
|
|
|
|
payload_object_type = None
|
|
|
|
payload_secret = None
|
|
|
|
else:
|
|
|
|
payload_unique_identifier = payload.unique_identifier
|
|
|
|
payload_object_type = payload.object_type
|
|
|
|
payload_secret = payload.secret
|
|
|
|
|
|
|
|
result = GetResult(batch_item.result_status,
|
|
|
|
batch_item.result_reason,
|
|
|
|
batch_item.result_message,
|
|
|
|
payload_object_type,
|
|
|
|
payload_unique_identifier,
|
|
|
|
payload_secret)
|
|
|
|
return result
|
|
|
|
|
|
|
|
def _destroy(self,
|
|
|
|
unique_identifier=None,
|
|
|
|
credential=None):
|
|
|
|
operation = Operation(OperationEnum.DESTROY)
|
|
|
|
|
|
|
|
uuid = None
|
|
|
|
if unique_identifier is not None:
|
|
|
|
uuid = attr.UniqueIdentifier(unique_identifier)
|
|
|
|
|
2014-11-06 16:59:33 +01:00
|
|
|
payload = destroy.DestroyRequestPayload(unique_identifier=uuid)
|
2014-08-11 22:34:30 +02:00
|
|
|
|
|
|
|
batch_item = messages.RequestBatchItem(operation=operation,
|
|
|
|
request_payload=payload)
|
|
|
|
message = self._build_request_message(credential, [batch_item])
|
|
|
|
self._send_message(message)
|
|
|
|
message = messages.ResponseMessage()
|
|
|
|
data = self._receive_message()
|
|
|
|
message.read(data)
|
|
|
|
batch_items = message.batch_items
|
|
|
|
batch_item = batch_items[0]
|
|
|
|
payload = batch_item.response_payload
|
|
|
|
|
|
|
|
if payload is None:
|
|
|
|
payload_unique_identifier = None
|
|
|
|
else:
|
|
|
|
payload_unique_identifier = payload.unique_identifier
|
|
|
|
|
|
|
|
result = DestroyResult(batch_item.result_status,
|
|
|
|
batch_item.result_reason,
|
|
|
|
batch_item.result_message,
|
|
|
|
payload_unique_identifier)
|
|
|
|
return result
|
|
|
|
|
|
|
|
def _register(self,
|
|
|
|
object_type=None,
|
|
|
|
template_attribute=None,
|
|
|
|
secret=None,
|
|
|
|
credential=None):
|
|
|
|
operation = Operation(OperationEnum.REGISTER)
|
|
|
|
|
|
|
|
if object_type is None:
|
|
|
|
raise ValueError('object_type cannot be None')
|
|
|
|
|
2014-11-06 16:59:33 +01:00
|
|
|
req_pl = register.RegisterRequestPayload(
|
2014-08-11 22:34:30 +02:00
|
|
|
object_type=object_type,
|
|
|
|
template_attribute=template_attribute,
|
|
|
|
secret=secret)
|
|
|
|
batch_item = messages.RequestBatchItem(operation=operation,
|
|
|
|
request_payload=req_pl)
|
|
|
|
|
|
|
|
message = self._build_request_message(credential, [batch_item])
|
|
|
|
self._send_message(message)
|
|
|
|
message = messages.ResponseMessage()
|
|
|
|
data = self._receive_message()
|
|
|
|
message.read(data)
|
|
|
|
batch_items = message.batch_items
|
|
|
|
batch_item = batch_items[0]
|
|
|
|
payload = batch_item.response_payload
|
|
|
|
|
|
|
|
if payload is None:
|
|
|
|
payload_unique_identifier = None
|
|
|
|
payload_template_attribute = None
|
|
|
|
else:
|
|
|
|
payload_unique_identifier = payload.unique_identifier
|
|
|
|
payload_template_attribute = payload.template_attribute
|
|
|
|
|
|
|
|
result = RegisterResult(batch_item.result_status,
|
|
|
|
batch_item.result_reason,
|
|
|
|
batch_item.result_message,
|
|
|
|
payload_unique_identifier,
|
|
|
|
payload_template_attribute)
|
|
|
|
return result
|
|
|
|
|
2014-09-04 20:42:20 +02:00
|
|
|
def _locate(self, maximum_items=None, storage_status_mask=None,
|
|
|
|
object_group_member=None, attributes=[], credential=None):
|
|
|
|
|
|
|
|
operation = Operation(OperationEnum.LOCATE)
|
|
|
|
|
|
|
|
mxi = None
|
|
|
|
ssmask = None
|
|
|
|
objgrp = None
|
|
|
|
|
|
|
|
if maximum_items is not None:
|
2014-11-06 16:59:33 +01:00
|
|
|
mxi = locate.LocateRequestPayload.MaximumItems(maximum_items)
|
2014-09-04 20:42:20 +02:00
|
|
|
if storage_status_mask is not None:
|
|
|
|
m = storage_status_mask
|
2014-11-06 16:59:33 +01:00
|
|
|
ssmask = locate.LocateRequestPayload.StorageStatusMask(m)
|
2014-09-04 20:42:20 +02:00
|
|
|
if object_group_member is not None:
|
|
|
|
o = object_group_member
|
2014-11-06 16:59:33 +01:00
|
|
|
objgrp = locate.LocateRequestPayload.ObjectGroupMember(o)
|
2014-09-04 20:42:20 +02:00
|
|
|
|
2014-11-06 16:59:33 +01:00
|
|
|
payload = locate.LocateRequestPayload(maximum_items=mxi,
|
|
|
|
storage_status_mask=ssmask,
|
|
|
|
object_group_member=objgrp,
|
|
|
|
attributes=attributes)
|
2014-09-04 20:42:20 +02:00
|
|
|
|
|
|
|
batch_item = messages.RequestBatchItem(operation=operation,
|
|
|
|
request_payload=payload)
|
|
|
|
|
|
|
|
message = self._build_request_message(credential, [batch_item])
|
|
|
|
self._send_message(message)
|
|
|
|
message = messages.ResponseMessage()
|
|
|
|
data = self._receive_message()
|
|
|
|
|
|
|
|
message.read(data)
|
|
|
|
batch_items = message.batch_items
|
|
|
|
batch_item = batch_items[0]
|
|
|
|
payload = batch_item.response_payload
|
|
|
|
|
|
|
|
if payload is None:
|
2014-09-09 06:14:02 +02:00
|
|
|
uuids = None
|
2014-09-04 20:42:20 +02:00
|
|
|
else:
|
2014-09-09 06:14:02 +02:00
|
|
|
uuids = payload.unique_identifiers
|
2014-09-04 20:42:20 +02:00
|
|
|
|
|
|
|
result = LocateResult(batch_item.result_status,
|
|
|
|
batch_item.result_reason,
|
|
|
|
batch_item.result_message,
|
2014-09-09 06:14:02 +02:00
|
|
|
uuids)
|
2014-09-04 20:42:20 +02:00
|
|
|
return result
|
|
|
|
|
2014-10-24 04:28:20 +02:00
|
|
|
# TODO (peter-hamilton) Augment to handle device credentials
|
|
|
|
def _build_credential(self):
|
|
|
|
if (self.username is None) and (self.password is None):
|
|
|
|
return None
|
|
|
|
if self.username is None:
|
|
|
|
raise ValueError('cannot build credential, username is None')
|
|
|
|
if self.password is None:
|
|
|
|
raise ValueError('cannot build credential, password is None')
|
|
|
|
|
|
|
|
credential_type = CredentialType.USERNAME_AND_PASSWORD
|
|
|
|
credential_value = {'Username': self.username,
|
|
|
|
'Password': self.password}
|
|
|
|
credential = self.credential_factory.create_credential(
|
|
|
|
credential_type,
|
|
|
|
credential_value)
|
|
|
|
return credential
|
|
|
|
|
2014-08-11 22:34:30 +02:00
|
|
|
def _build_request_message(self, credential, batch_items):
|
|
|
|
protocol_version = ProtocolVersion.create(1, 1)
|
|
|
|
|
2014-10-24 04:28:20 +02:00
|
|
|
if credential is None:
|
|
|
|
credential = self._build_credential()
|
|
|
|
|
2014-08-11 22:34:30 +02:00
|
|
|
authentication = None
|
|
|
|
if credential is not None:
|
|
|
|
authentication = Authentication(credential)
|
|
|
|
|
|
|
|
batch_count = BatchCount(len(batch_items))
|
|
|
|
req_header = messages.RequestHeader(protocol_version=protocol_version,
|
|
|
|
authentication=authentication,
|
|
|
|
batch_count=batch_count)
|
|
|
|
|
|
|
|
return messages.RequestMessage(request_header=req_header,
|
|
|
|
batch_items=batch_items)
|
|
|
|
|
|
|
|
def _send_message(self, message):
|
|
|
|
stream = BytearrayStream()
|
|
|
|
message.write(stream)
|
|
|
|
self.protocol.write(stream.buffer)
|
|
|
|
|
|
|
|
def _receive_message(self):
|
|
|
|
return self.protocol.read()
|
2014-09-17 09:24:58 +02:00
|
|
|
|
2015-01-09 19:11:05 +01:00
|
|
|
def _send_and_receive_message(self, request):
|
|
|
|
self._send_message(request)
|
|
|
|
response = messages.ResponseMessage()
|
|
|
|
data = self._receive_message()
|
|
|
|
response.read(data)
|
|
|
|
return response
|
|
|
|
|
2014-09-17 09:24:58 +02:00
|
|
|
def _set_variables(self, host, port, keyfile, certfile,
|
|
|
|
cert_reqs, ssl_version, ca_certs,
|
2014-10-24 04:28:20 +02:00
|
|
|
do_handshake_on_connect, suppress_ragged_eofs,
|
|
|
|
username, password):
|
2014-09-17 09:24:58 +02:00
|
|
|
conf = ConfigHelper()
|
|
|
|
|
|
|
|
self.host = conf.get_valid_value(
|
2015-02-27 16:48:07 +01:00
|
|
|
host, self.config, 'host', conf.DEFAULT_HOST)
|
2014-09-17 09:24:58 +02:00
|
|
|
|
|
|
|
self.port = int(conf.get_valid_value(
|
2015-02-27 16:48:07 +01:00
|
|
|
port, self.config, 'port', conf.DEFAULT_PORT))
|
2014-09-17 09:24:58 +02:00
|
|
|
|
|
|
|
self.keyfile = conf.get_valid_value(
|
2015-02-27 16:48:07 +01:00
|
|
|
keyfile, self.config, 'keyfile', None)
|
2014-09-17 09:24:58 +02:00
|
|
|
|
|
|
|
self.certfile = conf.get_valid_value(
|
2015-02-27 16:48:07 +01:00
|
|
|
certfile, self.config, 'certfile', None)
|
2014-09-17 09:24:58 +02:00
|
|
|
|
|
|
|
self.cert_reqs = getattr(ssl, conf.get_valid_value(
|
2015-02-27 16:48:07 +01:00
|
|
|
cert_reqs, self.config, 'cert_reqs', 'CERT_REQUIRED'))
|
2014-09-17 09:24:58 +02:00
|
|
|
|
|
|
|
self.ssl_version = getattr(ssl, conf.get_valid_value(
|
2015-02-27 16:48:07 +01:00
|
|
|
ssl_version, self.config, 'ssl_version', conf.DEFAULT_SSL_VERSION))
|
2014-09-17 09:24:58 +02:00
|
|
|
|
|
|
|
self.ca_certs = conf.get_valid_value(
|
2015-02-27 16:48:07 +01:00
|
|
|
ca_certs, self.config, 'ca_certs', conf.DEFAULT_CA_CERTS)
|
2014-09-17 09:24:58 +02:00
|
|
|
|
|
|
|
if conf.get_valid_value(
|
2015-02-27 16:48:07 +01:00
|
|
|
do_handshake_on_connect, self.config,
|
2014-09-17 09:24:58 +02:00
|
|
|
'do_handshake_on_connect', 'True') == 'True':
|
|
|
|
self.do_handshake_on_connect = True
|
|
|
|
else:
|
|
|
|
self.do_handshake_on_connect = False
|
|
|
|
|
|
|
|
if conf.get_valid_value(
|
2015-02-27 16:48:07 +01:00
|
|
|
suppress_ragged_eofs, self.config,
|
2014-09-17 09:24:58 +02:00
|
|
|
'suppress_ragged_eofs', 'True') == 'True':
|
|
|
|
self.suppress_ragged_eofs = True
|
|
|
|
else:
|
|
|
|
self.suppress_ragged_eofs = False
|
2014-10-24 04:28:20 +02:00
|
|
|
|
|
|
|
self.username = conf.get_valid_value(
|
2015-02-27 16:48:07 +01:00
|
|
|
username, self.config, 'username', conf.DEFAULT_USERNAME)
|
2014-10-24 04:28:20 +02:00
|
|
|
|
|
|
|
self.password = conf.get_valid_value(
|
2015-02-27 16:48:07 +01:00
|
|
|
password, self.config, 'password', conf.DEFAULT_PASSWORD)
|