addRole and assumeRole

This commit is contained in:
JoaoBastos023 2024-12-17 10:41:47 +00:00
parent d843c3560b
commit 78dce9c8dc
3 changed files with 22 additions and 7 deletions

View File

@ -35,9 +35,6 @@ def addRole(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 +44,6 @@ def addRole(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/create/', req = requests.post(f'http://{state['REP_ADDRESS']}/role/create/',
json=json.dumps({'role' : args.role}), json=json.dumps({'role' : args.role}),
@ -57,5 +53,8 @@ def addRole(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 added.")
sys.exit(0)
if __name__ == '__main__': if __name__ == '__main__':
addRole(sys.argv[1:]) addRole(sys.argv[1:])

View File

@ -63,6 +63,7 @@ def addSubject(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('Subject added.')
sys.exit(0) sys.exit(0)
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -34,9 +34,6 @@ def assumeRole(args):
logger.error("Need session file and username.") logger.error("Need session file and username.")
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.")
@ -46,6 +43,21 @@ def assumeRole(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)
# Get roles in session
try:
req = requests.get(f'http://{state['REP_ADDRESS']}/role/session/list', headers={'Authorization': args.session['token']})
req.raise_for_status()
except requests.exceptions.RequestException as errex:
logger.error("Failed to obtain response from server.")
sys.exit(-1)
# Validate role name
roles = req.json()
if args.role not in roles.items():
logger.error("Role does not exist.")
sys.exit(1)
# TODO: # TODO:
try: try:
req = requests.post(f'http://{state['REP_ADDRESS']}/role/session/assume/' + args.role, headers={'Authorization': args.session['token']}) req = requests.post(f'http://{state['REP_ADDRESS']}/role/session/assume/' + args.role, headers={'Authorization': args.session['token']})
@ -54,5 +66,8 @@ def assumeRole(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("You assumed the role %s.", args.role)
sys.exit(0)
if __name__ == '__main__': if __name__ == '__main__':
assumeRole(sys.argv[1:]) assumeRole(sys.argv[1:])