Skip to content

Commit

Permalink
Added easy-vault check-keyring command
Browse files Browse the repository at this point in the history
Signed-off-by: Andreas Maier <andreas.r.maier@gmx.de>
  • Loading branch information
andy-maier committed Apr 1, 2021
1 parent 40b746a commit 0fce3cb
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 9 deletions.
3 changes: 3 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ Released: not yet
'check_available() that return whether the keyring service is available
or check that it is available. (issue #34)

* Added a new 'easy-vault check-keyring' command that checks whether the
keyring service is available. (issue #36)

* Test: Improved test coverage. (issue #8)

**Cleanup:**
Expand Down
1 change: 1 addition & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ This command displays self-explanatory help, e.g.:
$ easy-vault --help
$ easy-vault encrypt --help
$ easy-vault decrypt --help
$ easy-vault check-keyring --help
.. _`Accessing the secrets in a program`:
Expand Down
3 changes: 3 additions & 0 deletions easy_vault/_key_ring_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
from __future__ import absolute_import, print_function
import os
import keyring
import keyring.backends.chainer # Required to import separately
import keyring.backends.null # Required to import separately
import keyring.backends.fail

__all__ = ['KeyRingLib', 'KeyRingException', 'KeyRingNotAvailable',
'KeyRingError']
Expand Down
2 changes: 1 addition & 1 deletion easy_vault/cli/_common_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

quiet_option = [ # pylint: disable=invalid-name
click.option('-q', '--quiet', is_flag=True, default=False,
help=u'Print no messages (except for password prompt).')]
help=u'Print no messages.')]


def add_options(options):
Expand Down
35 changes: 30 additions & 5 deletions easy_vault/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

from ._common_options import add_options, help_option, quiet_option
from .._version import __version__ as cli_version
from .._key_ring_lib import KeyRingLib
from .._key_ring_lib import KeyRingLib, KeyRingNotAvailable
from .._easy_vault import EasyVault, EasyVaultException
from .._password import get_password, set_password

Expand All @@ -37,6 +37,9 @@
def cli(ctx):
"""
The easy-vault command is used to encrypt and decrypt vault files.
The passwords for the vault files are stored in the keyring service
of the local system.
"""
pass
# Command will be executed automatically
Expand All @@ -52,8 +55,7 @@ def cli(ctx):
u'Mutually exclusive with --set-password')
@add_options(quiet_option)
@add_options(help_option)
@click.pass_obj
def cli_encrypt(context, vaultfile, **options):
def cli_encrypt(vaultfile, **options):
"""
Encrypt a vault file, if not yet encrypted.
Expand Down Expand Up @@ -120,8 +122,7 @@ def cli_encrypt(context, vaultfile, **options):
u'Mutually exclusive with --set-password')
@add_options(help_option)
@add_options(quiet_option)
@click.pass_obj
def cli_decrypt(context, vaultfile, **options):
def cli_decrypt(vaultfile, **options):
"""
Decrypt a vault file, if encrypted.
Expand Down Expand Up @@ -178,6 +179,30 @@ def cli_decrypt(context, vaultfile, **options):
verbose=verbose, echo=click.echo)


@cli.command('check-keyring')
@add_options(quiet_option)
@add_options(help_option)
def cli_check_keyring(**options):
"""
Check whether the keyring service is available.
If available, the command exits with 0.
If not available, the command prints an error message with some information
about the reasons, and exits with 1.
"""
verbose = not options['quiet']

try:
KeyRingLib.check_available()
except KeyRingNotAvailable as exc:
if verbose:
click.echo("Error: {}".format(exc))
click.get_current_context().exit(1)

if verbose:
click.echo("Success! Keyring service is available")


def check_exists(vaultfile):
if not os.path.exists(vaultfile):
raise click.ClickException(
Expand Down
3 changes: 0 additions & 3 deletions tests/utils/keyring_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@

import pytest
import keyring
import keyring.backends.chainer # Required to import separately
import keyring.backends.null # Required to import separately
import keyring.backends.fail

from easy_vault import KeyRingLib

Expand Down

0 comments on commit 0fce3cb

Please sign in to comment.