Skip to content

Commit

Permalink
Merge 2313ebb into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored Jun 20, 2021
2 parents dd215ec + 2313ebb commit bd4ac3c
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 137 deletions.
54 changes: 44 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,24 +90,58 @@ $ dmk get -e secRet007 /my/docs/target.docx
The `-e` parameter is optional. If it is not specified, the value will be
prompted for interactive input.

# Vault location
Vault location
==============

Entries will be stored in a file. By default, the file is named `vault.dmk` and
placed in the current user's `$HOME` directory.
Entries will be stored in a file.

It can be redefined with `$DMK_VAULT_FILE` environment variable:
You can check the current vault file location with `vault` command:

```
$ dmk vault
```
Output:
```
/home/username/vault.dmk
```

By default, it is `vault.dmk` in the current user's `$HOME` directory.

--------------------------------------------------------------------------------

The `-v` parameter overrides the location for a single run.

```
$ export DMK_VAULT_FILE=/path/to/vaultfile.data
$ dmk get ...
```
$ dmk -v /path/to/file.data vault
```

The `-s` parameter overrides both default and environment variable for a
single run:
Output:
```
/path/to/file.data
```

The parameter can be used with any commands:

```
$ dmk -v /path/to/file.data set
$ dmk -v /path/to/file.data get
```

--------------------------------------------------------------------------------

The `$DMK_VAULT_FILE` environment variable overrides the default location:

```
$ dmk get -v /path/to/vaultfile.data ...
$ export DMK_VAULT_FILE=/path/to/file.data
$ dmk vault
```
Output:
```
/path/to/file.data
```




# Under the hood

Expand Down
137 changes: 73 additions & 64 deletions dmk/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import time
from pathlib import Path
from typing import List
from typing import List, Optional

import click

Expand All @@ -14,16 +14,54 @@
from dmk.a_utils.randoms import get_noncrypt_random_bytes
from ._constants import __version__

CODN_FILE_ENVNAME = 'DMK_VAULT_FILE'
VAULT_FILE_ENVNAME = 'DMK_VAULT_FILE'
DEFAULT_STORAGE_FILE = "~/vault.dmk"

CODENAME_SHORT_ARG = "-e"
CODENAME_LONG_ARG = "--entry"
CODENAME_PROMT = "Secret name"
CODENAME_PROMT_CONFIRMATION = "Secret name (again)"

VAULT_ARG_SHORT = "-v"
VAULT_ARG_LONG = "--vault"


def validate_filename(ctx, param, value):
if value is None or not value.strip():
raise click.BadParameter("Storage filename must be specified")
return value


class Globals:
main: Optional[Main]

@classmethod
def the_main(cls) -> Main:
# for mypy
if cls.main is None:
raise TypeError
return cls.main
#vault_arg: Optional[str] = None


@click.group(invoke_without_command=True)
@click.option(VAULT_ARG_SHORT, VAULT_ARG_LONG,
envvar=VAULT_FILE_ENVNAME,
default=DEFAULT_STORAGE_FILE,
callback=validate_filename)
@click.pass_context
def dmk_cli(ctx, vault):
Globals.main = Main(vault)
if not ctx.invoked_subcommand:
click.echo(f"DMK: Dark Matter Keeper v{__version__}")
print('(c) 2021 Artem IG <ortemeo@gmail.com>')
click.echo()
click.echo("See https://github.com/rtmigo/dmk_py#readme")
click.echo()
click.echo(ctx.get_help())
ctx.exit(2)


@click.command(hidden=True)
def bench():
"""Measures the KDF speed: the private key computation time."""
Expand All @@ -38,91 +76,62 @@ def bench():
print(f'Mean {sum(a) / len(a):.3f} sec')


@click.command(name='set')
@click.option('-v', '--vault',
envvar=CODN_FILE_ENVNAME,
default=DEFAULT_STORAGE_FILE,
callback=validate_filename)
@click.option('-e',
'--entry',
codename_read_option = click.option(CODENAME_SHORT_ARG, CODENAME_LONG_ARG,
'codename',
prompt=CODENAME_PROMT,
hide_input=True)


@dmk_cli.command(name='set')
# @vault_option
@click.option(CODENAME_SHORT_ARG, CODENAME_LONG_ARG,
'codename',
prompt='Entry secret name',
prompt=CODENAME_PROMT,
hide_input=True,
confirmation_prompt="Repeat")
confirmation_prompt=CODENAME_PROMT_CONFIRMATION)
@click.option('-t', '--text', default=None)
@click.argument('file', nargs=-1, type=Path)
def set_cmd(vault: str, codename: str, text: str, file: List[Path]):
def set_cmd(codename: str, text: str, file: List[Path]):
"""Encrypts text or file to an entry."""

if len(file) >= 1:
if len(file) >= 2:
raise click.BadParameter("Exactly one file expected")
Main(vault).set_file(codename, str(file[0])) # todo not str
Globals.the_main().set_file(codename, str(file[0])) # todo not str
else:
if text is None:
text = click.prompt('Text')
Main(vault).set_text(codename, text)
Globals.the_main().set_text(codename, text)


@click.command(name='get')
@click.option('-v', '--vault',
envvar=CODN_FILE_ENVNAME,
default=DEFAULT_STORAGE_FILE,
callback=validate_filename)
@click.option('-e',
'--entry',
'codename',
prompt='Name',
hide_input=True)
@dmk_cli.command(name='get')
# @vault_option
@codename_read_option
@click.argument('file', nargs=-1, type=Path)
def getf_cmd(vault: str, codename: str, file: List[Path]):
def getf_cmd(codename: str, file: List[Path]):
"""Decrypts an entry and prints as text, or writes to file."""
if len(file) > 0:
Main(vault).get_file(codename, str(file[0]))
Globals.the_main().get_file(codename, str(file[0]))
else:
s = Main(vault).get_text(codename)
s = Globals.the_main().get_text(codename)
print(s)


@click.command()
@click.option('-v', '--vault',
envvar=CODN_FILE_ENVNAME,
callback=validate_filename)
@click.option('-e',
'--entry',
'codename',
prompt='Codename',
hide_input=True)
def eval(storage: str, codename: str):
@dmk_cli.command()
# @vault_option
@codename_read_option
def eval(codename: str):
"""Gets item data as text and executes it as shell command."""
Main(storage).eval(codename)


@click.group(
invoke_without_command=True,
# callback = lambda: print('zzz'),
)
# @click.version_option(message=f'%(prog)s {__version__}\n(c) {__copyright__}')
@click.pass_context
def dmk_cli(ctx):
if not ctx.invoked_subcommand:
click.echo(f"DMK: Dark Matter Keeper v{__version__}")
print('(c) 2021 Artem IG <ortemeo@gmail.com>')
click.echo()
click.echo("See https://github.com/rtmigo/dmk_py#readme")
click.echo()
click.echo(ctx.get_help())
ctx.exit(2)
Globals.the_main().eval(codename)

pass

@dmk_cli.command(name='vault')
def vault_cmd():
"""Prints the location of the vault file."""
click.echo(Globals.the_main().file_path)
#click.echo(f'Original: {Globals.main.}')
#click.echo(f'Resolved: {Main(Globals.vault_arg).file_path}')

dmk_cli.add_command(bench)
dmk_cli.add_command(getf_cmd)
dmk_cli.add_command(eval)
dmk_cli.add_command(set_cmd)

if __name__ == '__main__':
dmk_cli(None)
#if __name__ == '__main__':
# dmk_cli(None)
Loading

0 comments on commit bd4ac3c

Please sign in to comment.