Skip to content

Commit

Permalink
Use keyring for password storage
Browse files Browse the repository at this point in the history
Closes #56
  • Loading branch information
adammcmaster committed Oct 18, 2019
1 parent 8946258 commit bcbe859
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 16 deletions.
3 changes: 1 addition & 2 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
FROM python:3-alpine
FROM python:3

WORKDIR /usr/src/panoptes-cli

RUN apk --no-cache add git
RUN pip install git+git://github.com/zooniverse/panoptes-python-client.git

COPY . .
Expand Down
3 changes: 1 addition & 2 deletions Dockerfile.dev2
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
FROM python:2.7-alpine
FROM python:2.7

WORKDIR /usr/src/panoptes-cli

RUN apk --no-cache add git
RUN pip install git+git://github.com/zooniverse/panoptes-python-client.git

COPY . .
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.stable
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3-alpine
FROM python:3

RUN pip install panoptescli

Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.stable2
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:2.7-alpine
FROM python:2.7

RUN pip install panoptescli

Expand Down
35 changes: 29 additions & 6 deletions panoptes_cli/commands/configure.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import click
import os

import click
import keyring
import yaml

from panoptes_cli.scripts.panoptes import cli
Expand All @@ -25,19 +27,40 @@ def configure(ctx, edit_all):
if opt == 'endpoint' and not edit_all:
continue

is_password = opt == 'password'
ctx.parent.config[opt] = click.prompt(
opt,
default=value,
hide_input=is_password,
show_default=not is_password,
)

if not ctx.parent.config['endpoint'].startswith('https://'):
click.echo(
'Error: Invalid endpoint supplied. Endpoint must be an HTTPS URL.'
)
return -1

new_password = click.prompt(
'Password [leave blank for no change]',
hide_input=True,
show_default=False,
default='',
)
if new_password:
try:
keyring.set_password(
'panoptes',
ctx.parent.config['username'],
new_password,
)
except RuntimeError:
click.echo(
'Warning: Could not save your password to the keyring. '
'You will be asked for your password each time.',
err=True,
)

save_config(ctx.parent.config_file, ctx.parent.config)


with open(ctx.parent.config_file, 'w') as conf_f:
yaml.dump(ctx.parent.config, conf_f, default_flow_style=False)
def save_config(config_file, config):
with open(config_file, 'w') as conf_f:
yaml.dump(config, conf_f, default_flow_style=False)
2 changes: 1 addition & 1 deletion panoptes_cli/commands/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,4 @@ def token():
"""

click.echo("Token: {}".format(Panoptes.client().get_bearer_token()))
click.echo("Expiry time: {}".format(Panoptes.client().bearer_expires))
click.echo("Expiry time: {}".format(Panoptes.client().bearer_expires))
42 changes: 39 additions & 3 deletions panoptes_cli/scripts/panoptes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import click
import os

import click
import keyring
import yaml

from panoptes_client import Panoptes


Expand Down Expand Up @@ -28,7 +31,6 @@ def cli(ctx, endpoint, admin):
ctx.config = {
'endpoint': 'https://www.zooniverse.org',
'username': '',
'password': '',
}

try:
Expand All @@ -41,10 +43,44 @@ def cli(ctx, endpoint, admin):
ctx.config['endpoint'] = endpoint

if ctx.invoked_subcommand != 'configure':
try:
password = keyring.get_password('panoptes', ctx.config['username'])
except RuntimeError:
password = None

if 'password' in ctx.config:
if not password:
try:
password = ctx.config['password']
keyring.set_password(
'panoptes',
ctx.config['username'],
password,
)
retrieved_password = keyring.get_password(
'panoptes',
ctx.config['username'],
)

del ctx.config['password']
save_config(ctx.config_file, ctx.config)
except RuntimeError:
click.echo(
'Warning: Your password is stored insecurely and '
'secure keyrings are not supported on your system.',
err=True,
)

if not password:
password = click.prompt(
'Password for {}'.format(ctx.config['username']),
hide_input=True,
)

Panoptes.connect(
endpoint=ctx.config['endpoint'],
username=ctx.config['username'],
password=ctx.config['password'],
password=password,
admin=admin,
)

Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
'panoptes-client>=1.0,<2.0',
'humanize>=0.5.1,<0.6',
'pathvalidate>=0.29.0,<0.30',
'keyring>=19.2,<19.3',
],
entry_points='''
[console_scripts]
Expand Down

0 comments on commit bcbe859

Please sign in to comment.