Beispiel #1
0
        protected void SetSourceApplicationSecurityGroupInRule(PSSecurityRule rule)
        {
            if ((this.SourceApplicationSecurityGroup != null) || (this.SourceApplicationSecurityGroupId != null))
            {
                rule.SourceApplicationSecurityGroups = new List <PSApplicationSecurityGroup>();
            }

            if (this.SourceApplicationSecurityGroup != null)
            {
                foreach (var psApplicationSecurityGroup in this.SourceApplicationSecurityGroup)
                {
                    rule.SourceApplicationSecurityGroups.Add(psApplicationSecurityGroup);
                }
            }

            if (this.SourceApplicationSecurityGroupId != null)
            {
                foreach (var psApplicationSecurityGroupId in this.SourceApplicationSecurityGroupId)
                {
                    rule.SourceApplicationSecurityGroups.Add(new PSApplicationSecurityGroup {
                        Id = psApplicationSecurityGroupId
                    });
                }
            }
        }
        public override void Execute()
        {
            base.Execute();
            // Verify if the subnet exists in the NetworkSecurityGroup
            var rule = this.NetworkSecurityGroup.SecurityRules.SingleOrDefault(resource => string.Equals(resource.Name, this.Name, System.StringComparison.CurrentCultureIgnoreCase));

            if (rule != null)
            {
                throw new ArgumentException("Rule with the specified name already exists");
            }

            rule = new PSSecurityRule();

            rule.Name                     = this.Name;
            rule.Description              = this.Description;
            rule.Protocol                 = this.Protocol;
            rule.SourcePortRange          = this.SourcePortRange;
            rule.DestinationPortRange     = this.DestinationPortRange;
            rule.SourceAddressPrefix      = this.SourceAddressPrefix;
            rule.DestinationAddressPrefix = this.DestinationAddressPrefix;
            rule.Access                   = this.Access;
            rule.Priority                 = this.Priority;
            rule.Direction                = this.Direction;

            this.NetworkSecurityGroup.SecurityRules.Add(rule);

            WriteObject(this.NetworkSecurityGroup);
        }
        public override void Execute()
        {
            base.Execute();
            // Verify if the security rule exists in the NetworkSecurityGroup
            var rule = this.NetworkSecurityGroup.SecurityRules.SingleOrDefault(resource => string.Equals(resource.Name, this.Name, System.StringComparison.CurrentCultureIgnoreCase));

            if (rule != null)
            {
                throw new ArgumentException("Rule with the specified name already exists");
            }

            if ((this.SourceAddressPrefix != null) && (this.SourceAddressPrefix.Length > 0) && (this.SourceApplicationSecurityGroup != null) && (this.SourceApplicationSecurityGroup.Length > 0))
            {
                throw new ArgumentException($"{nameof(SourceAddressPrefix)} and {nameof(SourceApplicationSecurityGroup)} cannot be used simultaneously.");
            }

            if ((this.SourceAddressPrefix != null) && (this.SourceAddressPrefix.Length > 0) && (this.SourceApplicationSecurityGroupId != null) && (this.SourceApplicationSecurityGroupId.Length > 0))
            {
                throw new ArgumentException($"{nameof(SourceAddressPrefix)} and {nameof(SourceApplicationSecurityGroupId)} cannot be used simultaneously.");
            }

            if ((this.DestinationAddressPrefix != null) && (this.DestinationAddressPrefix.Length > 0) && (this.DestinationApplicationSecurityGroup != null) && (this.DestinationApplicationSecurityGroup.Length > 0))
            {
                throw new ArgumentException($"{nameof(DestinationAddressPrefix)} and {nameof(DestinationApplicationSecurityGroup)} cannot be used simultaneously.");
            }

            if ((this.DestinationAddressPrefix != null) && (this.DestinationAddressPrefix.Length > 0) && (this.DestinationApplicationSecurityGroupId != null) && (this.DestinationApplicationSecurityGroupId.Length > 0))
            {
                throw new ArgumentException($"{nameof(DestinationAddressPrefix)} and {nameof(DestinationApplicationSecurityGroupId)} cannot be used simultaneously.");
            }

            rule = new PSSecurityRule();

            rule.Name                     = this.Name;
            rule.Description              = this.Description;
            rule.Protocol                 = this.Protocol;
            rule.SourcePortRange          = this.SourcePortRange;
            rule.DestinationPortRange     = this.DestinationPortRange;
            rule.SourceAddressPrefix      = this.SourceAddressPrefix;
            rule.DestinationAddressPrefix = this.DestinationAddressPrefix;
            rule.Access                   = this.Access;
            rule.Priority                 = this.Priority;
            rule.Direction                = this.Direction;

            SetSourceApplicationSecurityGroupInRule(rule);
            SetDestinationApplicationSecurityGroupInRule(rule);

            this.NetworkSecurityGroup.SecurityRules.Add(rule);

            WriteObject(this.NetworkSecurityGroup);
        }
Beispiel #4
0
        public override void Execute()
        {
            base.Execute();
            var rule = new PSSecurityRule();

            rule.Name                     = this.Name;
            rule.Description              = this.Description;
            rule.Protocol                 = this.Protocol;
            rule.SourcePortRange          = this.SourcePortRange;
            rule.DestinationPortRange     = this.DestinationPortRange;
            rule.SourceAddressPrefix      = this.SourceAddressPrefix;
            rule.DestinationAddressPrefix = this.DestinationAddressPrefix;
            rule.Access                   = this.Access;
            rule.Priority                 = this.Priority;
            rule.Direction                = this.Direction;

            WriteObject(rule);
        }
