Ejemplo n.º 1
0
 def testTableCreation(self):
     prod_network = nacaddr.IP('10.0.0.0/8')
     prod_network.parent_token = 'PROD_NETWORK'
     corp_internal_one = nacaddr.IP('100.96.0.1/11')
     corp_internal_one.parent_token = 'CORP_INTERNAL'
     corp_internal_two = nacaddr.IP('172.16.0.0/16')
     corp_internal_two.parent_token = 'CORP_INTERNAL'
     self.naming.GetNetAddr('PROD_NETWORK').AndReturn([prod_network])
     self.naming.GetNetAddr('CORP_INTERNAL').AndReturn(
         [corp_internal_one, corp_internal_two])
     self.naming.GetServiceByProto('SMTP', 'tcp').AndReturn(['25'])
     self.mox.ReplayAll()
     acl = packetfilter.PacketFilter(
         policy.ParsePolicy(GOOD_HEADER + MULTIPLE_NAME_TERM, self.naming),
         EXP_INFO)
     result = str(acl)
     self.failUnless('table <PROD_NETWORK> {10.0.0.0/8}' in result,
                     'did not find PROD_NETWORKtable in header')
     self.failUnless(
         'table <CORP_INTERNAL> {100.96.0.1/11,\\\n'
         '172.16.0.0/16}' in result,
         'did not find CORP_INTERNAL table in header')
     self.failUnless(
         'pass quick proto { tcp } from { <CORP_INTERNAL> } to '
         '{ <PROD_NETWORK> } port { 25 } flags S/SA keep state' in result,
         'did not find actual term for multiple-name')
Ejemplo n.º 2
0
 def testBuildWarningTokens(self):
     pol1 = packetfilter.PacketFilter(
         policy.ParsePolicy(GOOD_HEADER + GOOD_WARNING_TERM, self.naming),
         EXP_INFO)
     st, sst = pol1._BuildTokens()
     self.assertEquals(st, SUPPORTED_TOKENS)
     self.assertEquals(sst, SUPPORTED_SUB_TOKENS)
Ejemplo n.º 3
0
    def testTableSameLongNameDiffFilter(self):
        prod_network = nacaddr.IP('10.0.0.0/8')
        prod_network.parent_token = 'PROD_NETWORK_EXTREAMLY_LONG_VERY_NO_GOOD_NAME'
        self.naming.GetNetAddr.return_value = [prod_network]
        self.naming.GetServiceByProto.return_value = ['53']

        acl = packetfilter.PacketFilter(
            policy.ParsePolicy(
                GOOD_HEADER_DIRECTIONAL + LONG_NAME_TERM_DNS_TCP +
                GOOD_HEADER_DIRECTIONAL + LONG_NAME_TERM_DNS_UDP, self.naming),
            EXP_INFO)
        result = str(acl)
        self.failUnless(
            'table <PROD_NETWORK_EXTREAMLY_LONG_VER> {10.0.0.0/8}' in result,
            'did not find shortened name in header.')
        self.failUnless(
            'pass out quick proto { tcp } from { any } to '
            '{ <PROD_NETWORK_EXTREAMLY_LONG_VER> } '
            'port { 53 } flags S/SA keep state' in result,
            'did not find actual TCP term for multiple-name')
        self.failUnless(
            'pass out quick proto { udp } from { any } to '
            '{ <PROD_NETWORK_EXTREAMLY_LONG_VER> } '
            'port { 53 } keep state' in result,
            'did not find actual UDP for multiple-name')

        self.naming.GetNetAddr.assert_has_calls([
            mock.call('PROD_NETWORK_EXTREAMLY_LONG_VERY_NO_GOOD_NAME'),
            mock.call('PROD_NETWORK_EXTREAMLY_LONG_VERY_NO_GOOD_NAME')
        ])
        self.naming.GetServiceByProto.assert_has_calls(
            [mock.call('DNS', 'tcp'),
             mock.call('DNS', 'udp')])
Ejemplo n.º 4
0
    def testExpiredTerm2(self, mock_warn):
        packetfilter.PacketFilter(
            policy.ParsePolicy(GOOD_HEADER + EXPIRED_TERM2, self.naming),
            EXP_INFO)

        mock_warn.assert_called_once_with(
            'WARNING: Term %s in policy %s is expired and '
            'will not be rendered.', 'expired_test2', 'test-filter')
