Skip to content

Commit

Permalink
Add env var as credential input in cli set default none
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Lau (AvengerMoJo) <[email protected]>
  • Loading branch information
Alex Lau (AvengerMoJo) committed Dec 11, 2023
1 parent 498a392 commit 04cf3f5
Show file tree
Hide file tree
Showing 2 changed files with 25 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
21 changes: 16 additions & 5 deletions osc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -1999,10 +1999,14 @@ 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)
user = os.environ.get("OSC_USERNAME", None)
if not user:
user_prompt = f"Username [{apiurl_no_scheme}]: "
user = raw_input(user_prompt)
passwd = os.environ.get("OSC_PASSWORD", None)
if not passwd:
pass_prompt = f"Password [{user}@{apiurl_no_scheme}]: "
passwd = getpass.getpass(pass_prompt)

Check warning on line 2009 in osc/conf.py

View check run for this annotation

Codecov / codecov/patch

osc/conf.py#L2002-L2009

Added lines #L2002 - L2009 were not covered by tests
creds_mgr_descr = select_credentials_manager_descr()
if initial:
config = {'user': user, 'pass': passwd}
Expand All @@ -2020,9 +2024,13 @@ 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 = os.environ.get("OSC_CREDENTIAL", None)
cred_pick = None

Check warning on line 2028 in osc/conf.py

View check run for this annotation

Codecov / codecov/patch

osc/conf.py#L2027-L2028

Added lines #L2027 - L2028 were not covered by tests
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)

Check warning on line 2033 in osc/conf.py

View check run for this annotation

Codecov / codecov/patch

osc/conf.py#L2032-L2033

Added lines #L2032 - L2033 were not covered by tests

from .core import build_table
headline = ('NUM', 'NAME', 'DESCRIPTION')
Expand All @@ -2031,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

Check warning on line 2043 in osc/conf.py

View check run for this annotation

Codecov / codecov/patch

osc/conf.py#L2042-L2043

Added lines #L2042 - L2043 were not covered by tests
else:
i = raw_input('Select credentials manager [default=1]: ')

Check warning on line 2045 in osc/conf.py

View check run for this annotation

Codecov / codecov/patch

osc/conf.py#L2045

Added line #L2045 was not covered by tests
if not i:
i = "1"
if not i.isdigit():
Expand Down

0 comments on commit 04cf3f5

Please sign in to comment.