Skip to content

Commit

Permalink
Add env var as credential input in cli
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Lau (AvengerMoJo) <alau@suse.com>
  • Loading branch information
Alex Lau (AvengerMoJo) committed Dec 4, 2023
1 parent 00331e5 commit d1c4eee
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ which is easier if you develop on osc.
When you use osc for the first time, it will ask you for your username and
password, and store it in `~/.config/osc/oscrc`.

Alternatively, you can set the environment variable to bypass the interactive input
session with the OSC_USERNAME as the username, OSC_PASSWORD as the password and
OSC_CREDENTIAL as ['Kernel keyring', 'Secret Service', 'Transient', 'Obfuscated',
'Config'] the credentials configuration like the following:

OSC_USERNAME ='username'
OSC_PASSWORD ='password'
OSC_CREDENTIAL = 'Kernel keyring'


## Keyrings

Expand Down
25 changes: 20 additions & 5 deletions osc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -1995,10 +1995,16 @@ def interactive_config_setup(conffile, apiurl, initial=True):
print()

apiurl_no_scheme = urlsplit(apiurl)[1] or apiurl
user_prompt = f"Username [{apiurl_no_scheme}]: "
user = raw_input(user_prompt)
pass_prompt = f"Password [{user}@{apiurl_no_scheme}]: "
passwd = getpass.getpass(pass_prompt)
if os.environ.get("OSC_USERNAME"):
user = os.environ.get("OSC_USERNAME")
else:
user_prompt = f"Username [{apiurl_no_scheme}]: "
user = raw_input(user_prompt)
if os.environ.get("OSC_PASSWORD"):
passwd = os.environ.get("OSC_PASSWORD")
else:
pass_prompt = f"Password [{user}@{apiurl_no_scheme}]: "
passwd = getpass.getpass(pass_prompt)
creds_mgr_descr = select_credentials_manager_descr()
if initial:
config = {'user': user, 'pass': passwd}
Expand All @@ -2016,9 +2022,15 @@ def select_credentials_manager_descr():
print('To use keyrings please install python%d-keyring.' % sys.version_info.major)
creds_mgr_descriptors = credentials.get_credentials_manager_descriptors()

cred = None
cred_pick = None
if os.environ.get("OSC_CREDENTIAL"):
cred = os.environ.get("OSC_CREDENTIAL")
rows = []
for i, creds_mgr_descr in enumerate(creds_mgr_descriptors, 1):
rows += [str(i), creds_mgr_descr.name(), creds_mgr_descr.description()]
if creds_mgr_descr.name() == cred:
cred_pick = str(i)

from .core import build_table
headline = ('NUM', 'NAME', 'DESCRIPTION')
Expand All @@ -2027,7 +2039,10 @@ def select_credentials_manager_descr():
for row in table:
print(row)

i = raw_input('Select credentials manager [default=1]: ')
if cred_pick:
i = cred_pick
else:
i = raw_input('Select credentials manager [default=1]: ')
if not i:
i = "1"
if not i.isdigit():
Expand Down

0 comments on commit d1c4eee

Please sign in to comment.