Ejemplo n.º 5
0
 def testNextTerm(self):
     acl = packetfilter.PacketFilter(
         policy.ParsePolicy(GOOD_HEADER + NEXT_TERM, self.naming), EXP_INFO)
     result = str(acl)
     self.failUnless('# term next' in result,
                     'did not find comment for next')
     self.failUnless(
         'pass from { any } to { any } flags S/SA keep state\n' in result,
         'did not find actual term for next-term')
Ejemplo n.º 6
0
    def testExpiringTerm(self, mock_info):
        exp_date = datetime.date.today() + datetime.timedelta(weeks=EXP_INFO)
        packetfilter.PacketFilter(
            policy.ParsePolicy(
                GOOD_HEADER + EXPIRING_TERM % exp_date.strftime('%Y-%m-%d'),
                self.naming), EXP_INFO)

        mock_info.assert_called_once_with(
            'INFO: Term %s in policy %s expires in '
            'less than two weeks.', 'is_expiring', 'test-filter')
Ejemplo n.º 7
0
 def testIcmpv6(self):
     acl = packetfilter.PacketFilter(
         policy.ParsePolicy(GOOD_HEADER + GOOD_TERM_ICMPV6, self.naming),
         EXP_INFO)
     result = str(acl)
     self.failUnless('# term good-term-icmpv6' in result,
                     'did not find comment for good-term-icmpv6')
     self.failUnless(
         'pass quick proto { ipv6-icmp } from { any } to { any } keep state\n'
         in result, 'did not find actual term for good-term-icmpv6')
Ejemplo n.º 8
0
 def testMultiprotocol(self):
     acl = packetfilter.PacketFilter(
         policy.ParsePolicy(GOOD_HEADER + MULTIPLE_PROTOCOLS_TERM,
                            self.naming), EXP_INFO)
     result = str(acl)
     self.failUnless('# term multi-proto' in result,
                     'did not find comment for multi-proto')
     self.failUnless(
         'pass quick proto { tcp udp icmp } from { any } to { any } keep state\n'
         in result, 'did not find actual term for multi-proto')
Ejemplo n.º 9
0
 def testStatelessEstablished(self):
     acl = packetfilter.PacketFilter(
         policy.ParsePolicy(GOOD_HEADER_STATELESS + TCP_STATE_TERM,
                            self.naming), EXP_INFO)
     result = str(acl)
     self.failUnless('# term tcp-established-only' in result,
                     'did not find comment for tcp-established-only')
     self.failUnless(
         'pass quick proto { tcp } from { any } to { any } flags A/A no state'
         in result, 'did not find actual term for tcp-established-only')
Ejemplo n.º 10
0
 def testTcpEstablished(self):
     acl = packetfilter.PacketFilter(
         policy.ParsePolicy(GOOD_HEADER + TCP_GOOD_ESTABLISHED_TERM,
                            self.naming), EXP_INFO)
     result = str(acl)
     self.failUnless('# term tcp-established-good' in result,
                     'did not find comment for tcp-established-good')
     self.failUnless(
         'pass quick proto { tcp } from { any } to { any } flags A/A keep state'
         in result, 'did not find actual term for udp-established')
Ejemplo n.º 11
0
 def testStatefulBlock(self):
     acl = packetfilter.PacketFilter(
         policy.ParsePolicy(GOOD_HEADER + DENY_TERM_TCP, self.naming),
         EXP_INFO)
     result = str(acl)
     self.failUnless('# term deny-term-tcp' in result,
                     'did not find comment for udp-established')
     self.failUnless(
         'block drop quick proto { tcp } from { any } to { any } flags S/SA'
         in result, 'did not find actual term for deny-term-tcp')
Ejemplo n.º 12
0
 def testMultilineComment(self):
     acl = packetfilter.PacketFilter(
         policy.ParsePolicy(GOOD_HEADER + MULTILINE_COMMENT, self.naming),
         EXP_INFO)
     result = str(acl)
     self.failUnless('# term multiline-comment' in result,
                     'did not find comment for multiline-comment')
     self.failUnless(
         '# This is a\n# multiline comment' in result,
         'did not find multiline comment for multiline-comment')
