Exemplo n.º 1
0
        static void ProcessNetworkAclFromTemplate(dynamic input, CFStack stack)
        {
            NetworkAcl nacl = new NetworkAcl();

            nacl.LogicalId = input.Name;
            nacl.Type      = "AWS::EC2::NetworkAcl";

            var props = input.Value["Properties"];

            foreach (var prop in props)
            {
                switch ((string)prop.Name)
                {
                case "VpcId":
                    var a = prop.Value;
                    foreach (var item in a)
                    {
                        nacl.Properties.VpcId = item.Value;
                    }
                    break;

                case "Tags":

                    break;
                }
            }

            stack.Resources.Add(nacl);
        }
Exemplo n.º 2
0
        private void CompareNetworkAcl(NetworkAcl originalResource1, NetworkAcl originalResource2, NetworkAcl copyResource, CFStack copyStack)
        {
            List <NetworkAclEntry> compareList = copyResource.Properties.NetworkAclEntry;

            foreach (NetworkAclEntry x in originalResource1.Properties.NetworkAclEntry)
            {
                //Note NetworkAclId is always null for AWS stack resource
                var y = originalResource2.Properties.NetworkAclEntry.Find(n => n != null && n.RuleNumber == x.RuleNumber && n.RuleAction == x.RuleAction && n.Egress.ToString() == x.Egress.ToString() && n.CidrBlock == x.CidrBlock && n.FromPort == x.FromPort && n.ToPort == x.ToPort && n.Protocol == x.Protocol);
                if (y == null)
                {
                    if (CompareRemoves == false)
                    {
                        var z = copyResource.Properties.NetworkAclEntry.Find(n => n != null && n.RuleNumber == x.RuleNumber && n.RuleAction == x.RuleAction && n.Egress.ToString() == x.Egress.ToString() && n.CidrBlock == x.CidrBlock && n.FromPort == x.FromPort && n.ToPort == x.ToPort && n.Protocol == x.Protocol);
                        if (z != null)
                        {
                            z.StateChanged = true;
                        }
                    }
                }
                else
                {
                    if (CompareRemoves == true)
                    {
                        var z = copyResource.Properties.NetworkAclEntry.Find(n => n != null && n.RuleNumber == x.RuleNumber && n.RuleAction == x.RuleAction && n.Egress.ToString() == x.Egress.ToString() && n.CidrBlock == x.CidrBlock && n.FromPort == x.FromPort && n.ToPort == x.ToPort && n.Protocol == x.Protocol);
                        if (z != null)
                        {
                            compareList.Remove(z);
                        }
                    }
                }
            }

            if (copyResource.Properties.NetworkAclEntry.Count() == 0)
            {
                copyStack.Resources.Remove(copyResource);
            }
        }
