def test_gpg_encrypt_decrypt_file_gpg( gpg_encrypt_decrypt_basic: GpgEncryptDecryptBasicFixture, tmp_enc_dec_dir: Path) -> None: fix = gpg_encrypt_decrypt_basic ori_file = tmp_enc_dec_dir.joinpath("file.txt") ori_file_content = ["Line1", "Line2"] write_text_file_content(ori_file, ori_file_content) ori_file_content_reread = read_text_file_content(ori_file) assert 2 == len(ori_file_content_reread) logging.info("encrypt_file_to_gpg_file") enc_gpg_file = encrypt_file_to_gpg_file( ori_file, tmp_enc_dec_dir.joinpath("file.txt.gpg"), pre_encode_to_b64=False, recipients=map(lambda x: x.fpr, fix.e_e.keys.all), **fix.e_e.as_proc_dict()) logging.info("decrypt_gpg_file_to_file") dec_file = decrypt_gpg_file_to_file( enc_gpg_file, tmp_enc_dec_dir.joinpath("decrypted-file.txt"), post_decode_from_b64=False, **fix.d_a.as_proc_auth_dict()) dec_file_content = read_text_file_content(dec_file) logging.info(f"dec_file_content: {dec_file_content}") assert 2 == len(dec_file_content) assert ori_file_content == dec_file_content
def write_dummy_files_to(d: Path): fn = d.joinpath("dummy.txt") dummy_content = ["Dummy linu1." "Dummy line2"] write_text_file_content(fn, dummy_content) fn_ro = d.joinpath("dummy-ro.txt") write_text_file_content(fn_ro, dummy_content) os.chmod(fn_ro, mode=0o444)
def test_write_and_read_file_content(temp_dir: Path) -> None: dummy_file = temp_dir.joinpath("dummy.txt") logging.info(f"dummy_file: {dummy_file}") content = ["Line1", "Line2"] write_text_file_content(dummy_file, content) read_content = read_text_file_content(dummy_file) assert 2 == len(read_content) assert content == read_content
def tgt_tmp_dir_w_dummy_files(tgt_tmp_dir: Path) -> Path: fn = tgt_tmp_dir.joinpath("dummy.txt") write_text_file_content(fn, ["Dummy src file content.\n"]) fn_ro = tgt_tmp_dir.joinpath("dummy-ro.txt") write_text_file_content(fn_ro, ["Dummy src file content.\n"]) os.chmod(fn_ro, mode=0o444) dir = tgt_tmp_dir.joinpath("dummy-dir") os.mkdir(dir) dir_ro = tgt_tmp_dir.joinpath("dummy-dir-ro") os.mkdir(dir_ro, mode=0o555) return tgt_tmp_dir
def create_dir_content_cached( module_filename: Path, dir: Path, generate_dir_content_fn: Callable[[Path], _LoadDirContentRetT], stale_after_s: Optional[float] = None, cache_dir_provider: OptICacheDirProvider = None, copy_ignore_fn: OptCopyIgnoreFnT = None, load_dir_content_fn: Optional[Callable[[Path], _LoadDirContentRetT]] = None, ) -> _LoadDirContentRetT: def default_load_dir_content(in_path: Path) -> _LoadDirContentRetT: pass if load_dir_content_fn is None: load_dir_content_fn = default_load_dir_content cache_id = generate_dir_content_fn.__name__ cache_state = obtain_cache_dir( module_filename, generate_dir_content_fn.__name__, stale_after_s=stale_after_s, cache_dir_provider=cache_dir_provider ) if cache_state.valid: assert cache_state.path is not None shutil.rmtree(dir) shutil.copytree(cache_state.path, dir, ignore=copy_ignore_fn) return load_dir_content_fn(dir) if cache_state.path is None: return generate_dir_content_fn(dir) generate_dir_content_fn(cache_state.path) # Write some info about what module / function gave rise to this cache. cache_info = cache_state.path.joinpath(".nsft-cache-info") write_text_file_content( cache_info, [ f"{module_filename}::{cache_id}"] ) shutil.rmtree(dir) shutil.copytree(cache_state.path, dir, ignore=copy_ignore_fn) return load_dir_content_fn(dir)
def src_tmp_dir_w_dummy_files(src_tmp_dir: Path) -> Path: fn = src_tmp_dir.joinpath("dummy.txt") write_text_file_content(fn, ["Dummy src file content.\n"]) fn_ro = src_tmp_dir.joinpath("dummy-ro.txt") write_text_file_content(fn_ro, ["Dummy src file content.\n"]) dir = src_tmp_dir.joinpath("dummy-dir") os.mkdir(dir, mode=0o555) dir_ro = src_tmp_dir.joinpath("dummy-dir-ro") os.mkdir(dir_ro, mode=0o555) # Make these files and dirs ro to flag manip errors. os.chmod(fn, mode=0o444) os.chmod(fn_ro, mode=0o444) os.chmod(src_tmp_dir, mode=0o555) return src_tmp_dir
def generate_gpg_encrypted_files_basic( out_dir: Path, gpg_enc_dec_fix: GpgEncryptDecryptBasicFixture) -> Path: fix = gpg_enc_dec_fix tmp_dir = out_dir original_dir = tmp_dir.joinpath("original") os.mkdir(original_dir) original_file = original_dir.joinpath("file.txt") original_file_content = ["Line1", "Line2"] write_text_file_content(original_file, original_file_content) original_file_no_ext = original_dir.joinpath("file") write_text_file_content(original_file_no_ext, original_file_content) # Make these files and dirs ro to flag manip errors. os.chmod(original_file, mode=0o444) os.chmod(original_dir, mode=0o555) enc_cases = [ ("r-all", fix.e_e.keys.all), ("r-e", [fix.e_e.keys.secret[0]]), ("r-a", [fix.d_a.keys.secret[0]]), ("r-b", [fix.d_b.keys.secret[0]]), ("r-ab", [fix.d_a.keys.secret[0], fix.d_b.keys.secret[0]]), ] for c_id, c_rs in enc_cases: encrypted_dir = tmp_dir.joinpath(f"encrypted-{c_id}") os.mkdir(encrypted_dir) enc_gpg_file, enc_gpg_b64_file = \ _encrypt_to_plain_and_b64_ro_gpg( encrypted_dir, original_file, c_rs, fix.e_e) # Create some fraudulous files that impersonates other via their extensions. for s, rp in [(original_file, "fraud-txt-file-as.txt.gpg"), (original_file, "fraud-txt-file-as.txt.b64.gpg"), (enc_gpg_file, "fraud-txt-gpg-file-as.txt.b64.gpg"), (enc_gpg_b64_file, "fraud-txt-b64-gpg-file-as.txt.gpg")]: op = encrypted_dir.joinpath(rp) shutil.copyfile(s, encrypted_dir.joinpath(rp)) os.chmod(op, mode=0o444) # Create some faudulous dir impersonating encrypted files # Create some fraudulous files that impersonates other via their extensions. for s, rp in [ (original_file, "fraud-dir-as.txt.gpg"), (original_file, "fraud-dir-as.txt.b64.gpg"), ]: op = encrypted_dir.joinpath(rp) os.mkdir(op, mode=0o555) shutil.copyfile(enc_gpg_b64_file, encrypted_dir.joinpath("file-txt-b64-gpg-no-ext")) shutil.copyfile(enc_gpg_file, encrypted_dir.joinpath("file-txt-gpg-no-ext")) os.chmod(encrypted_dir, mode=0o555) return tmp_dir