def test_build_credential(self): username = '******' password = '******' cred_type = CredentialType.USERNAME_AND_PASSWORD self.client.username = username self.client.password = password credential = self.client._build_credential() message = utils.build_er_error(credential.__class__, 'type', cred_type, credential.credential_type.value, 'value') self.assertEqual(CredentialType.USERNAME_AND_PASSWORD, credential.credential_type.value, message) message = utils.build_er_error( credential.__class__, 'type', username, credential.credential_value.username.value, 'value') self.assertEqual(username, credential.credential_value.username.value, message) message = utils.build_er_error( credential.__class__, 'type', password, credential.credential_value.password.value, 'value') self.assertEqual(password, credential.credential_value.password.value, message)
def _check_uuid(self, uuid, uuid_type): # Error check the UUID type and value not_expected = None message = utils.build_er_error(uuid_type, 'type', 'not {0}'.format(not_expected), uuid) self.assertNotEqual(not_expected, uuid, message) expected = uuid_type message = utils.build_er_error(uuid_type, 'type', expected, uuid) self.assertEqual(expected, type(uuid), message)
def _check_result_status(self, result_status, result_status_type, result_status_value): # Error check the result status type and value expected = result_status_type message = utils.build_er_error(result_status_type, 'type', expected, result_status) self.assertIsInstance(result_status, expected, message) expected = result_status_value message = utils.build_er_error(result_status_type, 'value', expected, result_status) self.assertEqual(expected, result_status, message)
def _check_object_type(self, object_type, object_type_type, object_type_value): # Error check the object type type and value expected = object_type_type message = utils.build_er_error(object_type_type, 'type', expected, object_type) self.assertIsInstance(object_type, expected, message) expected = object_type_value message = utils.build_er_error(object_type_type, 'value', expected, object_type) self.assertEqual(expected, object_type, message)
def _check_attribute_value(self, attribute_value, attribute_value_type, attribute_value_value): expected = attribute_value_type observed = type(attribute_value.value) message = utils.build_er_error(Attribute, 'type', expected, observed, 'attribute_value') self.assertEqual(expected, observed, message) expected = attribute_value_value observed = attribute_value.value message = utils.build_er_error(Attribute, 'value', expected, observed, 'attribute_value') self.assertEqual(expected, observed, message)
def _check_attribute_name(self, attribute_name, attribute_name_type, attribute_name_value): # Error check the attribute name type and value expected = attribute_name_type observed = type(attribute_name.value) message = utils.build_er_error(attribute_name_type, 'type', expected, observed) self.assertEqual(expected, observed, message) expected = attribute_name_value observed = attribute_name.value message = utils.build_er_error(attribute_name_type, 'value', expected, observed) self.assertEqual(expected, observed, message)
def test_destroy(self): credential_type = CredentialType.USERNAME_AND_PASSWORD credential_value = {'Username': '******', 'Password': '******'} credential = self.cred_factory.create_credential( credential_type, credential_value) result = self._create_symmetric_key() uuid = result.uuid.value # Verify the secret was created result = self.client.get(uuid=uuid, credential=credential) self._check_result_status(result.result_status.value, ResultStatus, ResultStatus.SUCCESS) self._check_object_type(result.object_type.value, ObjectType, ObjectType.SYMMETRIC_KEY) self._check_uuid(result.uuid.value, str) secret = result.secret expected = SymmetricKey message = utils.build_er_error(result.__class__, 'type', expected, secret, 'secret') self.assertIsInstance(secret, expected, message) # Destroy the SYMMETRIC_KEY object result = self.client.destroy(uuid, credential) self._check_result_status(result.result_status.value, ResultStatus, ResultStatus.SUCCESS) self._check_uuid(result.uuid.value, str) # Verify the secret was destroyed result = self.client.get(uuid=uuid, credential=credential) self._check_result_status(result.result_status.value, ResultStatus, ResultStatus.OPERATION_FAILED) expected = ResultReason observed = type(result.result_reason.value) message = utils.build_er_error(result.result_reason.__class__, 'type', expected, observed) self.assertEqual(expected, observed, message) expected = ResultReason.ITEM_NOT_FOUND observed = result.result_reason.value message = utils.build_er_error(result.result_reason.__class__, 'value', expected, observed) self.assertEqual(expected, observed, message)
def test_destroy(self): credential_type = CredentialType.USERNAME_AND_PASSWORD credential_value = {'Username': '******', 'Password': '******'} credential = self.cred_factory.create_credential(credential_type, credential_value) result = self._create_symmetric_key() uuid = result.uuid.value # Verify the secret was created result = self.client.get(uuid=uuid, credential=credential) self._check_result_status(result.result_status.enum, ResultStatus, ResultStatus.SUCCESS) self._check_object_type(result.object_type.enum, ObjectType, ObjectType.SYMMETRIC_KEY) self._check_uuid(result.uuid.value, str) secret = result.secret expected = SymmetricKey message = utils.build_er_error(result.__class__, 'type', expected, secret, 'secret') self.assertIsInstance(secret, expected, message) # Destroy the SYMMETRIC_KEY object result = self.client.destroy(uuid, credential) self._check_result_status(result.result_status.enum, ResultStatus, ResultStatus.SUCCESS) self._check_uuid(result.uuid.value, str) # Verify the secret was destroyed result = self.client.get(uuid=uuid, credential=credential) self._check_result_status(result.result_status.enum, ResultStatus, ResultStatus.OPERATION_FAILED) expected = ResultReason observed = type(result.result_reason.enum) message = utils.build_er_error(result.result_reason.__class__, 'type', expected, observed) self.assertEqual(expected, observed, message) expected = ResultReason.ITEM_NOT_FOUND observed = result.result_reason.enum message = utils.build_er_error(result.result_reason.__class__, 'value', expected, observed) self.assertEqual(expected, observed, message)
def _create_cryptographic_length(self, length): if length is not None and not isinstance(length, int): msg = utils.build_er_error(attributes.CryptographicLength, 'constructor argument type', int, type(length)) raise TypeError(msg) return attributes.CryptographicLength(length)
def _create_object_group(self, group): if group is not None and not isinstance(group, str): msg = utils.build_er_error(attributes.ObjectGroup, 'constructor argument type', str, type(group)) raise TypeError(msg) return attributes.ObjectGroup(group)
def _create_template(self, value): if value is None: return Template() else: if not isinstance(value, list): msg = utils.build_er_error(Template, 'constructor argument type', list, type(value)) raise TypeError(msg) else: for val in value: if not isinstance(val, Attribute): msg = utils.build_er_error(Template, 'constructor argument type', Attribute, type(val)) raise TypeError(msg) return Template(value)
def _create_contact_information(self, info): if info is None: return attributes.ContactInformation() else: if not isinstance(info, str): msg = utils.build_er_error(attributes.ContactInformation, "constructor argument type", str, type(info)) raise TypeError(msg) return attributes.ContactInformation(info)
def _create_contact_information(self, info): if info is None: return attributes.ContactInformation() else: if not isinstance(info, str): msg = utils.build_er_error(attributes.ContactInformation, 'constructor argument type', str, type(info)) raise TypeError(msg) return attributes.ContactInformation(info)
def _check_template_attribute(self, template_attribute, template_attribute_type, num_attributes, attribute_features): # Error check the template attribute type expected = template_attribute_type message = utils.build_er_error(template_attribute.__class__, 'type', expected, template_attribute) self.assertIsInstance(template_attribute, expected, message) attributes = template_attribute.attributes expected = num_attributes observed = len(attributes) message = utils.build_er_error(TemplateAttribute.__class__, 'number', expected, observed, 'attributes') for i in range(num_attributes): features = attribute_features[i] self._check_attribute(attributes[i], features[0], features[1], features[2], features[3])
def _create_application_specific_information(self, info): if info is None: return ApplicationSpecificInformation() else: application_namespace = info.get('application_namespace') application_data = info.get('application_data') if not isinstance(application_namespace, str): msg = utils.build_er_error(ApplicationSpecificInformation, 'constructor argument type', str, type(application_namespace)) raise TypeError(msg) if not isinstance(application_data, str): msg = utils.build_er_error(ApplicationSpecificInformation, 'constructor argument type', str, type(application_data)) raise TypeError(msg) return ApplicationSpecificInformation.create(application_namespace, application_data)
def _create_application_specific_information(self, info): if info is None: return attributes.ApplicationSpecificInformation() else: application_namespace = info.get('application_namespace') application_data = info.get('application_data') if not isinstance(application_namespace, str): msg = utils.build_er_error( attributes.ApplicationSpecificInformation, 'constructor argument type', str, type(application_namespace)) raise TypeError(msg) if not isinstance(application_data, str): msg = utils.build_er_error( attributes.ApplicationSpecificInformation, 'constructor argument type', str, type(application_data)) raise TypeError(msg) return attributes.ApplicationSpecificInformation.create( application_namespace, application_data)
def _create_application_specific_information(self, info): if info is None: return attributes.ApplicationSpecificInformation() else: application_namespace = info.get("application_namespace") application_data = info.get("application_data") if not isinstance(application_namespace, str): msg = utils.build_er_error( attributes.ApplicationSpecificInformation, "constructor argument type", str, type(application_namespace), ) raise TypeError(msg) if not isinstance(application_data, str): msg = utils.build_er_error( attributes.ApplicationSpecificInformation, "constructor argument type", str, type(application_data) ) raise TypeError(msg) return attributes.ApplicationSpecificInformation.create(application_namespace, application_data)
def create_attribute(self, name, value, index=None): value = self.value_factory.create_attribute_value(name, value) if isinstance(name, Enum): name = name.value elif isinstance(name, str): # Name is already a string, pass pass else: msg = utils.build_er_error(Attribute, 'name', '{} or {}'.format('Enum', 'str'), type(name)) raise TypeError(msg) return self._create_attribute(name, value, index)
def create_attribute(self, name, value, index=None): value = self.value_factory.create_attribute_value(name, value) if isinstance(name, Enum): name = name.value elif isinstance(name, str): # Name is already a string, pass pass else: msg = utils.build_er_error(Attribute, 'name', '{0} or {1}'.format('Enum', 'str'), type(name)) raise TypeError(msg) return self._create_attribute(name, value, index)
def test_get(self): credential_type = CredentialType.USERNAME_AND_PASSWORD credential_value = {'Username': '******', 'Password': '******'} credential = self.cred_factory.create_credential( credential_type, credential_value) result = self._create_symmetric_key() uuid = result.uuid.value result = self.client.get(uuid=uuid, credential=credential) self._check_result_status(result.result_status.value, ResultStatus, ResultStatus.SUCCESS) self._check_object_type(result.object_type.value, ObjectType, ObjectType.SYMMETRIC_KEY) self._check_uuid(result.uuid.value, str) # Check the secret type secret = result.secret expected = SymmetricKey message = utils.build_er_error(result.__class__, 'type', expected, secret, 'secret') self.assertIsInstance(secret, expected, message)
def test_get(self): credential_type = CredentialType.USERNAME_AND_PASSWORD credential_value = {'Username': '******', 'Password': '******'} credential = self.cred_factory.create_credential(credential_type, credential_value) result = self._create_symmetric_key() uuid = result.uuid.value result = self.client.get(uuid=uuid, credential=credential) self._check_result_status(result.result_status.enum, ResultStatus, ResultStatus.SUCCESS) self._check_object_type(result.object_type.enum, ObjectType, ObjectType.SYMMETRIC_KEY) self._check_uuid(result.uuid.value, str) # Check the secret type secret = result.secret expected = SymmetricKey message = utils.build_er_error(result.__class__, 'type', expected, secret, 'secret') self.assertIsInstance(secret, expected, message)
def test_register(self): credential_type = CredentialType.USERNAME_AND_PASSWORD credential_value = {'Username': '******', 'Password': '******'} credential = self.cred_factory.create_credential( credential_type, credential_value) object_type = ObjectType.SYMMETRIC_KEY algorithm_value = CryptoAlgorithmEnum.AES mask_flags = [ CryptographicUsageMask.ENCRYPT, CryptographicUsageMask.DECRYPT ] attribute_type = AttributeType.CRYPTOGRAPHIC_USAGE_MASK usage_mask = self.attr_factory.create_attribute( attribute_type, mask_flags) attributes = [usage_mask] template_attribute = TemplateAttribute(attributes=attributes) key_format_type = KeyFormatType(KeyFormatTypeEnum.RAW) key_data = ( b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' b'\x00') key_material = KeyMaterial(key_data) key_value = KeyValue(key_material) cryptographic_algorithm = CryptographicAlgorithm(algorithm_value) cryptographic_length = CryptographicLength(128) key_block = KeyBlock(key_format_type=key_format_type, key_compression_type=None, key_value=key_value, cryptographic_algorithm=cryptographic_algorithm, cryptographic_length=cryptographic_length, key_wrapping_data=None) secret = SymmetricKey(key_block) result = self.client.register(object_type, template_attribute, secret, credential) self._check_result_status(result.result_status.value, ResultStatus, ResultStatus.SUCCESS) self._check_uuid(result.uuid.value, str) # Check the template attribute type self._check_template_attribute(result.template_attribute, TemplateAttribute, 1, [[str, 'Unique Identifier', str, None]]) # Check that the returned key bytes match what was provided uuid = result.uuid.value result = self.client.get(uuid=uuid, credential=credential) self._check_result_status(result.result_status.value, ResultStatus, ResultStatus.SUCCESS) self._check_object_type(result.object_type.value, ObjectType, ObjectType.SYMMETRIC_KEY) self._check_uuid(result.uuid.value, str) # Check the secret type secret = result.secret expected = SymmetricKey message = utils.build_er_error(result.__class__, 'type', expected, secret, 'secret') self.assertIsInstance(secret, expected, message) key_block = result.secret.key_block key_value = key_block.key_value key_material = key_value.key_material expected = key_data observed = key_material.value message = utils.build_er_error(key_material.__class__, 'value', expected, observed, 'value') self.assertEqual(expected, observed, message)
def test_register(self): credential_type = CredentialType.USERNAME_AND_PASSWORD credential_value = {'Username': '******', 'Password': '******'} credential = self.cred_factory.create_credential(credential_type, credential_value) object_type = ObjectType.SYMMETRIC_KEY algorithm_value = CryptoAlgorithmEnum.AES mask_flags = [CryptographicUsageMask.ENCRYPT, CryptographicUsageMask.DECRYPT] attribute_type = AttributeType.CRYPTOGRAPHIC_USAGE_MASK usage_mask = self.attr_factory.create_attribute(attribute_type, mask_flags) attributes = [usage_mask] template_attribute = TemplateAttribute(attributes=attributes) key_format_type = KeyFormatType(KeyFormatTypeEnum.RAW) key_data = ( b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' b'\x00') key_material = KeyMaterial(key_data) key_value = KeyValue(key_material) cryptographic_algorithm = CryptographicAlgorithm(algorithm_value) cryptographic_length = CryptographicLength(128) key_block = KeyBlock( key_format_type=key_format_type, key_compression_type=None, key_value=key_value, cryptographic_algorithm=cryptographic_algorithm, cryptographic_length=cryptographic_length, key_wrapping_data=None) secret = SymmetricKey(key_block) result = self.client.register(object_type, template_attribute, secret, credential) self._check_result_status(result.result_status.enum, ResultStatus, ResultStatus.SUCCESS) self._check_uuid(result.uuid.value, str) # Check the template attribute type self._check_template_attribute(result.template_attribute, TemplateAttribute, 1, [[str, 'Unique Identifier', str, None]]) # Check that the returned key bytes match what was provided uuid = result.uuid.value result = self.client.get(uuid=uuid, credential=credential) self._check_result_status(result.result_status.enum, ResultStatus, ResultStatus.SUCCESS) self._check_object_type(result.object_type.enum, ObjectType, ObjectType.SYMMETRIC_KEY) self._check_uuid(result.uuid.value, str) # Check the secret type secret = result.secret expected = SymmetricKey message = utils.build_er_error(result.__class__, 'type', expected, secret, 'secret') self.assertIsInstance(secret, expected, message) key_block = result.secret.key_block key_value = key_block.key_value key_material = key_value.key_material expected = key_data observed = key_material.value message = utils.build_er_error(key_material.__class__, 'value', expected, observed, 'value') self.assertEqual(expected, observed, message)