1
0
mirror of https://github.com/gryf/ferrit.git synced 2026-02-13 21:45:45 +01:00

Tiny cleanup

This commit is contained in:
Szymon Datko
2019-10-25 15:07:57 +02:00
committed by gryf
parent 7d316947b4
commit 8960f88360
2 changed files with 62 additions and 42 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
.venv/ .venv/
*.swp *.swp
gerrit_fake_ssh_server.log gerrit_fake_ssh_server.log
jenkins.war

View File

@@ -1,20 +1,18 @@
#!/usr/bin/env python #!/usr/bin/env python
import inspect import inspect
import logging
import os import os
import select
import socket
import socketserver import socketserver
import sys import sys
import threading import threading
import time import time
import traceback import traceback
import logging
import paramiko import paramiko
from paramiko.py3compat import u
PORT = 2200 # it could be even 29418, which is standard gerrit port # it could be even 29418, which is standard gerrit port
PORT = 2200
FILE_DIR = os.path.dirname(__file__) FILE_DIR = os.path.dirname(__file__)
BASE_NAME = os.path.extsep.join(os.path.basename(__file__) BASE_NAME = os.path.extsep.join(os.path.basename(__file__)
@@ -23,19 +21,25 @@ HOST_KEY = paramiko.RSAKey(filename=os.path.join(FILE_DIR,
'gerrit-server-key'), 'gerrit-server-key'),
password='jenkins') password='jenkins')
COMMANDS_MAP = {'gerrit version': '', GERRIT_CMD_PROJECTS = """All-Projects
'gerrit stream-events': '{}\r\n'} All-Users
openstack
GERRIT_CMD_VERSION = 'gerrit version 2.16.7\n' openstack/nova
openstack/neutron
GERRIT_SHELL_MSG = """ """
GERRIT_CMD_VERSION = "ferrit version 1.0.0\n"
GERRIT_SHELL_MSG = """\r
**** Welcome to Ferrit Code Review ****\r **** Welcome to Ferrit Code Review ****\r
\r \r
Hi Jenkins, you have successfully connected over SSH.\r Hi Jenkins, you have successfully connected over SSH.\r
\r \r
Unfortunately, interactive shells are disabled.\r Unfortunately, interactive shells are disabled.\r
To clone a hosted Git repository, use:\r
\r \r
""" git clone ssh://localhost:{GERRIT_PORT}/REPOSITORY_NAME.git\r
\r
""".format(GERRIT_PORT=PORT)
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
LOG.setLevel(logging.DEBUG) LOG.setLevel(logging.DEBUG)
handler = logging.FileHandler(os.path.join(FILE_DIR, BASE_NAME + '.log')) handler = logging.FileHandler(os.path.join(FILE_DIR, BASE_NAME + '.log'))
@@ -43,73 +47,79 @@ handler.setFormatter(logging.Formatter('%(asctime)s [%(levelname)s] '
'%(filename)s:%(lineno)s - ' '%(filename)s:%(lineno)s - '
'%(message)s')) '%(message)s'))
LOG.addHandler(handler) LOG.addHandler(handler)
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(logging.Formatter('%(asctime)s [%(levelname)s] '
'%(filename)s:%(lineno)s - '
'%(message)s'))
LOG.addHandler(handler)
class Server(paramiko.ServerInterface): class Server(paramiko.ServerInterface):
def __init__(self, client_address): def __init__(self, client_address):
LOG.debug('%s', inspect.stack()[0][3])
LOG.debug('client_address: %s', client_address)
self.command = None
self.event = threading.Event() self.event = threading.Event()
self.client_address = client_address self.client_address = client_address
def check_channel_request(self, kind, chanid): def check_channel_request(self, kind, chanid):
LOG.debug('Kind: %s, chanid: %s', kind, chanid) LOG.debug('%s', inspect.stack()[0][3])
LOG.debug('kind: %s, chanid: %s', kind, chanid)
if kind == 'session': if kind == 'session':
return paramiko.OPEN_SUCCEEDED return paramiko.OPEN_SUCCEEDED
return paramiko.OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED return paramiko.OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED
def get_allowed_auths(self, username): def get_allowed_auths(self, username):
LOG.debug('%s', inspect.stack()[0][3])
LOG.debug('username: %s', username)
return "password,publickey" return "password,publickey"
def check_auth_password(self, username, password): def check_auth_password(self, username, password):
LOG.debug('%s', inspect.stack()[0][3])
LOG.debug('username: %s, password: %s', username, password)
return paramiko.AUTH_SUCCESSFUL return paramiko.AUTH_SUCCESSFUL
def check_auth_publickey(self, username, key): def check_auth_publickey(self, username, key):
LOG.debug('%s', inspect.stack()[0][3])
LOG.debug('username: %s, key: %s', username, str(key)[:11])
return paramiko.AUTH_SUCCESSFUL return paramiko.AUTH_SUCCESSFUL
def check_channel_exec_request(self, channel, command): def check_channel_exec_request(self, channel, command):
LOG.debug(inspect.stack()[0][3]) LOG.debug('%s', inspect.stack()[0][3])
LOG.debug('channel: %s, command: %s', channel.get_id(), command)
self.command = command self.command = command
self.event.set() self.event.set()
return True return True
def check_channel_shell_request(self, channel): def check_channel_shell_request(self, channel):
LOG.debug(inspect.stack()[0][3]) LOG.debug('%s', inspect.stack()[0][3])
self.command = None LOG.debug('channel: %s', channel.get_id())
self.event.set() self.event.set()
return True return True
def check_channel_subsystem_request(self, channel, name):
return True
def check_channel_window_change_request(self, channel, width, height,
pixelwidth, pixelheight):
return True
def check_channel_x11_request(self, channel, single_connection,
auth_protocol, auth_cookie, screen_number):
return True
def check_channel_forward_agent_request(self, channel):
return True
def check_global_request(self, kind, msg): def check_global_request(self, kind, msg):
return True LOG.debug('%s', inspect.stack()[0][3])
LOG.debug('kind: %s, msg: %s', kind, msg)
def check_channel_direct_tcpip_request(self, chanid, origin, destination):
return True return True
def check_channel_env_request(self, channel, name, value): def check_channel_env_request(self, channel, name, value):
LOG.debug("channel: %s, name: %s, value: %s", channel, name, value) LOG.debug('%s', inspect.stack()[0][3])
LOG.debug("channel: %s, name: %s, value: %s",
channel.get_id(), name, value)
return True return True
def check_channel_pty_request(self, channel, term, width, height, def check_channel_pty_request(self, channel, term, width, height,
pixelwidth, pixelheight, modes): pixelwidth, pixelheight, modes):
LOG.debug('%s', inspect.stack()[0][3])
LOG.debug("channel: %s, term: %s, width: %s, height: %s, "
"pixelwidth: %s, pixelheight: %s, modes: %s",
channel.get_id(), term, width, height,
pixelwidth, pixelheight, str(modes)[:7])
return True return True
class SSHHandler(socketserver.StreamRequestHandler): class SSHHandler(socketserver.StreamRequestHandler):
def handle(self): def handle(self):
self._prev = None
try: try:
transport = paramiko.Transport(self.connection) transport = paramiko.Transport(self.connection)
transport.add_server_key(HOST_KEY) transport.add_server_key(HOST_KEY)
@@ -132,24 +142,33 @@ class SSHHandler(socketserver.StreamRequestHandler):
return 1 return 1
if server.command: if server.command:
LOG.debug('server_command %s, mapped to: %s',
server.command.decode('utf-8'),
COMMANDS_MAP.get(server.command.decode('utf-8')))
cmd = server.command.decode('utf-8') cmd = server.command.decode('utf-8')
LOG.debug('received server command: %s', cmd)
if cmd == 'gerrit version': if cmd == 'gerrit version':
LOG.debug('sending version string')
channel.send(GERRIT_CMD_VERSION) channel.send(GERRIT_CMD_VERSION)
channel.close() channel.close()
elif cmd == 'gerrit ls-projects':
LOG.debug('sending list of projects')
channel.send(GERRIT_CMD_PROJECTS)
channel.close()
elif cmd == 'gerrit stream-events': elif cmd == 'gerrit stream-events':
LOG.debug('sending events from queue...')
while True: while True:
time.sleep(1) time.sleep(1)
pass # TODO: implement queue or something here
else: else:
LOG.debug('unknown command -- closing channel')
channel.close() channel.close()
else: else:
# interactive session LOG.debug('requested interactive session')
channel.send_stderr(GERRIT_SHELL_MSG) channel.send_stderr(GERRIT_SHELL_MSG)
channel.makefile("rU").read(1) # wait for user interaction channel.makefile("rU").read(1) # wait for any key press
return return
except Exception: except Exception: