Пример #1
0
def exec_quick_mode(args):
    """More automated script"""
    print("Insert the password that will be used for decrypting the pass "
          "store and encrypting the new keepass db:")
    password = getpass("-> ")
    try:
        mapper = args.custom
        if mapper is not None:
            mapper = import_custom_mapper(os.path.abspath(args.custom))
        reader = PassReader(path=args.input, password=password, mapper=mapper)
    except CustomMapperImportException:
        print(">> ERROR: error while importing the provided mapper.")
        exit(1)
    except Exception:
        print(">> ERROR: error while reading the password-store.")
        exit(1)

    try:
        print_reader_progress(reader)
        reader.parse_db()
    except CustomMapperExecException:
        print(">> ERROR: error while executing the provided mapper.")
        exit(1)
    except Exception:
        print("\n>> ERROR: error while parsing the password-store entries.")
        exit(1)

    try:
        print("")
        sys.stdout.write(f" > Creating the new keepass database... 0%\r")
        sys.stdout.flush()
        p2kp2 = P2KP2(password=password,
                      destination=args.output,
                      overwrite=args.force_overwrite)
        sys.stdout.write(f" > Creating the new keepass database... 100%\r")
        sys.stdout.flush()
    except DbAlreadyExistsException:
        print("")
        print("\n>> ERROR: keepass database file already exists! "
              "Use -f if you want to force overwriting.")
        exit(1)
    except Exception:
        print("")
        print("\n>> ERROR: error while writing the new db.")
        exit(1)

    try:
        print("")
        print_writer_progress(p2kp2, len(reader.entries))
        p2kp2.populate_db(reader)
        print("")
        print("ALL DONE! {} entries converted! Bye!".format(
            len(p2kp2.db.entries)))
    except Exception:
        print("")
        print("\n>> ERROR: error while adding entries to the new db.")
        exit(1)
Пример #2
0
    def test_should_raise_an_exception_if_a_provided_custom_mapper_contains_errors(
            self):
        """... it should raise an exception if a provided custom mapper contains errors"""
        def custom_broken_mapper(_):
            raise Exception

        pr = PassReader(path="tests/password-store",
                        mapper=custom_broken_mapper)
        with pytest.raises(CustomMapperExecException):
            pr.parse_pass_entry("test1")
Пример #3
0
 def setup(self, request, reset_db_after_all_class_test):
     """TestP2Kp2AddEntry setup"""
     reader = PassReader(path="tests/password-store")
     reader.parse_db()
     request.cls.reader = reader
     request.cls.p2kp2 = P2KP2(password=test_pass, destination=test_db_file)
     request.cls.pass_entry0 = list(filter(lambda x: x.title == "test1", self.reader.entries))[0]
     request.cls.pass_entry1 = list(filter(lambda x: x.title == "test4", self.reader.entries))[0]
     request.cls.entry0 = request.cls.p2kp2.add_entry(request.cls.pass_entry0)
     request.cls.entry1 = request.cls.p2kp2.add_entry(request.cls.pass_entry1)
Пример #4
0
 def setup(self, request, reset_db_after_all_class_test):
     """TestP2Kp2 setup"""
     # prepare the PassReader
     reader = PassReader(path="tests/password-store")
     reader.parse_db()
     request.cls.pr = reader
     # populate the new db
     p2kp2 = P2KP2(password=test_pass, destination=test_db_file)
     p2kp2.populate_db(reader)
     # prepare it to be read
     request.cls.db = PyKeePass(test_db_file, password=test_pass)
Пример #5
0
    def test_should_apply_the_mapper_when_provided(self):
        """... it should apply the mapper when provided"""
        def custom_mapper(entry: PassEntry) -> PassEntry:
            entry.title += "_modified"
            if 'cell_number' in entry.custom_properties:
                entry.custom_properties["cell_number"] = "11111111"
            return entry

        pr = PassReader(path="tests/password-store", mapper=custom_mapper)
        entry = pr.parse_pass_entry("test1")
        assert entry.title == "test1_modified"
        assert entry.custom_properties['cell_number'] == "11111111"
Пример #6
0
 def test_should_be_able_to_decrypt_an_entry_with_a_given_password(self):
     """Pass entry should be able to decrypt an entry with a given password."""
     pr = PassReader(path="tests/password-store-with-pass",
                     password="******")
     decrypted_entry = PassEntry.decrypt_entry(pr, "test1")
     entry = 'F_Yq^5vgeyMCgYf-tW\\!T7Uj|\n---\nurl: someurl.com\n'
     assert decrypted_entry == entry
