Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Master API key #290

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions abm/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
import sys

import yaml

sys.path.append(os.path.dirname(os.path.realpath(__file__)))


Expand Down
17 changes: 17 additions & 0 deletions abm/lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,20 @@ class Keys:
COLLECTION = 'collection'
HISTORY_BASE_NAME = 'output_history_base_name'
HISTORY_NAME = 'history_name'


# def get_master_api_key():
# '''
# Get the master API key from the environment or configuration file.
# '''
# if 'GALAXY_MASTER_API_KEY' in os.environ:
# return os.environ['GALAXY_MASTER_API_KEY']
# config_path = os.path.expanduser("~/.abm/config.yml")
# if not os.path.exists(config_path):
# raise RuntimeError(f"ERROR: Configuration file not found: {config_path}")
# with open(config_path, 'r') as f:
# config = yaml.safe_load(f)
# key = config.get('GALAXY_MASTER_API_KEY', None)
# if key == None:
# raise RuntimeError("ERROR: GALAXY_MASTER_API_KEY not found in config.yml")
# return key
38 changes: 28 additions & 10 deletions abm/lib/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,26 @@ def __init__(self, *args):
if len(args) == 1:
arg = args[0]
if type(arg) == str:
self.GALAXY_SERVER, self.API_KEY, self.KUBECONFIG = parse_profile(arg)
self.GALAXY_SERVER, self.API_KEY, self.KUBECONFIG, self.MASTER_KEY = parse_profile(arg)
elif type(arg) == dict:
self.GALAXY_SERVER = arg['GALAXY_SERVER']
self.API_KEY = arg['API_KEY']
self.KUBECONFIG = arg['KUBECONFIG']
if 'MASTER_KEY' in arg:
self.MASTER_KEY = arg['MASTER_KEY']
else:
self.MASTER_KEY = None
else:
raise Exception(f'Invalid arg for Context: {type(arg)}')
elif len(args) == 3:
elif len(args) == 3 or len(args) == 4:
self.GALAXY_SERVER = args[0]
self.API_KEY = args[1]
self.KUBECONFIG = args[2]
if len(args) == 4:
self.MASTER_KEY = args[3]
else:
raise Exception(
f'Invalid args for Context. Expected one or three, found {len(args)}'
f'Invalid args for Context. Expected one or four, found {len(args)}'
)


Expand All @@ -86,7 +92,7 @@ def print_yaml(obj):
get_yaml_parser().dump(obj, sys.stdout)


def connect(context: Context):
def connect(context: Context, use_master_key=False):
"""
Create a connection to the Galaxy instance

Expand All @@ -100,7 +106,14 @@ def connect(context: Context):
print('ERROR: The Galaxy API key has not been set. Please check your')
print(' configuration in ~/.abm/profile.yml and try again.')
sys.exit(1)
gi = bioblend.galaxy.GalaxyInstance(url=context.GALAXY_SERVER, key=context.API_KEY)
key = context.API_KEY
if use_master_key:
if context.MASTER_KEY is None:
print('ERROR: The Galaxy master key has not been set. Please check your')
print(' configuration in ~/.abm/profile.yml and try again.')
sys.exit(1)
key = context.MASTER_KEY
gi = bioblend.galaxy.GalaxyInstance(url=context.GALAXY_SERVER, key=key)
gi.max_get_attempts = 3
gi.get_retry_delay = 1
return gi
Expand All @@ -113,7 +126,7 @@ def _set_active_profile(profile_name: str):
:param profile_name:
:return:
"""
lib.GALAXY_SERVER, lib.API_KEY, lib.KUBECONFIG = parse_profile(profile_name)
lib.GALAXY_SERVER, lib.API_KEY, lib.KUBECONFIG, lib.MASTER_KEY = parse_profile(profile_name)
return lib.GALAXY_SERVER != None


Expand Down Expand Up @@ -174,10 +187,11 @@ def parse_profile(profile_name: str):
:param profile_name: path to the profile to parse
:return: a tuple containing the Galaxy URL, API key, and path to the kubeconfig
'''
nones = (None, None, None, None)
profiles = load_profiles()
if profiles is None:
print(f'ERROR: Could not locate an abm profile file in {PROFILE_SEARCH_PATH}')
return None, None, None
return nones
if profile_name not in profiles:
print(f'ERROR: {profile_name} is not the name of a valid profile.')
keys = list(profiles.keys())
Expand All @@ -186,11 +200,15 @@ def parse_profile(profile_name: str):
', '.join([f"'{k}'" for k in keys[0:-2]]) + f", and '{keys[-1]}'"
)
print(f'The defined profile names are: {quoted_keys}')
return None, None, None
return nones
profile = profiles[profile_name]
kube = None
master = 'galaxypassword'
if 'kube' in profile:
return (profile['url'], profile['key'], os.path.expanduser(profile['kube']))
return (profile['url'], profile['key'], None)
kube = os.path.expanduser(profile['kube'])
if 'master' in profile:
master = profile['master']
return (profile['url'], profile['key'], kube, master)


def run(command, env: dict = None):
Expand Down
2 changes: 1 addition & 1 deletion abm/lib/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def error_message(msg='Invalid command'):
return
url = datasets[args[0]]
elif len(args) == 3:
server, key = parse_profile(args[0])
server, key, kube, master = parse_profile(args[0])
if server is None:
error_message(f"Invalid server profile name: {args[0]}")
return
Expand Down
16 changes: 4 additions & 12 deletions abm/lib/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@


def do_list(context: Context, args: list):
# TODO the master API key needs to be parameterized or specified in a config file.
context.API_KEY = "galaxypassword"
gi = connect(context)
gi = connect(context, use_master_key=True)
user_list = gi.users.get_users()
pprint(user_list)

Expand All @@ -22,9 +20,7 @@ def get_api_key(context: Context, args: list):
print("ERROR: no user email given")
return

# TODO the master API key needs to be parameterized or specified in a config file.
context.API_KEY = "galaxypassword"
gi = connect(context)
gi = connect(context, use_master_key=True)
user_list = gi.users.get_users(f_email=args[0])
if user_list is None or len(user_list) == 0:
print("WARNING: no such user")
Expand Down Expand Up @@ -52,9 +48,7 @@ def create(context: Context, args: list):
print(f"ERROR: {email} does not look like a valid email address")
return

# TODO the master API key needs to be parameterized or specified in a config file.
context.API_KEY = "galaxypassword"
gi = connect(context)
gi = connect(context, use_master_key=True)
user_record = gi.users.create_local_user(name, email, password)
id = user_record['id']
key = gi.users.create_user_apikey(id)
Expand All @@ -67,9 +61,7 @@ def show(context: Context, args: list):
print("ERROR: no user email given")
return

# TODO the master API key needs to be parameterized or specified in a config file.
context.API_KEY = "galaxypassword"
gi = connect(context)
gi = connect(context, use_master_key=True)
id = _get_user_id(gi, args[0])
if id is None:
return
Expand Down