Пример #1
0
 def test_store_keys_failure_non_private_key_passed(self):
     ''' store_keys should fail if privkey is not a valid key '''
     privkey = 'privkey'
     privkey_file = '/tmp/test_store_keys_failure_non_private_key_passed.priv'
     pubkey_file = '/tmp/test_store_keys_failure_non_private_key_passed.pub'
     with self.assertRaises(AttributeError) as cm:
         crypto.store_keys(privkey, privkey_file, pubkey_file)
Пример #2
0
 def test_store_keys_failure_no_permission_to_create_public_file(self):
     ''' store_keys should fail if process has no permission to create public file '''
     privkey = crypto.generate_rsa_key()
     privkey_file = '/tmp/test_store_keys_failure_permission_to_create_public_file.priv'
     pubkey_file = '/root/test_store_keys_failure_permission_to_create_public_file.pub'
     with self.assertRaises(OSError) as cm:
         crypto.store_keys(privkey, privkey_file, pubkey_file)
     self.assertEqual(cm.exception.errno, 13)
     self.assertFalse(os.path.isfile(privkey_file))
     self.assertFalse(os.path.isfile(pubkey_file))
Пример #3
0
 def test_load_private_key_failure_public_key_filename_passed(self):
     ''' load_private_key should fail if we pass the public key file instead of the private one '''
     random_string = ''.join(
         random.SystemRandom().choice(string.ascii_uppercase +
                                      string.digits) for _ in range(10))
     privkey_file = os.path.join('/tmp/', random_string + '.priv')
     pubkey_file = os.path.join('/tmp/', random_string + '.pub')
     privkey = crypto.generate_rsa_key()
     pubkey_generated = privkey.public_key()
     crypto.store_keys(privkey=privkey,
                       privkey_file=privkey_file,
                       pubkey_file=pubkey_file)
     with self.assertRaises(ValueError) as cm:
         loadedkey = crypto.load_private_key(pubkey_file)
     os.remove(privkey_file)
     os.remove(pubkey_file)
Пример #4
0
def get_private_key():
    privkey_file = config.config.key
    if not os.path.isfile(privkey_file):
        logging.logger.debug('Generating RSA keys...')
        key_dir=os.path.dirname(privkey_file)
        pubkey_file=os.path.join(key_dir,defaults.RSA_PUB_KEY)
        privkey=crypto.generate_rsa_key()
        crypto.store_keys(privkey=privkey, privkey_file=privkey_file, pubkey_file=pubkey_file)
        logging.logger.debug('Keys stored successfully on disk')
        pubkey=privkey.public_key()
        key_str=crypto.get_printable_pubkey(pubkey)
        logging.logger.info('This is the public key, add it to your Komlog account:\n'+key_str)
        return privkey
    else:
        privkey=crypto.load_private_key(privkey_file)
        return privkey
Пример #5
0
 def test_load_public_key_success(self):
     ''' load_public_key should succeed and return the public key '''
     random_string = ''.join(
         random.SystemRandom().choice(string.ascii_uppercase +
                                      string.digits) for _ in range(10))
     privkey_file = os.path.join('/tmp/', random_string + '.priv')
     pubkey_file = os.path.join('/tmp/', random_string + '.pub')
     privkey = crypto.generate_rsa_key()
     pubkey_generated = privkey.public_key()
     crypto.store_keys(privkey=privkey,
                       privkey_file=privkey_file,
                       pubkey_file=pubkey_file)
     pubkey_loaded = crypto.load_public_key(pubkey_file)
     os.remove(privkey_file)
     os.remove(pubkey_file)
     self.assertIsNotNone(pubkey_loaded)
     self.assertEqual(crypto.serialize_public_key(pubkey_generated),
                      crypto.serialize_public_key(pubkey_loaded))