Skip to content

Commit

Permalink
Add user options for login and logout
Browse files Browse the repository at this point in the history
  • Loading branch information
gpoppino committed Apr 22, 2024
1 parent 649988e commit d476690
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
14 changes: 14 additions & 0 deletions src/sumacli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@ def perform_utils_tasks(args):
perform_suma_scheduling(factory, args)


def perform_user_tasks(args):
client = suma_xmlrpc_client.SumaClient(args.config)

if args.login:
client.login()
elif args.logout:
client.logout()


def main():
logging_file = "/etc/sumacli/logging.conf"
if not os.path.isfile(logging_file):
Expand Down Expand Up @@ -177,6 +186,11 @@ def main():
utils_parser.add_argument("-f", "--save-action-ids-file", help="File name to save action IDs of scheduled jobs.")
utils_parser.set_defaults(func=perform_utils_tasks)

user_parser = subparsers.add_parser("user", help="User management commands.")
user_parser.add_argument("-i", "--login", help="Logs in to the server.", action="store_true")
user_parser.add_argument("-o", "--logout", help="Logs out the user from the server.", action="store_true")
user_parser.set_defaults(func=perform_user_tasks)

args = parser.parse_args()
if args.cmd == 'patch':
if args.policy is None and args.all_patches is False and args.bugfix is False and \
Expand Down
15 changes: 7 additions & 8 deletions src/sumacli/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,17 @@ class SumaClient:
def __init__(self, config_file=None):
self.__config_manager = ConfigManager(config_file)
self.__session_manager = SessionManager()
self.__client = None
self.__logger = logging.getLogger(__name__)

context = ssl.create_default_context()
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
self.__client = ServerProxy(self.__config_manager.manager_api_url, context=context)

def __getattr__(self, name):
return _MultiCallMethod(self, name)

def login(self):
if self.__client is None:
context = ssl.create_default_context()
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
self.__client = ServerProxy(self.__config_manager.manager_api_url, context=context)

if self.__session_manager.session_key is not None:
# try to run a query to the server to see if the session is still valid
try:
Expand Down Expand Up @@ -73,7 +71,8 @@ def logout(self):
self.__client.auth.logout(self.__session_manager.session_key)
self.__client("close")()
del self.__session_manager.session_key
self.__logger.info(f'User {self.__config_manager.manager_login} logged out')
if self.__config_manager.manager_login is not None:
self.__logger.info(f'User {self.__config_manager.manager_login} logged out')

def is_logged_in(self):
return self.__session_manager.session_key is not None
Expand Down
17 changes: 9 additions & 8 deletions src/sumacli/session_mgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ def session_key(self, session_key):

@session_key.deleter
def session_key(self):
with open(self.__session_file, 'r') as in_session_file:
lines = in_session_file.readlines()
for line in lines:
if line.startswith(self.__config_manager.manager_login):
lines.remove(line)

with open(self.__session_file, 'w') as out_session_file:
out_session_file.writelines(lines)
if os.path.isfile(self.__session_file):
with open(self.__session_file, 'r') as in_session_file:
lines = in_session_file.readlines()
for line in lines:
if line.startswith(self.__config_manager.manager_login):
lines.remove(line)

with open(self.__session_file, 'w') as out_session_file:
out_session_file.writelines(lines)

0 comments on commit d476690

Please sign in to comment.