Updating the CreateKeyPair demo

This change updates the CreateKeyPair demo, adding the following items:
* a name option for specifying the name of the key pair to create
* a CryptographicUsageMask attribute sent with the CreateKeyPair request
* enhanced attribute logging for private/public key template attributes
This commit is contained in:
Peter Hamilton 2015-03-10 11:29:08 -04:00
parent 908aece78a
commit f784b67f3a
2 changed files with 66 additions and 19 deletions

View File

@ -16,6 +16,7 @@
from kmip.core.enums import AttributeType from kmip.core.enums import AttributeType
from kmip.core.enums import CredentialType from kmip.core.enums import CredentialType
from kmip.core.enums import CryptographicAlgorithm from kmip.core.enums import CryptographicAlgorithm
from kmip.core.enums import CryptographicUsageMask as UsageMaskEnum
from kmip.core.enums import Operation from kmip.core.enums import Operation
from kmip.core.enums import ResultStatus from kmip.core.enums import ResultStatus
from kmip.core.enums import NameType from kmip.core.enums import NameType
@ -26,6 +27,7 @@ from kmip.core.factories.attributes import AttributeFactory
from kmip.core.factories.credentials import CredentialFactory from kmip.core.factories.credentials import CredentialFactory
from kmip.core.attributes import Name from kmip.core.attributes import Name
from kmip.core.attributes import CryptographicUsageMask
from kmip.core.objects import CommonTemplateAttribute from kmip.core.objects import CommonTemplateAttribute
from kmip.core.objects import PrivateKeyTemplateAttribute from kmip.core.objects import PrivateKeyTemplateAttribute
@ -49,21 +51,24 @@ if __name__ == '__main__':
config = opts.config config = opts.config
algorithm = opts.algorithm algorithm = opts.algorithm
length = opts.length length = opts.length
name = opts.name
# Exit early if the arguments are not specified # Exit early if the arguments are not specified
if algorithm is None: if algorithm is None:
logging.debug('No algorithm provided, exiting early from demo') logging.error('No algorithm provided, exiting early from demo')
sys.exit() sys.exit()
if length is None: if length is None:
logging.debug("No key length provided, exiting early from demo") logging.error("No key length provided, exiting early from demo")
sys.exit()
if name is None:
logging.error("No key name provided, exiting early from demo")
sys.exit() sys.exit()
attribute_type = AttributeType.CRYPTOGRAPHIC_ALGORITHM attribute_type = AttributeType.CRYPTOGRAPHIC_ALGORITHM
algorithm_enum = getattr(CryptographicAlgorithm, algorithm, None) algorithm_enum = getattr(CryptographicAlgorithm, algorithm, None)
if algorithm_enum is None: if algorithm_enum is None:
logging.debug("{0} not found".format(algorithm)) logging.error("Invalid algorithm specified; exiting early from demo")
logging.debug("Invalid algorithm specified, exiting early from demo")
sys.exit() sys.exit()
# Build and setup logging and needed factories # Build and setup logging and needed factories
@ -92,17 +97,22 @@ if __name__ == '__main__':
algorithm_obj = attribute_factory.create_attribute(attribute_type, algorithm_obj = attribute_factory.create_attribute(attribute_type,
algorithm_enum) algorithm_enum)
attribute_type = AttributeType.CRYPTOGRAPHIC_LENGTH name_value = Name.NameValue(name)
length_obj = attribute_factory.create_attribute(attribute_type,
length)
name = Attribute.AttributeName('Name') name = Attribute.AttributeName('Name')
name_value = Name.NameValue('Test Key')
name_type = Name.NameType(NameType.UNINTERPRETED_TEXT_STRING) name_type = Name.NameType(NameType.UNINTERPRETED_TEXT_STRING)
value = Name(name_value=name_value, name_type=name_type) value = Name(name_value=name_value, name_type=name_type)
name = Attribute(attribute_name=name, attribute_value=value) name = Attribute(attribute_name=name, attribute_value=value)
attributes = [algorithm_obj, length_obj, name] name = Attribute.AttributeName('Cryptographic Usage Mask')
value = CryptographicUsageMask(
UsageMaskEnum.ENCRYPT.value | UsageMaskEnum.DECRYPT.value)
usage_mask = Attribute(attribute_name=name, attribute_value=value)
attribute_type = AttributeType.CRYPTOGRAPHIC_LENGTH
length_obj = attribute_factory.create_attribute(attribute_type,
length)
attributes = [algorithm_obj, length_obj, name, usage_mask]
common = CommonTemplateAttribute(attributes=attributes) common = CommonTemplateAttribute(attributes=attributes)
private = PrivateKeyTemplateAttribute(attributes=attributes) private = PrivateKeyTemplateAttribute(attributes=attributes)
public = PublicKeyTemplateAttribute(attributes=attributes) public = PublicKeyTemplateAttribute(attributes=attributes)
@ -114,20 +124,26 @@ if __name__ == '__main__':
client.close() client.close()
# Display operation results # Display operation results
logger.debug('create_key_pair() result status: {0}'.format( logger.info('create_key_pair() result status: {0}'.format(
result.result_status.enum)) result.result_status.enum))
if result.result_status.enum == ResultStatus.SUCCESS: if result.result_status.enum == ResultStatus.SUCCESS:
logger.debug('created private key UUID: {0}'.format( logger.info('created private key UUID: {0}'.format(
result.private_key_uuid)) result.private_key_uuid))
logger.debug('created public key UUID: {0}'.format( logger.info('created public key UUID: {0}'.format(
result.public_key_uuid)) result.public_key_uuid))
logger.debug('created private key template attribute: {0}'.format(
result.private_key_template_attribute)) if result.private_key_template_attribute is not None:
logger.debug('created public key template attribute: {0}'.format( logger.info('private key template attribute:')
result.public_key_template_attribute)) utils.log_template_attribute(
logger, result.private_key_template_attribute)
if result.public_key_template_attribute is not None:
logger.info('public key template attribute:')
utils.log_template_attribute(
logger, result.public_key_template_attribute)
else: else:
logger.debug('create() result reason: {0}'.format( logger.info('create() result reason: {0}'.format(
result.result_reason.enum)) result.result_reason.enum))
logger.debug('create() result message: {0}'.format( logger.info('create() result message: {0}'.format(
result.result_message.value)) result.result_message.value))

