Example #1
0
    def init_trust(self):
        # Create a temporary directory and save a test key-pair to it:
        temp_dir = tempfile.TemporaryDirectory()
        temp_path = temp_dir.name
        private_key, public_key = TrustBasics.generateNewKeyPair()
        private_path = os.path.join(temp_path, "test_private_key.pem")
        public_path = os.path.join(temp_path, "test_public_key.pem")
        TrustBasics.saveKeyPair(private_key, private_path, public_path,
                                _passphrase)

        # Create random files:
        all_paths = [
            os.path.abspath(os.path.join(temp_path, x, y, z))
            for x in _folder_names for y in _subfolder_names
            for z in _file_names
        ]
        for path in all_paths:
            folder_path = os.path.dirname(path)
            if not os.path.exists(folder_path):
                os.makedirs(folder_path)
            with open(path, "w") as file:
                file.write("".join(
                    random.choice(['a', 'b', 'c', '0', '1', '2', '\n'])
                    for _ in range(1024)))

        # Instantiate a trust object with the public key that was just generated:
        violation_callback = MagicMock()
        trust = Trust(
            public_path)  # No '.getInstance', since key & handler provided.
        trust._violation_handler = violation_callback
        yield temp_path, private_path, trust, violation_callback

        temp_dir.cleanup()
Example #2
0
    def init_trust(self):
        # Create a temporary directory and save a test key-pair to it:
        temp_dir = tempfile.TemporaryDirectory()
        temp_path = temp_dir.name
        private_key, public_key = TrustBasics.generateNewKeyPair()
        private_path = os.path.join(temp_path, "test_private_key.pem")
        public_path = os.path.join(temp_path, "test_public_key.pem")
        TrustBasics.saveKeyPair(private_key, private_path, public_path,
                                _passphrase)

        # Create random files:
        all_paths = [
            os.path.abspath(os.path.join(temp_path, x, y, z))
            for x in _folder_names for y in _subfolder_names
            for z in _file_names
        ]
        for path in all_paths:
            folder_path = os.path.dirname(path)
            if not os.path.exists(folder_path):
                os.makedirs(folder_path)
            with open(path, "w") as file:
                file.write("".join(
                    random.choice(['a', 'b', 'c', '0', '1', '2', '\n'])
                    for _ in range(1024)))

        # Set up mocked Central File Storage & plugin file (don't move the files yet, though):
        CentralFileStorage.setIsEnterprise(True)

        central_storage_dir = tempfile.TemporaryDirectory()
        central_storage_path = central_storage_dir.name
        large_plugin_path = os.path.join(temp_path, _folder_names[2])
        store_folder = os.path.join(large_plugin_path, _subfolder_names[0])
        store_file = os.path.join(large_plugin_path, _file_names[2])

        central_storage_dict = [[
            f"{_subfolder_names[0]}", f"{_subfolder_names[0]}", "1.0.0",
            CentralFileStorage._hashItem(store_folder)
        ],
                                [
                                    f"{_file_names[2]}", f"{_file_names[2]}",
                                    "1.0.0",
                                    CentralFileStorage._hashItem(store_file)
                                ]]
        central_storage_file_path = os.path.join(
            large_plugin_path, TrustBasics.getCentralStorageFilename())
        with open(central_storage_file_path, "w") as file:
            json.dump(central_storage_dict, file, indent=2)

        # Instantiate a trust object with the public key that was just generated:
        violation_callback = MagicMock()
        trust = Trust(
            public_path)  # No '.getInstance', since key & handler provided.
        trust._violation_handler = violation_callback
        yield temp_path, private_path, trust, violation_callback, central_storage_path

        temp_dir.cleanup()
        central_storage_dir.cleanup()
        CentralFileStorage.setIsEnterprise(False)
Example #3
0
def createAndStoreNewKeyPair(private_filename: str, public_filename: str,
                             optional_password: Optional[str]) -> None:
    """Creates a new public and private key, and saves them to the provided filenames.

    :param private_filename: Filename to save the private key to.
    :param public_filename: Filename to save the public key to.
    :param optional_password: Private keys can have a password (or not).
    """

    password = None if optional_password == "" else optional_password
    private_key, public_key = TrustBasics.generateNewKeyPair()
    TrustBasics.saveKeyPair(private_key, private_filename, public_filename,
                            password)
Example #4
0
def createAndStoreNewKeyPair(private_filename: str, public_filename: str,
                             optional_password: Optional[str]) -> None:
    """Creates a new public and private key, and saves them to the provided filenames.

    See also 'Trust.py' in the main library and the related scripts; 'signfile.py', 'signfolder.py' in this folder.

    :param private_filename: Filename to save the private key to.
    :param public_filename: Filename to save the public key to.
    :param optional_password: Private keys can have a password (or not).
    """

    password = None if optional_password == "" else optional_password
    private_key, public_key = TrustBasics.generateNewKeyPair()
    TrustBasics.saveKeyPair(private_key, private_filename, public_filename,
                            password)
Example #5
0
    def init_trust(self):
        # create a temporary directory and save a test key-pair to it:
        temp_dir = tempfile.TemporaryDirectory()
        temp_path = temp_dir.name
        private_key, public_key = TrustBasics.generateNewKeyPair()
        private_path = os.path.join(temp_path, "test_private_key.pem")
        public_path = os.path.join(temp_path, "test_public_key.pem")
        TrustBasics.saveKeyPair(private_key, private_path, public_path, _passphrase)

        # create random files:
        all_paths = [os.path.abspath(os.path.join(temp_path, x, y, z))
                     for x in _folder_names for y in _subfolder_names for z in _file_names]
        for path in all_paths:
            folder_path = os.path.dirname(path)
            if not os.path.exists(folder_path):
                os.makedirs(folder_path)
            with open(path, "w") as file:
                file.write("".join(random.choice(['a', 'b', 'c', '0', '1', '2', '\n']) for _ in range(1024)))

        # instantiate a trust object with the public key that was just generated:
        trust = Trust(public_path)  # Don't use Trust.getInstance as that uses the 'normal' public key instead of test.
        yield temp_path, private_path, trust

        temp_dir.cleanup()
Example #6
0
def createAndStoreNewKeyPair(private_filename: str, public_filename: str,
                             optional_password: Optional[str]) -> None:
    password = None if optional_password == "" else optional_password
    private_key, public_key = TrustBasics.generateNewKeyPair()
    TrustBasics.saveKeyPair(private_key, private_filename, public_filename,
                            password)
Example #7
0
 def test_signNonexisting(self):
     private_key, public_key = TrustBasics.generateNewKeyPair()
     assert TrustBasics.getFileSignature("file-not-found",
                                         private_key) is None
Example #8
0
 def test_keyIOFails(self):
     private_key, public_key = TrustBasics.generateNewKeyPair()
     assert not TrustBasics.saveKeyPair(private_key, public_key,
                                        "file-not-found", _passphrase)
     assert TrustBasics.loadPrivateKey("key-not-found", _passphrase) is None