Skip to content

Commit c4fa132

Browse files
authored
Merge pull request #17 from ShibalovRoman/automation
SmartMove adding an option to run from the CLI
2 parents ac85c46 + 7256b71 commit c4fa132

File tree

18 files changed

+1171
-345
lines changed

18 files changed

+1171
-345
lines changed

CiscoMigration/CiscoCommands.cs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,6 @@ private void ParseNetworks()
438438
{
439439
ConversionIncidentType = ConversionIncidentType.Informative;
440440
ConversionIncidentMessage = "An Object (network) can only hold one fqdn, host, range or subnet";
441-
Console.WriteLine(ConversionIncidentMessage);
442441
}
443442
}
444443

@@ -502,7 +501,6 @@ private void ParseServices()
502501
{
503502
ConversionIncidentType = ConversionIncidentType.ManualActionRequired;
504503
ConversionIncidentMessage = "Unrecognized service protocol (" + ServiceProtocol + ")";
505-
Console.WriteLine(ConversionIncidentMessage);
506504
}
507505
break;
508506
}
@@ -513,7 +511,6 @@ private void ParseServices()
513511
{
514512
ConversionIncidentType = ConversionIncidentType.Informative;
515513
ConversionIncidentMessage = "An Object (service) can only hold one service";
516-
Console.WriteLine(ConversionIncidentMessage);
517514
}
518515
}
519516
}
@@ -546,7 +543,6 @@ public override void Parse(CiscoCommand command, CiscoCommand prevCommand, Dicti
546543
{
547544
ConversionIncidentType = ConversionIncidentType.ManualActionRequired;
548545
ConversionIncidentMessage = "Invalid host IP address (" + HostAddress + "). Using IP 1.1.1.1.";
549-
Console.WriteLine(ConversionIncidentMessage);
550546

551547
HostAddress = "1.1.1.1";
552548
}
@@ -578,7 +574,6 @@ public override void Parse(CiscoCommand command, CiscoCommand prevCommand, Dicti
578574
{
579575
ConversionIncidentType = ConversionIncidentType.ManualActionRequired;
580576
ConversionIncidentMessage = "Invalid IP subnet (" + Network + "/" + Netmask + "). Using IP subnet 1.1.1.0/255.255.255.0.";
581-
Console.WriteLine(ConversionIncidentMessage);
582577

583578
Network = "1.1.1.0";
584579
Netmask = "255.255.255.0";
@@ -602,7 +597,6 @@ public override void Parse(CiscoCommand command, CiscoCommand prevCommand, Dicti
602597
{
603598
ConversionIncidentType = ConversionIncidentType.ManualActionRequired;
604599
ConversionIncidentMessage = "Invalid range starting IP address (" + RangeFrom + "). Using IP 0.0.0.0.";
605-
Console.WriteLine(ConversionIncidentMessage);
606600

607601
RangeFrom = "0.0.0.0";
608602
}
@@ -612,7 +606,6 @@ public override void Parse(CiscoCommand command, CiscoCommand prevCommand, Dicti
612606
{
613607
ConversionIncidentType = ConversionIncidentType.ManualActionRequired;
614608
ConversionIncidentMessage = "Invalid range ending IP address (" + RangeTo + "). Using IP 255.255.255.255.";
615-
Console.WriteLine(ConversionIncidentMessage);
616609

617610
RangeTo = "255.255.255.255";
618611
}
@@ -685,7 +678,6 @@ public override void Parse(CiscoCommand command, CiscoCommand prevCommand, Dicti
685678
// "service tcp source eq ssh destination eq ssh" ---> wrong!!! ---> ignore source!!!
686679
ConversionIncidentType = ConversionIncidentType.Informative;
687680
ConversionIncidentMessage = "Cannot convert a service defined as both source service and destination service. Ignoring source service.";
688-
Console.WriteLine(ConversionIncidentMessage);
689681

690682
IsDestination = true;
691683
Operator = command.GetParam(nextParamId + 1);
@@ -724,7 +716,6 @@ public override void Parse(CiscoCommand command, CiscoCommand prevCommand, Dicti
724716
{
725717
ConversionIncidentType = ConversionIncidentType.ManualActionRequired;
726718
ConversionIncidentMessage = "Unrecognized service protocol (" + Protocol + ")";
727-
Console.WriteLine(ConversionIncidentMessage);
728719
}
729720
break;
730721
}
@@ -767,7 +758,6 @@ public override void Parse(CiscoCommand command, CiscoCommand prevCommand, Dicti
767758
{
768759
ConversionIncidentType = ConversionIncidentType.ManualActionRequired;
769760
ConversionIncidentMessage = "Invalid IP address (" + IpAddress + "). Using IP 1.1.1.1.";
770-
Console.WriteLine(ConversionIncidentMessage);
771761

772762
IpAddress = "1.1.1.1";
773763
}
@@ -796,7 +786,6 @@ public override void Parse(CiscoCommand command, CiscoCommand prevCommand, Dicti
796786
{
797787
ConversionIncidentType = ConversionIncidentType.ManualActionRequired;
798788
ConversionIncidentMessage = "Invalid IP subnet (" + IpAddress + "/" + Netmask + "). Using IP subnet 1.1.1.0/255.255.255.0.";
799-
Console.WriteLine(ConversionIncidentMessage);
800789

801790
IpAddress = "1.1.1.0";
802791
Netmask = "255.255.255.0";
@@ -921,7 +910,6 @@ public override void Parse(CiscoCommand command, CiscoCommand prevCommand, Dicti
921910
// "service-object tcp source eq ssh destination eq ssh" ---> wrong!!! ---> ignore source!!!
922911
ConversionIncidentType = ConversionIncidentType.Informative;
923912
ConversionIncidentMessage = "Cannot convert a service defined as both source service and destination service. Ignoring source service.";
924-
Console.WriteLine(ConversionIncidentMessage);
925913

926914
IsDestination = true;
927915
Operator = command.GetParam(nextParamId + 1);
@@ -960,7 +948,6 @@ public override void Parse(CiscoCommand command, CiscoCommand prevCommand, Dicti
960948
{
961949
ConversionIncidentType = ConversionIncidentType.ManualActionRequired;
962950
ConversionIncidentMessage = "Unrecognized service protocol (" + Protocol + ")";
963-
Console.WriteLine(ConversionIncidentMessage);
964951
}
965952
break;
966953
}
@@ -1456,7 +1443,6 @@ public override void Parse(CiscoCommand command, CiscoCommand prevCommand, Dicti
14561443
{
14571444
ConversionIncidentType = ConversionIncidentType.ManualActionRequired;
14581445
ConversionIncidentMessage = "Invalid IP subnet (" + IpAddress + "/" + Netmask + ").";
1459-
Console.WriteLine(ConversionIncidentMessage);
14601446
}
14611447
break;
14621448
}
@@ -1520,7 +1506,6 @@ public override void Parse(CiscoCommand command, CiscoCommand prevCommand, Dicti
15201506
{
15211507
ConversionIncidentType = ConversionIncidentType.ManualActionRequired;
15221508
ConversionIncidentMessage = "Cannot resolve route destination IP address (" + command.GetParam(2) + "). Using IP 1.1.1.1.";
1523-
Console.WriteLine(ConversionIncidentMessage);
15241509

15251510
DestinationIp = "1.1.1.1";
15261511
DestinationNetmask = "255.255.255.255";
@@ -1530,7 +1515,6 @@ public override void Parse(CiscoCommand command, CiscoCommand prevCommand, Dicti
15301515
{
15311516
ConversionIncidentType = ConversionIncidentType.ManualActionRequired;
15321517
ConversionIncidentMessage = "Invalid IP address (" + DestinationIp + "). Using IP 1.1.1.1.";
1533-
Console.WriteLine(ConversionIncidentMessage);
15341518

15351519
DestinationIp = "1.1.1.1";
15361520
DestinationNetmask = "255.255.255.255";
@@ -1717,7 +1701,6 @@ private void ParseObjectNatCommand(CiscoCommand command, CiscoCommand prevComman
17171701

17181702
ConversionIncidentType = ConversionIncidentType.ManualActionRequired;
17191703
ConversionIncidentMessage = "Unrecognized service protocol (" + ServiceProtocol + ")";
1720-
Console.WriteLine(ConversionIncidentMessage);
17211704
}
17221705
}
17231706
break;
@@ -1738,7 +1721,6 @@ private void ParseObjectNatCommand(CiscoCommand command, CiscoCommand prevComman
17381721
{
17391722
ConversionIncidentType = ConversionIncidentType.Informative;
17401723
ConversionIncidentMessage = "Interface fall-back for dynamic object NAT is not supported";
1741-
Console.WriteLine(ConversionIncidentMessage);
17421724
}
17431725
break;
17441726
}
@@ -1786,7 +1768,6 @@ private void ParseRegularNatCommand(CiscoCommand command, CiscoCommand prevComma
17861768
{
17871769
ConversionIncidentType = ConversionIncidentType.Informative;
17881770
ConversionIncidentMessage = "Not handling NAT with dynamic destination";
1789-
Console.WriteLine(ConversionIncidentMessage);
17901771
return;
17911772
}
17921773

@@ -1805,7 +1786,6 @@ private void ParseRegularNatCommand(CiscoCommand command, CiscoCommand prevComma
18051786
{
18061787
ConversionIncidentType = ConversionIncidentType.Informative;
18071788
ConversionIncidentMessage = "Not handling NAT with dynamic source";
1808-
Console.WriteLine(ConversionIncidentMessage);
18091789
}
18101790
}
18111791
}
@@ -1847,15 +1827,13 @@ public override void Parse(CiscoCommand command, CiscoCommand prevCommand, Dicti
18471827
break;
18481828

18491829
default:
1850-
Console.WriteLine("Error: unknown access-group traffic direction (" + command.GetParam(2) + ").");
18511830
break;
18521831
}
18531832

18541833
if (Direction != DirectionType.Inbound && Direction != DirectionType.Global)
18551834
{
18561835
ConversionIncidentType = ConversionIncidentType.Informative;
18571836
ConversionIncidentMessage = "Outbound ACLs will not be converted";
1858-
Console.WriteLine(ConversionIncidentMessage);
18591837
}
18601838
}
18611839
}
@@ -2343,7 +2321,6 @@ public override void Parse(CiscoCommand command, CiscoCommand prevCommand, Dicti
23432321
ProtocolReference = strProtocol;
23442322
ConversionIncidentType = ConversionIncidentType.ManualActionRequired;
23452323
ConversionIncidentMessage = "Unrecognized service protocol (" + strProtocol + ")";
2346-
Console.WriteLine(ConversionIncidentMessage);
23472324
}
23482325
break;
23492326
}

CiscoMigration/CiscoConverter.cs

Lines changed: 64 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ limitations under the License.
2626
using Newtonsoft.Json;
2727
using System.Diagnostics;
2828
using System.Globalization;
29+
using System.Threading;
2930

3031
namespace CiscoMigration
3132
{
@@ -92,7 +93,6 @@ public string AutoGeneratedName()
9293
return "network_" + IpAddress + "_" + MaskPrefix;
9394
}
9495

95-
Console.WriteLine("Error: unrecognized network object - Ip={0}, Mask={1}", IpAddress, NetMask);
9696
return "_Err_in_network-line_" + CiscoCommandId;
9797
}
9898

@@ -106,7 +106,6 @@ public string AutoGeneratedNetworkName()
106106
return "network_" + IpAddress + "_" + MaskPrefix;
107107
}
108108

109-
Console.WriteLine("Error: unrecognized network object - Ip={0}, Mask={1}, Prefix={2}", IpAddress, NetMask, MaskPrefix);
110109
return "_Err_in_network-line_" + CiscoCommandId;
111110
}
112111

@@ -183,10 +182,6 @@ public static ProtocolType ProtocolStringToProtocolType(ref string sProtocol)
183182
protocol = ProtocolType.KnownOtherIpProtocol;
184183
sProtocol = serviceName;
185184
}
186-
else
187-
{
188-
Console.WriteLine("Error: Unrecognized service protocol '{0}'", sProtocol);
189-
}
190185
break;
191186
}
192187

@@ -221,10 +216,6 @@ public static string AutoGeneratedName(CheckPointObjectsRepository cpObjects, st
221216
break;
222217

223218
default:
224-
if (!string.IsNullOrEmpty(sPortOperator))
225-
{
226-
Console.WriteLine("Error: unsupported port operator '{0}'", sPortOperator);
227-
}
228219
break;
229220
}
230221

@@ -307,7 +298,6 @@ public static string AutoGeneratedName(CheckPointObjectsRepository cpObjects, Pr
307298
}
308299
else
309300
{
310-
Console.WriteLine("Error: unsupported port operator '{0}' for protocol IP", portOperator);
311301
name = "_Err_in_service-line_" + ciscoCommandId;
312302
}
313303
return name;
@@ -318,7 +308,6 @@ public static string AutoGeneratedName(CheckPointObjectsRepository cpObjects, Pr
318308
return serviceFound ? name : sPort;
319309

320310
case ProtocolType.NA:
321-
Console.WriteLine("Error: Unrecognized service protocol");
322311
return "_Err_in_service-line_" + ciscoCommandId;
323312
}
324313

@@ -427,10 +416,6 @@ public static CheckPointObject CreateServiceObj(CheckPointObjectsRepository cpOb
427416
break;
428417

429418
case ProtocolType.Ip:
430-
if (portOperator != TcpUdpPortOperatorType.All)
431-
{
432-
Console.WriteLine("Error: Service protocol is 'IP', but the service is not 'any'");
433-
}
434419
// Skip, a predefined "any" object is used!!!
435420
break;
436421

@@ -448,7 +433,6 @@ public static CheckPointObject CreateServiceObj(CheckPointObjectsRepository cpOb
448433
break;
449434

450435
case ProtocolType.NA:
451-
Console.WriteLine("Error: Service protocol is 'NA'");
452436
break;
453437
}
454438

@@ -522,6 +506,7 @@ private static void GetServicePortRanges(string servicePort, out int from, out i
522506
private IList<CiscoCommand> _ciscoSshCommands;
523507
private Cisco_Hostname _ciscoHostnameCommand;
524508
private List<Cisco_AccessList> _ciscoGlobalAclCommands = new List<Cisco_AccessList>();
509+
private string _outputFormat;
525510

526511
private List<CheckPoint_NAT_Rule> _cpPreorderedNatRules = new List<CheckPoint_NAT_Rule>();
527512

@@ -4522,10 +4507,7 @@ private bool IsFirewallRuleMatchedByNATRule(CheckPoint_Zone parentLayerRuleZone,
45224507
return true;
45234508
}
45244509

4525-
if (fwRule.Service.Count == 0)
4526-
{
4527-
// TODO: ???
4528-
}
4510+
if (fwRule.Service.Count == 0) {}
45294511
else if (fwRule.Service.Count == 1 && fwRule.Service[0].Name == CheckPointObject.Any)
45304512
{
45314513
// There is only one service in FW rule and it is "any", no matter what NAT rule service is...
@@ -4683,19 +4665,29 @@ private string BuildInspectedRuleLinkTag(string ciscoClassMapName)
46834665

46844666
#region Public Methods
46854667

4686-
public override void Initialize(VendorParser vendorParser, string vendorFilePath, string toolVersion, string targetFolder, string domainName)
4668+
public override void Initialize(VendorParser vendorParser, string vendorFilePath, string toolVersion, string targetFolder, string domainName, string outputFormat = "json")
46874669
{
46884670
_ciscoParser = (CiscoParser)vendorParser;
46894671
if (_ciscoParser == null)
46904672
{
46914673
throw new InvalidDataException("Unexpected!!!");
46924674
}
4675+
this._outputFormat = outputFormat;
46934676

4694-
base.Initialize(vendorParser, vendorFilePath, toolVersion, targetFolder, domainName);
4677+
base.Initialize(vendorParser, vendorFilePath, toolVersion, targetFolder, domainName, outputFormat);
46954678
}
46964679

4697-
public override void Convert(bool convertNat)
4680+
public override Dictionary<string, int> Convert(bool convertNat)
46984681
{
4682+
if (IsConsoleRunning)
4683+
Progress = new ProgressBar();
4684+
4685+
if (IsConsoleRunning)
4686+
{
4687+
Console.WriteLine("Converting obects ...");
4688+
Progress.SetProgress(20);
4689+
Thread.Sleep(1000);
4690+
}
46994691
RaiseConversionProgress(20, "Converting obects ...");
47004692
_cpObjects.Initialize(); // must be first!!!
47014693

@@ -4715,25 +4707,65 @@ public override void Convert(bool convertNat)
47154707
Add_or_Modify_InterfaceNetworkGroups();
47164708
Add_ServicesAndServiceGroups();
47174709
Add_TimeRanges();
4710+
4711+
if (IsConsoleRunning)
4712+
{
4713+
Console.WriteLine("Converting rules ...");
4714+
Progress.SetProgress(30);
4715+
Thread.Sleep(1000);
4716+
}
47184717
RaiseConversionProgress(30, "Converting rules ...");
47194718
Add_Package();
47204719

47214720
if (convertNat)
47224721
{
4722+
if (IsConsoleRunning)
4723+
{
4724+
Console.WriteLine("Converting NAT rules ...");
4725+
Progress.SetProgress(40);
4726+
Thread.Sleep(1000);
4727+
}
47234728
RaiseConversionProgress(40, "Converting NAT rules ...");
47244729
Add_object_NAT();
47254730
Add_NAT_Rules();
4731+
4732+
if (IsConsoleRunning)
4733+
{
4734+
Console.WriteLine("Creating NAT rulebase ...");
4735+
Progress.SetProgress(50);
4736+
Thread.Sleep(1000);
4737+
}
47264738
RaiseConversionProgress(50, "Creating NAT rulebase ...");
47274739
CreateNATRulebase();
4740+
4741+
if (IsConsoleRunning)
4742+
{
4743+
Console.WriteLine("Creating Firewall rulebase ...");
4744+
Progress.SetProgress(60);
4745+
Thread.Sleep(1000);
4746+
}
47284747
RaiseConversionProgress(60, "Creating Firewall rulebase ...");
47294748
MatchNATRulesIntoFirewallPolicy();
47304749
}
47314750

47324751
// This should be done here, after all objects are converted!!!
47334752
EnforceObjectNameValidity();
47344753

4754+
if (IsConsoleRunning)
4755+
{
4756+
Console.WriteLine("Optimizing Firewall rulebase ...");
4757+
Progress.SetProgress(70);
4758+
Thread.Sleep(1000);
4759+
}
47354760
RaiseConversionProgress(70, "Optimizing Firewall rulebase ...");
47364761
Add_Optimized_Package();
4762+
4763+
if (IsConsoleRunning)
4764+
{
4765+
Console.WriteLine("Generating CLI scripts ...");
4766+
Progress.SetProgress(80);
4767+
Thread.Sleep(1000);
4768+
}
47374769
RaiseConversionProgress(80, "Generating CLI scripts ...");
47384770
CreateObjectsScript();
47394771
CreatePackagesScript();
@@ -4748,6 +4780,14 @@ public override void Convert(bool convertNat)
47484780
ConversionIncidentsCommandsCount = _conversionIncidents.GroupBy(error => error.LineNumber).Count();
47494781

47504782
CreateSmartConnector();
4783+
4784+
4785+
if (IsConsoleRunning)
4786+
{
4787+
Progress.SetProgress(100);
4788+
Progress.Dispose();
4789+
}
4790+
return new Dictionary<string, int>() { { "warnings", ConversionIncidentCategoriesCount } };
47514791
}
47524792

47534793
public override int RulesInConvertedPackage()

0 commit comments

Comments
 (0)