From 49341e56df1040f64f2f07b5efdee6d3c535f42c Mon Sep 17 00:00:00 2001
From: Mike McLean <mikem@redhat.com>
Date: May 02 2023 19:45:27 +0000
Subject: [PATCH 1/2] fakehub --user option


---

diff --git a/devtools/fakehub b/devtools/fakehub
index 787bf43..07eefb1 100755
--- a/devtools/fakehub
+++ b/devtools/fakehub
@@ -14,7 +14,8 @@ from urllib.parse import quote
 
 sys.path.insert(0, os.getcwd())
 import koji
-from kojihub import kojixmlrpc
+from kojihub import auth, kojixmlrpc, kojihub
+from koji.context import context
 import koji.xmlrpcplus
 
 
@@ -34,6 +35,38 @@ def handle_rpc(self, environ):
         raise
 
 
+# Fake session for authenticated calls
+class FakeSession(auth.Session):
+
+    def __init__(self, user):
+        user = kojihub.get_user(user, strict=True)
+        self.logged_in = True
+        self.id = 1
+        self.user_id = user['id']
+        self.authtype = koji.AUTHTYPES['GSSAPI']
+        self.hostip = '127.0.0.1'
+        self.master = None
+        self.callnum = 1
+        self.message = 'THIS IS A FAKE SESSION'
+        self.exclusive = False
+        self.user_data = user
+        self.session_data = {'msg': 'this is a fake session'}
+        self._perms = None
+        self._groups = None
+        self._host_id = ''
+
+
+original_check_session = kojixmlrpc.ModXMLRPCRequestHandler.check_session
+
+
+# alternate check_session, used to emulate auth
+def check_session(self):
+    if 'KOJI_FAKEHUB_USER' in context.environ:
+        context.session = FakeSession(context.environ['KOJI_FAKEHUB_USER'])
+    else:
+        original_check_session(self)
+
+
 def get_url(environ):
     url = environ['wsgi.url_scheme']+'://'
 
@@ -67,6 +100,7 @@ def get_options():
     parser = optparse.OptionParser(usage='%prog [options] <policy_file>')
     parser.add_option('--pdb', action='store_true',
                       help='drop into pdb on error')
+    parser.add_option('--user', '-u', help='execute as user')
     opts, args = parser.parse_args()
 
     # parse request from args
@@ -124,6 +158,10 @@ def main():
     environ['wsgi.input'] = io.StringIO(options.request)
     environ['REQUEST_METHOD'] = 'POST'
     environ['CONTENT_TYPE'] = 'text/xml'
+    if options.user:
+        environ['KOJI_FAKEHUB_USER'] = options.user
+        kojixmlrpc.ModXMLRPCRequestHandler.check_session = check_session
+
     set_config(environ)
     print('RESULT:')
     data = kojixmlrpc.application(environ, start_response)

From 76a39698e36a01be835f607a18fb7cbedb32deef Mon Sep 17 00:00:00 2001
From: Mike McLean <mikem@redhat.com>
Date: May 02 2023 19:49:20 +0000
Subject: [PATCH 2/2] fakehub --exclusive option


---

diff --git a/devtools/fakehub b/devtools/fakehub
index 07eefb1..4e7920d 100755
--- a/devtools/fakehub
+++ b/devtools/fakehub
@@ -38,7 +38,7 @@ def handle_rpc(self, environ):
 # Fake session for authenticated calls
 class FakeSession(auth.Session):
 
-    def __init__(self, user):
+    def __init__(self, user, exclusive=False):
         user = kojihub.get_user(user, strict=True)
         self.logged_in = True
         self.id = 1
@@ -48,7 +48,7 @@ class FakeSession(auth.Session):
         self.master = None
         self.callnum = 1
         self.message = 'THIS IS A FAKE SESSION'
-        self.exclusive = False
+        self.exclusive = exclusive
         self.user_data = user
         self.session_data = {'msg': 'this is a fake session'}
         self._perms = None
@@ -62,7 +62,8 @@ original_check_session = kojixmlrpc.ModXMLRPCRequestHandler.check_session
 # alternate check_session, used to emulate auth
 def check_session(self):
     if 'KOJI_FAKEHUB_USER' in context.environ:
-        context.session = FakeSession(context.environ['KOJI_FAKEHUB_USER'])
+        context.session = FakeSession(context.environ['KOJI_FAKEHUB_USER'],
+                                      context.environ.get('KOJI_FAKEHUB_EXCLUSIVE', False))
     else:
         original_check_session(self)
 
@@ -101,6 +102,8 @@ def get_options():
     parser.add_option('--pdb', action='store_true',
                       help='drop into pdb on error')
     parser.add_option('--user', '-u', help='execute as user')
+    parser.add_option('--exclusive', '-x', action='store_true',
+                      help='emulate an exclusive session')
     opts, args = parser.parse_args()
 
     # parse request from args
@@ -161,6 +164,8 @@ def main():
     if options.user:
         environ['KOJI_FAKEHUB_USER'] = options.user
         kojixmlrpc.ModXMLRPCRequestHandler.check_session = check_session
+        if options.exclusive:
+            environ['KOJI_FAKEHUB_EXCLUSIVE'] = True
 
     set_config(environ)
     print('RESULT:')

