use of org.batfish.datamodel.flow.Trace in project batfish by batfish.
the class BidirectionalTracerouteAnswererTest method testMultipath.
@Test
public void testMultipath() {
Trace forwardTrace1 = new Trace(ACCEPTED, ImmutableList.of());
Trace forwardTrace2 = new Trace(DENIED_IN, ImmutableList.of());
Trace forwardTrace3 = new Trace(DELIVERED_TO_SUBNET, ImmutableList.of());
TraceAndReverseFlow forwardTarf1 = new TraceAndReverseFlow(forwardTrace1, REVERSE_FLOW, ImmutableSet.of());
TraceAndReverseFlow forwardTarf2 = new TraceAndReverseFlow(forwardTrace2, null, ImmutableSet.of());
TraceAndReverseFlow forwardTarf3 = new TraceAndReverseFlow(forwardTrace3, REVERSE_FLOW, ImmutableSet.of());
Trace reverseTrace1 = new Trace(DENIED_IN, ImmutableList.of());
Trace reverseTrace2 = new Trace(EXITS_NETWORK, ImmutableList.of());
TraceAndReverseFlow reverseTarf1 = new TraceAndReverseFlow(reverseTrace1, null, ImmutableSet.of());
TraceAndReverseFlow reverseTarf2 = new TraceAndReverseFlow(reverseTrace2, FORWARD_FLOW, ImmutableSet.of());
TracerouteEngine tracerouteEngine = forFlows(ImmutableMap.of(FORWARD_FLOW, ImmutableList.of(forwardTarf1, forwardTarf2, forwardTarf3), REVERSE_FLOW, ImmutableList.of(reverseTarf1, reverseTarf2)));
List<BidirectionalTrace> bidirectionalTraces = computeBidirectionalTraces(ImmutableSet.of(FORWARD_FLOW), tracerouteEngine, false);
assertThat(bidirectionalTraces, contains(new BidirectionalTrace(FORWARD_FLOW, forwardTrace1, ImmutableSet.of(), REVERSE_FLOW, reverseTrace1), new BidirectionalTrace(FORWARD_FLOW, forwardTrace1, ImmutableSet.of(), REVERSE_FLOW, reverseTrace2), new BidirectionalTrace(FORWARD_FLOW, forwardTrace2, ImmutableSet.of(), null, null), new BidirectionalTrace(FORWARD_FLOW, forwardTrace3, ImmutableSet.of(), REVERSE_FLOW, reverseTrace1), new BidirectionalTrace(FORWARD_FLOW, forwardTrace3, ImmutableSet.of(), REVERSE_FLOW, reverseTrace2)));
}
use of org.batfish.datamodel.flow.Trace in project batfish by batfish.
the class BidirectionalTracerouteAnswererTest method testGroupTraces.
@Test
public void testGroupTraces() {
Trace t1 = new Trace(ACCEPTED, ImmutableList.of());
Trace t2 = new Trace(EXITS_NETWORK, ImmutableList.of());
Trace t3 = new Trace(NEIGHBOR_UNREACHABLE, ImmutableList.of());
Trace t4 = new Trace(DENIED_IN, ImmutableList.of());
FirewallSessionTraceInfo session1 = new FirewallSessionTraceInfo("session1", Accept.INSTANCE, ImmutableSet.of(), DUMMY_SESSION_FLOW, null);
FirewallSessionTraceInfo session2 = new FirewallSessionTraceInfo("session2", Accept.INSTANCE, ImmutableSet.of(), DUMMY_SESSION_FLOW, null);
{
// All BidirectionalTraces have the same key, so are in the same group.
BidirectionalTrace bt1 = new BidirectionalTrace(FORWARD_FLOW, t1, ImmutableSet.of(), REVERSE_FLOW, t3);
BidirectionalTrace bt2 = new BidirectionalTrace(FORWARD_FLOW, t1, ImmutableSet.of(), REVERSE_FLOW, t4);
BidirectionalTrace bt3 = new BidirectionalTrace(FORWARD_FLOW, t2, ImmutableSet.of(), REVERSE_FLOW, t3);
BidirectionalTrace bt4 = new BidirectionalTrace(FORWARD_FLOW, t2, ImmutableSet.of(), REVERSE_FLOW, t4);
List<BidirectionalTrace> bts = ImmutableList.of(bt1, bt2, bt3, bt4);
assertThat(groupTraces(bts), hasEntry(equalTo(Key.from(bt1)), equalTo(bts)));
}
{
// Traces with different forward flows are in different groups
BidirectionalTrace bt1 = new BidirectionalTrace(FORWARD_FLOW, t1, ImmutableSet.of(), REVERSE_FLOW, t2);
BidirectionalTrace bt2 = new BidirectionalTrace(REVERSE_FLOW, t1, ImmutableSet.of(), REVERSE_FLOW, t2);
assertThat(groupTraces(ImmutableList.of(bt1, bt2)), equalTo(ImmutableMap.of(Key.from(bt1), ImmutableList.of(bt1), Key.from(bt2), ImmutableList.of(bt2))));
}
{
// Traces with different number of sessions are in different groups
BidirectionalTrace bt1 = new BidirectionalTrace(FORWARD_FLOW, t1, ImmutableSet.of(), REVERSE_FLOW, t2);
BidirectionalTrace bt2 = new BidirectionalTrace(FORWARD_FLOW, t1, ImmutableSet.of(session1), REVERSE_FLOW, t2);
assertThat(groupTraces(ImmutableList.of(bt1, bt2)), equalTo(ImmutableMap.of(Key.from(bt1), ImmutableList.of(bt1), Key.from(bt2), ImmutableList.of(bt2))));
}
{
// Traces with different sessions are in different groups
BidirectionalTrace bt1 = new BidirectionalTrace(FORWARD_FLOW, t1, ImmutableSet.of(session1), REVERSE_FLOW, t2);
BidirectionalTrace bt2 = new BidirectionalTrace(FORWARD_FLOW, t1, ImmutableSet.of(session2), REVERSE_FLOW, t2);
assertThat(groupTraces(ImmutableList.of(bt1, bt2)), equalTo(ImmutableMap.of(Key.from(bt1), ImmutableList.of(bt1), Key.from(bt2), ImmutableList.of(bt2))));
}
{
// Traces with different reverse flows are in different groups
BidirectionalTrace bt1 = new BidirectionalTrace(FORWARD_FLOW, t1, ImmutableSet.of(), REVERSE_FLOW, t2);
BidirectionalTrace bt2 = new BidirectionalTrace(FORWARD_FLOW, t1, ImmutableSet.of(), FORWARD_FLOW, t2);
assertThat(groupTraces(ImmutableList.of(bt1, bt2)), equalTo(ImmutableMap.of(Key.from(bt1), ImmutableList.of(bt1), Key.from(bt2), ImmutableList.of(bt2))));
}
}
use of org.batfish.datamodel.flow.Trace in project batfish by batfish.
the class BidirectionalTracerouteAnswererTest method testSessions.
/**
* Make sure we don't mix traces and sessions.
*/
@Test
public void testSessions() {
Trace sessionForwardTrace = new Trace(ACCEPTED, ImmutableList.of());
Trace noSessionForwardTrace = new Trace(DELIVERED_TO_SUBNET, ImmutableList.of());
FirewallSessionTraceInfo session = new FirewallSessionTraceInfo("session", Accept.INSTANCE, ImmutableSet.of(), DUMMY_SESSION_FLOW, null);
TraceAndReverseFlow sessionForwardTarf = new TraceAndReverseFlow(sessionForwardTrace, REVERSE_FLOW, ImmutableList.of(session));
TraceAndReverseFlow noSessionForwardTarf = new TraceAndReverseFlow(noSessionForwardTrace, REVERSE_FLOW, ImmutableList.of());
Trace sessionReverseTrace = new Trace(DENIED_IN, ImmutableList.of());
Trace noSessionReverseTrace = new Trace(DENIED_OUT, ImmutableList.of());
TraceAndReverseFlow sessionReverseTarf = new TraceAndReverseFlow(sessionReverseTrace, null, ImmutableList.of());
TraceAndReverseFlow noSessionReverseTarf = new TraceAndReverseFlow(noSessionReverseTrace, null, ImmutableList.of());
TracerouteEngine tracerouteEngine = forSessions(ImmutableMap.of(ImmutableSet.of(), ImmutableMap.of(FORWARD_FLOW, ImmutableList.of(sessionForwardTarf, noSessionForwardTarf), REVERSE_FLOW, ImmutableList.of(noSessionReverseTarf)), ImmutableSet.of(session), ImmutableMap.of(REVERSE_FLOW, ImmutableList.of(sessionReverseTarf))));
List<BidirectionalTrace> bidirectionalTraces = computeBidirectionalTraces(ImmutableSet.of(FORWARD_FLOW), tracerouteEngine, false);
assertThat(bidirectionalTraces, containsInAnyOrder(new BidirectionalTrace(FORWARD_FLOW, sessionForwardTrace, ImmutableSet.of(session), REVERSE_FLOW, sessionReverseTrace), new BidirectionalTrace(FORWARD_FLOW, noSessionForwardTrace, ImmutableSet.of(), REVERSE_FLOW, noSessionReverseTrace)));
}
use of org.batfish.datamodel.flow.Trace in project batfish by batfish.
the class F5BigipStructuredGrammarTest method testDnat.
@Test
public void testDnat() throws IOException {
String snapshotName = "dnat";
String natHostname = "f5_bigip_structured_dnat";
String hostname = "host1";
String hostFilename = hostname + ".json";
Batfish batfish = BatfishTestUtils.getBatfishFromTestrigText(TestrigText.builder().setConfigurationFiles(SNAPSHOTS_PREFIX + snapshotName, natHostname).setHostsFiles(SNAPSHOTS_PREFIX + snapshotName, hostFilename).build(), _folder);
NetworkSnapshot snapshot = batfish.getSnapshot();
batfish.computeDataPlane(snapshot);
{
// DNAT modulo ARP
Flow flow = Flow.builder().setDstIp(Ip.parse("192.0.2.1")).setDstPort(80).setIngressInterface("/Common/SOME_VLAN").setIngressNode(natHostname).setIpProtocol(IpProtocol.TCP).setSrcIp(Ip.parse("8.8.8.8")).setSrcPort(50000).build();
SortedMap<Flow, List<Trace>> flowTraces = batfish.getTracerouteEngine(snapshot).computeTraces(ImmutableSet.of(flow), false);
List<Trace> traces = flowTraces.get(flow);
Optional<TransformationStepDetail> stepDetailOptional = traces.stream().map(Trace::getHops).flatMap(Collection::stream).map(Hop::getSteps).flatMap(Collection::stream).map(Step::getDetail).filter(Predicates.instanceOf(TransformationStepDetail.class)).map(TransformationStepDetail.class::cast).filter(d -> d.getTransformationType() == TransformationType.DEST_NAT).findFirst();
assertTrue("There is a DNAT transformation step.", stepDetailOptional.isPresent());
TransformationStepDetail detail = stepDetailOptional.get();
assertThat(detail.getFlowDiffs(), hasItem(equalTo(FlowDiff.flowDiff(IpField.DESTINATION, Ip.parse("192.0.2.1"), Ip.parse("192.0.2.10")))));
}
{
// DNAT with ARP
Flow flow = Flow.builder().setDstIp(Ip.parse("192.0.2.1")).setDstPort(80).setIngressNode(hostname).setIpProtocol(IpProtocol.TCP).setSrcIp(Ip.parse("192.0.2.2")).setSrcPort(50000).build();
SortedMap<Flow, List<Trace>> flowTraces = batfish.getTracerouteEngine(snapshot).computeTraces(ImmutableSet.of(flow), false);
List<Trace> traces = flowTraces.get(flow);
Optional<TransformationStepDetail> stepDetailOptional = traces.stream().map(Trace::getHops).flatMap(Collection::stream).map(Hop::getSteps).flatMap(Collection::stream).map(Step::getDetail).filter(Predicates.instanceOf(TransformationStepDetail.class)).map(TransformationStepDetail.class::cast).filter(d -> d.getTransformationType() == TransformationType.DEST_NAT).findFirst();
assertTrue("There is a DNAT transformation step.", stepDetailOptional.isPresent());
TransformationStepDetail detail = stepDetailOptional.get();
assertThat(detail.getFlowDiffs(), hasItem(equalTo(FlowDiff.flowDiff(IpField.DESTINATION, Ip.parse("192.0.2.1"), Ip.parse("192.0.2.10")))));
}
{
// bidirectional traceroute with DNAT
Flow flow = Flow.builder().setDstIp(Ip.parse("192.0.2.1")).setDstPort(80).setIngressInterface("/Common/SOME_VLAN").setIngressNode(natHostname).setIpProtocol(IpProtocol.TCP).setSrcIp(Ip.parse("8.8.8.8")).setSrcPort(50000).build();
SortedMap<Flow, List<TraceAndReverseFlow>> flowTraces = batfish.getTracerouteEngine(snapshot).computeTracesAndReverseFlows(ImmutableSet.of(flow), false);
List<TraceAndReverseFlow> traces = flowTraces.get(flow);
assertThat(traces, hasSize(1));
Flow reverseFlow = traces.get(0).getReverseFlow();
assertThat(reverseFlow, equalTo(Flow.builder().setSrcIp(Ip.parse("192.0.2.10")).setSrcPort(80).setIngressInterface("/Common/SOME_VLAN").setIngressNode(natHostname).setIpProtocol(IpProtocol.TCP).setDstIp(Ip.parse("8.8.8.8")).setDstPort(50000).build()));
Set<FirewallSessionTraceInfo> sessions = traces.get(0).getNewFirewallSessions();
SortedMap<Flow, List<TraceAndReverseFlow>> reverseFlowTraces = batfish.getTracerouteEngine(snapshot).computeTracesAndReverseFlows(ImmutableSet.of(reverseFlow), sessions, false);
Optional<TransformationStepDetail> stepDetailOptional = reverseFlowTraces.get(reverseFlow).stream().map(TraceAndReverseFlow::getTrace).map(Trace::getHops).flatMap(Collection::stream).map(Hop::getSteps).flatMap(Collection::stream).map(Step::getDetail).filter(Predicates.instanceOf(TransformationStepDetail.class)).map(TransformationStepDetail.class::cast).filter(d -> d.getTransformationType() == TransformationType.SOURCE_NAT).findFirst();
assertTrue("There is a DNAT transformation step.", stepDetailOptional.isPresent());
TransformationStepDetail detail = stepDetailOptional.get();
assertThat(detail.getFlowDiffs(), hasItem(equalTo(FlowDiff.flowDiff(IpField.SOURCE, Ip.parse("192.0.2.10"), Ip.parse("192.0.2.1")))));
}
}
use of org.batfish.datamodel.flow.Trace in project batfish by batfish.
the class F5BigipStructuredGrammarTest method testSnatBidirectionalTraceroute.
@Test
public void testSnatBidirectionalTraceroute() throws IOException {
String hostname = "f5_bigip_structured_snat";
parseConfig(hostname);
Batfish batfish = getBatfishForConfigurationNames(hostname);
NetworkSnapshot snapshot = batfish.getSnapshot();
batfish.computeDataPlane(snapshot);
// SNAT via snat /Common/snat1
Flow flow = Flow.builder().setDstIp(Ip.parse("192.0.2.1")).setDstPort(80).setIngressInterface("/Common/vlan1").setIngressNode(hostname).setIpProtocol(IpProtocol.TCP).setSrcIp(Ip.parse("8.8.8.8")).setSrcPort(50000).build();
SortedMap<Flow, List<TraceAndReverseFlow>> flowTraces = batfish.getTracerouteEngine(snapshot).computeTracesAndReverseFlows(ImmutableSet.of(flow), false);
List<TraceAndReverseFlow> traces = flowTraces.get(flow);
assertThat(traces, hasSize(1));
Flow reverseFlow = traces.get(0).getReverseFlow();
assertThat(reverseFlow, equalTo(Flow.builder().setSrcIp(Ip.parse("192.0.2.1")).setSrcPort(80).setIngressInterface("/Common/vlan1").setIngressNode(hostname).setIpProtocol(IpProtocol.TCP).setDstIp(Ip.parse("10.200.1.2")).setDstPort(1024).build()));
Set<FirewallSessionTraceInfo> sessions = traces.get(0).getNewFirewallSessions();
SortedMap<Flow, List<TraceAndReverseFlow>> reverseFlowTraces = batfish.getTracerouteEngine(snapshot).computeTracesAndReverseFlows(ImmutableSet.of(reverseFlow), sessions, false);
Optional<TransformationStepDetail> stepDetailOptional = reverseFlowTraces.get(reverseFlow).stream().map(TraceAndReverseFlow::getTrace).map(Trace::getHops).flatMap(Collection::stream).map(Hop::getSteps).flatMap(Collection::stream).map(Step::getDetail).filter(Predicates.instanceOf(TransformationStepDetail.class)).map(TransformationStepDetail.class::cast).filter(d -> d.getTransformationType() == TransformationType.DEST_NAT).findFirst();
assertTrue("There is a DNAT transformation step.", stepDetailOptional.isPresent());
TransformationStepDetail detail = stepDetailOptional.get();
assertThat(detail.getFlowDiffs(), contains(FlowDiff.flowDiff(IpField.DESTINATION, Ip.parse("10.200.1.2"), Ip.parse("8.8.8.8")), FlowDiff.flowDiff(PortField.DESTINATION, 1024, 50000)));
}
Aggregations