Exemple #1
0
def _anonymize_dir(input_dir, output_dir, netconan_config=None):
    # type: (str, str, str) -> None
    """
    Anonymize files in input dir and save to output dir.

    Uses Netconan with the provided configuration file to perform anonymization.  If no configuration is provided, only IP addresses and password will be anonymized.

    :param input_dir: directory containing files to anonymize
    :type input_dir: string
    :param output_dir: directory to store anonymized files in
    :type output_dir: string
    :param netconan_config: path to Netconan configuration file
    :type netconan_config: string
    """
    args = [
        '-i', str(input_dir),
        '-o', str(output_dir),
    ]
    if netconan_config is not None:
        args.extend([
            '-c', netconan_config
        ])
    else:
        args.extend([
            '-a',
            '-p',
        ])
    netconan.main(args)
def test_end_to_end(tmpdir):
    """Test Netconan main with simulated input file and commandline args."""
    filename = "test.txt"
    input_dir = tmpdir.mkdir("input")
    input_dir.join(filename).write(INPUT_CONTENTS)

    output_dir = tmpdir.mkdir("output")
    output_file = output_dir.join(filename)

    ref_file = tmpdir.join(filename)
    ref_file.write(REF_CONTENTS)

    args = [
        '-i', str(input_dir),
        '-o', str(output_dir),
        '-s', 'TESTSALT',
        '-a',
        '-p',
        '-w', 'intentionet,sensitive',
        '-r', 'reservedword',
        '-n', '65432,12345',
    ]
    main(args)

    # Make sure output file matches the ref
    assert(filecmp.cmp(str(ref_file), str(output_file)))
def test_version(capsys):
    """Test that version info is printed."""
    with pytest.raises(SystemExit):
        main(["--version"])
    captured = capsys.readouterr()
    # Python2 prints version info in err instead of out
    if six.PY2:
        assert __version__ in captured.err
    else:
        assert __version__ in captured.out
def run_test(input_dir, output_dir, filename, ref, args):
    """Executes a test that the given filename is netconan-ified to ref."""
    used_args = args + ["-i", str(input_dir), "-o", str(output_dir)]
    main(used_args)

    # Compare lines for more readable failed assertion message
    t_ref = ref.split("\n")
    with open(str(output_dir.join(filename))) as f_out:
        t_out = f_out.read().split("\n")

    # Make sure output file lines match ref lines
    assert t_ref == t_out
def test_end_to_end_no_anonymization(tmpdir):
    """Test Netconan main with simulated input file and no anonymization args."""
    filename = "test.txt"
    input_dir = tmpdir.mkdir("input")
    input_dir.join(filename).write(INPUT_CONTENTS)

    output_dir = tmpdir.mkdir("output")
    output_file = output_dir.join(filename)

    args = [
        '-i', str(input_dir),
        '-o', str(output_dir),
        '-s', 'TESTSALT',
        '-r', 'reservedword',
    ]
    main(args)

    # Make sure no output file was generated
    # when no anonymization args are supplied
    assert(not os.path.exists(str(output_file)))
Exemple #6
0
def test_end_to_end(tmpdir):
    """Test Netconan main with simulated input file and commandline args."""
    input_contents = """
# Intentionet's sensitive test file
ip address 192.168.2.1 255.255.255.255
my hash is $1$salt$ABCDEFGHIJKLMNOPQRS
AS num 12345 and 65432 should be changed
password foobar
password reservedword

"""
    ref_contents = """
# 1cbbc2's fd8607 test file
ip address 201.235.139.13 255.255.255.255
my hash is $1$0000$CxUUGIrqPb7GaB5midrQZ.
AS num 8625 and 64818 should be changed
password netconanRemoved1
password reservedword

"""

    filename = "test.txt"
    input_dir = tmpdir.mkdir("input")
    input_dir.join(filename).write(input_contents)

    output_dir = tmpdir.mkdir("output")
    output_file = output_dir.join(filename)

    ref_file = tmpdir.join(filename)
    ref_file.write(ref_contents)

    args = [
        '-i',
        str(input_dir), '-o',
        str(output_dir), '-s', 'TESTSALT', '-a', '-p', '-w',
        'intentionet,sensitive', '-r', 'reservedword', '-n', '65432,12345'
    ]
    main(args)

    # Make sure output file matches the ref
    assert (filecmp.cmp(str(ref_file), str(output_file)))
Exemple #7
0
def test_end_to_end(tmpdir):
    """Test Netconan main with simulated input file and commandline args."""
    filename = "test.txt"
    input_dir = tmpdir.mkdir("input")
    input_dir.join(filename).write(INPUT_CONTENTS)

    output_dir = tmpdir.mkdir("output")
    output_file = output_dir.join(filename)

    ref_file = tmpdir.join(filename)
    ref_file.write(REF_CONTENTS)

    args = [
        '-i',
        str(input_dir),
        '-o',
        str(output_dir),
        '-s',
        'TESTSALT',
        '-a',
        '-p',
        '-w',
        'intentionet,sensitive',
        '-r',
        'reservedword',
        '-n',
        '65432,12345',
        '--preserve-addresses',
        '11.11.0.0/16,111.111.111.111',
        '--preserve-prefixes',
        '192.168.2.0/24',
    ]
    main(args)

    with open(str(ref_file)) as f_ref, open(str(output_file)) as f_out:
        # Compare lines for more readable failed assertion message
        t_ref = f_ref.read().split('\n')
        t_out = f_out.read().split('\n')

    # Make sure output file lines match ref lines
    assert t_ref == t_out
Exemple #8
0
def _anonymize_dir(input_dir: str,
                   output_dir: str,
                   netconan_config: Optional[str] = None) -> None:
    """
    Anonymize files in input dir and save to output dir.

    Uses Netconan with the provided configuration file to perform anonymization.  If no configuration is provided, only IP addresses and password will be anonymized.

    :param input_dir: directory containing files to anonymize
    :type input_dir: string
    :param output_dir: directory to store anonymized files in
    :type output_dir: string
    :param netconan_config: path to Netconan configuration file
    :type netconan_config: string
    """
    args = ["-i", str(input_dir), "-o", str(output_dir)]
    if netconan_config is not None:
        args.extend(["-c", netconan_config])
    else:
        args.extend(["-a", "-p"])
    netconan.main(args)
def test_version(capsys):
    """Test that version info is printed."""
    with pytest.raises(SystemExit):
        main(["--version"])
    captured = capsys.readouterr()
    assert __version__ in captured.out