public void WAFCRUDTest() { var handler1 = new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK }; var handler2 = new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK }; using (MockContext context = MockContext.Start(this.GetType())) { // Create clients var frontDoorMgmtClient = FrontDoorTestUtilities.GetFrontDoorManagementClient(context, handler1); var resourcesClient = FrontDoorTestUtilities.GetResourceManagementClient(context, handler2); // Create resource group var resourceGroupName = FrontDoorTestUtilities.CreateResourceGroup(resourcesClient); // Create a frontDoor WAF policy string policyName = TestUtilities.GenerateName("policy"); WebApplicationFirewallPolicy createParameters = new WebApplicationFirewallPolicy { Location = "global", Tags = new Dictionary <string, string> { { "key1", "value1" }, { "key2", "value2" } }, PolicySettings = new PolicySettings { EnabledState = "Enabled", Mode = "Prevention", CustomBlockResponseBody = "PGh0bWw+SGVsbG88L2h0bWw+", CustomBlockResponseStatusCode = 403, RedirectUrl = "http://www.bing.com" }, CustomRules = new CustomRuleList( new List <CustomRule> { new CustomRule { Name = "rule1", EnabledState = "Enabled", Priority = 1, RuleType = "RateLimitRule", RateLimitThreshold = 1000, MatchConditions = new List <MatchCondition> { new MatchCondition { MatchVariable = "RemoteAddr", OperatorProperty = "IPMatch", MatchValue = new List <string> { "192.168.1.0/24", "10.0.0.0/24" } } }, Action = "Block" } } ), ManagedRules = new ManagedRuleSetList( new List <ManagedRuleSet> { new ManagedRuleSet { RuleSetType = "DefaultRuleSet", RuleSetVersion = "1.0", Exclusions = new List <ManagedRuleExclusion> { new ManagedRuleExclusion { MatchVariable = ManagedRuleExclusionMatchVariable.RequestBodyPostArgNames, SelectorMatchOperator = ManagedRuleExclusionSelectorMatchOperator.Contains, Selector = "query" } }, RuleGroupOverrides = new List <ManagedRuleGroupOverride> { new ManagedRuleGroupOverride { RuleGroupName = "SQLI", Exclusions = new List <ManagedRuleExclusion> { new ManagedRuleExclusion { MatchVariable = ManagedRuleExclusionMatchVariable.RequestHeaderNames, SelectorMatchOperator = ManagedRuleExclusionSelectorMatchOperator.Equals, Selector = "User-Agent" } }, Rules = new List <ManagedRuleOverride> { new ManagedRuleOverride { RuleId = "942100", Action = "Redirect", EnabledState = "Disabled", Exclusions = new List <ManagedRuleExclusion> { new ManagedRuleExclusion { MatchVariable = ManagedRuleExclusionMatchVariable.QueryStringArgNames, SelectorMatchOperator = ManagedRuleExclusionSelectorMatchOperator.Equals, Selector = "search" } } } } } } } }) }; var policy = frontDoorMgmtClient.Policies.CreateOrUpdate(resourceGroupName, policyName, createParameters); // validate the created policy VerifyPolicy(policy, createParameters); // Retrieve policy var retrievedPolicy = frontDoorMgmtClient.Policies.Get(resourceGroupName, policyName); // validate that correct policy is retrieved VerifyPolicy(retrievedPolicy, createParameters); // update Policy CustomRule geoFilter = new CustomRule { Name = "rule2", Priority = 2, RuleType = "MatchRule", MatchConditions = new List <MatchCondition> { new MatchCondition { MatchVariable = "RemoteAddr", OperatorProperty = "GeoMatch", MatchValue = new List <string> { "US" } } }, Action = "Allow" }; retrievedPolicy.CustomRules.Rules.Add(geoFilter); var updatedPolicy = frontDoorMgmtClient.Policies.CreateOrUpdate(resourceGroupName, policyName, retrievedPolicy); // validate that Policy is correctly updated VerifyPolicy(updatedPolicy, retrievedPolicy); // Delete Policy frontDoorMgmtClient.Policies.Delete(resourceGroupName, policyName); // Verify that Policy is deleted Assert.ThrowsAny <ErrorResponseException>(() => { frontDoorMgmtClient.Policies.Get(resourceGroupName, policyName); }); FrontDoorTestUtilities.DeleteResourceGroup(resourcesClient, resourceGroupName); } }
public void FrontDoorCRUDTest() { var handler1 = new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK }; var handler2 = new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK }; using (MockContext context = MockContext.Start(this.GetType())) { // Create clients var frontDoorMgmtClient = FrontDoorTestUtilities.GetFrontDoorManagementClient(context, handler1); var resourcesClient = FrontDoorTestUtilities.GetResourceManagementClient(context, handler2); // Get subscription id string subid = frontDoorMgmtClient.SubscriptionId; // Create resource group var resourceGroupName = FrontDoorTestUtilities.CreateResourceGroup(resourcesClient); // Create two different frontDoor string frontDoorName = TestUtilities.GenerateName("frontDoor"); ForwardingConfiguration forwardingConfiguration = new ForwardingConfiguration( forwardingProtocol: "MatchRequest", backendPool: new refID("/subscriptions/" + subid + "/resourceGroups/" + resourceGroupName + "/providers/Microsoft.Network/frontDoors/" + frontDoorName + "/backendPools/backendPool1")); RoutingRule routingrule1 = new RoutingRule( name: "routingrule1", frontendEndpoints: new List <refID> { new refID("/subscriptions/" + subid + "/resourceGroups/" + resourceGroupName + "/providers/Microsoft.Network/frontDoors/" + frontDoorName + "/frontendEndpoints/frontendEndpoint1") }, acceptedProtocols: new List <string> { "Https" }, patternsToMatch: new List <string> { "/*" }, routeConfiguration: forwardingConfiguration, enabledState: "Enabled" ); HealthProbeSettingsModel healthProbeSettings1 = new HealthProbeSettingsModel( name: "healthProbeSettings1", path: "/", protocol: "Http", intervalInSeconds: 120, healthProbeMethod: "Get", enabledState: "Enabled" ); LoadBalancingSettingsModel loadBalancingSettings1 = new LoadBalancingSettingsModel( name: "loadBalancingSettings1", additionalLatencyMilliseconds: 0, sampleSize: 4, successfulSamplesRequired: 2 ); Backend backend1 = new Backend( address: "contoso1.azurewebsites.net", httpPort: 80, httpsPort: 443, enabledState: "Enabled", weight: 1, priority: 2 ); BackendPool backendPool1 = new BackendPool( name: "backendPool1", backends: new List <Backend> { backend1 }, loadBalancingSettings: new refID("/subscriptions/" + subid + "/resourceGroups/" + resourceGroupName + "/providers/Microsoft.Network/frontDoors/" + frontDoorName + "/loadBalancingSettings/loadBalancingSettings1"), healthProbeSettings: new refID("/subscriptions/" + subid + "/resourceGroups/" + resourceGroupName + "/providers/Microsoft.Network/frontDoors/" + frontDoorName + "/healthProbeSettings/healthProbeSettings1") ); BackendPoolsSettings backendPoolsSettings1 = new BackendPoolsSettings( sendRecvTimeoutSeconds: 123 ); FrontendEndpoint frontendEndpoint1 = new FrontendEndpoint( name: "frontendEndpoint1", hostName: frontDoorName + ".azurefd.net", sessionAffinityEnabledState: "Disabled", sessionAffinityTtlSeconds: 0 ); FrontDoorModel createParameters = new FrontDoorModel { Location = "global", FriendlyName = frontDoorName, Tags = new Dictionary <string, string> { { "key1", "value1" }, { "key2", "value2" } }, RoutingRules = new List <RoutingRule> { routingrule1 }, LoadBalancingSettings = new List <LoadBalancingSettingsModel> { loadBalancingSettings1 }, HealthProbeSettings = new List <HealthProbeSettingsModel> { healthProbeSettings1 }, FrontendEndpoints = new List <FrontendEndpoint> { frontendEndpoint1 }, BackendPools = new List <BackendPool> { backendPool1 }, BackendPoolsSettings = backendPoolsSettings1 }; var createdFrontDoor = frontDoorMgmtClient.FrontDoors.CreateOrUpdate(resourceGroupName, frontDoorName, createParameters); // validate that correct frontdoor is created VerifyFrontDoor(createdFrontDoor, createParameters); // Retrieve frontdoor var retrievedFrontDoor = frontDoorMgmtClient.FrontDoors.Get(resourceGroupName, frontDoorName); // validate that correct frontdoor is retrieved VerifyFrontDoor(retrievedFrontDoor, createParameters); // update FrontDoor retrievedFrontDoor.Tags = new Dictionary <string, string> { { "key3", "value3" }, { "key4", "value4" } }; var updatedFrontDoor = frontDoorMgmtClient.FrontDoors.CreateOrUpdate(resourceGroupName, frontDoorName, retrievedFrontDoor); // validate that frontDoor is correctly updated VerifyFrontDoor(updatedFrontDoor, retrievedFrontDoor); // Delete frontDoor frontDoorMgmtClient.FrontDoors.Delete(resourceGroupName, frontDoorName); // Verify that frontDoor is deleted Assert.ThrowsAny <ErrorResponseException>(() => { frontDoorMgmtClient.FrontDoors.Get(resourceGroupName, frontDoorName); }); FrontDoorTestUtilities.DeleteResourceGroup(resourcesClient, resourceGroupName); } }
public void WAFCRUDTest() { var handler1 = new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK }; var handler2 = new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK }; using (MockContext context = MockContext.Start(this.GetType().FullName)) { // Create clients var frontDoorMgmtClient = FrontDoorTestUtilities.GetFrontDoorManagementClient(context, handler1); var resourcesClient = FrontDoorTestUtilities.GetResourceManagementClient(context, handler2); // Create resource group var resourceGroupName = FrontDoorTestUtilities.CreateResourceGroup(resourcesClient); // Create a frontDoor string policyName = TestUtilities.GenerateName("policy"); WebApplicationFirewallPolicy1 createParameters = new WebApplicationFirewallPolicy1 { Location = "global", Tags = new Dictionary <string, string> { { "key1", "value1" }, { "key2", "value2" } }, PolicySettings = new PolicySettings { EnabledState = "Enabled", Mode = "Prevention" }, CustomRules = new CustomRules( new List <CustomRule> { new CustomRule { Name = "rule1", Priority = 1, RuleType = "RateLimitRule", RateLimitThreshold = 1000, MatchConditions = new List <MatchCondition1> { new MatchCondition1 { MatchVariable = "RemoteAddr", OperatorProperty = "IPMatch", MatchValue = new List <string> { "192.168.1.0/24", "10.0.0.0/24" } } }, Action = "Block" } } ), ManagedRules = new ManagedRuleSets( new List <ManagedRuleSet> { new AzureManagedRuleSet { Priority = 1, RuleGroupOverrides = new List <AzureManagedOverrideRuleGroup> { new AzureManagedOverrideRuleGroup { RuleGroupOverride = "SqlInjection", Action = "Block" }, } } }) }; var policy = frontDoorMgmtClient.Policies.CreateOrUpdate(resourceGroupName, policyName, createParameters); // validate the created policy VerifyPolicy(policy, createParameters); // Retrieve policy var retrievedPolicy = frontDoorMgmtClient.Policies.Get(resourceGroupName, policyName); // validate that correct policy is retrieved VerifyPolicy(retrievedPolicy, createParameters); // update Policy CustomRule geoFilter = new CustomRule { Name = "rule2", Priority = 2, RuleType = "MatchRule", MatchConditions = new List <MatchCondition1> { new MatchCondition1 { MatchVariable = "RemoteAddr", OperatorProperty = "GeoMatch", MatchValue = new List <string> { "US" } } }, Action = "Allow" }; retrievedPolicy.CustomRules.Rules.Add(geoFilter); var updatedPolicy = frontDoorMgmtClient.Policies.CreateOrUpdate(resourceGroupName, policyName, retrievedPolicy); // validate that Policy is correctly updated VerifyPolicy(updatedPolicy, retrievedPolicy); // Delete Policy frontDoorMgmtClient.Policies.Delete(resourceGroupName, policyName); // Verify that Policy is deleted Assert.ThrowsAny <ErrorResponseException>(() => { frontDoorMgmtClient.Policies.Get(resourceGroupName, policyName); }); FrontDoorTestUtilities.DeleteResourceGroup(resourcesClient, resourceGroupName); } }
public void FrontDoorCRUDTestWithRulesEngine() { var handler1 = new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK }; var handler2 = new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK }; using (MockContext context = MockContext.Start(this.GetType())) { // Create clients var frontDoorMgmtClient = FrontDoorTestUtilities.GetFrontDoorManagementClient(context, handler1); var resourcesClient = FrontDoorTestUtilities.GetResourceManagementClient(context, handler2); // Get subscription id string subid = frontDoorMgmtClient.SubscriptionId; // Create resource group var resourceGroupName = FrontDoorTestUtilities.CreateResourceGroup(resourcesClient); // Create two different frontDoor string frontDoorName = TestUtilities.GenerateName("frontDoor"); ForwardingConfiguration forwardingConfiguration = new ForwardingConfiguration( forwardingProtocol: "MatchRequest", backendPool: new refID("/subscriptions/" + subid + "/resourceGroups/" + resourceGroupName + "/providers/Microsoft.Network/frontDoors/" + frontDoorName + "/backendPools/backendPool1")); RoutingRule routingrule1 = new RoutingRule( name: "routingrule1", frontendEndpoints: new List <refID> { new refID("/subscriptions/" + subid + "/resourceGroups/" + resourceGroupName + "/providers/Microsoft.Network/frontDoors/" + frontDoorName + "/frontendEndpoints/frontendEndpoint1") }, acceptedProtocols: new List <string> { "Https" }, patternsToMatch: new List <string> { "/*" }, routeConfiguration: forwardingConfiguration, enabledState: "Enabled" ); HealthProbeSettingsModel healthProbeSettings1 = new HealthProbeSettingsModel( name: "healthProbeSettings1", path: "/", protocol: "Http", intervalInSeconds: 120, healthProbeMethod: "Get", enabledState: "Enabled" ); LoadBalancingSettingsModel loadBalancingSettings1 = new LoadBalancingSettingsModel( name: "loadBalancingSettings1", additionalLatencyMilliseconds: 0, sampleSize: 4, successfulSamplesRequired: 2 ); Backend backend1 = new Backend( address: "contoso1.azurewebsites.net", httpPort: 80, httpsPort: 443, enabledState: "Enabled", weight: 1, priority: 2 ); BackendPool backendPool1 = new BackendPool( name: "backendPool1", backends: new List <Backend> { backend1 }, loadBalancingSettings: new refID("/subscriptions/" + subid + "/resourceGroups/" + resourceGroupName + "/providers/Microsoft.Network/frontDoors/" + frontDoorName + "/loadBalancingSettings/loadBalancingSettings1"), healthProbeSettings: new refID("/subscriptions/" + subid + "/resourceGroups/" + resourceGroupName + "/providers/Microsoft.Network/frontDoors/" + frontDoorName + "/healthProbeSettings/healthProbeSettings1") ); BackendPoolsSettings backendPoolsSettings1 = new BackendPoolsSettings( sendRecvTimeoutSeconds: 123 ); FrontendEndpoint frontendEndpoint1 = new FrontendEndpoint( name: "frontendEndpoint1", hostName: frontDoorName + ".azurefd.net", sessionAffinityEnabledState: "Disabled", sessionAffinityTtlSeconds: 0 ); FrontDoorModel createParameters = new FrontDoorModel { Location = "global", FriendlyName = frontDoorName, Tags = new Dictionary <string, string> { { "key1", "value1" }, { "key2", "value2" } }, RoutingRules = new List <RoutingRule> { routingrule1 }, LoadBalancingSettings = new List <LoadBalancingSettingsModel> { loadBalancingSettings1 }, HealthProbeSettings = new List <HealthProbeSettingsModel> { healthProbeSettings1 }, FrontendEndpoints = new List <FrontendEndpoint> { frontendEndpoint1 }, BackendPools = new List <BackendPool> { backendPool1 }, BackendPoolsSettings = backendPoolsSettings1 }; var createdFrontDoor = frontDoorMgmtClient.FrontDoors.CreateOrUpdate(resourceGroupName, frontDoorName, createParameters); // validate that correct frontdoor is created VerifyFrontDoor(createdFrontDoor, createParameters); // Create rules engine for front door RulesEngineAction rulesEngineAction1 = new RulesEngineAction( requestHeaderActions: new List <HeaderAction> { }, responseHeaderActions: new List <HeaderAction> { }, routeConfigurationOverride: new ForwardingConfiguration( customForwardingPath: null, forwardingProtocol: "HttpsOnly", cacheConfiguration: new CacheConfiguration(queryParameterStripDirective: "StripNone", dynamicCompression: "Disabled"), backendPool: new refID("/subscriptions/" + subid + "/resourceGroups/" + resourceGroupName + "/providers/Microsoft.Network/frontDoors/" + frontDoorName + "/backendPools/backendPool1") ) ); RulesEngineMatchCondition rulesEngineMatchCondition1 = new RulesEngineMatchCondition( rulesEngineMatchVariable: "RequestHeader", selector: "Rules-engine-action", rulesEngineOperator: "Equal", rulesEngineMatchValue: new List <string> { "Route-override-forwarding" }, negateCondition: false, transforms: new List <string> { } ); RulesEngineRule rule1 = new RulesEngineRule( priority: 1, name: "Rules1", matchProcessingBehavior: "Stop", action: rulesEngineAction1, matchConditions: new List <RulesEngineMatchCondition> { rulesEngineMatchCondition1 } ); RulesEngine rulesEngine1 = new RulesEngine( rules: new List <RulesEngineRule> { rule1 }, name: "RulesEngine1" ); var rulesEngineParameters = new List <RulesEngine> { rulesEngine1 }; var createdRulesEngine = frontDoorMgmtClient.RulesEngines.CreateOrUpdate(resourceGroupName, frontDoorName, rulesEngine1.Name, rulesEngineParameters[0]); // Validate correct rules engine created VerifyRulesEngine(createdRulesEngine, rulesEngineParameters[0]); // Retrieve rules engines for front door var retrievedRulesEngines = frontDoorMgmtClient.RulesEngines.ListByFrontDoor(resourceGroupName, frontDoorName).ToList <RulesEngine>(); // Validate correct rules engines retrieved from front door VerifyRulesEngines(retrievedRulesEngines, rulesEngineParameters); // Retrieve frontdoor and validate rules engine present var retrievedFrontDoor = frontDoorMgmtClient.FrontDoors.Get(resourceGroupName, frontDoorName); VerifyFrontDoor(retrievedFrontDoor, createParameters); VerifyRulesEngines(retrievedFrontDoor.RulesEngines, rulesEngineParameters); // Link rules engine to routing rule // why did sdk not autogenerate into subresource? retrievedFrontDoor.RoutingRules[0].RulesEngine = new refID("/subscriptions/" + subid + "/resourceGroups/" + resourceGroupName + "/providers/Microsoft.Network/frontDoors/" + frontDoorName + "/rulesEngines/" + rulesEngine1.Name); var updatedFrontDoor = frontDoorMgmtClient.FrontDoors.CreateOrUpdate(resourceGroupName, frontDoorName, retrievedFrontDoor); // Validate rules engine has been linked. VerifyFrontDoor(updatedFrontDoor, retrievedFrontDoor); // Unlink rules engine and delete rules engine. frontDoorMgmtClient.FrontDoors.CreateOrUpdate(resourceGroupName, frontDoorName, createParameters); frontDoorMgmtClient.RulesEngines.Delete(resourceGroupName, frontDoorName, rulesEngine1.Name); // Verify rules engine deleted retrievedRulesEngines = frontDoorMgmtClient.RulesEngines.ListByFrontDoor(resourceGroupName, frontDoorName).ToList <RulesEngine>(); Assert.Empty(retrievedRulesEngines); // Delete frontDoor frontDoorMgmtClient.FrontDoors.Delete(resourceGroupName, frontDoorName); // Verify that frontDoor is deleted Assert.ThrowsAny <ErrorResponseException>(() => { frontDoorMgmtClient.FrontDoors.Get(resourceGroupName, frontDoorName); }); FrontDoorTestUtilities.DeleteResourceGroup(resourcesClient, resourceGroupName); } }
public void NetworkExperimentCRUDTest() { var handler1 = new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK }; var handler2 = new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK }; using (MockContext context = MockContext.Start(this.GetType())) { // Create clients var frontDoorMgmtClient = FrontDoorTestUtilities.GetFrontDoorManagementClient(context, handler1); var resourcesClient = FrontDoorTestUtilities.GetResourceManagementClient(context, handler2); // Get subscription id string subid = frontDoorMgmtClient.SubscriptionId; // Create resource group var resourceGroupName = FrontDoorTestUtilities.CreateResourceGroup(resourcesClient); // Create profile and experiment names string profileName = TestUtilities.GenerateName("networkExperimentProfile"); string experimentName = TestUtilities.GenerateName("experiment"); Profile profile = new Profile( enabledState: "Enabled", location: "EastUS", tags: new Dictionary <string, string> { { "key1", "value1" }, { "key2", "value2" } }); Experiment experiment = new Experiment( endpointA: new Endpoint( endpointProperty: "www.bing.com", name: "bing"), endpointB: new Endpoint( endpointProperty: "www.constoso.com", name: "contoso")); var createdProfile = frontDoorMgmtClient.NetworkExperimentProfiles.CreateOrUpdate(profileName, resourceGroupName, profile); // validate that correct profile is created VerifyProfile(profile, createdProfile); // Retrieve profile var retrievedProfile = frontDoorMgmtClient.NetworkExperimentProfiles.Get(resourceGroupName, profileName); // validate that correct profile is retrieved VerifyProfile(profile, retrievedProfile); // update profile retrievedProfile.Tags = new Dictionary <string, string> { { "key3", "value3" }, { "key4", "value4" } }; var updatedProfile = frontDoorMgmtClient.NetworkExperimentProfiles.CreateOrUpdate(profileName, resourceGroupName, retrievedProfile); // validate that profile is correctly updated VerifyProfile(retrievedProfile, updatedProfile); // add experiment to profile var createdExperiment = frontDoorMgmtClient.Experiments.CreateOrUpdate(resourceGroupName, profileName, experimentName, experiment); // validate experiment VerifyExperiment(experiment, createdExperiment); // get experiment var retrievedExperiment = frontDoorMgmtClient.Experiments.Get(resourceGroupName, profileName, experimentName); // validate experiment VerifyExperiment(experiment, retrievedExperiment); // delete experiment frontDoorMgmtClient.Experiments.Delete(resourceGroupName, profileName, experimentName); // verify experiment is deleted Assert.ThrowsAny <ErrorResponseException>(() => { frontDoorMgmtClient.Experiments.Get(resourceGroupName, profileName, experimentName); }); // delete profile frontDoorMgmtClient.NetworkExperimentProfiles.Delete(resourceGroupName, profileName); // Verify that profile is deleted Assert.ThrowsAny <ErrorResponseException>(() => { frontDoorMgmtClient.NetworkExperimentProfiles.Get(resourceGroupName, profileName); }); FrontDoorTestUtilities.DeleteResourceGroup(resourcesClient, resourceGroupName); } }