Skip to content

Commit

Permalink
Add features
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaSBrown committed Nov 26, 2023
1 parent c43adbf commit 3e08ab6
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 65 deletions.
103 changes: 66 additions & 37 deletions python/datafed_pkg/datafed/CLI.py
Original file line number Diff line number Diff line change
Expand Up @@ -2140,55 +2140,84 @@ def _epDefaultSet(current, endpoint):


@_cli.command(name="setup")
@click.option("-r", "--reset-all", is_flag=True, help="Reset the configuration
file (WARNING this will remove the existing .ini file), redownload the
DataFed server public key and recreate the users pubic and private key
pair.")
@click.option("-a", "--reset-all-credentials", is_flag=True, help="Recreate the
users pubic and private keys, and redownload and use the DataFed servers
public key.")
@click.option("-u", "--reset-user-credentials", is_flag=True, help="Recreate the
users pubic and private keys only.")
@click.option("-m", "--reset-server-credentials", is_flag=True, help="Redownload and use the DataFed servers
public key.")
@click.option("-f", "--reset-config-file", is_flag=True, help="Reset the
configuration file with defaults.")
@click.option(
"-r",
"--reset-all",
is_flag=True,
help="Reset the configuration"
" file (WARNING this will remove the existing .ini file), redownload "
"the DataFed server public key and recreate the users pubic and private"
"key pair.",
)
@click.option(
"-a",
"--reset-all-credentials",
is_flag=True,
help="Recreate the"
" users pubic and private keys, and redownload and use the DataFed "
"servers public key.",
)
@click.option(
"-u",
"--reset-user-credentials",
is_flag=True,
help="Recreate " "the users pubic and private keys only.",
)
@click.option(
"-m",
"--reset-server-credentials",
is_flag=True,
help=("Redownload and use the DataFed servers public key."),
)
@click.option(
"-f",
"--reset-config-file",
is_flag=True,
help="Reset the " "configuration file with defaults.",
)
@click.option("-s", "--show", is_flag=True, help="Show all configuration options.")
@click.option("-c", "--show-config-file", is_flag=True, help="Show contents of
config file.")
@click.option(
"-c", "--show-config-file", is_flag=True, help="Show contents of " "config file."
)
@click.pass_context
def _setup(ctx):
def _setup(
ctx,
reset_all,
reset_all_credentials,
reset_user_credentials,
reset_server_credentials,
reset_config_file,
show,
show_config_file,
):
"""
Setup local credentials. This command installs DataFed credentials for the
current user in the configured client configuration directory. Subsequent
use of the DataFed CLI will read these credentials instead of requiring
manual authentication.
"""

cfg_dir = _capi.cfg.get("client_cfg_dir")
pub_file = _capi.cfg.get("client_pub_key_file")
priv_file = _capi.cfg.get("client_priv_key_file")

if cfg_dir is None and (pub_file is None or priv_file is None):
raise Exception(
"Client configuration directory and/or client key files not configured"
)

reply = _capi.generateCredentials()

if pub_file is None:
pub_file = os.path.join(cfg_dir, "datafed-user-key.pub")
if cfg_dir is None:
raise Exception("Client configuration directory is not configured")

keyf = open(pub_file, "w")
keyf.write(reply[0].pub_key)
keyf.close()
if reset_all:
reset_all_credentials = True
reset_user_credentials = True
reset_server_credentials = True
reset_config_file = True
elif reset_all_credentials:
reset_user_credentials = True
reset_server_credentials = True

if priv_file is None:
priv_file = os.path.join(cfg_dir, "datafed-user-key.priv")
_capi.setupClientConfigFile(reset_config_file)
_capi.setupCredentials(reset_user_credentials)
_capi.setupServerCredentials(reset_server_credentials)

keyf = open(priv_file, "w")
keyf.write(reply[0].priv_key)
keyf.close()
if show:
_capi.cfg.printSettingInfo()
elif show_config_file:
_capi.cfg.printSettingInfo(priority=2)
_capi.cfg.printSettingInfo(priority=3)

if _output_mode_sticky != _OM_RETN:
_print_ack_reply()
Expand Down
47 changes: 30 additions & 17 deletions python/datafed_pkg/datafed/CommandLib.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ class methods and replies are (currently) returned as Google Protobuf message ob
)

def __init__(self, opts={}):
self._initialize(opts)

def _initialize(self, opts={}):
if not isinstance(opts, dict):
raise Exception("CommandLib API options parameter must be a dictionary.")

Expand Down Expand Up @@ -2305,9 +2308,7 @@ def setupCredentials(self, overwrite=True):
"Client configuration directory and/or client key files not configured"
)

msg = auth.GenerateCredentialsRequest()

reply = self._mapi.sendRecv(msg)
reply = self.generateCredentials()

if pub_file is None:
pub_file = os.path.join(cfg_dir, "datafed-user-key.pub")
Expand All @@ -2325,8 +2326,6 @@ def setupCredentials(self, overwrite=True):
keyf.write(reply[0].priv_key)
keyf.close()



def setupServerCredentials(self, overwrite=True):
"""
Download server public key
Expand All @@ -2338,7 +2337,6 @@ def setupServerCredentials(self, overwrite=True):
if overwrite:
write = True


serv_key_file = self.cfg.get("server_pub_key_file")
if serv_key_file is None:
raise Exception(
Expand All @@ -2364,11 +2362,26 @@ def setupServerCredentials(self, overwrite=True):
url = "https://" + host + "/datafed-core-key.pub"
wget.download(url, out=serv_key_file)


def setupAllCredentials(self, overwrite=True):
self.setupServerCredentials(overwrite)
self.setupCredentials(overwrite)

def setupClientConfigFile(self, opts={}, overwrite=True):
write = False
if overwrite:
write = True

cfg_file = self.cfg.get("client_cfg_file")
if cfg_file is None:
write = True
elif not os.path.exists(cfg_file):
write = True
else:
if overwrite:
os.remove(cfg_file)

if write:
self._initialize(opts)

def setContext(self, item_id=None):
"""
Expand Down Expand Up @@ -2761,18 +2774,18 @@ def _setSaneDefaultOptions(self):
opts["server_pub_key_file"] = serv_key_file
save = True

# Will not overwrite the server core public key if it already
# Will not overwrite the server core public key if it already
# exists
self.setupServerCredentials(overwrite=False)
# if not serv_key_file:
# raise Exception(
# "Could not find location of server public key file."
# )
#
# if not os.path.exists(serv_key_file):
# # Make default server pub key file
# url = "https://" + opts["server_host"] + "/datafed-core-key.pub"
# wget.download(url, out=serv_key_file)
# if not serv_key_file:
# raise Exception(
# "Could not find location of server public key file."
# )
#
# if not os.path.exists(serv_key_file):
# # Make default server pub key file
# url = "https://" + opts["server_host"] + "/datafed-core-key.pub"
# wget.download(url, out=serv_key_file)

if "client_pub_key_file" not in opts or "client_priv_key_file" not in opts:
if "client_cfg_dir" not in opts:
Expand Down
34 changes: 23 additions & 11 deletions python/datafed_pkg/datafed/Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,20 +360,32 @@ def _loadConfigFile(self, cfg_file, priority):
#
# Prints all set settings with key, value, and source information
#
def printSettingInfo(self):
def printSettingInfo(self, priority=None):
if priority:
if priority < 1 or priority > 5:
raise Exception(
"Unsupported priority encountered cannot print "
"settings for the provided priority."
)

p = 0
for k, v in self._opts.items():
p = v["pri"]
if p == 5:
print(' {} = "{}" (assumed)'.format(k, v["val"]))
elif p == 4:
print(' {} = "{}" from {}'.format(k, v["val"], _opt_info[k][2]))
elif p == 3:
print(' {} = "{}" from server config file'.format(k, v["val"]))
elif p == 2:
print(' {} = "{}" from client config file'.format(k, v["val"]))
elif p == 1:
print(' {} = "{}" from CLI option'.format(k, v["val"]))
if priority is None:
# Print everything
if p == 5:
print(' {} = "{}" (assumed)'.format(k, v["val"]))
elif p == 4:
print(' {} = "{}" from {}'.format(k, v["val"], _opt_info[k][2]))
elif p == 3:
print(' {} = "{}" from server config file'.format(k, v["val"]))
elif p == 2:
print(' {} = "{}" from client config file'.format(k, v["val"]))
elif p == 1:
print(' {} = "{}" from CLI option'.format(k, v["val"]))
else:
if p == priority:
print(' {} = "{}" '.format(k, v["val"]))

##
# @brief Get dictionary of all set configuration options.
Expand Down

0 comments on commit 3e08ab6

Please sign in to comment.