Skip to content

Commit

Permalink
feat: inital pass at env vars for creds in import
Browse files Browse the repository at this point in the history
  • Loading branch information
gormaniac committed Oct 4, 2023
1 parent b6a1afe commit f3ea8ca
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/stormlibpp/import.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
"""Recursively execute all Storm scripts in a given folder on a given Synapse Cortex over HTTP."""
"""Recursively execute all Storm scripts in a given folder on a given Synapse Cortex over HTTP.
If using the --http argument, a password will be prompted for. The CORTEX_PASS environment
variable may be used to avoid the prompt.
"""


import argparse
Expand All @@ -22,6 +26,7 @@
from .httpcore import HttpCortex
from .output import handle_msg, log_storm_msg, OUTP
from .stormcli import start_storm_cli
from .utils import get_cortex_creds


def endswith(text, items):
Expand Down Expand Up @@ -218,10 +223,14 @@ def get_args(argv: list[str]):
)
parser.add_argument(
"--user",
# FIXME - This explanation is backwards - --user overrides CORTEX_USER
help=(
"The Synapse user to authenticate with -"
" by default the return of getpass.getuser()"
"The Cortex user to authenticate with - by default the return of"
" getpass.getuser(). May override this with the CORTEX_USER env var."
" If neither --user or CORTEX_USER are passed, a prompt will appear"
" to either input a value or accept the default. Only works with --http."
),
# FIXME - I don't think we actually want this default
default=getpass.getuser(),
)
parser.add_argument(
Expand Down Expand Up @@ -274,10 +283,9 @@ async def main(argv: list[str]):
return "Can't use both --cortex and --local!"
elif args.cortex:
if args.http:
synuser = args.user
synpass = getpass.getpass()
user, passwd = get_cortex_creds(args.user)
core_obj = functools.partial(
HttpCortex, args.cortex, synuser, synpass, ssl_verify=not args.no_verify
HttpCortex, args.cortex, user, passwd, ssl_verify=not args.no_verify
)
else:
core_obj = functools.partial(cortex_proxy_contextmanager, args.cortex)
Expand Down
19 changes: 19 additions & 0 deletions src/stormlibpp/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Miscellaneous functions that are helpful for working with Storm/Synapse."""


import getpass
import os

import synapse.exc as s_exc
Expand Down Expand Up @@ -62,3 +63,21 @@ def normver(ver: str | tuple) -> tuple[str, tuple]:
raise TypeError("Can only use a str or tuple as a Storm pkg version")

return (verstr, vertup)


def get_cortex_creds(_user):
if _user:
username = _user
elif (envusr := os.environ.get("CORTEX_USER")):
username = envusr
else:
gpusr = getpass.getuser()
inusr = input(f"Username [{gpusr}]: ")
username = inusr if inusr else gpusr

if (envpw := os.environ.get("CORTEX_PASS")):
password = envpw
else:
password = getpass.getpass()

return username, password

0 comments on commit f3ea8ca

Please sign in to comment.