Ejemplo n.º 1
0
        public override void ExecuteCmdlet()
        {
            var existingPolicy = FrontDoorManagementClient.Policies.List(ResourceGroupName)
                                 .Where(p => p.Name.ToLower() == Name.ToLower());

            if (existingPolicy.Count() != 0)
            {
                throw new PSArgumentException(string.Format(Resources.Error_CreateExistingWebApplicationFirewallPolicy,
                                                            Name,
                                                            ResourceGroupName));
            }
            var updateParameters = new Management.FrontDoor.Models.WebApplicationFirewallPolicy
            {
                Location    = "global",
                CustomRules = new Management.FrontDoor.Models.CustomRuleList()
                {
                    Rules = Customrule?.ToList().Select(x => x.ToSdkCustomRule()).ToList()
                },
                ManagedRules = new Management.FrontDoor.Models.ManagedRuleSetList()
                {
                    ManagedRuleSets = ManagedRule?.ToList().Select(x => x.ToSdkAzManagedRule()).ToList()
                },
                PolicySettings = new Management.FrontDoor.Models.PolicySettings
                {
                    EnabledState                  = this.IsParameterBound(c => c.EnabledState) ? EnabledState.ToString() : PSEnabledState.Enabled.ToString(),
                    Mode                          = this.IsParameterBound(c => c.Mode) ? Mode : PSMode.Prevention.ToString(),
                    CustomBlockResponseBody       = CustomBlockResponseBody == null ? CustomBlockResponseBody : Convert.ToBase64String(Encoding.UTF8.GetBytes(CustomBlockResponseBody)),
                    CustomBlockResponseStatusCode = this.IsParameterBound(c => c.CustomBlockResponseStatusCode) ? CustomBlockResponseStatusCode : (int?)null,
                    RedirectUrl                   = RedirectUrl,
                    RequestBodyCheck              = this.IsParameterBound(c => c.RequestBodyCheck) ? RequestBodyCheck : PSEnabledState.Enabled.ToString()
                },
                Sku = this.IsParameterBound(c => c.Sku) ? new Management.FrontDoor.Models.Sku(Sku) : null
            };

            if (ShouldProcess(Resources.WebApplicationFirewallPolicyTarget, string.Format(Resources.CreateWebApplicationFirewallPolicy, Name)))
            {
                try
                {
                    var policy = FrontDoorManagementClient.Policies.CreateOrUpdate(
                        ResourceGroupName,
                        Name,
                        updateParameters
                        );
                    WriteObject(policy.ToPSPolicy());
                }
                catch (Microsoft.Azure.Management.FrontDoor.Models.ErrorResponseException e)
                {
                    throw new PSArgumentException(string.Format(
                                                      Resources.Error_ErrorResponseFromServer,
                                                      e.Response.Content));
                }
            }
        }
Ejemplo n.º 2
0
        public override void ExecuteCmdlet()
        {
            if (ParameterSetName == ObjectParameterSet)
            {
                ResourceIdentifier identifier = new ResourceIdentifier(InputObject.Id);
                ResourceGroupName = identifier.ResourceGroupName;
                Name = InputObject.Name;
            }
            else if (ParameterSetName == ResourceIdParameterSet)
            {
                ResourceIdentifier identifier = new ResourceIdentifier(ResourceId);
                ResourceGroupName = identifier.ResourceGroupName;
                Name = identifier.ResourceName;
            }


            var existingPolicy = FrontDoorManagementClient.Policies.List(ResourceGroupName)
                                 .FirstOrDefault(x => x.Name.ToLower() == Name.ToLower());


            if (existingPolicy == null)
            {
                throw new PSArgumentException(string.Format(
                                                  Resources.Error_WebApplicationFirewallPolicyNotFound,
                                                  Name,
                                                  ResourceGroupName));
            }

            SdkPolicy updateParameters;

            if (ParameterSetName == ObjectParameterSet)
            {
                updateParameters = InputObject.ToSdkFirewallPolicy();
            }
            else
            {
                updateParameters = existingPolicy;
            }

            if (this.IsParameterBound(c => c.EnabledState))
            {
                updateParameters.PolicySettings.EnabledState = EnabledState.ToString();
            }

            if (this.IsParameterBound(c => c.Mode))
            {
                updateParameters.PolicySettings.Mode = Mode;
            }

            if (this.IsParameterBound(c => c.Customrule))
            {
                updateParameters.CustomRules = new Management.FrontDoor.Models.CustomRuleList()
                {
                    Rules = Customrule.ToList().Select(x => x.ToSdkCustomRule()).ToList()
                };
            }

            if (this.IsParameterBound(c => c.ManagedRule))
            {
                updateParameters.ManagedRules = new Management.FrontDoor.Models.ManagedRuleSetList()
                {
                    ManagedRuleSets = ManagedRule.ToList().Select(x => x.ToSdkAzManagedRule()).ToList()
                };
            }

            if (this.IsParameterBound(c => c.CustomBlockResponseBody))
            {
                updateParameters.PolicySettings.CustomBlockResponseBody = CustomBlockResponseBody == null ? CustomBlockResponseBody : Convert.ToBase64String(Encoding.UTF8.GetBytes(CustomBlockResponseBody));
            }

            if (this.IsParameterBound(c => c.CustomBlockResponseStatusCode))
            {
                updateParameters.PolicySettings.CustomBlockResponseStatusCode = CustomBlockResponseStatusCode;
            }

            if (this.IsParameterBound(c => c.RedirectUrl))
            {
                updateParameters.PolicySettings.RedirectUrl = RedirectUrl;
            }

            if (this.IsParameterBound(c => c.RequestBodyCheck))
            {
                updateParameters.PolicySettings.RequestBodyCheck = RequestBodyCheck;
            }

            if (ShouldProcess(Resources.WebApplicationFirewallPolicyTarget, string.Format(Resources.WebApplicationFirewallPolicyChangeWarning, Name)))
            {
                try
                {
                    var policy = FrontDoorManagementClient.Policies.CreateOrUpdate(
                        ResourceGroupName,
                        Name,
                        updateParameters
                        );
                    WriteObject(policy.ToPSPolicy());
                }
                catch (Microsoft.Azure.Management.FrontDoor.Models.ErrorResponseException e)
                {
                    throw new PSArgumentException(string.Format(
                                                      Resources.Error_ErrorResponseFromServer,
                                                      e.Response.Content));
                }
            }
        }