2022-11-23 19:18:40 +00:00
|
|
|
from sshim import *
|
|
|
|
import paramiko
|
|
|
|
import os
|
2022-11-25 15:41:02 +00:00
|
|
|
import uuid
|
|
|
|
import lxd_interface
|
|
|
|
import threading
|
|
|
|
import logging
|
2022-11-26 23:06:01 +00:00
|
|
|
import time
|
2022-11-26 23:48:22 +00:00
|
|
|
import inspect
|
2022-11-25 15:41:02 +00:00
|
|
|
|
2022-11-26 22:24:31 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2022-11-24 19:59:21 +00:00
|
|
|
|
|
|
|
|
2022-11-25 15:41:02 +00:00
|
|
|
def check_channel_shell_request(self, channel):
|
|
|
|
logger.debug(channel)
|
|
|
|
Runner(self, channel).start()
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2022-11-23 19:18:40 +00:00
|
|
|
def check_auth_none(self, username):
|
2022-11-24 19:59:21 +00:00
|
|
|
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-26 22:24:31 +00:00
|
|
|
logger.debug(os.environ["ssh-username"])
|
2022-11-23 19:18:40 +00:00
|
|
|
if username == os.environ["ssh-username"] and password == os.environ["ssh-password"]:
|
|
|
|
return paramiko.AUTH_SUCCESSFUL
|
|
|
|
return paramiko.AUTH_FAILED
|
|
|
|
|
|
|
|
|
|
|
|
def check_auth_publickey(self, username, key):
|
|
|
|
return paramiko.AUTH_FAILED
|
|
|
|
|
|
|
|
|
2022-11-25 15:41:02 +00:00
|
|
|
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
|
2022-11-25 15:41:02 +00:00
|
|
|
self.daemon = True
|
|
|
|
self.client = client
|
|
|
|
self.channel = channel
|
|
|
|
self.channel.settimeout(None)
|
2022-11-26 23:06:01 +00:00
|
|
|
self.transport = None
|
2022-11-25 15:41:02 +00:00
|
|
|
|
|
|
|
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 15:41:02 +00:00
|
|
|
|
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-25 16:08:10 +00:00
|
|
|
tmp_channel = ssh_client.invoke_shell()
|
|
|
|
|
2022-11-26 23:48:22 +00:00
|
|
|
self.channel.other_channel = tmp_channel
|
|
|
|
self.channel.__getattribute__ = Patch.__getattribute__
|
2022-11-26 23:06:01 +00:00
|
|
|
|
|
|
|
while True:
|
|
|
|
time.sleep(1000)
|
2022-11-25 15:41:02 +00:00
|
|
|
|
|
|
|
|
2022-11-26 23:48:22 +00:00
|
|
|
class Patch:
|
|
|
|
def __getattribute__(self, item):
|
|
|
|
getattr(self.other_channel, item)
|
|
|
|
|
2022-11-23 19:18:40 +00:00
|
|
|
|
2022-11-25 15:41:02 +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
|
2022-11-24 19:59:21 +00:00
|
|
|
Handler.enable_auth_gssapi = paramiko.server.ServerInterface.enable_auth_gssapi
|
2022-11-25 15:41:02 +00:00
|
|
|
Handler.get_allowed_auths = paramiko.server.ServerInterface.get_allowed_auths
|