Пример #7
0
    def test_should_have_mapper_pointing_to_a_function_when_custom_is_provided(
            self):
        """... it should have mapper pointing to a function when custom is provided"""
        def custom_mapper():
            pass

        pr = PassReader(path="tests/password-store", mapper=custom_mapper)
        assert pr.mapper is custom_mapper
Пример #8
0
def exec_normal_mode(args):
    """Interactive script."""
    # Intro message and warnings
    intro = "Welcome! pass2keepass2 will convert your pass database into a keepass2 one.\n\n" \
            "> WARNING < This script DOES NOT try to be memory secure: your password will NOT be " \
            "encrypted while in memory, so you probably want to execute this on a trusted hardware.\n\n" \
            "The script will now read your input password-store, so you will probably be asked to " \
            "unlock it.\nKeep in mind this may take a while, depending on the number of entries.\n\n" \
            "Input password-store: {}\n" \
            "Output keepass2 database: {}\n" \
        .format(os.path.abspath(args.input) if args.input is not None else os.path.expanduser("~/.password-store"),
                os.path.abspath(args.output) if args.output is not None else os.path.abspath("pass.kdbx"))
    print(intro)
    answer = input("Are you ready to proceed? [Y/n] ")
    if not (answer.lower() == "y" or answer.lower() == ""):
        print("Ok, bye!")
        exit(1)

    # Read the pass db
    print("\r")
    try:
        reader = PassReader(path=args.input)
    except Exception:
        print(">> ERROR: error while reading the password-store.")
        exit(1)

    # Parse the entries
    try:
        print_reader_progress(reader)
        reader.parse_db()
        print(
            "\n\nPassword-store decrypted! {} entries are ready to be converted."
            .format(len(reader.entries)))
    except Exception:
        print("")
        print("\n>> ERROR: error while parsing the password-store entries.")
        exit(1)

    # Choose a password for keepass
    print("Now choose a strong password for your new keepass database!\n")
    password = None
    while password is None:
        p1 = getpass("A strong password: "******"Enter it again! ")
        if p1 == p2:
            password = p1
        else:
            print("\n >>> Entered passwords do not match, try again.\n")

    # Write the keepass db
    print(
        "\nAlright! It's finally time to write the keepass db. Hold tight, this might take a while!"
    )
    try:
        print("")
        sys.stdout.write(f" > Creating the new keepass database... 0%\r")
        sys.stdout.flush()
        p2kp2 = P2KP2(password=password,
                      destination=args.output,
                      overwrite=args.force_overwrite)
        sys.stdout.write(f" > Creating the new keepass database... 100%\r")
        sys.stdout.flush()
    except DbAlreadyExistsException:
        print("")
        print(">> ERROR: keepass database file already exists! "
              "Use -f if you want to force overwriting.")
        exit(1)

    try:
        print("")
        print_writer_progress(p2kp2, len(reader.entries))
        p2kp2.populate_db(reader)
        print("")
        print(
            "\nALL DONE! {} entries have been added to the new keepass database!\nHave a nice day!"
            .format(len(p2kp2.db.entries)))
    except Exception:
        print("")
        print("\n>> ERROR: error while adding entries to the new db.")
        exit(1)
Пример #9
0
 def setup(self, request):
     """TestPassReader setup"""
     request.cls.pr = PassReader(path="tests/password-store")
Пример #10
0
 def test_should_set_a_none_custom_mapper_by_default(self):
     """... it should set a None custom mapper by default"""
     pr = PassReader(path="tests/password-store")
     assert pr.mapper is None
Пример #11
0
 def test_should_expose_the_password(self):
     """Pass reader init should expose the password."""
     password = "******"
     pr = PassReader(password=password)
     assert pr.password == password
Пример #12
0
 def test_should_support_a_custom_password_store_path(self):
     """Pass reader init should support a custom password-store path."""
     custom_path = "~/some-other-folder"
     pr = PassReader(path=custom_path)
     assert pr.path == os.path.expanduser(custom_path)
Пример #13
0
 def test_should_have_sane_defaults(self):
     """Pass reader init should have sane defaults."""
     pr = PassReader()
     assert pr.path == os.path.expanduser("~/.password-store")