Beispiel #5
0
        public override void Execute()
        {
            base.Execute();

            if ((this.SourceAddressPrefix != null) && (this.SourceAddressPrefix.Count > 0) && (this.SourceApplicationSecurityGroup != null) && (this.SourceApplicationSecurityGroup.Count > 0))
            {
                throw new ArgumentException($"{nameof(SourceAddressPrefix)} and {nameof(SourceApplicationSecurityGroup)} cannot be used simultaneously.");
            }

            if ((this.SourceAddressPrefix != null) && (this.SourceAddressPrefix.Count > 0) && (this.SourceApplicationSecurityGroupId != null) && (this.SourceApplicationSecurityGroupId.Count > 0))
            {
                throw new ArgumentException($"{nameof(SourceAddressPrefix)} and {nameof(SourceApplicationSecurityGroupId)} cannot be used simultaneously.");
            }

            if ((this.DestinationAddressPrefix != null) && (this.DestinationAddressPrefix.Count > 0) && (this.DestinationApplicationSecurityGroup != null) && (this.DestinationApplicationSecurityGroup.Count > 0))
            {
                throw new ArgumentException($"{nameof(DestinationAddressPrefix)} and {nameof(DestinationApplicationSecurityGroup)} cannot be used simultaneously.");
            }

            if ((this.DestinationAddressPrefix != null) && (this.DestinationAddressPrefix.Count > 0) && (this.DestinationApplicationSecurityGroupId != null) && (this.DestinationApplicationSecurityGroupId.Count > 0))
            {
                throw new ArgumentException($"{nameof(DestinationAddressPrefix)} and {nameof(DestinationApplicationSecurityGroupId)} cannot be used simultaneously.");
            }

            var rule = new PSSecurityRule();

            rule.Name                     = this.Name;
            rule.Description              = this.Description;
            rule.Protocol                 = this.Protocol;
            rule.SourcePortRange          = this.SourcePortRange;
            rule.DestinationPortRange     = this.DestinationPortRange;
            rule.SourceAddressPrefix      = this.SourceAddressPrefix;
            rule.DestinationAddressPrefix = this.DestinationAddressPrefix;
            rule.Access                   = this.Access;
            rule.Priority                 = this.Priority;
            rule.Direction                = this.Direction;

            SetSourceApplicationSecurityGroupInRule(rule);
            SetDestinationApplicationSecurityGroupInRule(rule);

            WriteObject(rule);
        }
        public PSSecurityGroupViewResult GetSecurityGroupView(string resourceGroupName, string name, MNM.SecurityGroupViewParameters properties, string expandResource = null)
        {
            MNM.SecurityGroupViewResult securityGroupView = this.NetworkWatcherClient.GetVMSecurityRules(resourceGroupName, name, properties);
            var networkInterfaces = new PSSecurityGroupViewResult();

            networkInterfaces.NetworkInterfaces = new List <PSSecurityGroupView>();

            foreach (var view in securityGroupView.NetworkInterfaces)
            {
                PSSecurityGroupView securityRules = new PSSecurityGroupView();

                securityRules.NetworkInterfaceId = view.Id;

                if (view.SecurityRuleAssociations.NetworkInterfaceAssociation != null)
                {
                    securityRules.NetworkInterfaceSecurityRules = new List <PSSecurityRule>();
                    var customSecurityRulesList = view.SecurityRuleAssociations.NetworkInterfaceAssociation.SecurityRules;

                    foreach (var rule in customSecurityRulesList)
                    {
                        PSSecurityRule psRule = Mapper.Map <PSSecurityRule>(rule);
                        securityRules.NetworkInterfaceSecurityRules.Add(psRule);
                    }
                }

                if (view.SecurityRuleAssociations.SubnetAssociation != null)
                {
                    securityRules.SubnetId            = view.SecurityRuleAssociations.SubnetAssociation.Id;
                    securityRules.SubnetSecurityRules = new List <PSSecurityRule>();

                    var subnetSecurityRulesList = view.SecurityRuleAssociations.SubnetAssociation.SecurityRules;

                    foreach (var rule in subnetSecurityRulesList)
                    {
                        PSSecurityRule psRule = Mapper.Map <PSSecurityRule>(rule);
                        securityRules.SubnetSecurityRules.Add(psRule);
                    }
                }

                securityRules.DefaultSecurityRules = new List <PSSecurityRule>();
                var defaultSecurityRulesList = view.SecurityRuleAssociations.DefaultSecurityRules;

                foreach (var rule in defaultSecurityRulesList)
                {
                    PSSecurityRule psRule = Mapper.Map <PSSecurityRule>(rule);
                    securityRules.DefaultSecurityRules.Add(psRule);
                }

                securityRules.EffectiveSecurityRules = new List <PSEffectiveSecurityRule>();
                var effectiveSecurityRulesList = view.SecurityRuleAssociations.EffectiveSecurityRules;

                foreach (var rule in effectiveSecurityRulesList)
                {
                    PSEffectiveSecurityRule psRule = Mapper.Map <PSEffectiveSecurityRule>(rule);
                    securityRules.EffectiveSecurityRules.Add(psRule);
                }

                networkInterfaces.NetworkInterfaces.Add(securityRules);
            }

            return(networkInterfaces);
        }