Some updates

This commit is contained in:
JoaoBastos023 2024-12-18 08:44:07 +00:00
parent 3cb86f17f2
commit c5744de19a
4 changed files with 41 additions and 12 deletions

View File

@ -31,13 +31,10 @@ def listRoles(args):
args = parser.parse_args() args = parser.parse_args()
#Check number of arguments #Check number of arguments
if not args.session or not args.role: if not args.session:
logger.error("Need session file and username.") logger.error("Need session file.")
sys.exit(1) sys.exit(1)
#Validate role name
#TODO
# Check for session file # Check for session file
if not os.path.isfile(BASE_DIR + args.session): if not os.path.isfile(BASE_DIR + args.session):
logger.error("File '" + args.session + "' not found.") logger.error("File '" + args.session + "' not found.")
@ -47,7 +44,6 @@ def listRoles(args):
with open(BASE_DIR + args.session, 'r') as f: with open(BASE_DIR + args.session, 'r') as f:
args.session = json.load(f) args.session = json.load(f)
# TODO:
try: try:
req = requests.get(f'http://{state['REP_ADDRESS']}/role/session/list', headers={'Authorization': args.session['token']}) req = requests.get(f'http://{state['REP_ADDRESS']}/role/session/list', headers={'Authorization': args.session['token']})
req.raise_for_status() req.raise_for_status()
@ -55,5 +51,18 @@ def listRoles(args):
logger.error("Failed to obtain response from server.") logger.error("Failed to obtain response from server.")
sys.exit(-1) sys.exit(-1)
roles = req.json()
if not args.role:
for r in roles.items():
sys.stdout.write(r)
sys.exit(0)
elif args.role not in roles.items():
logger.error("Role %s does not exist.", args.role)
sys.exit(1)
else:
logger.info("Role %s exists.", args.role)
sys.exit(0)
if __name__ == '__main__': if __name__ == '__main__':
listRoles(sys.argv[1:]) listRoles(sys.argv[1:])

View File

@ -69,5 +69,11 @@ def listSubjectRoles(args):
logger.error("Failed to obtain response from server.") logger.error("Failed to obtain response from server.")
sys.exit(-1) sys.exit(-1)
roles = req.json()
for r in roles.items():
sys.stdout.write(r)
sys.exit(0)
if __name__ == '__main__': if __name__ == '__main__':
listSubjectRoles(sys.argv[1:]) listSubjectRoles(sys.argv[1:])

View File

@ -5,6 +5,7 @@ import logging
import requests import requests
import json import json
import argparse import argparse
import subprocess
from subject import main from subject import main
@ -36,7 +37,11 @@ def reactivateRole(args):
sys.exit(1) sys.exit(1)
#Validate role name #Validate role name
#TODO process = subprocess.Popen(f"./rep_list_roles {args.role}", shell=True)
process.wait()
if process.returncode != 0:
logger.error("Role does not exist.")
sys.exit(1)
# Check for session file # Check for session file
if not os.path.isfile(BASE_DIR + args.session): if not os.path.isfile(BASE_DIR + args.session):
@ -47,7 +52,6 @@ def reactivateRole(args):
with open(BASE_DIR + args.session, 'r') as f: with open(BASE_DIR + args.session, 'r') as f:
args.session = json.load(f) args.session = json.load(f)
# TODO:
try: try:
req = requests.post(f'http://{state['REP_ADDRESS']}/role/' + args.role + '/activate', headers={'Authorization': args.session['token']}) req = requests.post(f'http://{state['REP_ADDRESS']}/role/' + args.role + '/activate', headers={'Authorization': args.session['token']})
req.raise_for_status() req.raise_for_status()
@ -55,5 +59,8 @@ def reactivateRole(args):
logger.error("Failed to obtain response from server.") logger.error("Failed to obtain response from server.")
sys.exit(-1) sys.exit(-1)
logger.info("Role %s has been reactivated.", args.role)
sys.exit(0)
if __name__ == '__main__': if __name__ == '__main__':
reactivateRole(sys.argv[1:]) reactivateRole(sys.argv[1:])

View File

@ -5,6 +5,7 @@ import logging
import requests import requests
import json import json
import argparse import argparse
import subprocess
from subject import main from subject import main
@ -35,9 +36,6 @@ def suspendRole(args):
logger.error("Need session file and role.") logger.error("Need session file and role.")
sys.exit(1) sys.exit(1)
#Validate role name
#TODO
# Check for session file # Check for session file
if not os.path.isfile(BASE_DIR + args.session): if not os.path.isfile(BASE_DIR + args.session):
logger.error("File '" + args.session + "' not found.") logger.error("File '" + args.session + "' not found.")
@ -47,7 +45,13 @@ def suspendRole(args):
with open(BASE_DIR + args.session, 'r') as f: with open(BASE_DIR + args.session, 'r') as f:
args.session = json.load(f) args.session = json.load(f)
# TODO: #Validate role name
process = subprocess.Popen(f"./rep_list_roles {args.role}", shell=True)
process.wait()
if process.returncode != 0:
logger.error("Role does not exist.")
sys.exit(1)
try: try:
req = requests.post(f'http://{state['REP_ADDRESS']}/role/' + args.role + '/suspend', headers={'Authorization': args.session['token']}) req = requests.post(f'http://{state['REP_ADDRESS']}/role/' + args.role + '/suspend', headers={'Authorization': args.session['token']})
req.raise_for_status() req.raise_for_status()
@ -55,5 +59,8 @@ def suspendRole(args):
logger.error("Failed to obtain response from server.") logger.error("Failed to obtain response from server.")
sys.exit(-1) sys.exit(-1)
logger.info("Role %s has been suspended.", args.role)
sys.exit(0)
if __name__ == '__main__': if __name__ == '__main__':
suspendRole(sys.argv[1:]) suspendRole(sys.argv[1:])