Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Use keyring for password storage #135

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blank line contains whitespace

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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blank line at end of file

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(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

local variable 'retrieved_password' is assigned to but never used

'panoptes',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trailing whitespace

ctx.config['username'],
)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To do: Compare password and retrieved_password here to verify keyring is working.


del ctx.config['password']
save_config(ctx.config_file, ctx.config)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'save_config' may be undefined, or defined from star imports: panoptes_cli.commands.configure, panoptes_cli.commands.info, panoptes_cli.commands.project, panoptes_cli.commands.subject, panoptes_cli.commands.subject_set, panoptes_cli.commands.user, panoptes_cli.commands.workflow

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