Skip to content

Authorization: Changes to the Get-AzureRoleAssignment commandlet to also display classic admins and perform group expansion for users #846

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Sep 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
eb0a088
Authorization:Changes to Get-AzureroleAssignment commandlet to expand…
namratab Sep 2, 2015
43cec70
Update with official Authorization 'nuget package
namratab Sep 3, 2015
2f0d532
Fix packages.config changes that were missed
namratab Sep 3, 2015
cf14a21
Revert other RPs to use the older version of the Authorization packag…
namratab Sep 3, 2015
24b042e
Use latest Authorization package for Network and Keyvalut projects du…
namratab Sep 10, 2015
dae6fda
[Test:do not merge]Attempt to fix SQl tests by using Record matcher t…
namratab Sep 14, 2015
5f2f930
Merge changes with upstream dev
namratab Sep 14, 2015
aa1e830
Fix build error
namratab Sep 14, 2015
ffa2ac1
Merge branch 'dev' of github.com:Azure/azure-powershell into roleassi…
namratab Sep 14, 2015
7766973
Fix break
namratab Sep 14, 2015
3aba220
Re-record certain Resources tests
namratab Sep 15, 2015
f24dff3
Merge branch 'dev' of github.com:Azure/azure-powershell into roleassi…
namratab Sep 15, 2015
27791a5
Merge branch 'dev' of github.com:Azure/azure-powershell into roleassi…
namratab Sep 15, 2015
eca4e40
Fix Compute and Dns tests to exclude api version for permissions
namratab Sep 15, 2015
2ece092
Update api version for remaining test projects and add exclusion for …
namratab Sep 15, 2015
8090605
Merge branch 'dev' of github.com:Azure/azure-powershell into roleassi…
namratab Sep 15, 2015
cd74631
Re-record operation insight tests
namratab Sep 15, 2015
eca9f59
Fix api management test
namratab Sep 15, 2015
af617d6
Refactor recordmatcher code
namratab Sep 15, 2015
c4e0d6a
Address review feedback for authorization commands
namratab Sep 15, 2015
b39bd6d
Merge branch 'dev' of github.com:Azure/azure-powershell into roleassi…
namratab Sep 15, 2015
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
<Compile Include="Mocks\MockClientFactory.cs" />
<Compile Include="Mocks\MockTokenAuthenticationFactory.cs" />
<Compile Include="PermissiveRecordMatcher.cs" />
<Compile Include="PermissiveRecordMatcherWithApiExclusion.cs" />
<Compile Include="PowerShellExtensions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SMTestBase.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.Azure.Test.HttpRecorder;

