Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Prompt for username/password when not in config file. #4

Merged
merged 2 commits into from Jul 12, 2013
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 12 additions & 7 deletions README.rst
Expand Up @@ -19,13 +19,18 @@ An optional package (though highly recommend and included in
it, remove it from ``requirements.txt`` and store your ldap password in it, remove it from ``requirements.txt`` and store your ldap password in
plaintext. plaintext.


``invtool`` requires you to store your ldap username in a clear text ``invtool`` supports a few different ways of setting your credentials. Your
configuration file. Your ldap password can be stored either in plaintext in the username can either be stored in the configuration file or passed at the
same file or else in the system keyring. In order to use the keyring you need command line. Your password may be stored in the configuration file (in
`python-keyring <https://pypi.python.org/pypi/keyring>`_. ``invtool`` tries to plain text), stored in a `python-keyring <https://pypi.python.org/pypi/keyring>`_,
find a configuration file by first looking at ``./etc/invtool.conf`` and if it or passed on the command line. If you do not want to store your credentials
can't find anything there, ``/etc/invtool.conf``. Here is a quick tip for a on disk at all, you must remove the ldap_username AND ldap_password entries
non-root install:: from the configuration file.

``invtool`` will look for a configuration file at ``./etc/invtool.conf`` and
if it can't find anything there, ``/etc/invtool.conf``.

Here is a quick tip for a non-root install::


git clone git@github.com:uberj/inv-tool.git invtool git clone git@github.com:uberj/inv-tool.git invtool
cd invtool cd invtool
Expand Down
31 changes: 18 additions & 13 deletions invtool/lib/config.py
Expand Up @@ -39,15 +39,18 @@
except ImportError: except ImportError:
KEYRING_PRESENT = False KEYRING_PRESENT = False


# No auth required for dev
if dev == 'True': if dev == 'True':
auth = None auth = None
AUTH_TYPE = None AUTH_TYPE = None
# Can't use keyring and a password in the config at the same time.
elif (config.has_option('authorization', 'ldap_password') and elif (config.has_option('authorization', 'ldap_password') and
config.has_option('authorization', 'keyring')): config.has_option('authorization', 'keyring')):
raise Exception( raise Exception(
"ldap_password and keyring are mutually exclusive " "ldap_password and keyring are mutually exclusive "
"in config file '{0}'".format(CONFIG_FILE) "in config file '{0}'".format(CONFIG_FILE)
) )
# If there's an existing keyring, let's use it!
elif (config.has_option('authorization', 'ldap_username') and elif (config.has_option('authorization', 'ldap_username') and
config.get('authorization', 'ldap_username') != '' and config.get('authorization', 'ldap_username') != '' and
config.has_option('authorization', 'keyring') and config.has_option('authorization', 'keyring') and
Expand Down Expand Up @@ -77,14 +80,8 @@
print("Saved password to keyring") print("Saved password to keyring")
auth = tuple(auth) auth = tuple(auth)
AUTH_TYPE = 'keyring' AUTH_TYPE = 'keyring'
elif (config.has_option('authorization', 'ldap_username') and # If there's no existing keyring and we have keyring support
config.has_option('authorization', 'ldap_password')): # let's try to be nice and create a keyring.
# use plaintext
auth = (
config.get('authorization', 'ldap_username'),
config.get('authorization', 'ldap_password')
)
AUTH_TYPE = 'plaintext'
elif KEYRING_PRESENT: elif KEYRING_PRESENT:
# configure credentials # configure credentials
auth = ( auth = (
Expand All @@ -103,9 +100,17 @@
keyring.set_password(config.get('authorization', 'keyring'), *auth) keyring.set_password(config.get('authorization', 'keyring'), *auth)
print("Saved password to keyring") print("Saved password to keyring")
AUTH_TYPE = 'keyring' AUTH_TYPE = 'keyring'
# If there's no keyring support, let's try to get the username and password
# from the config or command line
else: else:
raise Exception( if config.has_option('authorization', 'ldap_username'):
"Unable to get or set ldap password." username = config.get('authorization', 'ldap_username')
"Install the keyring module or set ldap_password" else:
"in config file '{0}'".format(CONFIG_FILE) username = raw_input('ldap username: ')
) if config.has_option('authorization', 'ldap_password'):
password = config.get('authorization', 'ldap_password')
else:
password = getpass.getpass('ldap password: ')
# use plaintext
auth = (username, password)
AUTH_TYPE = 'plaintext'