Add search command
This commit is contained in:
parent
d15fb5c02e
commit
24ef0b01ea
1 changed files with 42 additions and 0 deletions
|
@ -15,6 +15,7 @@ import json
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
|
import re
|
||||||
import logging
|
import logging
|
||||||
from xml.dom import minidom
|
from xml.dom import minidom
|
||||||
import binascii
|
import binascii
|
||||||
|
@ -1291,6 +1292,38 @@ class NcPasswordClient:
|
||||||
)
|
)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def search(self, pattern, limit):
|
||||||
|
'''Search for a password (or passwords) matching in any field the given pattern'''
|
||||||
|
passwords = self.nc.list_passwords()
|
||||||
|
if passwords is None:
|
||||||
|
self.info(
|
||||||
|
{
|
||||||
|
"action": "search",
|
||||||
|
"message": "No passwords obtained"
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return False
|
||||||
|
count = 0
|
||||||
|
for password in passwords:
|
||||||
|
for key in password.keys():
|
||||||
|
match = re.search(password[key], pattern)
|
||||||
|
if match:
|
||||||
|
self.info(
|
||||||
|
password
|
||||||
|
)
|
||||||
|
count += 1
|
||||||
|
if count >= limit:
|
||||||
|
return True
|
||||||
|
self.info(
|
||||||
|
{
|
||||||
|
"action": "search",
|
||||||
|
"message": "No passwords found with the given pattern",
|
||||||
|
"patter": pattern,
|
||||||
|
"total_passwords": len(passwords)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return False
|
||||||
|
|
||||||
def _init_log(self):
|
def _init_log(self):
|
||||||
''' Initialize log object '''
|
''' Initialize log object '''
|
||||||
self._log = logging.getLogger("nc_password_client")
|
self._log = logging.getLogger("nc_password_client")
|
||||||
|
@ -1455,6 +1488,15 @@ def delete_passwords_folder(ctx, name):
|
||||||
'''Delete a password folder'''
|
'''Delete a password folder'''
|
||||||
ctx.obj['NcPasswordClient'].delete_passwords_folder(name)
|
ctx.obj['NcPasswordClient'].delete_passwords_folder(name)
|
||||||
|
|
||||||
|
@cli.command()
|
||||||
|
@click.option('--pattern', '-p', required=True, help='Regular expression pattern to search in the password fields')
|
||||||
|
@click.option('--limit', '-l', default=1, help='Maximun number of passwords to show')
|
||||||
|
@click_config_file.configuration_option()
|
||||||
|
@click.pass_context
|
||||||
|
def search(ctx, pattern, limit):
|
||||||
|
'''Search for a password (or passwords) that match the given pattern in its fields'''
|
||||||
|
ctx.obj['NcPasswordClient'].search(pattern, limit)
|
||||||
|
|
||||||
@cli.command()
|
@cli.command()
|
||||||
@click.option(
|
@click.option(
|
||||||
'--limit', '-l',
|
'--limit', '-l',
|
||||||
|
|
Loading…
Reference in a new issue