namespace Microsoft.WindowsAzure.Commands.ScenarioTest
{
public class PermissiveRecordMatcherWithApiExclusion : IRecordMatcher
{
private bool _ignoreGenericResource;
private Dictionary<string, string> _providersToIgnore;

public PermissiveRecordMatcherWithApiExclusion(bool ignoreResourcesClient, Dictionary<string, string> providers)
{
_ignoreGenericResource = ignoreResourcesClient;
_providersToIgnore = providers;
}

public string GetMatchingKey(System.Net.Http.HttpRequestMessage request)
{
var path = request.RequestUri.PathAndQuery;
if (path.Contains("?&"))
{
path = path.Replace("?&", "?");
}

string version;
if (ContainsIgnoredProvider(path, out version))
{
path = RemoveApiVersion(path, version);
}

var encodedPath = Convert.ToBase64String(Encoding.UTF8.GetBytes(path));
return string.Format("{0} {1}", request.Method, encodedPath);
}

public string GetMatchingKey(RecordEntry recordEntry)
{
var encodedPath = recordEntry.EncodedRequestUri;
if (recordEntry.RequestUri.Contains("?&"))
{
var updatedPath = recordEntry.RequestUri.Replace("?&", "?");

string version;
if (ContainsIgnoredProvider(updatedPath, out version))
{
updatedPath = RemoveApiVersion(updatedPath, version);
}

encodedPath = Convert.ToBase64String(Encoding.UTF8.GetBytes(updatedPath));
}

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

private bool ContainsIgnoredProvider(string requestUri, out string version)
{
if (_ignoreGenericResource && !requestUri.Contains("providers"))
{
version = String.Empty;
return true;
}

foreach (var provider in _providersToIgnore)
{
var providerString = string.Format("providers/{0}", provider.Key);
if (requestUri.Contains(providerString))
{
version = provider.Value;
return true;
}
}

version = string.Empty;
return false;
}

private string RemoveApiVersion(string requestUri, string version)
{
return Regex.Replace(requestUri, @"\?api-version=[^&]+", string.Format("?api-version={0}", version));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
<Compile Include="EnvironmentSetupHelper.cs" />
<Compile Include="Mocks\MockClientFactory.cs" />
<Compile Include="Mocks\MockCommandRuntime.cs" />
<Compile Include="PermissiveRecordMatcherWithApiExclusion.cs" />
<Compile Include="PSCmdletExtensions.cs" />
<Compile Include="Constants.cs" />
<Compile Include="Mocks\MockAccessToken.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.Azure.Test.HttpRecorder;

namespace Microsoft.WindowsAzure.Commands.ScenarioTest
{
// Excludes api version when matching mocked records.
// If alternate api version is provided, uses that to match records else removes the api-version matching.
public class PermissiveRecordMatcherWithApiExclusion : IRecordMatcher
{
private bool _ignoreGenericResource;
private Dictionary<string, string> _providersToIgnore;

public PermissiveRecordMatcherWithApiExclusion(bool ignoreResourcesClient, Dictionary<string, string> providers)
{
_ignoreGenericResource = ignoreResourcesClient;
_providersToIgnore = providers;
}

public string GetMatchingKey(System.Net.Http.HttpRequestMessage request)
{
var path = request.RequestUri.PathAndQuery;
if (path.Contains("?&"))
{
path = path.Replace("?&", "?");
}

string version;
if (ContainsIgnoredProvider(path, out version))
{
path = RemoveOrReplaceApiVersion(path, version);
}

var encodedPath = Convert.ToBase64String(Encoding.UTF8.GetBytes(path));
return string.Format("{0} {1}", request.Method, encodedPath);
}

public string GetMatchingKey(RecordEntry recordEntry)
{
var encodedPath = recordEntry.EncodedRequestUri;
if (recordEntry.RequestUri.Contains("?&"))
{
var updatedPath = recordEntry.RequestUri.Replace("?&", "?");


string version;
if (ContainsIgnoredProvider(updatedPath, out version))
{
updatedPath = RemoveOrReplaceApiVersion(updatedPath, version);
}

encodedPath = Convert.ToBase64String(Encoding.UTF8.GetBytes(updatedPath));
}

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

private bool ContainsIgnoredProvider(string requestUri, out string version)
{
if (_ignoreGenericResource && !requestUri.Contains("providers"))
{
version = String.Empty;
return true;
}

foreach (var provider in _providersToIgnore)
{
var providerString = string.Format("providers/{0}", provider.Key);
if (requestUri.Contains(providerString))
{
version = provider.Value;
return true;
}
}


version = string.Empty;
return false;
}

private string RemoveOrReplaceApiVersion(string requestUri, string version)
{
if (!string.IsNullOrWhiteSpace(version))
{
return Regex.Replace(requestUri, @"\?api-version=[^&]+", string.Format("?api-version={0}", version));
}
else
{
return Regex.Replace(requestUri, @"\?api-version=[^&]+", string.Empty);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Collections.Generic;

namespace Microsoft.Azure.Commands.ApiManagement.Test.ScenarioTests
{
using System;
Expand Down Expand Up @@ -146,6 +148,9 @@ private void RunPowerShellTest(params string[] scripts)
// "TEST_ORGID_AUTHENTICATION",
// "SubscriptionId=;Environment=");
#endif
Dictionary<string, string> d = new Dictionary<string, string>();
d.Add("Microsoft.Authorization", "2014-07-01-preview");
HttpMockServer.Matcher = new PermissiveRecordMatcherWithApiExclusion(false, d);

using (var context = UndoContext.Current)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,8 @@
<Reference Include="Microsoft.Azure.Gallery">
<HintPath>..\..\..\packages\Microsoft.Azure.Gallery.2.6.2-preview\lib\net40\Microsoft.Azure.Gallery.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Management.Authorization, Version=0.9.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Authorization.0.19.2-preview\lib\net40\Microsoft.Azure.Management.Authorization.dll</HintPath>
<Reference Include="Microsoft.Azure.Management.Authorization">
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Authorization.1.0.0\lib\net40\Microsoft.Azure.Management.Authorization.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Management.Batch">
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Batch.1.4.0\lib\net40\Microsoft.Azure.Management.Batch.dll</HintPath>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using Microsoft.Azure.Test.HttpRecorder;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Microsoft.Azure.Commands.Batch.Test.ScenarioTests
Expand Down Expand Up @@ -73,7 +74,9 @@ public void RunPsTestWorkflow(
string callingClassType,
string mockName)
{
HttpMockServer.Matcher = new PermissiveRecordMatcher();
Dictionary<string, string> d = new Dictionary<string, string>();
d.Add("Microsoft.Authorization", "2014-07-01-preview");
HttpMockServer.Matcher = new PermissiveRecordMatcherWithApiExclusion(false, d);
using (UndoContext context = UndoContext.Current)
{
context.Start(callingClassType, mockName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<package id="Microsoft.Azure.Common.Authentication" version="1.1.4-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.Azure.Gallery" version="2.6.2-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Authorization" version="0.19.2-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Authorization" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Batch" version="1.4.0" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Resources" version="2.18.7-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Test.Framework" version="1.0.5715.36130-prerelease" targetFramework="net45" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\packages\Microsoft.Azure.Graph.RBAC.1.7.0-preview\lib\net40\Microsoft.Azure.Graph.RBAC.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Management.Authorization, Version=0.9.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Authorization.0.19.2-preview\lib\net40\Microsoft.Azure.Management.Authorization.dll</HintPath>
<Reference Include="Microsoft.Azure.Management.Authorization">
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Authorization.1.0.0\lib\net40\Microsoft.Azure.Management.Authorization.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Management.Compute, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Compute.8.2.0\lib\net40\Microsoft.Azure.Management.Compute.dll</HintPath>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
using Microsoft.Azure.Management.Storage;
using Microsoft.Azure.Test;
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Azure.Common.Authentication;
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
Expand Down Expand Up @@ -96,6 +97,10 @@ public void RunPsTestWorkflow(
string callingClassType,
string mockName)
{
Dictionary<string, string> d = new Dictionary<string, string>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could u move this code to RMTestBase instead of duplicating it everywhere in the code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is used only for a few of the test projects which depend on authorization RP. Not needed for all tests. Is there benefit in moving it there? Keeping it here also makes it explicit that en exclusion is added for particular test projects only.

d.Add("Microsoft.Authorization", "2014-07-01-preview");
HttpMockServer.Matcher = new PermissiveRecordMatcherWithApiExclusion(false, d);

using (UndoContext context = UndoContext.Current)
{
context.Start(callingClassType, mockName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.Azure.Gallery" version="2.6.2-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Graph.RBAC" version="1.7.0-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Authorization" version="0.19.2-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Authorization" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Compute" version="8.2.0" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Network" version="2.0.9-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Resources" version="2.18.7-preview" targetFramework="net45" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\packages\Microsoft.Azure.Graph.RBAC.1.7.0-preview\lib\net40\Microsoft.Azure.Graph.RBAC.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Management.Authorization, Version=0.9.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Authorization.0.19.2-preview\lib\net40\Microsoft.Azure.Management.Authorization.dll</HintPath>
<Reference Include="Microsoft.Azure.Management.Authorization">
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Authorization.1.0.0\lib\net40\Microsoft.Azure.Management.Authorization.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.Management.Compute, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Compute.8.2.0\lib\net40\Microsoft.Azure.Management.Compute.dll</HintPath>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.Azure.Gallery" version="2.6.2-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Graph.RBAC" version="1.7.0-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Authorization" version="0.19.2-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Authorization" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Compute" version="8.2.0" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Network" version="2.0.9-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Resources" version="2.18.7-preview" targetFramework="net45" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@
<HintPath>..\..\..\packages\Microsoft.Azure.KeyVault.Core.1.0.0\lib\net40\Microsoft.Azure.KeyVault.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.Management.Authorization, Version=0.9.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Reference Include="Microsoft.Azure.Management.Authorization">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Authorization.0.19.2-preview\lib\net40\Microsoft.Azure.Management.Authorization.dll</HintPath>
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Authorization.1.0.0\lib\net40\Microsoft.Azure.Management.Authorization.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Management.DataFactories, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\Microsoft.Azure.Management.DataFactories.3.0.0\lib\net45\Microsoft.Azure.Management.DataFactories.dll</HintPath>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using System.Collections.Generic;
using Microsoft.Azure.Common.Authentication;
using Microsoft.Azure.Gallery;
using Microsoft.Azure.Management.Authorization;
Expand Down Expand Up @@ -53,7 +54,9 @@ protected void SetupManagementClients()

protected void RunPowerShellTest(params string[] scripts)
{
HttpMockServer.Matcher = new PermissiveRecordMatcher();
Dictionary<string, string> d = new Dictionary<string, string>();
d.Add("Microsoft.Authorization", "2014-07-01-preview");
HttpMockServer.Matcher = new PermissiveRecordMatcherWithApiExclusion(false, d);
using (UndoContext context = UndoContext.Current)
{
context.Start(TestUtilities.GetCallingClass(2), TestUtilities.GetCurrentMethodName(2));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.Azure.Gallery" version="2.6.2-preview" targetFramework="net45" />
<package id="Microsoft.Azure.KeyVault.Core" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Authorization" version="0.19.2-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Authorization" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Resources" version="2.18.7-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Test.Framework" version="1.0.5715.36130-prerelease" targetFramework="net45" />
<package id="Microsoft.Azure.Test.HttpRecorder" version="1.0.5715.36130-prerelease" targetFramework="net45" />
<package id="Microsoft.Azure.Management.DataFactories" version="3.0.0" targetFramework="net45" /> <package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />
<package id="Microsoft.Azure.Management.DataFactories" version="3.0.0" targetFramework="net45" />
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />
<package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net45" />
<package id="Microsoft.Bcl.Build" version="1.0.14" targetFramework="net45" />
<package id="Microsoft.Data.Edm" version="5.6.4" targetFramework="net45" />
Expand Down
Loading