def test_get_keys(): gpg = get_GPG('{"One": 1, "Two": 2}', 'secrets_tmp4', '12345') cmdc = CommandControler(gpg) keys = cmdc.get_keys() assert keys == ['One', 'Two'] remove_files(['secrets_tmp4'])
def test_get_json(): gpg = get_GPG('{"One": 1, "Two": 2}', 'secrets_tmp1', '12345') cmdc = CommandControler(gpg) json = cmdc.get_json() assert json == '{"One": 1, "Two": 2}' remove_files(['secrets_tmp1'])
def test_modify_id_no_id(): gpg = get_GPG('{"One": {"user":"******","password":"******","url":"url1","other":"other1"}}', 'secrets_tmp43', '12345') cmdc = CommandControler(gpg) with pytest.raises(KeyError): cmdc.modify_id('Two','Three') remove_files(['secrets_tmp43'])
def test_show_keys_empty(capsys): gpg = get_GPG('{}', 'secrets_tmp8', '12345') cmdc = CommandControler(gpg) cmdc.show_keys() out, err = capsys.readouterr() assert out == '' remove_files(['secrets_tmp8'])
def test_show_keys(capsys): gpg = get_GPG('{"One": 1, "Two": 2}', 'secrets_tmp7', '12345') cmdc = CommandControler(gpg) cmdc.show_keys() out, err = capsys.readouterr() assert out == 'One\nTwo\n' remove_files(['secrets_tmp7'])
def test_add_content_id_json_firt_element(): gpg = GPGTools(key = '12345') cmdc = CommandControler(gpg) cmdc.add_content_id_json("example", '{"user":"******","password":"******","url":"example","other":"example"}') json_gpg = json.loads(str(gpg.decrypt_content())) assert json_gpg == {"example": {"user":"******","password":"******","url":"example","other":"example"}} remove_files(['secrets'])
def test_show_keys_no_json(capsys): gpg = get_GPG('d', 'secrets_tmp9', '12345') with pytest.raises(ValueError): cmdc = CommandControler(gpg) cmdc.show_keys() out, err = capsys.readouterr() assert out == '' remove_files(['secrets_tmp9'])
def test_del_id(): gpg = get_GPG('{"One": {"user":"******","password":"******","url":"url1","other":"other1"}}', 'secrets_tmp44', '12345') cmdc = CommandControler(gpg) cmdc.del_id('One') json_gpg = json.loads(str(gpg.decrypt_content())) assert json_gpg == {} remove_files(['secrets_tmp44'])
def test_set_json(): gpg = get_GPG('{"One": 1, "Two": 2}', 'secrets_tmp2', '12345') gpg2 = GPGTools(file = 'secrets_tmp3', key = '12345') cmdc = CommandControler(gpg2) cmdc.set_json('{"One": 1, "Two": 2}') file1 = open('secrets_tmp2', 'r') file2 = open('secrets_tmp3', 'r') assert file1.read() != file2.read() remove_files(['secrets_tmp2', 'secrets_tmp3'])
def test_update_keys(): get_files('secrets_tmp10', 'secrets_tmp11') gpg = GPGTools(file = 'secrets_tmp10', key = '12345') cmdc = CommandControler(gpg) cmdc.update_keys() file1 = open('secrets_tmp10','r') file2 = open('secrets_tmp11','r') assert file1.read() != file2.read() remove_files(['secrets_tmp10','secrets_tmp11'])
def test_add_content_id_json(): json_gpg = {"One": {"user":"******","password":"******","url":"url1","other":"other1"},"example": {"user":"******","password":"******","url":"example","other":"example"}} gpg2 = get_GPG('{"One": {"user":"******","password":"******","url":"url1","other":"other1"}}', 'secrets_tmp39', '12345') cmdc = CommandControler(gpg2) cmdc.add_content_id_json("example", '{"user":"******","password":"******","url":"example","other":"example"}') json_gpg2 = json.loads(str(gpg2.decrypt_content())) assert json_gpg2 == json_gpg remove_files(['secrets_tmp39'])
def test_add_content(monkeypatch): gpg = get_GPG('{"One": {"user":"******","password":"******","url":"url1","other":"other1"},"example": {"user":"******","password":"******","url":"example","other":"example"}}', 'secrets_tmp5', '12345') gpg2 = get_GPG('{"One": {"user":"******","password":"******","url":"url1","other":"other1"}}', 'secrets_tmp6', '12345') json_msg = '{"example": {"user":"******","password":"******","url":"example","other":"example"}}' cmdc = CommandControler(gpg2) cmdc.add_content(json_msg) json_gpg = json.loads(str(gpg.decrypt_content())) json_gpg2 = json.loads(str(gpg2.decrypt_content())) assert json_gpg == json_gpg2 remove_files(['secrets_tmp5', 'secrets_tmp6'])
def test_no_update_keys(): ''' This test ensure that the codification works properly in the future this could be moved to the test_gpg_tools ''' get_files('secrets_tmp12', 'secrets_tmp13') gpg = GPGTools(file = 'secrets_tmp12', key = '12345') cmdc = CommandControler(gpg) file1 = open('secrets_tmp12','r') file2 = open('secrets_tmp13','r') assert file1.read() == file2.read() remove_files(['secrets_tmp12','secrets_tmp13'])
def __init__(self, gpg): self.cmdc = CommandControler(gpg) self.FILE = gpg.get_file()
class InteractiveCMD: cmdc = '' FILE = '' def __init__(self, gpg): self.cmdc = CommandControler(gpg) self.FILE = gpg.get_file() def id_or_list(self): keys_list = self.cmdc.get_keys() id = raw_input( "Introduce the identifier name or 'list' for list all identifiers: " ).replace(' ', '_') while id.lower() == 'list' or id not in keys_list: if id.lower() == 'list': self.show_keys() id = raw_input( "Introduce the identifier name or 'list' for list all identifiers: " ).replace(' ', '_') if id not in keys_list: id = raw_input( "Introduce a valid identifier name or 'list' for list all identifiers: " ).replace(' ', '_') return id def add_content(self): id = raw_input('Please Introduce an Identifier: ').replace(' ', '_') if (os.path.isfile(self.FILE)): while (id in self.cmdc.get_keys()): id = raw_input('Please Introduce an Identifier: ').replace( ' ', '_') for el in KEYS: KEYS[el] = raw_input("Please introduce a value for '" + str(el.lower()) + "' field, or leave it empty: ") new_json_content = {id: dict(KEYS)} self.cmdc.add_content(json.dumps(new_json_content)) def modify_content(self): id = self.id_or_list() json_content = json.loads(self.cmdc.get_json()) json_content.pop(id, None) new_json = {} print('Leave all elements without value for delete the entry') for e in KEYS: element = raw_input('New ' + str(e) + ': ') new_json[e] = element if all(values == '' for key, values in new_json.items()): jkv = json.dumps(json_content, sort_keys=True) self.cmdc.set_json(jkv) print('Done! Identifier ' + id + ' has been deleted') else: json_content[id] = new_json jkv = json.dumps(json_content, sort_keys=True) self.cmdc.set_json(jkv) print('Done! Identifier ' + id + ' has been modified') def show_keys(self): self.cmdc.show_keys() def update_keys(self): self.cmdc.update_keys() def print_decrypt_content(self): id = self.id_or_list() json_content = json.loads(self.cmdc.get_json()) output = raw_input('Show values? (Y/n): ') while output.lower() != 'y' and output.lower() != 'n' and output.lower( ) != 'yes' and output.lower() != 'no' and output.lower() != '': output = raw_input('Show values? (Y/n): ') if output.lower() == '' or output.lower() == 'y' or output.lower( ) == 'yes': for e in KEYS: print(str(e) + ': ' + str(json_content[id][e])) output = raw_input( 'Copy any elemento to clipboard? (N/element name): ') while output.lower() not in KEYS and output.lower( ) != '' and output.lower() != 'n' and output.lower() != 'no': output = raw_input( "Please choose 'no' for leave. For copy and element 'user', 'password', 'url' or 'other': " ) if output.lower() != '' and output.lower() != 'no' and output.lower( ) != 'n': if platform.system() == 'Darwin': pyperclip.copy(json_content[id][output.lower()]) else: print('Only Darwin platforms') def exit(self): sys.exit(0) def raw_input_menu(self, option, switcher): while True: try: option = int(option) if option not in range(1, switcher): raise ValueError break except: option = raw_input('Please, choose correct option: ') return option def interactive_menu(self): if os.path.isfile(self.FILE): switcher = { 0: lambda: '', 1: self.add_content, 2: self.modify_content, 3: self.print_decrypt_content, 4: self.show_keys, 5: self.update_keys, 6: self.exit } option = raw_input( '\t1: Add Key/Value Pair\n\t2: Modify/Delete Key/Value Pair\n\t3: Decrypt Key/Value Pair\n\t4: Show Keys\n\t5: Update public keys\n\t6: Exit\nChoose: ' ) option = self.raw_input_menu(option, len(switcher)) func = switcher.get(option, lambda: 'nothing') return func() else: print('The file' + self.FILE + 'has not been found, using -i/--interactive argument.') switcher = {0: lambda: '', 1: self.add_content, 2: self.exit} option = raw_input('\t1: Add\n\t2: Exit\nChoose: ') option = self.raw_input_menu(option, len(switcher)) func = switcher.get(option, lambda: 'nothing') return func() def main(self): pass
def main(argv): gpg = GPGTools(file=FILE) icmd = InteractiveCMD(gpg) cmdc = CommandControler(gpg) if os.path.isfile(FILE): parser = argparse.ArgumentParser( description='Manager for sensible information under PGP') parser.add_argument( '-i', '--interactive', help='display the interactive menu for pwd-manager', action='store_true') parser.add_argument('-l', '--list', help='list all the stored identifiers', action='store_true') parser.add_argument( '-u', '--user', metavar='identifier', help='return the username for the given identifier') parser.add_argument( '-p', '--password', metavar='identifier', help='return the password for the given identifier') parser.add_argument('-ur', '--url', metavar='identifier', help='return the URL for the given identifier') parser.add_argument( '-o', '--other', metavar='identifier', help='return the other for the given identifier') parser.add_argument( '-a', '--all', metavar='identifier', help='display all values for the given identifier') parser.add_argument( '-ak', '--addkey', nargs=2, metavar=('identifier', '{"user":"******", ...}'), help= 'add element to secrets, second argument must be a valid json string. Allowed keys user, password, url and other' ) parser.add_argument( '-mk', '--modkey', nargs=2, metavar=('identifier', '{"user":"******", ...}'), help= 'modify element to secrets, second argument must be a valid json string. Allowed keys user, password, url and other' ) parser.add_argument('-mi', '--modid', nargs=2, metavar=('old_identifier', 'new_identifier'), help='modify id from an element') parser.add_argument( '-d', '--delete', nargs=1, metavar='identifier', help= 'modify element to secrets, second argument must be a valid json string' ) args = parser.parse_args() if args.interactive: icmd.interactive_menu() elif args.list: cmdc.show_keys() elif args.user: cmdc.get_key_value(args.user, 'user') elif args.password: cmdc.get_key_value(args.password, 'pass') elif args.url: cmdc.get_key_value(args.url, 'url') elif args.other: cmdc.get_key_value(args.other, 'other') elif args.all: cmdc.get_key_value(args.all, 'all') elif args.addkey: cmdc.add_content_id_json(args.addkey[0], str(args.addkey[1:][0])) elif args.modkey: cmdc.modify_content(args.modkey[0], str(args.modkey[1:][0])) elif args.modid: cmdc.modify_id(args.modid[0], args.modid[1]) elif args.delete: cmdc.del_id(args.delete[0]) else: parser.print_help() else: parser = argparse.ArgumentParser( description= '''Manager for sensible information under PGP. Use -i/--interactive or -ak/--addkey to introduce your first key and for more option will be displayed.''' ) parser.add_argument( '-i', '--interactive', help='display the interactive menu for pwd-manager', action='store_true') parser.add_argument( '-ak', '--addkey', nargs=2, metavar=('identifier', '{"user":"******", ...}'), help= 'add element to secrets, second argument must be a valid json string. Allowed keys user, password, url and other' ) args = parser.parse_args() if args.interactive: icmd.interactive_menu() elif args.addkey: cmdc.add_content_id_json(args.addkey[0], str(args.addkey[1:][0])) else: parser.print_help()