Exemplo n.º 3
0
        private void WriteOutput(CFStack s, RichTextBox rtb, string source, string name)
        {
            rtb.Clear();

            rtb.AppendText("Source: " + source); rtb.AppendText(Environment.NewLine);
            rtb.AppendText("Name/Path: " + name); rtb.AppendText(Environment.NewLine); rtb.AppendText(Environment.NewLine);

            rtb.AppendText("Decription: " + s.Description); rtb.AppendText(Environment.NewLine);

            rtb.AppendText("Resources"); rtb.AppendText(Environment.NewLine);
            foreach (var resource in s.Resources)
            {
                rtb.AppendText(tab1); rtb.AppendText(resource.LogicalId); rtb.AppendText(Environment.NewLine);
                rtb.AppendText(tab2); rtb.AppendText("Type: " + resource.Type); rtb.AppendText(Environment.NewLine);

                var    properties = resource.Properties;
                string type       = resource.Type;
                switch (type)
                {
                case "AWS::EC2::SecurityGroup":
                    EC2SecurityGroup group = (EC2SecurityGroup)resource;
                    rtb.AppendText(tab3); rtb.AppendText("Group Description: " + group.Properties.GroupDescription); rtb.AppendText(Environment.NewLine);
                    var rules = group.Properties.SecurityGroupIngress.OrderBy(a => a.IpProtocol).ThenBy(a => a.ToPort).ThenBy(a => a.FromPort).ThenBy(a => a.CidrIp).ThenBy(a => a.SourceSecurityGroupId);
                    foreach (var rule in rules)
                    {
                        if (rule.StateChanged == false)
                        {
                            rtb.AppendText(tab4);
                        }
                        else
                        {
                            rtb.AppendText(diffMarker); rtb.AppendText(tab3);
                        }

                        //Check if Protocol exists in Dictionary else use the number
                        if (Protocols.ContainsKey(rule.IpProtocol))
                        {
                            rtb.AppendText("Protocol: " + Protocols[rule.IpProtocol] + " (" + rule.IpProtocol + ") | ");
                        }
                        else
                        {
                            rtb.AppendText("Protocol: " + "Custom" + " (" + rule.IpProtocol + ") | ");
                        }


                        if (rule.FromPort.Equals(rule.ToPort, StringComparison.Ordinal))
                        {
                            rtb.AppendText("Port Range: " + rule.FromPort + " | ");
                        }
                        else
                        {
                            rtb.AppendText("Port Range: " + rule.FromPort + "-" + rule.ToPort + " | ");
                        }


                        if (rule.CidrIp != null)
                        {
                            rtb.AppendText("Source: " + rule.CidrIp);
                        }
                        else
                        {
                            rtb.AppendText("Source: " + rule.SourceSecurityGroupId);
                        }

                        rtb.AppendText(Environment.NewLine);
                    }
                    break;

                case "AWS::EC2::NetworkAcl":
                    NetworkAcl acl = (NetworkAcl)resource;
                    rtb.AppendText(tab3); rtb.AppendText("VpcId: " + acl.Properties.VpcId); rtb.AppendText(Environment.NewLine);
                    var  aclEntry         = acl.Properties.NetworkAclEntry.OrderBy(a => a.Egress).ThenBy(a => a.RuleNumber);
                    bool egressDisplayed  = false;
                    bool ingressDisplayed = false;
                    foreach (var rule in aclEntry)
                    {
                        //Rule #, Type, Protocol, Port Range, Source, Allow/Deny
                        if (rule.Egress == false && ingressDisplayed == false)
                        {
                            rtb.AppendText(tab4); rtb.AppendText("Inbound Rules"); rtb.AppendText(Environment.NewLine);
                            ingressDisplayed = true;
                        }
                        else if (rule.Egress == true && egressDisplayed == false)
                        {
                            rtb.AppendText(tab4); rtb.AppendText("Outbound Rules"); rtb.AppendText(Environment.NewLine);
                            egressDisplayed = true;
                        }

                        if (rule.StateChanged == false)
                        {
                            rtb.AppendText(tab5);
                        }
                        else
                        {
                            rtb.AppendText(diffMarker); rtb.AppendText(tab4);
                        }

                        rtb.AppendText("Rule: " + rule.RuleNumber + " | ");

                        //Check if Protocol exists in Dictionary else use the number
                        if (Protocols.ContainsKey(rule.Protocol))
                        {
                            rtb.AppendText("Protocol: " + Protocols[rule.Protocol] + " (" + rule.Protocol + ") | ");
                        }
                        else
                        {
                            rtb.AppendText("Protocol: " + "Custom" + " (" + rule.Protocol + ") | ");
                        }

                        //ALL ports could be 0-0 or -1 or 0-65535
                        if (rule.FromPort.Equals(rule.ToPort, StringComparison.Ordinal))
                        {
                            if ((rule.FromPort == "0" && rule.ToPort == "0") || (rule.FromPort == "-1" && rule.ToPort == "-1"))
                            {
                                rtb.AppendText("Port Range: ALL | ");
                            }
                            else
                            {
                                rtb.AppendText("Port Range: " + rule.FromPort + " | ");
                            }
                        }
                        else
                        {
                            if (rule.FromPort == "0" && rule.ToPort == "65535")
                            {
                                rtb.AppendText("Port Range: ALL | ");
                            }
                            else
                            {
                                rtb.AppendText("Port Range: " + rule.FromPort + "-" + rule.ToPort + " | ");
                            }
                        }
                        rtb.AppendText("Source: " + rule.CidrBlock + " | ");
                        rtb.AppendText("Allow/Deny: " + rule.RuleAction.ToUpper());
                        rtb.AppendText(Environment.NewLine);
                    }
                    break;
                }

                rtb.AppendText(Environment.NewLine);
            }

            // Highlight Textbox Lines
            for (int i = 0; i < rtb.Lines.Length; i++)
            {
                if (rtb.Lines[i].Contains("*"))
                {
                    int selectionStart = rtb.GetFirstCharIndexFromLine(i);
                    int selectionEnd   = rtb.Lines[i].Count();
                    rtb.SelectionStart     = selectionStart;
                    rtb.SelectionLength    = selectionEnd;
                    rtb.SelectionBackColor = System.Drawing.Color.Yellow;
                }
            }
        }
