Esempio n. 1
0
        static void ProcessEC2SecurityGroupIngressFromTemplate(dynamic input, CFStack stack)
        {
            EC2SecurityGroupIngress sgi = new EC2SecurityGroupIngress();

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

            //FormatProtocol - Protocol could be a number or text (e.g. 6 or tcp)
            sgi.IpProtocol = FormatProtocol(rule.IpProtocol.ToString());
            //-------------------------------------------------------------------

            //FormatPortRange - Port range could be 0-0 -1-1 0-65535
            string from = "";
            string to   = "";

            FormatPortRange(rule.FromPort.ToString(), rule.ToPort.ToString(), out from, out to);
            sgi.FromPort = from;
            sgi.ToPort   = to;
            //-------------------------------------------------------------------

            sgi.CidrIp = rule.CidrIp;
            if (rule.SourceSecurityGroupId != null)
            {
                if (rule.SourceSecurityGroupId != null)
                {
                    var Ids = rule.SourceSecurityGroupId;
                    if (Ids.Value == null)
                    {
                        foreach (var Id in Ids)
                        {
                            sgi.SourceSecurityGroupId = Id.Value;
                        }
                    }
                    else
                    {
                        sgi.SourceSecurityGroupId = Ids.Value;
                    }
                }
            }

            if (rule.GroupId != null)
            {
                sgi.GroupName = rule.GroupId["Ref"].Value;
                EC2SecurityGroup x = stack.Resources.Find(n => n != null && n.LogicalId == rule.GroupId["Ref"].Value);
                if (x != null)
                {
                    x.Properties.SecurityGroupIngress.Add(sgi);
                }
                else
                {
//TODO
//Either remember and process orphaned ingress rule
//Or write out to error log.
                    MessageBox.Show("Error", "Did not find security group " + sgi.GroupName + " to add ingress rule to", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
Esempio n. 2
0
        private void CompareSecurityGroups(EC2SecurityGroup originalResource1, EC2SecurityGroup originalResource2, EC2SecurityGroup copyResource, CFStack copyStack)
        {
            List <EC2SecurityGroupIngress> compareList = copyResource.Properties.SecurityGroupIngress;

            foreach (EC2SecurityGroupIngress x in originalResource1.Properties.SecurityGroupIngress)
            {
                var y = originalResource2.Properties.SecurityGroupIngress.Find(n => n != null && n.CidrIp == x.CidrIp && n.FromPort == x.FromPort && n.ToPort == x.ToPort && n.IpProtocol == x.IpProtocol && n.SourceSecurityGroupId == x.SourceSecurityGroupId);
                if (y == null)
                {
                    if (CompareRemoves == false)
                    {
                        var z = copyResource.Properties.SecurityGroupIngress.Find(n => n != null && n.CidrIp == x.CidrIp && n.FromPort == x.FromPort && n.ToPort == x.ToPort && n.IpProtocol == x.IpProtocol && n.SourceSecurityGroupId == x.SourceSecurityGroupId);
                        if (z != null)
                        {
                            z.StateChanged = true;
                        }
                    }
                }
                else
                {
                    if (CompareRemoves == true)
                    {
                        var z = copyResource.Properties.SecurityGroupIngress.Find(n => n != null && n.CidrIp == x.CidrIp && n.FromPort == x.FromPort && n.ToPort == x.ToPort && n.IpProtocol == x.IpProtocol && n.SourceSecurityGroupId == x.SourceSecurityGroupId);
                        if (z != null)
                        {
                            copyResource.Properties.SecurityGroupIngress.Remove(z);
                        }
                    }
                }
            }

            if (copyResource.Properties.SecurityGroupIngress.Count() == 0)
            {
                copyStack.Resources.Remove(copyResource);
            }
        }
Esempio 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;
                }
            }
        }
Esempio n. 4
0
        public static void ProcessEC2SecurityGroupFromAWS(StackResourceSummary resource, CFStack stack, AmazonEC2Client ec2Client, Dictionary <string, string> secGroupMap, string stackName)
        {
            DescribeSecurityGroupsRequest secGroupRequest = new DescribeSecurityGroupsRequest();

            //Set request to use Phisical Id
            secGroupRequest.GroupIds = new List <string> {
                resource.PhysicalResourceId
            };

            //Attempt to get security group using physical Id
            DescribeSecurityGroupsResponse response = GetSecurityGroup(ec2Client, secGroupRequest);

            if (response == null)
            {
                //Set request to use Logical Id and Stack Name Tags
                secGroupRequest.GroupIds.Clear();
                List <Filter> f = new List <Filter>();
                f.Add(new Filter {
                    Name = "tag:aws:cloudformation:logical-id", Values = new List <string>()
                    {
                        resource.LogicalResourceId
                    }
                });
                f.Add(new Filter {
                    Name = "tag:aws:cloudformation:stack-name", Values = new List <string>()
                    {
                        stackName
                    }
                });
                secGroupRequest.Filters = f;
                //Attempt to get security group using logical Id
                response = GetSecurityGroup(ec2Client, secGroupRequest);
            }

            if (response == null | response.SecurityGroups.Count == 0)
            {
                return;
            }

            foreach (SecurityGroup group in response.SecurityGroups)
            {
                EC2SecurityGroup sg = new EC2SecurityGroup();
                sg.LogicalId = resource.LogicalResourceId;
                if (log)
                {
                    Utils.WriteToFile(logFile, "AWS SG: " + sg.LogicalId.ToString(), true);
                }
                sg.Type = "AWS::EC2::SecurityGroup";
                sg.Properties.GroupDescription = group.Description;
                sg.Properties.VpcId            = group.VpcId;

                foreach (IpPermission perms in group.IpPermissions)
                {
                    for (int i = 0; i < perms.IpRanges.Count; i++)
                    {
                        EC2SecurityGroupIngress sgi = new EC2SecurityGroupIngress();

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

                        //FormatPortRange - Port range could be 0-0 -1-1 0-65535
                        string from = "";
                        string to   = "";
                        FormatPortRange(perms.FromPort.ToString(), perms.ToPort.ToString(), out from, out to);
                        sgi.FromPort = from;
                        sgi.ToPort   = to;
                        //------------------------------------------------------

                        sgi.CidrIp = perms.IpRanges[i];
                        sg.Properties.SecurityGroupIngress.Add(sgi);

                        if (log)
                        {
                            Utils.WriteToFile(logFile, " Protocol: " + perms.IpProtocol + " | From: " + perms.FromPort.ToString() + " To: " + perms.ToPort.ToString(), true);
                        }
                    }
                    for (int i = 0; i < perms.UserIdGroupPairs.Count; i++)
                    {
                        EC2SecurityGroupIngress sgi = new EC2SecurityGroupIngress();
                        //FormatProtocol - Protocol could be a number or text (e.g. 6 or tcp)
                        sgi.IpProtocol = FormatProtocol(perms.IpProtocol);
                        //--------------------------------------------------------------------

                        //FormatPortRange - Port range could be 0-0 -1-1 0-65535
                        string from = "";
                        string to   = "";
                        FormatPortRange(perms.FromPort.ToString(), perms.ToPort.ToString(), out from, out to);
                        sgi.FromPort = from;
                        sgi.ToPort   = to;
                        //-------------------------------------------------------

                        sg.Properties.SecurityGroupIngress.Add(sgi);
                        string groupName;
                        if (secGroupMap.TryGetValue(perms.UserIdGroupPairs[i].GroupId, out groupName))
                        {
                            sgi.SourceSecurityGroupId = groupName;
                        }
                        else
                        {
                            sgi.SourceSecurityGroupId = perms.UserIdGroupPairs[i].GroupId;
                        }

                        if (log)
                        {
                            Utils.WriteToFile(logFile, " Protocol: " + perms.IpProtocol + " | From: " + perms.FromPort.ToString() + " To: " + perms.ToPort.ToString(), true);
                        }
                    }
                }
                stack.Resources.Add(sg);
            }
        }
Esempio n. 5
0
        static void ProcessEC2SecurityGroupFromTemplate(dynamic input, CFStack stack)
        {
            EC2SecurityGroup sg = new EC2SecurityGroup();

            sg.LogicalId = input.Name;
            sg.Type      = "AWS::EC2::SecurityGroup";

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

            foreach (var prop in props)
            {
                //Attempt to deal with values that are calculated using CloudFormation functions like Join.
                if (prop.Value.ToString().Contains("Fn::"))
                {
                    prop.Value = "Calulated value";
                }

                switch ((string)prop.Name)
                {
                case "GroupDescription":
                    sg.Properties.GroupDescription = prop.Value.ToString();
                    break;

                case "VpcId":
                    var a = prop.Value;
                    sg.Properties.VpcId = a.Value;
                    break;

                case "Tags":

                    break;

                case "SecurityGroupIngress":
                    var ingressRules = prop.Value;

                    foreach (var rule in ingressRules)
                    {
                        EC2SecurityGroupIngress sgi = new EC2SecurityGroupIngress();

                        //FormatProtocol - Protocol could be a number or text (e.g. 6 or tcp)
                        sgi.IpProtocol = FormatProtocol(rule.IpProtocol.ToString());
                        //-------------------------------------------------------------------

                        //FormatPortRange - Port range could be 0-0 -1-1 0-65535
                        string from = "";
                        string to   = "";
                        FormatPortRange(rule.FromPort.ToString(), rule.ToPort.ToString(), out from, out to);
                        sgi.FromPort = from;
                        sgi.ToPort   = to;
                        //-------------------------------------------------------

                        sgi.CidrIp = rule.CidrIp;
                        if (rule.SourceSecurityGroupId != null)
                        {
                            var Ids = rule.SourceSecurityGroupId;
                            foreach (var Id in Ids)
                            {
                                sgi.SourceSecurityGroupId = Id.Value;
                            }
                        }
                        sg.Properties.SecurityGroupIngress.Add(sgi);
                    }
                    break;
                }
            }

            stack.Resources.Add(sg);
        }