Skip to content

Commit af5b8cc

Browse files
author
John Paul Kee
committed
Create new cloned permissive record matcher named permissive record matcher with resource api exclusion and update sql tests to use that new matcher for safety
1 parent c972fc5 commit af5b8cc

File tree

4 files changed

+266
-115
lines changed

4 files changed

+266
-115
lines changed

src/Sql/Sql.Test/ScenarioTests/SqlTestsBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ protected void RunPowerShellTest(params string[] scripts)
7878
{"Microsoft.Azure.Graph.RBAC.Version1_6.GraphRbacManagementClient", "1.42-previewInternal"},
7979
{"Microsoft.Azure.Management.Resources.ResourceManagementClient", "2016-02-01"}
8080
};
81-
HttpMockServer.Matcher = new PermissiveRecordMatcherWithApiExclusion(true, d, providersToIgnore, resourceTypesToIgnoreApiVersion);
81+
HttpMockServer.Matcher = new PermissiveRecordMatcherWithResourceApiExclusion(true, d, providersToIgnore, resourceTypesToIgnoreApiVersion);
8282
HttpMockServer.RecordsDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SessionRecords");
8383

8484
// Enable undo functionality as well as mock recording

src/Sql/Sql.Test/UnitTests/PermissiveRecordMatcherWithApiExclusionUnitTests.cs renamed to src/Sql/Sql.Test/UnitTests/PermissiveRecordMatcherWithResourceApiExclusionUnitTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77

