lxd-project/sshim_patch.py

84 lines
2.7 KiB
Python
Raw Normal View History

2022-11-23 19:18:40 +00:00
from sshim import *
import paramiko
import os
import uuid
import lxd_interface
import threading
import logging
2022-11-28 19:25:39 +00:00
import select
2022-11-26 23:06:01 +00:00
import time
2022-11-26 23:48:22 +00:00
import inspect
2022-11-26 22:24:31 +00:00
logger = logging.getLogger(__name__)
def check_channel_shell_request(self, channel):
2022-11-28 19:25:39 +00:00
logger.debug("Check channel shell request: %s" % channel.get_id())
Runner(self, channel).start()
return True
2022-11-23 19:18:40 +00:00
def check_auth_none(self, username):
if username == os.environ["ssh-username"]:
return paramiko.AUTH_PARTIALLY_SUCCESSFUL
return paramiko.AUTH_FAILED
2022-11-23 19:18:40 +00:00
def check_auth_password(self, username, password):
2022-11-28 19:55:27 +00:00
logger.debug(f"{username} just tried to connect")
if username == os.environ["SSH_USERNAME"] and password == os.environ["SSH_PASSWORD"]:
2022-11-23 19:18:40 +00:00
return paramiko.AUTH_SUCCESSFUL
return paramiko.AUTH_FAILED
def check_auth_publickey(self, username, key):
return paramiko.AUTH_FAILED
class Runner(threading.Thread):
def __init__(self, client, channel: paramiko.Channel):
threading.Thread.__init__(self, name='sshim.Runner(%s)' % channel.chanid)
self.instance_name = "instance-" + str(uuid.uuid4())
2022-11-25 16:08:10 +00:00
self.instance_password = str(uuid.uuid4()) # TODO: secure password generation
self.daemon = True
self.client = client
self.channel = channel
self.channel.settimeout(None)
2022-11-26 23:06:01 +00:00
self.transport = None
def run(self) -> None:
2022-11-26 23:06:01 +00:00
vm_ip = lxd_interface.create_instance(self.instance_name, self.instance_password)['address']
2022-11-25 16:08:10 +00:00
with paramiko.SSHClient() as ssh_client:
2022-11-26 23:06:01 +00:00
ssh_client.set_missing_host_key_policy(paramiko.WarningPolicy)
ssh_client.connect(vm_ip, username='root', password=self.instance_password)
self.transport = ssh_client.get_transport()
2022-11-28 19:25:39 +00:00
client_channel = ssh_client.invoke_shell()
2022-11-26 23:06:01 +00:00
while True:
2022-11-28 19:25:39 +00:00
r, w, e = select.select([client_channel, self.channel], [], [])
if self.channel in r:
x = self.channel.recv(1024)
if len(x) == 0:
break
client_channel.send(x)
if client_channel in r:
x = client_channel.recv(1024)
if len(x) == 0:
break
self.channel.send(x)
2022-11-26 23:48:22 +00:00
2022-11-28 19:38:07 +00:00
client_channel.close()
self.channel.close()
lxd_interface.destroy_instance(self.instance_name)
2022-11-23 19:18:40 +00:00
Handler.check_channel_shell_request = check_channel_shell_request
2022-11-23 19:18:40 +00:00
Handler.check_auth_none = check_auth_none
Handler.check_auth_password = check_auth_password
Handler.check_auth_publickey = check_auth_publickey
Handler.enable_auth_gssapi = paramiko.server.ServerInterface.enable_auth_gssapi
Handler.get_allowed_auths = paramiko.server.ServerInterface.get_allowed_auths