private void UpdateAfdSecurityPolicy()
        {
            try
            {
                PSAfdSecurityPolicy currentPsAfdSecurityPolicy = this.CdnManagementClient.SecurityPolicies.Get(this.ResourceGroupName, this.ProfileName, this.SecurityPolicyName).ToPSAfdSecurityPolicy();

                SecurityPolicyWebApplicationFirewallParameters securityPolicyWafParameters = new SecurityPolicyWebApplicationFirewallParameters();

                if (ParameterSetName == ObjectParameterSet)
                {
                    securityPolicyWafParameters = this.CreateSecurityPolicyWafParametersByObject(currentPsAfdSecurityPolicy);
                }

                if (ParameterSetName == FieldsParameterSet)
                {
                    securityPolicyWafParameters = this.CreateSecurityPolicyWafParametersByFields(currentPsAfdSecurityPolicy);
                }

                this.CdnManagementClient.SecurityPolicies.Patch(this.ResourceGroupName, this.ProfileName, this.SecurityPolicyName, securityPolicyWafParameters);
            }
            catch (AfdErrorResponseException errorResponse)
            {
                throw new PSArgumentException(errorResponse.Response.Content);
            }
        }
Exemple #2
0
        private void CreateAfdSecurityPolicy()
        {
            try
            {
                SecurityPolicyWebApplicationFirewallParameters securityPolicyParameters = new SecurityPolicyWebApplicationFirewallParameters();
                securityPolicyParameters.WafPolicy    = new ResourceReference(this.WafPolicyId);
                securityPolicyParameters.Associations = new List <SecurityPolicyWebApplicationFirewallAssociation>();

                SecurityPolicyWebApplicationFirewallAssociation securityPolicyWebApplicationFirewallAssociation = new SecurityPolicyWebApplicationFirewallAssociation();
                securityPolicyWebApplicationFirewallAssociation.Domains         = new List <ResourceReference>();
                securityPolicyWebApplicationFirewallAssociation.PatternsToMatch = new List <string>
                {
                    "/*",
                };

                foreach (string domainId in this.DomainId)
                {
                    ResourceReference resourceReference = new ResourceReference(domainId);
                    securityPolicyWebApplicationFirewallAssociation.Domains.Add(resourceReference);
                }

                securityPolicyParameters.Associations.Add(securityPolicyWebApplicationFirewallAssociation);

                PSAfdSecurityPolicy psAfdSecurityPolicy = this.CdnManagementClient.SecurityPolicies.Create(this.ResourceGroupName, this.ProfileName, this.SecurityPolicyName, securityPolicyParameters).ToPSAfdSecurityPolicy();

                WriteObject(psAfdSecurityPolicy);
            }
            catch (AfdErrorResponseException errorResponse)
            {
                throw new PSArgumentException(errorResponse.Response.Content);
            }
        }
        private void ResourceIdParameterSetCmdlet()
        {
            ResourceIdentifier parsedAfdSecurityPolicyResourceId = new ResourceIdentifier(this.ResourceId);

            this.ProfileName        = parsedAfdSecurityPolicyResourceId.GetResourceName("profiles");
            this.ResourceGroupName  = parsedAfdSecurityPolicyResourceId.ResourceGroupName;
            this.SecurityPolicyName = parsedAfdSecurityPolicyResourceId.ResourceName;

            PSAfdSecurityPolicy psAfdSecurityPolicy = this.CdnManagementClient.SecurityPolicies.Get(this.ResourceGroupName, this.ProfileName, this.SecurityPolicyName).ToPSAfdSecurityPolicy();

            WriteObject(psAfdSecurityPolicy);
        }
        private void FieldsParameterSetCmdlet()
        {
            bool isSecurityPolicyName = this.MyInvocation.BoundParameters.ContainsKey("SecurityPolicyName");

            if (isSecurityPolicyName)
            {
                PSAfdSecurityPolicy psAfdSecurityPolicy = this.CdnManagementClient.SecurityPolicies.Get(this.ResourceGroupName, this.ProfileName, this.SecurityPolicyName).ToPSAfdSecurityPolicy();

                WriteObject(psAfdSecurityPolicy);
            }
            else
            {
                List <PSAfdSecurityPolicy> psAfdSecurityPolicyList = this.CdnManagementClient.SecurityPolicies.ListByProfile(this.ResourceGroupName, this.ProfileName)
                                                                     .Select(afdSecurityPolicy => afdSecurityPolicy.ToPSAfdSecurityPolicy())
                                                                     .ToList();

                WriteObject(psAfdSecurityPolicyList);
            }
        }
        private SecurityPolicyWebApplicationFirewallParameters CreateSecurityPolicyWafParametersByFields(PSAfdSecurityPolicy currentSecurityPolicy)
        {
            bool isWafPolicy = this.MyInvocation.BoundParameters.ContainsKey("WafPolicyId");
            bool isDomainIds = this.MyInvocation.BoundParameters.ContainsKey("DomainId");

            SecurityPolicyWebApplicationFirewallParameters securityPolicyWafParameters = new SecurityPolicyWebApplicationFirewallParameters
            {
                WafPolicy    = new ResourceReference(currentSecurityPolicy.WafPolicyId),
                Associations = new List <SecurityPolicyWebApplicationFirewallAssociation>()
            };

            SecurityPolicyWebApplicationFirewallAssociation securityPolicyWafAssociation = new SecurityPolicyWebApplicationFirewallAssociation();

            securityPolicyWafAssociation.Domains         = currentSecurityPolicy.Domains;
            securityPolicyWafAssociation.PatternsToMatch = currentSecurityPolicy.PatternsToMatch;

            securityPolicyWafParameters.Associations.Add(securityPolicyWafAssociation);

            if (isWafPolicy)
            {
                securityPolicyWafParameters.WafPolicy = new ResourceReference(this.WafPolicyId);
            }

            if (isDomainIds)
            {
                securityPolicyWafAssociation.Domains = new List <ResourceReference>();

                foreach (string domainId in this.DomainId)
                {
                    securityPolicyWafAssociation.Domains.Add(new ResourceReference(domainId));
                }

                //securityPolicyWafParameters.Associations = securityPolicyWafAssociation;
            }

            return(securityPolicyWafParameters);
        }
        private SecurityPolicyWebApplicationFirewallParameters CreateSecurityPolicyWafParametersByObject(PSAfdSecurityPolicy currentSecurityPolicy)
        {
            SecurityPolicyWebApplicationFirewallParameters securityPolicyWafParameters = new SecurityPolicyWebApplicationFirewallParameters
            {
                WafPolicy    = new ResourceReference(currentSecurityPolicy.WafPolicyId),
                Associations = new List <SecurityPolicyWebApplicationFirewallAssociation>()
            };

            SecurityPolicyWebApplicationFirewallAssociation securityPolicyWafAssociation = new SecurityPolicyWebApplicationFirewallAssociation();

            securityPolicyWafAssociation.Domains         = currentSecurityPolicy.Domains;
            securityPolicyWafAssociation.PatternsToMatch = currentSecurityPolicy.PatternsToMatch;

            securityPolicyWafParameters.Associations.Add(securityPolicyWafAssociation);

            if (currentSecurityPolicy.WafPolicyId != this.SecurityPolicy.WafPolicyId)
            {
                securityPolicyWafParameters.WafPolicy = new ResourceReference(this.SecurityPolicy.WafPolicyId);
            }

            return(securityPolicyWafParameters);
        }