View File

@ -83,6 +83,14 @@ def build_cli_parser(operation):
default=None, default=None,
dest="length", dest="length",
help="Key length in bits (e.g., 128, 256)") help="Key length in bits (e.g., 128, 256)")
parser.add_option(
"-n",
"--name",
action="store",
type="str",
default=None,
dest="name",
help="Name of key pair to create")
elif operation is Operation.DESTROY: elif operation is Operation.DESTROY:
parser.add_option( parser.add_option(
"-i", "-i",
@ -135,3 +143,26 @@ def build_cli_parser(operation):
raise ValueError("unrecognized operation: {0}".format(operation)) raise ValueError("unrecognized operation: {0}".format(operation))
return parser return parser
def log_template_attribute(logger, template_attribute):
names = template_attribute.names
attributes = template_attribute.attributes
logger.info('number of template attribute names: {0}'.format(len(names)))
for i in range(len(names)):
name = names[i]
logger.info('name {0}: {1}'.format(i, name))
logger.info('number of attributes: {0}'.format(len(attributes)))
for i in range(len(attributes)):
attribute = attributes[i]
attribute_name = attribute.attribute_name
attribute_index = attribute.attribute_index
attribute_value = attribute.attribute_value
logger.info('attribute {0}:'.format(i))
logger.info(' attribute_name: {0}'.format(attribute_name))
logger.info(' attribute_index: {0}'.format(attribute_index))
logger.info(' attribute_value: {0}'.format(
repr(attribute_value)))