From 68ea17b85481125ae2e5da461389c6437e436b0b Mon Sep 17 00:00:00 2001 From: gryf Date: Fri, 25 Oct 2019 11:03:51 +0200 Subject: [PATCH] Cleaned up code a bit --- gerrit_fake_http_server.py | 453 +------------------------------------ gerrit_fake_ssh_server.py | 377 +++++++++++++++--------------- 2 files changed, 189 insertions(+), 641 deletions(-) diff --git a/gerrit_fake_http_server.py b/gerrit_fake_http_server.py index 621f99b..75c50ac 100644 --- a/gerrit_fake_http_server.py +++ b/gerrit_fake_http_server.py @@ -2,448 +2,14 @@ import bottle @bottle.route('/plugins/events-log/') -def events(params=None): - return bottle.redirect('/plugins/events-log/Documentation/index.html', code=302) - -@bottle.route('/plugins/events-log/Documentation/index.html') def events(params=None): return """ -Plugin events-log -

Plugin events-log

- - -
Nameevents-log plugin
Versionv2.13-178-g68fcb6a4b1
-

About

-

This plugin listens to stream events and stores them in a database. The events can be retrieved through REST API. Some use cases for this plugin include debugging for admins; also users who use scripts which listen to stream events can query for any events that they might have missed.

-

Documentation

- -

REST APIs

- - - -""" +)]}' +{ + "name": "plugins/events-log", + "clone_url": "http://localhost:8181/plugins/events-log", + "description": "This plugin listens to stream events and stores them in a database. The events can be retrieved through REST API." +}""" @bottle.route('/a/projects/') @@ -452,9 +18,7 @@ def projects(params=None): Possible params (accessible via bottle.request.params) is 'd' """ print(params) - return """ -)]}' -{ + return { "All-Projects": { "id": "All-Projects", "description": "Rights inherited by all other projects.", @@ -502,8 +66,5 @@ def projects(params=None): ] } } -""" - return {} - bottle.run(host='localhost', port=8181, debug=True) diff --git a/gerrit_fake_ssh_server.py b/gerrit_fake_ssh_server.py index bd23357..e4435cc 100644 --- a/gerrit_fake_ssh_server.py +++ b/gerrit_fake_ssh_server.py @@ -1,195 +1,182 @@ -#!/usr/bin/env python - -from binascii import hexlify -import os -import socket -import sys -import threading -import traceback -import socketserver -import logging -import time -import inspect -import select -#import random - -import paramiko -from paramiko.py3compat import b, u - -PORT = 2200 -LOG_FILE = 'fakessh.log' - - -host_key = paramiko.RSAKey(filename='gerrit-server-key', password='jenkins') - -COMMANDS_MAP = {'gerrit version': '', - 'gerrit stream-events': '{}\r\n'} - -GERRIT_CMD_VERSION = 'gerrit version 2.16.7\n' - -GERRIT_SHELL_MSG = '''\r - **** Welcome to Gerrit Code Review ****\r -\r - Hi Jenkins, you have successfully connected over SSH.\r -\r - Unfortunately, interactive shells are disabled.\r - To clone a hosted Git repository, use:\r -\r - git clone ssh://jenkins@review.dev.cloud.company.net:29418/REPOSITORY_NAME.git\r -\r -''' - - -class Server(paramiko.ServerInterface): - - def __init__(self, client_address): - self.event = threading.Event() - self.client_address = client_address - - def check_channel_request(self, kind, chanid): - print(kind, chanid) - if kind == 'session': - return paramiko.OPEN_SUCCEEDED - return paramiko.OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED - - def get_allowed_auths(self, username): - return "password,publickey" - - def check_auth_password(self, username, password): - return paramiko.AUTH_SUCCESSFUL - - def check_auth_publickey(self, username, key): - return paramiko.AUTH_SUCCESSFUL - - def check_channel_exec_request(self, channel, command): - print(inspect.stack()[0][3]) - self.command = command - self.event.set() - return True - - def check_channel_shell_request(self, channel): - print(inspect.stack()[0][3]) - self.command = None - self.event.set() - return True - - def check_channel_subsystem_request(self, channel, name): - print(inspect.stack()[0][3]) - return True - def check_channel_window_change_request(self, channel, width, height, pixelwidth, pixelheight): - print(inspect.stack()[0][3]) - return True - def check_channel_x11_request(self, channel, single_connection, auth_protocol, auth_cookie, screen_number): - print(inspect.stack()[0][3]) - return True - def check_channel_forward_agent_request(self, channel): - print(inspect.stack()[0][3]) - return True - def check_global_request(self, kind, msg): - print(inspect.stack()[0][3]) - return True - def check_channel_direct_tcpip_request(self, chanid, origin, destination): - print(inspect.stack()[0][3]) - return True - def check_channel_env_request(self, channel, name, value): - print(inspect.stack()[0][3]) - return True - - def check_channel_pty_request(self, channel, term, width, height, - pixelwidth, pixelheight, modes): - print(inspect.stack()[0][3]) - return True - - -class SSHHandler(socketserver.StreamRequestHandler): - def handle(self): - self._prev = None - try: - t = paramiko.Transport(self.connection) - t.add_server_key(host_key) - server = Server(self.client_address) - try: - t.start_server(server=server) - except paramiko.SSHException: - return - - while True: - print('powstaje kanał') - - # wait for auth - chan = t.accept(20) - if chan is None: - t.close() - return 1 - - print('czekanie na coś') - - #import pdb; pdb.set_trace() - - server.event.wait(10) - if not server.event.is_set(): - t.close() - return 1 - - # import pdb; pdb.set_trace() - print('coś przyszło!\n') - - #import pdb; pdb.set_trace() - - if server.command: - print('Jest komenda! {}'.format(server.command)) - print('server_command %s' % server.command.decode('utf-8')) - print('%s' % COMMANDS_MAP.get(server.command.decode('utf-8'))) - #chan.send(COMMANDS_MAP.get(server.command.decode('utf-8'), '{}\r')) - #chan.close() - - cmd = server.command.decode('utf-8') - - if cmd == 'gerrit version': - chan.send(GERRIT_CMD_VERSION) - chan.close() - elif cmd == 'gerrit stream-events': - while True: - time.sleep(1) - else: - chan.close() - else: - chan.send_stderr(GERRIT_SHELL_MSG) - f = chan.makefile("rU") - f.read(1) - print(f.read(1)) - print(f.read(1)) - print(f.read(1)) - while True: - r, w, e = select.select([chan, sys.stdin], [], []) - if chan in r: - try: - x = u(chan.recv(1024)) - if len(x) == 0: - sys.stdout.write("\r\n*** EOF\r\n") - break - sys.stdout.write(x) - sys.stdout.flush() - except socket.timeout: - pass - if sys.stdin in r: - x = sys.stdin.read(1) - if len(x) == 0: - break - chan.send(x) - - - except Exception as e: - traceback.print_exc() - finally: - try: - t.close() - except: - pass - - -sshserver = socketserver.ThreadingTCPServer(('127.0.0.1', PORT), SSHHandler) -sshserver.serve_forever() - - - +#!/usr/bin/env python +import inspect +import os +import select +import socket +import socketserver +import sys +import threading +import time +import traceback + +import paramiko +from paramiko.py3compat import u + +#PORT = 29418 +PORT = 2200 + +host_key = paramiko.RSAKey(filename=os.path.join(os.path.dirname(__file__), + 'gerrit-server-key'), + password='jenkins') + +COMMANDS_MAP = {'gerrit version': '', + 'gerrit stream-events': '{}\r\n'} + +GERRIT_CMD_VERSION = 'gerrit version 2.16.7\n' + +GERRIT_SHELL_MSG = """ + **** Welcome to Ferrit Code Review ****\r +\r + Hi Jenkins, you have successfully connected over SSH.\r +\r + Unfortunately, interactive shells are disabled.\r +\r +""" + + +class Server(paramiko.ServerInterface): + + def __init__(self, client_address): + self.event = threading.Event() + self.client_address = client_address + + def check_channel_request(self, kind, chanid): + print('Kind: %s, chanid: %s', kind, chanid) + if kind == 'session': + return paramiko.OPEN_SUCCEEDED + return paramiko.OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED + + def get_allowed_auths(self, username): + return "password,publickey" + + def check_auth_password(self, username, password): + return paramiko.AUTH_SUCCESSFUL + + def check_auth_publickey(self, username, key): + return paramiko.AUTH_SUCCESSFUL + + def check_channel_exec_request(self, channel, command): + print("stack: %s", inspect.stack()[0][3]) + self.command = command + self.event.set() + return True + + def check_channel_shell_request(self, channel): + print(inspect.stack()[0][3]) + self.command = None + self.event.set() + 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): + return True + + def check_channel_direct_tcpip_request(self, chanid, origin, destination): + return True + + def check_channel_env_request(self, channel, name, value): + print("channel: %s, name: %s, value: %s", channel, name, value) + return True + + def check_channel_pty_request(self, channel, term, width, height, + pixelwidth, pixelheight, modes): + return True + + +class SSHHandler(socketserver.StreamRequestHandler): + def handle(self): + self._prev = None + try: + transport = paramiko.Transport(self.connection) + transport.add_server_key(host_key) + server = Server(self.client_address) + try: + transport.start_server(server=server) + except paramiko.SSHException: + return + + while True: + print('powstaje kanał') + + # wait for auth + channel = transport.accept(20) + if channel is None: + transport.close() + return 1 + + print('czekanie na coś') + + server.event.wait(10) + if not server.event.is_set(): + transport.close() + return 1 + + print('coś przyszło!\n') + + __import__('pdb').set_trace() + if server.command: + print('server_command %s' % server.command.decode('utf-8')) + print('%s' % + COMMANDS_MAP.get(server.command.decode('utf-8'))) + + cmd = server.command.decode('utf-8') + + if cmd == 'gerrit version': + channel.send(GERRIT_CMD_VERSION) + channel.close() + elif cmd == 'gerrit stream-events': + while True: + time.sleep(1) + else: + channel.close() + else: + __import__('pdb').set_trace() + channel.send_stderr(GERRIT_SHELL_MSG) + fobj = channel.makefile("rU") + fobj.read(1) + print(fobj.read(1)) + print(fobj.read(1)) + print(fobj.read(1)) + while True: + r, w, e = select.select([channel, sys.stdin], [], []) + if channel in r: + try: + x = u(channel.recv(1024)) + if len(x) == 0: + sys.stdout.write("\r\n*** EOF\r\n") + break + sys.stdout.write(x) + sys.stdout.flush() + except socket.timeout: + pass + if sys.stdin in r: + x = sys.stdin.read(1) + if len(x) == 0: + break + channel.send(x) + + except Exception as e: + traceback.print_exc() + finally: + try: + # channel.close() + transport.close() + except: + pass + + +sshserver = socketserver.ThreadingTCPServer(('127.0.0.1', PORT), SSHHandler) +sshserver.serve_forever()