Ejemplo n.º 13
0
 def testFlags(self):
     acl = packetfilter.PacketFilter(
         policy.ParsePolicy(GOOD_HEADER + FLAGS_TERM, self.naming),
         EXP_INFO)
     result = str(acl)
     self.failUnless('# term flags' in result,
                     'did not find comment for flags')
     self.failUnless(
         'pass quick proto { tcp } from { any } to { any } '
         'flags SF/SF' in result, 'did not find actual term for flags')
Ejemplo n.º 14
0
 def testUdpStatelessEstablished(self):
     acl = packetfilter.PacketFilter(
         policy.ParsePolicy(GOOD_HEADER_STATELESS + UDP_ESTABLISHED_TERM,
                            self.naming), EXP_INFO)
     result = str(acl)
     self.failUnless('# term udp-established' in result,
                     'did not find comment for udp-established')
     self.failUnless(
         'pass quick proto { udp } from { any } to { any } no state'
         in result, 'did not find actual term for udp-established')
Ejemplo n.º 15
0
 def testExpiredTerm2(self):
     self.mox.StubOutWithMock(packetfilter.logging, 'warn')
     # create mock to ensure we warn about expired terms being skipped
     packetfilter.logging.warn(
         'WARNING: Term %s in policy %s is expired and '
         'will not be rendered.', 'expired_test2', 'test-filter')
     self.mox.ReplayAll()
     packetfilter.PacketFilter(
         policy.ParsePolicy(GOOD_HEADER + EXPIRED_TERM2, self.naming),
         EXP_INFO)
Ejemplo n.º 16
0
 def testInet6(self):
     acl = packetfilter.PacketFilter(
         policy.ParsePolicy(GOOD_HEADER_INET6 + GOOD_TERM_LOG, self.naming),
         EXP_INFO)
     result = str(acl)
     self.failUnless('# term good-term-log' in result,
                     'did not find comment for good-term-log')
     self.failUnless(
         'pass quick log inet6 proto { tcp } from { any } to { any } flags S/SA '
         'keep state\n' in result,
         'did not find actual term for good-term-log')
Ejemplo n.º 17
0
 def testIcmpTypes(self):
     acl = packetfilter.PacketFilter(
         policy.ParsePolicy(GOOD_HEADER + GOOD_TERM_ICMP_TYPES,
                            self.naming), EXP_INFO)
     result = str(acl)
     self.failUnless('# term good-term-icmp-types' in result,
                     'did not find comment for good-term-icmp-types')
     self.failUnless(
         'block drop quick proto { icmp } from { any } to { any } '
         'icmp-type { 0, 3, 11 }' in result,
         'did not find actual term for good-term-icmp-types')
Ejemplo n.º 18
0
 def testNextLogTerm(self):
     self.mox.ReplayAll()
     acl = packetfilter.PacketFilter(
         policy.ParsePolicy(GOOD_HEADER + NEXT_LOG_TERM, self.naming),
         EXP_INFO)
     result = str(acl)
     self.failUnless('# term next-log' in result,
                     'did not find comment for next-log')
     self.failUnless(
         'pass log from { any } to { any } flags S/SA keep state\n'
         in result, 'did not find actual term for next-log-term')
Ejemplo n.º 19
0
 def testExpiringTerm(self):
     self.mox.StubOutWithMock(packetfilter.logging, 'info')
     # create mock to ensure we inform about expiring terms
     packetfilter.logging.info(
         'INFO: Term %s in policy %s expires in '
         'less than two weeks.', 'is_expiring', 'test-filter')
     self.mox.ReplayAll()
     exp_date = datetime.date.today() + datetime.timedelta(weeks=EXP_INFO)
     packetfilter.PacketFilter(
         policy.ParsePolicy(
             GOOD_HEADER + EXPIRING_TERM % exp_date.strftime('%Y-%m-%d'),
             self.naming), EXP_INFO)