Exemplo n.º 4
0
        static void ProcessNetworkAclEntryFromTemplate(dynamic input, CFStack stack)
        {
            NetworkAclEntry ne = new NetworkAclEntry();

            var rule = input.Value["Properties"];

            ne.Protocol   = rule.Protocol;
            ne.CidrBlock  = rule.CidrBlock;
            ne.Egress     = (bool)rule.Egress;
            ne.RuleNumber = rule.RuleNumber;
            ne.RuleAction = rule.RuleAction;


            if (rule.NetworkAclId != null)
            {
                var a = rule.NetworkAclId;
                foreach (var item in a)
                {
                    ne.NetworkAclId = item.Value;
                }
            }

            if (rule.PortRange != null)
            {
                var range = rule.PortRange;
                foreach (var item in range)
                {
                    if (item.Name == "To")
                    {
                        if (item.Value == "-1")
                        {
                            ne.ToPort = "ALL";
                        }
                        else
                        {
                            ne.ToPort = item.Value;
                        }
                    }
                    if (item.Name == "From")
                    {
                        if (item.Value == "-1")
                        {
                            ne.ToPort = "ALL";
                        }
                        else
                        {
                            ne.FromPort = item.Value;
                        }
                    }
                }
            }
            else //If port range is not specified in the template then AWS sets it to ALL
            {
                ne.FromPort = "ALL";
                ne.ToPort   = "ALL";
            }

            //Format PortRange
            string from = "";
            string to   = "";

            FormatPortRange(ne.FromPort, ne.ToPort, out from, out to);
            ne.FromPort = from;
            ne.ToPort   = to;

            if (rule.Icmp != null)
            {
                //May not be needed
            }

            if (ne.NetworkAclId != null)
            {
                NetworkAcl x = stack.Resources.Find(n => n != null && n.LogicalId == ne.NetworkAclId);
                if (x != null)
                {
                    x.Properties.NetworkAclEntry.Add(ne);
                }
                else
                {
//TODO
//Either remember and process orphaned ingress rule
//Or write out to error log.
                    MessageBox.Show("Error", "Did not find NACL " + ne.NetworkAclId + " to add entry to", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
Exemplo n.º 5
0
        // -----------------------------------------------------------------------
        // Live Stack

        public static void ProcessNetworkAclFromAWS(StackResourceSummary resource, CFStack stack, AmazonEC2Client ec2Client, string stackName)
        {
            DescribeNetworkAclsRequest naclRequest = new DescribeNetworkAclsRequest();

            naclRequest.NetworkAclIds = new List <string> {
                resource.PhysicalResourceId
            };

            DescribeNetworkAclsResponse response = ec2Client.DescribeNetworkAcls(naclRequest);

            foreach (Amazon.EC2.Model.NetworkAcl nacl in response.NetworkAcls)
            {
                NetworkAcl n = new NetworkAcl();
                n.LogicalId = resource.LogicalResourceId;
                if (log)
                {
                    Utils.WriteToFile(logFile, "AWS NACL: " + n.LogicalId.ToString(), true);
                }
                n.Type             = "AWS::EC2::NetworkAcl";
                n.Properties.VpcId = nacl.VpcId;

                foreach (Amazon.EC2.Model.NetworkAclEntry e in nacl.Entries)
                {
                    NetworkAclEntry ne = new NetworkAclEntry();
                    ne.RuleNumber = e.RuleNumber.ToString();
                    ne.CidrBlock  = e.CidrBlock;
                    ne.Egress     = e.Egress;
                    if (e.PortRange == null)
                    {
                        ne.FromPort = "ALL"; ne.ToPort = "ALL";
                    }
                    else
                    {
                        //FormatPortRange - Port range could be 0-0 -1-1 0-65535
                        string from = "";
                        string to   = "";
                        FormatPortRange(e.PortRange.From.ToString(), e.PortRange.To.ToString(), out from, out to);
                        ne.FromPort = from;
                        ne.ToPort   = to;
                        //------------------------------------------------------
                    }

                    //FormatProtocol - Protocol could be a number or text (e.g. 6 or tcp)
                    ne.Protocol = FormatProtocol(e.Protocol);
                    //-------------------------------------------------------------------

                    ne.RuleAction = e.RuleAction;
                    //ICMP not included.

                    n.Properties.NetworkAclEntry.Add(ne);

                    if (e.PortRange == null)
                    {
                        if (log)
                        {
                            Utils.WriteToFile(logFile, ne.RuleNumber + " Protocol: " + e.Protocol + " | From: " + "null" + " To: " + "null", true);
                        }
                    }
                    else
                    {
                        if (log)
                        {
                            Utils.WriteToFile(logFile, ne.RuleNumber + " Protocol: " + e.Protocol + " | From: " + e.PortRange.From.ToString() + " To: " + e.PortRange.To.ToString(), true);
                        }
                    }
                }

                stack.Resources.Add(n);
            }
        }