Esempio n. 1
0
def getRsaKeyFile(filename, password=None):
    try:
        key = RSAKey(filename=filename, password=password)
    except IOError:
        print 'Generating new server RSA key.'
        key = RSAKey.generate(1024)
        key.write_private_key_file(filename, password=password)
    return key
Esempio n. 2
0
def getRsaKeyFile(filename, password=None):
    try:
        key = RSAKey(filename=filename, password=password)
    except IOError:
        log.info('Generating new server RSA key and saving in file %r.' % filename)
        key = RSAKey.generate(1024)
        key.write_private_key_file(filename, password=password)
    return key
Esempio n. 3
0
def getRsaKeyFile(filename, password=None):
    try:
        key = RSAKey(filename=filename, password=password)
    except IOError:
        log.info('Generating new server RSA key and saving in file %r.' % filename)
        key = RSAKey.generate(1024)
        key.write_private_key_file(filename, password=password)
    return key
Esempio n. 4
0
def get_rsa_key_file(filename, password=None):
    try:
        key = RSAKey(filename=filename, password=password)
    except IOError:
        logger.info('RSA Key file not found, generating a new one: {}'.format(filename))
        key = RSAKey.generate(1024)
        key.write_private_key_file(filename, password=password)
    return key
Esempio n. 5
0
 def test_keyfile_is_actually_encrypted(self):
     # Read an existing encrypted private key
     file_ = _support("test_rsa_password.key")
     password = "******"
     newfile = file_ + ".new"
     newpassword = "******"
     key = RSAKey(filename=file_, password=password)
     # Write out a newly re-encrypted copy with a new password.
     # When the bug under test exists, this will ValueError.
     try:
         key.write_private_key_file(newfile, password=newpassword)
         self.assert_keyfile_is_encrypted(newfile)
     finally:
         os.remove(newfile)
Esempio n. 6
0
 def test_keyfile_is_actually_encrypted(self):
     # Read an existing encrypted private key
     file_ = test_path('test_rsa_password.key')
     password = '******'
     newfile = file_ + '.new'
     newpassword = '******'
     key = RSAKey(filename=file_, password=password)
     # Write out a newly re-encrypted copy with a new password.
     # When the bug under test exists, this will ValueError.
     try:
         key.write_private_key_file(newfile, password=newpassword)
         self.assert_keyfile_is_encrypted(newfile)
     finally:
         os.remove(newfile)
Esempio n. 7
0
    def load_ssh_key(self, length=2048):
        cluster_name = self.get_active_cluster()
        cluster_folder = os.path.join(self.config_folder, cluster_name)
        filename = cluster_name + "_rsa"
        filepath = os.path.join(cluster_folder, filename)
        if os.path.exists(filepath):
            self.log.debug("loading private key: %s", filepath)
            k = RSAKey(filename=filepath)
        else:
            self.log.debug("generating new private key: %s", filepath)
            k = RSAKey.generate(length)
            k.write_private_key_file(filepath)

        return filepath, k
Esempio n. 8
0
 def test_salt_size(self):
     # Read an existing encrypted private key
     file_ = test_path('test_rsa_password.key')
     password = '******'
     newfile = file_ + '.new'
     newpassword = '******'
     key = RSAKey(filename=file_, password=password)
     # Write out a newly re-encrypted copy with a new password.
     # When the bug under test exists, this will ValueError.
     try:
         key.write_private_key_file(newfile, password=newpassword)
         # Verify the inner key data still matches (when no ValueError)
         key2 = RSAKey(filename=newfile, password=newpassword)
         self.assertEqual(key, key2)
     finally:
         os.remove(newfile)
Esempio n. 9
0
 def test_salt_size(self):
     # Read an existing encrypted private key
     file_ = test_path('test_rsa_password.key')
     password = '******'
     newfile = file_ + '.new'
     newpassword = '******'
     key = RSAKey(filename=file_, password=password)
     # Write out a newly re-encrypted copy with a new password.
     # When the bug under test exists, this will ValueError.
     try:
         key.write_private_key_file(newfile, password=newpassword)
         # Verify the inner key data still matches (when no ValueError)
         key2 = RSAKey(filename=newfile, password=newpassword)
         self.assertEqual(key, key2)
     finally:
         os.remove(newfile)
Esempio n. 10
0
  def listen(self):
    NETCONF_Server.set_instance(self)
    try:
      sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
      #sock.bind(('', self.port))
      sock.bind((self.host, self.port))
    except Exception as e:
      Logger.fatal('Bind failed: ' + str(e))

    try:
      sock.listen(100)
    except Exception as e:
      Logger.fatal('Listen failed: ' + str(e))
    Logger.info(f'Listening for connections on port {self.port}...')

    host_key = None
    try:
      host_key = RSAKey(filename=self.host_key_filename)
    except:
      pass
    if not host_key:
      Logger.info(f'Generating new host key')
      host_key = RSAKey.generate(2048)
      if self.host_key_filename:
        host_key.write_private_key_file(self.host_key_filename, password=None)
        Logger.info(f"Wrote host key to file, '{self.host_key_filename}'")

    while True:
      try:
        Logger.info(f'Waiting for client to connect')
        client, addr = sock.accept()
      except Exception as e:
        Logger.fatal('Accept failed: ' + str(e))
      self.sock = client
      (ip, port) = addr
      Logger.info(f'Client {ip}:{port} connected')
      self.handle_connection(host_key)
      Logger.info(f'Client {ip}:{port} disconnected')