Ejemplo n.º 20
0
 def testMultipleHeader(self):
     acl = packetfilter.PacketFilter(
         policy.ParsePolicy(
             GOOD_HEADER_STATELESS + GOOD_TERM_LOG + GOOD_HEADER_INET6 +
             GOOD_TERM_ICMP, self.naming), EXP_INFO)
     result = str(acl)
     self.failUnless(
         'pass quick log proto { tcp } from { any } to { any } no state'
         in result, 'did not find actual term for good-term-log')
     self.failUnless(
         'pass quick inet6 proto { icmp } from { any } to { any } no state'
         in result, 'did not find actual term for good-term-icmp')
Ejemplo n.º 21
0
    def testBuildTokens(self):
        ip = nacaddr.IP('10.0.0.0/8')
        ip.parent_token = 'PROD_NETWORK'
        self.naming.GetNetAddr.return_value = [ip]
        self.naming.GetServiceByProto.return_value = ['25']

        pol1 = packetfilter.PacketFilter(
            policy.ParsePolicy(GOOD_HEADER + GOOD_TERM_TCP, self.naming),
            EXP_INFO)
        st, sst = pol1._BuildTokens()
        self.assertEquals(st, SUPPORTED_TOKENS)
        self.assertEquals(sst, SUPPORTED_SUB_TOKENS)
Ejemplo n.º 22
0
 def testPortRange(self):
     self.naming.GetServiceByProto('HIGH_PORTS',
                                   'tcp').AndReturn(['12345-12354'])
     self.mox.ReplayAll()
     acl = packetfilter.PacketFilter(
         policy.ParsePolicy(GOOD_HEADER + PORTRANGE_TERM, self.naming),
         EXP_INFO)
     result = str(acl)
     self.failUnless('# term portrange' in result,
                     'did not find comment for portrange')
     self.failUnless(
         'pass quick proto { tcp } from { any } to { any } '
         'port { 12345:12354 }' in result,
         'did not find actual term for portrange')
Ejemplo n.º 23
0
 def testDirectional(self):
     ip = nacaddr.IP('10.0.0.0/8')
     ip.parent_token = 'PROD_NETWORK'
     self.naming.GetNetAddr('PROD_NETWORK').AndReturn([ip])
     self.naming.GetServiceByProto('SMTP', 'tcp').AndReturn(['25'])
     self.mox.ReplayAll()
     acl = packetfilter.PacketFilter(
         policy.ParsePolicy(GOOD_HEADER_DIRECTIONAL + GOOD_TERM_TCP,
                            self.naming), EXP_INFO)
     result = str(acl)
     self.failUnless('# term good-term-tcp' in result,
                     'did not find comment for good-term-tcp')
     self.failUnless(
         'pass out quick proto { tcp } from { any } to { <PROD_NETWORK> } port '
         '{ 25 }' in result, 'did not find actual term for good-term-tcp')
Ejemplo n.º 24
0
    def testPortRange(self):
        self.naming.GetServiceByProto.return_value = ['12345-12354']

        acl = packetfilter.PacketFilter(
            policy.ParsePolicy(GOOD_HEADER + PORTRANGE_TERM, self.naming),
            EXP_INFO)
        result = str(acl)
        self.failUnless('# term portrange' in result,
                        'did not find comment for portrange')
        self.failUnless(
            'pass quick proto { tcp } from { any } to { any } '
            'port { 12345:12354 }' in result,
            'did not find actual term for portrange')

        self.naming.GetServiceByProto.assert_called_once_with(
            'HIGH_PORTS', 'tcp')
Ejemplo n.º 25
0
    def testTcp(self):
        ip = nacaddr.IP('10.0.0.0/8')
        ip.parent_token = 'PROD_NETWORK'
        self.naming.GetNetAddr.return_value = [ip]
        self.naming.GetServiceByProto.return_value = ['25']

        acl = packetfilter.PacketFilter(
            policy.ParsePolicy(GOOD_HEADER + GOOD_TERM_TCP, self.naming),
            EXP_INFO)
        result = str(acl)
        self.failUnless('# term good-term-tcp' in result,
                        'did not find comment for good-term-tcp')
        self.failUnless(
            'pass quick proto { tcp } from { any } to { <PROD_NETWORK> } port '
            '{ 25 }' in result, 'did not find actual term for good-term-tcp')

        self.naming.GetNetAddr.assert_called_once_with('PROD_NETWORK')
        self.naming.GetServiceByProto.assert_called_once_with('SMTP', 'tcp')