88
namespace Microsoft.Azure.Commands.Sql.Test.UnitTests
99
{
10-
public class PermissiveRecordMatcherUnitTests
10+
public class PermissiveRecordMatcherWithResourceApiExclusionUnitTests
1111
{
1212
[Fact]
1313
[Trait(Category.AcceptanceType, Category.CheckIn)]
14-
public void PermissiveRecordMatcherWithApiExclusion_ContainsIgnoredProvider()
14+
public void PermissiveRecordMatcherWithResourceApiExclusion_ContainsIgnoredProvider()
1515
{
1616
var testRequestUris = new string[]
1717
{
@@ -70,7 +70,7 @@ private void TestContainsIgnoredProvider(
7070
var numIgnored = 0;
7171
foreach (var testUri in requestUrisToTest)
7272
{
73-
var result = PermissiveRecordMatcherWithApiExclusion.ContainsIgnoredProvider(
73+
var result = PermissiveRecordMatcherWithResourceApiExclusion.ContainsIgnoredProvider(
7474
requestUri: testUri,
7575
shouldIgnoreGenericResource: false,
7676
providersToIgnore: new Dictionary<string, string>(),

tools/ScenarioTest.ResourceManager/PermissiveRecordMatcherWithApiExclusion.cs

Lines changed: 34 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,33 @@
1-
// ----------------------------------------------------------------------------------
2-
//
3-
// Copyright Microsoft Corporation
4-
// Licensed under the Apache License, Version 2.0 (the "License");
5-
// you may not use this file except in compliance with the License.
6-
// You may obtain a copy of the License at
7-
// http://www.apache.org/licenses/LICENSE-2.0
8-
// Unless required by applicable law or agreed to in writing, software
9-
// distributed under the License is distributed on an "AS IS" BASIS,
10-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11-
// See the License for the specific language governing permissions and
12-
// limitations under the License.
13-
// ----------------------------------------------------------------------------------
14-
15-
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
1615
using Microsoft.Azure.Test.HttpRecorder;
1716
using System;
1817
using System.Collections.Generic;
19-
using System.Diagnostics;
2018
using System.Linq;
2119
using System.Text;
2220
using System.Text.RegularExpressions;
2321

2422
namespace Microsoft.WindowsAzure.Commands.ScenarioTest
2523
{
26-
// Excludes api version when matching mocked records.
24+
// Excludes api version when matching mocked records.
2725
// If alternate api version is provided, uses that to match records else removes the api-version matching.
2826
public class PermissiveRecordMatcherWithApiExclusion : IRecordMatcher
2927
{
3028
protected bool _ignoreGenericResource;
3129
protected Dictionary<string, string> _providersToIgnore;
3230
protected Dictionary<string, string> _userAgentsToIgnore;
33-
protected string[] _resourcesToIgnore;
3431

3532
public PermissiveRecordMatcherWithApiExclusion(bool ignoreResourcesClient, Dictionary<string, string> providers)
3633
{
@@ -48,30 +45,18 @@ public PermissiveRecordMatcherWithApiExclusion(
4845
_userAgentsToIgnore = userAgents;
4946
}
5047

51-
public PermissiveRecordMatcherWithApiExclusion(
52-
bool ignoreResourcesClient,
53-
Dictionary<string, string> providers,
54-
Dictionary<string, string> userAgents,
55-
string[] resourcesToIgnore)
56-
{
57-
_ignoreGenericResource = ignoreResourcesClient;
58-
_providersToIgnore = providers;
59-
_userAgentsToIgnore = userAgents;
60-
_resourcesToIgnore = resourcesToIgnore;
61-
}
62-
6348
public virtual string GetMatchingKey(System.Net.Http.HttpRequestMessage request)
6449
{
65-
var requestUri = request.RequestUri.PathAndQuery;
66-
if (requestUri.Contains("?&"))
50+
var path = request.RequestUri.PathAndQuery;
51+
if (path.Contains("?&"))
6752
{
68-
requestUri = requestUri.Replace("?&", "?");
53+
path = path.Replace("?&", "?");
6954
}
7055

7156
string version;
72-
if (ContainsIgnoredProvider(requestUri, out version))
57+
if (ContainsIgnoredProvider(path, out version))
7358
{
74-
requestUri = RemoveOrReplaceApiVersion(requestUri, version);
59+
path = RemoveOrReplaceApiVersion(path, version);
7560
}
7661
else if (_userAgentsToIgnore != null && _userAgentsToIgnore.Any())
7762
{
@@ -82,62 +67,46 @@ public virtual string GetMatchingKey(System.Net.Http.HttpRequestMessage request)
8267
{
8368
if (agent.Value.Any(v => v.StartsWith(userAgnet.Key, StringComparison.OrdinalIgnoreCase)))
8469
{
85-
requestUri = RemoveOrReplaceApiVersion(requestUri, userAgnet.Value);
70+
path = RemoveOrReplaceApiVersion(path, userAgnet.Value);
8671
break;
8772
}
8873
}
8974
}
9075
}
9176

92-
var encodedPath = Convert.ToBase64String(Encoding.UTF8.GetBytes(requestUri));
77+
var encodedPath = Convert.ToBase64String(Encoding.UTF8.GetBytes(path));
9378
return string.Format("{0} {1}", request.Method, encodedPath);
9479
}
9580

9681
public virtual string GetMatchingKey(RecordEntry recordEntry)
9782
{
9883
var encodedPath = recordEntry.EncodedRequestUri;
99-
var requestUri = recordEntry.RequestUri;
84+
var path = recordEntry.RequestUri;
10085
var changed = false;
101-
if (requestUri.Contains("?&"))
86+
if (path.Contains("?&"))
10287
{
103-
requestUri = recordEntry.RequestUri.Replace("?&", "?");
88+
path = recordEntry.RequestUri.Replace("?&", "?");
10489
changed = true;
10590
}
10691

10792
string version;
108-
if (ContainsIgnoredProvider(requestUri, out version))
93+
if (ContainsIgnoredProvider(path, out version))
10994
{
110-
requestUri = RemoveOrReplaceApiVersion(requestUri, version);
95+
path = RemoveOrReplaceApiVersion(path, version);
11196
changed = true;
11297
}
11398

11499
if (changed)
115100
{
116-
encodedPath = Convert.ToBase64String(Encoding.UTF8.GetBytes(requestUri));
101+
encodedPath = Convert.ToBase64String(Encoding.UTF8.GetBytes(path));
117102
}
118103

119104
return string.Format("{0} {1}", recordEntry.RequestMethod, encodedPath);
120105
}
121106

122-
public bool ContainsIgnoredProvider(string requestUri, out string version)
107+
protected bool ContainsIgnoredProvider(string requestUri, out string version)
123108
{
124-
return ContainsIgnoredProvider(requestUri, _ignoreGenericResource, _providersToIgnore, _resourcesToIgnore, apiVersion: out version);
125-
}
126-
127-
/// <summary>
128-
/// Helper method to determine whether or not this request api version should be ignored
129-
/// </summary>
130-
/// <param name="requestUri">The request uri</param>
131-
/// <param name="apiVersion">The api verison to use</paraam>
132-
/// <returns></returns>
133-
public static bool ContainsIgnoredProvider(
134-
string requestUri,
135-
bool shouldIgnoreGenericResource,
136-
Dictionary<string, string> providersToIgnore,
137-
string[] resourcesToIgnore,
138-
out string apiVersion)
139-
{
140-
if (shouldIgnoreGenericResource &&
109+
if (_ignoreGenericResource &&
141110
!requestUri.Contains("providers/") &&
142111
!requestUri.StartsWith("/certificates?", StringComparison.InvariantCultureIgnoreCase) &&
143112
!requestUri.StartsWith("/pools", StringComparison.InvariantCultureIgnoreCase) &&
@@ -147,68 +116,22 @@ public static bool ContainsIgnoredProvider(
147116
!requestUri.Contains("/servicePrincipals?") &&
148117
!requestUri.StartsWith("/webhdfs/v1/?aclspec", StringComparison.InvariantCultureIgnoreCase))
149118
{
150-
apiVersion = String.Empty;
119+
version = String.Empty;
151120
return true;
152121
}
153122

154-
// Ignore resource providers
155-
foreach (var provider in providersToIgnore)
123+
foreach (var provider in _providersToIgnore)
156124
{
157125
var providerString = string.Format("providers/{0}", provider.Key);
158126
if (requestUri.Contains(providerString))
159127
{
160-
apiVersion = provider.Value;
128+
version = provider.Value;
161129
return true;
162130
}
163131
}
164132

165-
if (resourcesToIgnore != null && resourcesToIgnore.Any())
166-
{
167-
// If we're looking at a specific provider and we have top level resource from this provider to ignore
168-
foreach (var resourceToIgnore in resourcesToIgnore)
169-
{
170-
string[] segments = requestUri.Split(new char[] { '/' }, options: StringSplitOptions.RemoveEmptyEntries);
171-
172-
// /subscriptions/.../resourceGroups/.../providers/Microsoft.X/resourceType...?api-version=Y
173-
var regex = new Regex(@"\/subscriptions\/[0-9A-Fa-f-]*\/resourceGroups\/[a-zA-Z0-9_-]*\/providers\/[a-zA-Z0-9_-]*.[a-zA-Z0-9_-]*\/.*[?api-version=[0-9-]*]?");
174-
if (regex.IsMatch(requestUri))
175-
{
176-
if (segments.Length > 7)
177-
{
178-
var resourceIdentifier = new ResourceIdentifier(requestUri);
179-
if (resourceIdentifier.ResourceType == resourceToIgnore)
180-
{
181-
apiVersion = String.Empty;
182-
return true;
183-
}
184-
}
185-
else if (segments.Length == 7)
186-
{
187-
var resourceType = $"{segments[5]}/{segments[6]}";
188-
if (resourceType.Contains(resourceToIgnore))
189-
{
190-
apiVersion = String.Empty;
191-
return true;
192-
}
193-
}
194-
}
195-
196-
// /subscriptions/.../providers/Microsoft.Provider/resourceType
197-
regex = new Regex(@"\/subscriptions\/[0-9A-Fa-f-]*\/providers\/[a-zA-Z0-9_-]*.[a-zA-Z0-9_-]*\/.*?[api-version=[0-9-]*]?");
198-
if (regex.IsMatch(requestUri))
199-
{
200-
var resourceType = $"{segments[3]}/{segments[4]}";
201-
if (resourceType.Contains(resourceToIgnore))
202-
{
203-
apiVersion = String.Empty;
204-
return true;
205-
}
206-
}
207-
}
208-
}
209-
210133

211-
apiVersion = string.Empty;
134+
version = string.Empty;
212135
return false;
213136
}
214137

0 commit comments

Comments
 (0)