Skip to content

Commit

Permalink
Ksm 508 ksm cli add secret lookup by title (#595)
Browse files Browse the repository at this point in the history
* KSM-508 Added record lookup by title

* Update README.md
  • Loading branch information
idimov-keeper authored Apr 26, 2024
1 parent 4f7e0d3 commit 0c493eb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
3 changes: 2 additions & 1 deletion integration/keeper_secrets_manager_cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ For more information see our official documentation page https://docs.keeper.io/

## 1.1.4

- KSM-507: Added `ksm secret delete` functionality
- KSM-507: Added `ksm secret delete` command
- KSM-508: Added search by title to `ksm secret list` command

## 1.1.3

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -552,21 +552,20 @@ def secret_command(ctx):
@click.option('--recursive', '-r', is_flag=True, help='List recursively all records including subfolders of the folder UID')
@click.option('--query', '-q', type=str, help='List records matching the JSONPath query')
@click.option('--show-value', '-v', is_flag=True, help='Print matching value instead of record title')
@click.option('--title', '-t', type=str, help='List only records with title matching the regex')
@click.option('--json', is_flag=True, help='Return secret as JSON')
@click.pass_context
def secret_list_command(ctx, uid, folder, recursive, query, show_value, json):
def secret_list_command(ctx, uid, folder, recursive, query, show_value, title, json):
"""List all secrets"""

output = "text"
if json is True:
output = "json"

output = "json" if json is True else "text"
ctx.obj["secret"].secret_list(
uids=uid,
folder=folder,
recursive=recursive,
query=query,
show_value=show_value,
title=title,
output_format=output,
use_color=ctx.obj["cli"].use_color
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,13 +472,19 @@ def _format_list(record_dict, use_color=True, columns=None):
table.add_row([record["uid"], record["type"], record["title"]])
return "\n" + table.get_string() + "\n"

def secret_list(self, uids=None, folder=None, recursive=False, query=None, show_value=False, output_format='json', use_color=None):
def secret_list(self, uids=None, folder=None, recursive=False, query=None, show_value=False, title="", output_format='json', use_color=None):

if use_color is None:
use_color = self.cli.use_color

loadrefs = True if query else False # to load fields[] and custom[]
record_dict = self.query(uids=uids, folder=folder, recursive=recursive, output_format='dict', load_references=loadrefs, unmask=True, use_color=use_color)
if title:
try:
filtered = [x for x in record_dict if re.search(title, x.get("title", ""), re.IGNORECASE)]
record_dict = filtered
except Exception as err:
raise KsmCliException(f"Check your regex '{title}' - error: {str(err)}")
if query:
items = self._query_jsonpath_list(query, record_dict)
if output_format == 'text':
Expand Down

0 comments on commit 0c493eb

Please sign in to comment.