2
0
mirror of https://github.com/openkmip/pykmip synced 2025-12-18 01:03:49 +00:00

Updating the Register unit demo

This change updates the Register unit demo, adding support for the
registration of different types of secret objects. The changes required
to support this include implementation updates to the KeyValue backing
objects.
This commit is contained in:
Peter Hamilton
2015-03-13 16:31:39 -04:00
parent d263302077
commit 43ecea23a6
8 changed files with 358 additions and 214 deletions

View File

@@ -13,19 +13,11 @@
# License for the specific language governing permissions and limitations
# under the License.
from kmip.core.enums import AttributeType
from kmip.core.enums import CredentialType
from kmip.core.enums import CryptographicAlgorithm
from kmip.core.enums import CryptographicUsageMask
from kmip.core.enums import KeyFormatType
from kmip.core.enums import ObjectType
from kmip.core.enums import Operation
from kmip.core.enums import ResultStatus
from kmip.core.factories.attributes import AttributeFactory
from kmip.core.factories.credentials import CredentialFactory
from kmip.core.factories.secrets import SecretFactory
from kmip.core.objects import TemplateAttribute
from kmip.demos import utils
@@ -38,87 +30,45 @@ import sys
if __name__ == '__main__':
# Build and parse arguments
parser = utils.build_cli_parser(Operation.REGISTER)
opts, args = parser.parse_args(sys.argv[1:])
username = opts.username
password = opts.password
config = opts.config
algorithm = opts.algorithm
length = opts.length
object_type = opts.type
format_type = opts.format
# Exit early if the arguments are not specified
if algorithm is None:
logging.debug('No algorithm provided, exiting early from demo')
sys.exit()
if length is None:
logging.debug("No key length provided, exiting early from demo")
object_type = getattr(ObjectType, object_type, None)
if object_type is None:
logging.error("Invalid object type specified; exiting early from demo")
sys.exit()
key_format_type = getattr(KeyFormatType, format_type, None)
if key_format_type is None:
logging.error(
"Invalid key format type specified; exiting early from demo")
# Build and setup logging and needed factories
f_log = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir,
'logconfig.ini')
logging.config.fileConfig(f_log)
logger = logging.getLogger(__name__)
attribute_factory = AttributeFactory()
credential_factory = CredentialFactory()
secret_factory = SecretFactory()
# Build the KMIP server account credentials
# TODO (peter-hamilton) Move up into KMIPProxy
if (username is None) and (password is None):
credential = None
else:
credential_type = CredentialType.USERNAME_AND_PASSWORD
credential_value = {'Username': username,
'Password': password}
credential = credential_factory.create_credential(credential_type,
credential_value)
# Build the client and connect to the server
client = KMIPProxy(config=config)
client.open()
# Build the different object attributes
object_type = ObjectType.SYMMETRIC_KEY
attribute_type = AttributeType.CRYPTOGRAPHIC_ALGORITHM
algorithm_enum = getattr(CryptographicAlgorithm, algorithm, None)
if algorithm_enum is None:
logging.debug("{0} not found".format(algorithm))
logging.debug("Invalid algorithm specified, exiting early from demo")
client.close()
sys.exit()
mask_flags = [CryptographicUsageMask.ENCRYPT,
CryptographicUsageMask.DECRYPT]
attribute_type = AttributeType.CRYPTOGRAPHIC_USAGE_MASK
usage_mask = attribute_factory.create_attribute(attribute_type,
mask_flags)
# Create the template attribute for the secret and then build the secret
usage_mask = utils.build_cryptographic_usage_mask(logger, object_type)
attributes = [usage_mask]
template_attribute = TemplateAttribute(attributes=attributes)
secret_features = {}
secret = utils.build_object(logger, object_type, key_format_type)
key_format_type = KeyFormatType.RAW
secret_features.update([('key_format_type', key_format_type)])
# Build the client, connect to the server, register the secret, and
# disconnect from the server
client = KMIPProxy(config=config)
# TODO (peter-hamilton) Replace with calls to crypto libraries
key_data = {'bytes': bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00')}
secret_features.update([('key_value', key_data)])
secret_features.update([('cryptographic_algorithm', algorithm_enum)])
secret_features.update([('cryptographic_length', length)])
secret = secret_factory.create(object_type, secret_features)
# Register the SYMMETRIC_KEY object
result = client.register(object_type, template_attribute, secret,
credential)
client.open()
result = client.register(object_type, template_attribute, secret)
client.close()
# Display operation results