Ejemplo n.º 26
0
 def testTableNameShortened(self):
     prod_network = nacaddr.IP('10.0.0.0/8')
     prod_network.parent_token = 'PROD_NETWORK_EXTREAMLY_LONG_VERY_NO_GOOD_NAME'
     self.naming.GetNetAddr(
         'PROD_NETWORK_EXTREAMLY_LONG_VERY_NO_GOOD_NAME').AndReturn(
             [prod_network])
     self.naming.GetServiceByProto('SMTP', 'tcp').AndReturn(['25'])
     self.mox.ReplayAll()
     acl = packetfilter.PacketFilter(
         policy.ParsePolicy(GOOD_HEADER_DIRECTIONAL + LONG_NAME_TERM,
                            self.naming), EXP_INFO)
     result = str(acl)
     self.failUnless(
         'table <PROD_NETWORK_EXTREAMLY_LONG_VER> {10.0.0.0/8}' in result,
         'did not find shortened name in header.')
     self.failUnless(
         'pass out quick proto { tcp } from { any } to '
         '{ <PROD_NETWORK_EXTREAMLY_LONG_VER> } '
         'port { 25 } flags S/SA keep state' in result,
         'did not find actual term for multiple-name')
Ejemplo n.º 27
0
def RenderFile(input_file, output_directory, definitions, exp_info,
               write_files):
    """Render a single file.

  Args:
    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.
    write_files: a list of file tuples, (output_file, acl_text), to write
  """
    logging.debug('rendering file: %s into %s', input_file, output_directory)
    pol = None
    jcl = False
    acl = False
    asacl = False
    aacl = False
    bacl = False
    eacl = False
    gcefw = False
    ips = False
    ipt = False
    spd = False
    nsx = False
    pcap_accept = False
    pcap_deny = False
    pf = False
    srx = False
    jsl = False
    nft = False
    win_afw = False
    xacl = False
    paloalto = False

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

    try:
        pol = policy.ParsePolicy(conf,
                                 definitions,
                                 optimize=FLAGS.optimize,
                                 base_dir=FLAGS.base_directory,
                                 shade_check=FLAGS.shade_check)
    except policy.ShadingError as e:
        logging.warn('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 'aruba' in platforms:
        aacl = copy.deepcopy(pol)
    if 'ipset' in platforms:
        ips = copy.deepcopy(pol)
    if 'iptables' in platforms:
        ipt = copy.deepcopy(pol)
    if 'nsxv' in platforms:
        nsx = 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 'ciscoxr' in platforms:
        xacl = copy.deepcopy(pol)
    if 'nftables' in platforms:
        nft = copy.deepcopy(pol)
    if 'gce' in platforms:
        gcefw = copy.deepcopy(pol)
    if 'paloalto' in platforms:
        paloalto = copy.deepcopy(pol)

    if not output_directory.endswith('/'):
        output_directory += '/'

    try:
        if jcl:
            acl_obj = juniper.Juniper(jcl, 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(acl, 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 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 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 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 paloalto:
            acl_obj = paloaltofw.PaloAltoFW(paloalto, exp_info)
            RenderACL(str(acl_obj), acl_obj.SUFFIX, output_directory,
                      input_file, write_files)
    # TODO(robankeny) add additional errors.
    except (juniper.Error, junipersrx.Error, cisco.Error, ipset.Error,
            iptables.Error, speedway.Error, pcap.Error, aclgenerator.Error,
            aruba.Error, nftables.Error, gce.Error):
        raise ACLGeneratorError(
            'Error generating target ACL for %s:\n%s%s' %
            (input_file, sys.exc_info()[0], sys.exc_info()[1]))
Ejemplo n.º 28
0
def render_filters(source_file, definitions_obj, shade_check, exp_info):
    count = 0
    [(eacl, aacl, bacl, jcl, acl, asa, xacl, ipt, ips, pf, spd, spk, srx, dem)
     ] = [(False, False, False, False, False, False, False, False, False,
           False, False, False, False, False)]

    pol = policy.ParsePolicy(open(source_file).read(),
                             definitions_obj,
                             shade_check=shade_check)

    for header in pol.headers:
        if 'arista' in header.platforms:
            eacl = copy.deepcopy(pol)
        if 'aruba' in header.platforms:
            aacl = copy.deepcopy(pol)
        if 'brocade' in header.platforms:
            bacl = copy.deepcopy(pol)
        if 'juniper' in header.platforms:
            jcl = copy.deepcopy(pol)
        if 'cisco' in header.platforms:
            acl = copy.deepcopy(pol)
        if 'ciscoasa' in header.platforms:
            asa = copy.deepcopy(pol)
        if 'ciscoxr' in header.platforms:
            xacl = copy.deepcopy(pol)
        if 'iptables' in header.platforms:
            ipt = copy.deepcopy(pol)
        if 'ipset' in header.platforms:
            ips = copy.deepcopy(pol)
        if 'packetfilter' in header.platforms:
            pf = copy.deepcopy(pol)
        if 'speedway' in header.platforms:
            spd = copy.deepcopy(pol)
        # SRX needs to be un-optimized for correct building of the address book
        # entries.
        if 'srx' in header.platforms:
            unoptimized_pol = policy.ParsePolicy(open(source_file).read(),
                                                 definitions_obj,
                                                 optimize=False)
            srx = copy.deepcopy(unoptimized_pol)
        if 'demo' in header.platforms:
            dem = copy.deepcopy(pol)
    if eacl:
        fw = arista.Arista(eacl, exp_info)
        do_output_filter(str(fw), filter_name(source_file, fw._SUFFIX))
        count += 1
    if aacl:
        fw = aruba.Aruba(aacl, exp_info)
        do_output_filter(str(fw), filter_name(source_file, fw._SUFFIX))
        count += 1
    if bacl:
        fw = brocade.Brocade(bacl, exp_info)
        do_output_filter(str(fw), filter_name(source_file, fw._SUFFIX))
        count += 1
    if jcl:
        fw = juniper.Juniper(jcl, exp_info)
        do_output_filter(str(fw), filter_name(source_file, fw._SUFFIX))
        count += 1
    if acl:
        fw = cisco.Cisco(acl, exp_info)
        do_output_filter(str(fw), filter_name(source_file, fw._SUFFIX))
        count += 1
    if asa:
        fw = ciscoasa.CiscoASA(asa, exp_info)
        do_output_filter(str(fw), filter_name(source_file, fw._SUFFIX))
        count += 1
    if xacl:
        fw = ciscoxr.CiscoXR(xacl, exp_info)
        do_output_filter(str(fw), filter_name(source_file, fw._SUFFIX))
        count += 1
    if ipt:
        fw = iptables.Iptables(ipt, exp_info)
        do_output_filter(str(fw), filter_name(source_file, fw._SUFFIX))
        count += 1
    if ips:
        fw = ipset.Ipset(ips, exp_info)
        do_output_filter(str(fw), filter_name(source_file, fw._SUFFIX))
        count += 1
    if pf:
        fw = packetfilter.PacketFilter(pf, exp_info)
        do_output_filter(str(fw), filter_name(source_file, fw._SUFFIX))
        count += 1
    if spd:
        fw = speedway.Speedway(spd, exp_info)
        do_output_filter(str(fw), filter_name(source_file, fw._SUFFIX))
        count += 1
    if srx:
        fw = junipersrx.JuniperSRX(srx, exp_info)
        do_output_filter(str(fw), filter_name(source_file, fw._SUFFIX))
        count += 1
    if dem:
        fw = demo.Demo(dem, exp_info)
        do_output_filter(str(fw), filter_name(source_file, fw._SUFFIX))
        count += 1

    return count
Ejemplo n.º 29
0
 def testBadProtoError(self):
     self.mox.ReplayAll()
     acl = packetfilter.PacketFilter(
         policy.ParsePolicy(GOOD_HEADER + BAD_PROTO_TERM, self.naming),
         EXP_INFO)
     self.assertRaises(packetfilter.UnsupportedProtoError, str, acl)
Ejemplo n.º 30
0
 def testBadFlags(self):
     self.mox.ReplayAll()
     acl = packetfilter.PacketFilter(
         policy.ParsePolicy(GOOD_HEADER + TCP_BAD_ESTABLISHED_TERM,
                            self.naming), EXP_INFO)
     self.assertRaises(aclgenerator.UnsupportedFilterError, str, acl)