Example #1
0
    def testMixedDaddr(self):
        self.naming.GetNetAddr.return_value = TEST_IPS

        acl = openconfig.OpenConfig(
            policy.ParsePolicy(GOOD_HEADER_MIXED + GOOD_DADDR, self.naming),
            EXP_INFO)
        expected = json.loads(GOOD_JSON_MIXED_DADDR)
        self.assertEqual(expected, json.loads(str(acl)))

        self.naming.GetNetAddr.assert_called_once_with('CORP_EXTERNAL')
Example #2
0
    def testDport(self):
        self.naming.GetServiceByProto.side_effect = [['53'], ['53']]

        acl = openconfig.OpenConfig(
            policy.ParsePolicy(GOOD_HEADER + GOOD_DPORT, self.naming),
            EXP_INFO)
        expected = json.loads(GOOD_JSON_DPORT)
        self.assertEqual(expected, json.loads(str(acl)))

        self.naming.GetServiceByProto.assert_has_calls(
            [mock.call('DNS', 'tcp')])
Example #3
0
    def testEverything(self):
        self.naming.GetServiceByProto.side_effect = [['53'], ['53']]
        self.naming.GetNetAddr.return_value = TEST_IPS

        acl = openconfig.OpenConfig(
            policy.ParsePolicy(GOOD_HEADER + GOOD_EVERYTHING, self.naming),
            EXP_INFO)
        expected = json.loads(GOOD_JSON_EVERYTHING)
        self.assertEqual(expected, json.loads(str(acl)))

        self.naming.GetServiceByProto.assert_has_calls(
            [mock.call('DNS', 'udp'),
             mock.call('DNS', 'tcp')])
Example #4
0
def RenderFile(base_directory: str, input_file: pathlib.Path,
               output_directory: pathlib.Path, definitions: naming.Naming,
               exp_info: int, optimize: bool, shade_check: bool,
               write_files: WriteList):
    """Render a single file.

  Args:
    base_directory: The base directory to look for acls.
    input_file: the name of the input policy file.
    output_directory: the directory in which we place the rendered file.
    definitions: the definitions from naming.Naming().
    exp_info: print a info message when a term is set to expire in that many
      weeks.
    optimize: a boolean indicating if we should turn on optimization or not.
    shade_check: should we raise an error if a term is completely shaded
    write_files: a list of file tuples, (output_file, acl_text), to write
  """
    output_relative = input_file.relative_to(base_directory).parent.parent
    output_directory = output_directory / output_relative

    logging.debug('rendering file: %s into %s', input_file, output_directory)

    pol = None
    jcl = False
    acl = False
    atp = False
    asacl = False
    aacl = False
    bacl = False
    eacl = False
    gca = False
    gcefw = False
    gcphf = False
    ips = False
    ipt = False
    msmpc = False
    spd = False
    nsx = False
    oc = False
    pcap_accept = False
    pcap_deny = False
    pf = False
    srx = False
    jsl = False
    nft = False
    win_afw = False
    nxacl = False
    xacl = False
    paloalto = False

    try:
        with open(input_file) as f:
            conf = f.read()
            logging.debug('opened and read %s', input_file)
    except IOError as e:
        logging.warning('bad file: \n%s', e)
        raise

    try:
        pol = policy.ParsePolicy(conf,
                                 definitions,
                                 optimize=optimize,
                                 base_dir=base_directory,
                                 shade_check=shade_check)
    except policy.ShadingError as e:
        logging.warning('shading errors for %s:\n%s', input_file, e)
        return
    except (policy.Error, naming.Error):
        raise ACLParserError(
            'Error parsing policy file %s:\n%s%s' %
            (input_file, sys.exc_info()[0], sys.exc_info()[1]))

    platforms = set()
    for header in pol.headers:
        platforms.update(header.platforms)

    if 'juniper' in platforms:
        jcl = copy.deepcopy(pol)
    if 'cisco' in platforms:
        acl = copy.deepcopy(pol)
    if 'ciscoasa' in platforms:
        asacl = copy.deepcopy(pol)
    if 'brocade' in platforms:
        bacl = copy.deepcopy(pol)
    if 'arista' in platforms:
        eacl = copy.deepcopy(pol)
    if 'arista_tp' in platforms:
        atp = copy.deepcopy(pol)
    if 'aruba' in platforms:
        aacl = copy.deepcopy(pol)
    if 'ipset' in platforms:
        ips = copy.deepcopy(pol)
    if 'iptables' in platforms:
        ipt = copy.deepcopy(pol)
    if 'msmpc' in platforms:
        msmpc = copy.deepcopy(pol)
    if 'nsxv' in platforms:
        nsx = copy.deepcopy(pol)
    if 'openconfig' in platforms:
        oc = copy.deepcopy(pol)
    if 'packetfilter' in platforms:
        pf = copy.deepcopy(pol)
    if 'pcap' in platforms:
        pcap_accept = copy.deepcopy(pol)
        pcap_deny = copy.deepcopy(pol)
    if 'speedway' in platforms:
        spd = copy.deepcopy(pol)
    if 'srx' in platforms:
        srx = copy.deepcopy(pol)
    if 'srxlo' in platforms:
        jsl = copy.deepcopy(pol)
    if 'windows_advfirewall' in platforms:
        win_afw = copy.deepcopy(pol)
    if 'cisconx' in platforms:
        nxacl = copy.deepcopy(pol)
    if 'ciscoxr' in platforms:
        xacl = copy.deepcopy(pol)
    if 'nftables' in platforms:
        nft = copy.deepcopy(pol)
    if 'gce' in platforms:
        gcefw = copy.deepcopy(pol)
    if 'gcp_hf' in platforms:
        gcphf = copy.deepcopy(pol)
    if 'paloalto' in platforms:
        paloalto = copy.deepcopy(pol)
    if 'cloudarmor' in platforms:
        gca = copy.deepcopy(pol)

    acl_obj: aclgenerator.ACLGenerator

    try:
        if jcl:
            acl_obj = juniper.Juniper(jcl, exp_info)
            RenderACL(str(acl_obj), acl_obj.SUFFIX, output_directory,
                      input_file, write_files)
        if msmpc:
            acl_obj = junipermsmpc.JuniperMSMPC(msmpc, exp_info)
            RenderACL(str(acl_obj), acl_obj.SUFFIX, output_directory,
                      input_file, write_files)
        if srx:
            acl_obj = junipersrx.JuniperSRX(srx, exp_info)
            RenderACL(str(acl_obj), acl_obj.SUFFIX, output_directory,
                      input_file, write_files)
        if acl:
            acl_obj = cisco.Cisco(acl, exp_info)
            RenderACL(str(acl_obj), acl_obj.SUFFIX, output_directory,
                      input_file, write_files)
        if asacl:
            acl_obj = ciscoasa.CiscoASA(asacl, exp_info)
            RenderACL(str(acl_obj), acl_obj.SUFFIX, output_directory,
                      input_file, write_files)
        if aacl:
            acl_obj = aruba.Aruba(aacl, exp_info)
            RenderACL(str(acl_obj), acl_obj.SUFFIX, output_directory,
                      input_file, write_files)
        if bacl:
            acl_obj = brocade.Brocade(bacl, exp_info)
            RenderACL(str(acl_obj), acl_obj.SUFFIX, output_directory,
                      input_file, write_files)
        if eacl:
            acl_obj = arista.Arista(eacl, exp_info)
            RenderACL(str(acl_obj), acl_obj.SUFFIX, output_directory,
                      input_file, write_files)
        if atp:
            acl_obj = arista_tp.AristaTrafficPolicy(atp, exp_info)
            RenderACL(str(acl_obj), acl_obj.SUFFIX, output_directory,
                      input_file, write_files)
        if ips:
            acl_obj = ipset.Ipset(ips, exp_info)
            RenderACL(str(acl_obj), acl_obj.SUFFIX, output_directory,
                      input_file, write_files)
        if ipt:
            acl_obj = iptables.Iptables(ipt, exp_info)
            RenderACL(str(acl_obj), acl_obj.SUFFIX, output_directory,
                      input_file, write_files)
        if nsx:
            acl_obj = nsxv.Nsxv(nsx, exp_info)
            RenderACL(str(acl_obj), acl_obj.SUFFIX, output_directory,
                      input_file, write_files)
        if oc:
            acl_obj = openconfig.OpenConfig(oc, exp_info)
            RenderACL(str(acl_obj), acl_obj.SUFFIX, output_directory,
                      input_file, write_files)
        if spd:
            acl_obj = speedway.Speedway(spd, exp_info)
            RenderACL(str(acl_obj), acl_obj.SUFFIX, output_directory,
                      input_file, write_files)
        if pcap_accept:
            acl_obj = pcap.PcapFilter(pcap_accept, exp_info)
            RenderACL(str(acl_obj), '-accept' + acl_obj.SUFFIX,
                      output_directory, input_file, write_files)
        if pcap_deny:
            acl_obj = pcap.PcapFilter(pcap_deny, exp_info, invert=True)
            RenderACL(str(acl_obj), '-deny' + acl_obj.SUFFIX, output_directory,
                      input_file, write_files)
        if pf:
            acl_obj = packetfilter.PacketFilter(pf, exp_info)
            RenderACL(str(acl_obj), acl_obj.SUFFIX, output_directory,
                      input_file, write_files)
        if win_afw:
            acl_obj = windows_advfirewall.WindowsAdvFirewall(win_afw, exp_info)
            RenderACL(str(acl_obj), acl_obj.SUFFIX, output_directory,
                      input_file, write_files)
        if jsl:
            acl_obj = srxlo.SRXlo(jsl, exp_info)
            RenderACL(str(acl_obj), acl_obj.SUFFIX, output_directory,
                      input_file, write_files)
        if nxacl:
            acl_obj = cisconx.CiscoNX(nxacl, exp_info)
            RenderACL(str(acl_obj), acl_obj.SUFFIX, output_directory,
                      input_file, write_files)
        if xacl:
            acl_obj = ciscoxr.CiscoXR(xacl, exp_info)
            RenderACL(str(acl_obj), acl_obj.SUFFIX, output_directory,
                      input_file, write_files)
        if nft:
            acl_obj = nftables.Nftables(nft, exp_info)
            RenderACL(str(acl_obj), acl_obj.SUFFIX, output_directory,
                      input_file, write_files)
        if gcefw:
            acl_obj = gce.GCE(gcefw, exp_info)
            RenderACL(str(acl_obj), acl_obj.SUFFIX, output_directory,
                      input_file, write_files)
        if gcphf:
            acl_obj = gcp_hf.HierarchicalFirewall(gcphf, exp_info)
            RenderACL(str(acl_obj), acl_obj.SUFFIX, output_directory,
                      input_file, write_files)

        if paloalto:
            acl_obj = paloaltofw.PaloAltoFW(paloalto, exp_info)
            RenderACL(str(acl_obj), acl_obj.SUFFIX, output_directory,
                      input_file, write_files)
        if gca:
            acl_obj = cloudarmor.CloudArmor(gca, exp_info)
            RenderACL(str(acl_obj), acl_obj.SUFFIX, output_directory,
                      input_file, write_files)
    # TODO(robankeny) add additional errors.
    except (juniper.Error, junipermsmpc.Error, junipersrx.Error, cisco.Error,
            ipset.Error, iptables.Error, speedway.Error, pcap.Error,
            aclgenerator.Error, aruba.Error, nftables.Error, gce.Error,
            cloudarmor.Error) as e:
        raise ACLGeneratorError('Error generating target ACL for %s:\n%s' %
                                (input_file, e))