Skip to content

Commit 26f9266

Browse files
committed
Changes for New-AzureRoleDefinition commandlet that allows creating new roles
1 parent 732f80f commit 26f9266

File tree

24 files changed

+261
-27
lines changed

24 files changed

+261
-27
lines changed

src/ResourceManager/Batch/Commands.Batch.Test/Commands.Batch.Test.csproj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@
5959
<Reference Include="Microsoft.Azure.Gallery">
6060
<HintPath>..\..\..\packages\Microsoft.Azure.Gallery.2.6.2-preview\lib\net40\Microsoft.Azure.Gallery.dll</HintPath>
6161
</Reference>
62-
<Reference Include="Microsoft.Azure.Management.Authorization">
63-
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Authorization.0.18.0-preview\lib\net40\Microsoft.Azure.Management.Authorization.dll</HintPath>
62+
<Reference Include="Microsoft.Azure.Management.Authorization, Version=0.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
63+
<SpecificVersion>False</SpecificVersion>
64+
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Authorization.0.18.2-preview\lib\net40\Microsoft.Azure.Management.Authorization.dll</HintPath>
6465
</Reference>
6566
<Reference Include="Microsoft.Azure.Management.Batch">
6667
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Batch.1.3.0-preview\lib\net40\Microsoft.Azure.Management.Batch.dll</HintPath>
@@ -181,6 +182,7 @@
181182
<Compile Include="WorkItems\RemoveBatchWorkItemCommandTests.cs" />
182183
</ItemGroup>
183184
<ItemGroup>
185+
<None Include="app.config" />
184186
<None Include="MSSharedLibKey.snk" />
185187
<None Include="packages.config">
186188
<SubType>Designer</SubType>

src/ResourceManager/Batch/Commands.Batch.Test/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<package id="Microsoft.Azure.Common.Authentication" version="1.0.22-preview" targetFramework="net45" />
77
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
88
<package id="Microsoft.Azure.Gallery" version="2.6.2-preview" targetFramework="net45" />
9-
<package id="Microsoft.Azure.Management.Authorization" version="0.18.0-preview" targetFramework="net45" />
9+
<package id="Microsoft.Azure.Management.Authorization" version="0.18.2-preview" targetFramework="net45" />
1010
<package id="Microsoft.Azure.Management.Batch" version="1.3.0-preview" targetFramework="net45" />
1111
<package id="Microsoft.Azure.Management.Resources" version="2.18.0-preview" targetFramework="net45" />
1212
<package id="Microsoft.Azure.Test.Framework" version="1.0.5571.32271-prerelease" targetFramework="net45" />

src/ResourceManager/DataFactories/Commands.DataFactories.Test/Commands.DataFactories.Test.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@
5858
<HintPath>..\..\..\packages\Microsoft.Azure.Gallery.2.6.0-preview\lib\net40\Microsoft.Azure.Gallery.dll</HintPath>
5959
<HintPath>..\..\..\packages\Microsoft.Azure.Gallery.2.6.2-preview\lib\net40\Microsoft.Azure.Gallery.dll</HintPath>
6060
</Reference>
61-
<Reference Include="Microsoft.Azure.Management.Authorization">
61+
<Reference Include="Microsoft.Azure.Management.Authorization, Version=0.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
6262
<SpecificVersion>False</SpecificVersion>
63-
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Authorization.0.18.0-preview\lib\net40\Microsoft.Azure.Management.Authorization.dll</HintPath>
63+
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Authorization.0.18.2-preview\lib\net40\Microsoft.Azure.Management.Authorization.dll</HintPath>
6464
</Reference>
6565
<Reference Include="Microsoft.Azure.Management.DataFactories">
6666
<HintPath>..\..\..\packages\Microsoft.Azure.Management.DataFactories.0.15.6-preview\lib\net40\Microsoft.Azure.Management.DataFactories.dll</HintPath>
@@ -175,6 +175,7 @@
175175
<Compile Include="UnitTests\SetDataFactoryGatewayTests.cs" />
176176
</ItemGroup>
177177
<ItemGroup>
178+
<None Include="app.config" />
178179
<None Include="MSSharedLibKey.snk" />
179180
<None Include="packages.config">
180181
<SubType>Designer</SubType>

src/ResourceManager/DataFactories/Commands.DataFactories.Test/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<package id="Microsoft.Azure.Common.Authentication" version="1.0.22-preview" targetFramework="net45" />
66
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
77
<package id="Microsoft.Azure.Gallery" version="2.6.2-preview" targetFramework="net45" />
8-
<package id="Microsoft.Azure.Management.Authorization" version="0.18.0-preview" targetFramework="net45" />
8+
<package id="Microsoft.Azure.Management.Authorization" version="0.18.2-preview" targetFramework="net45" />
99
<package id="Microsoft.Azure.Management.DataFactories" version="0.15.6-preview" targetFramework="net45" />
1010
<package id="Microsoft.Azure.Management.Resources" version="2.18.0-preview" targetFramework="net45" />
1111
<package id="Microsoft.Azure.Test.Framework" version="1.0.5571.32271-prerelease" targetFramework="net45" />

src/ResourceManager/Resources/Commands.Resources.Test/Commands.Resources.Test.csproj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@
6363
<SpecificVersion>False</SpecificVersion>
6464
<HintPath>..\..\..\packages\Microsoft.Azure.Graph.RBAC.1.7.0-preview\lib\net40\Microsoft.Azure.Graph.RBAC.dll</HintPath>
6565
</Reference>
66-
<Reference Include="Microsoft.Azure.Management.Authorization">
67-
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Authorization.0.18.0-preview\lib\net40\Microsoft.Azure.Management.Authorization.dll</HintPath>
66+
<Reference Include="Microsoft.Azure.Management.Authorization, Version=0.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
67+
<SpecificVersion>False</SpecificVersion>
68+
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Authorization.0.18.2-preview\lib\net40\Microsoft.Azure.Management.Authorization.dll</HintPath>
6869
</Reference>
6970
<Reference Include="Microsoft.Azure.ResourceManager">
7071
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Resources.2.18.0-preview\lib\net40\Microsoft.Azure.ResourceManager.dll</HintPath>
@@ -173,6 +174,7 @@
173174
<Compile Include="ScenarioTests\ResourceGroupTests.cs" />
174175
<Compile Include="ScenarioTests\ResourceTests.cs" />
175176
<Compile Include="ScenarioTests\RoleAssignmentTests.cs" />
177+
<Compile Include="ScenarioTests\RoleDefinitionTests.cs" />
176178
<Compile Include="Templates\TestAzureResourceGroupTemplateCommandTests.cs" />
177179
<Compile Include="Templates\SaveAzureResourceGroupGalleryTemplateCommandTests.cs" />
178180
<Compile Include="Templates\GetAzureResourceGroupGalleryTemplateCommandTests.cs" />
@@ -245,6 +247,7 @@
245247
<Content Include="ScenarioTests\AuthorizationTests.ps1">
246248
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
247249
</Content>
250+
<None Include="Resources\NewRoleDefinition.json" />
248251
<None Include="ScenarioTests\ActiveDirectoryTests.ps1">
249252
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
250253
</None>
@@ -266,6 +269,7 @@
266269
<None Include="ScenarioTests\RoleAssignmentTests.ps1">
267270
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
268271
</None>
272+
<None Include="ScenarioTests\RoleDefinitionTests.ps1" />
269273
<None Include="SessionRecords\Microsoft.Azure.Commands.Resources.Test.ScenarioTests.ActiveDirectoryTests\TestGetADUserWithMail.json">
270274
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
271275
</None>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"Name": "CustomRole Test Role",
3+
"Description": "Test role",
4+
"Actions": [
5+
"Microsoft.Authorization/*/read",
6+
"Microsoft.Support/*"
7+
],
8+
"NotActions": [],
9+
"AssignableScopes": ["Scope1" , "Scope2"]
10+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
16+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
17+
using Xunit;
18+
19+
namespace Microsoft.Azure.Commands.Resources.Test.ScenarioTests
20+
{
21+
public class RoleDefinitionTests
22+
{
23+
[Fact(Skip = "Not implemented")]
24+
[Trait(Category.AcceptanceType, Category.CheckIn)]
25+
public void RoleDefinitionCreateTests()
26+
{
27+
ResourcesController.NewInstance.RunPsTest("Test-RoleDefinitionCreateTests");
28+
}
29+
}
30+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
16+
<#
17+
.SYNOPSIS
18+
Tests verify scenarios for RoleDefinitions creation.
19+
#>
20+
function Test-RoleDefinitionCreateTests
21+
{
22+
# Basic positive case - read from file
23+
$rdName = 'CustomRole Test Role'
24+
New-AzureRoleDefinition -InputFile .\Resources\NewRoleDefinition.json
25+
$rd = Get-AzureRoleDefinition -Name $rdName
26+
Assert-NotNull $rd
27+
Assert-AreEqual "Test role" $rd.Description
28+
Assert-AreEqual $true $rd.IsCustom
29+
Assert-NotNull $rd.Actions
30+
Assert-AreEqual "Microsoft.Authorization/*/read" $rd.Actions[0]
31+
Assert-AreEqual "Microsoft.Support/*" $rd.Actions[1]
32+
Assert-NotNull $rd.AssignableScopes
33+
# The below scopes may need to be changed to actual scope values like /subscriptions/.... to satisfy the ARM access checks for PUT requests
34+
Assert-AreEqual "Scope1" $rd.AssignableScopes[0]
35+
Assert-AreEqual "Scope2" $rd.AssignableScopes[1]
36+
37+
# Basic positive case - read from object
38+
$roleDef = Get-AzureRoleDefinition -Name "Virtual Machine Contributor"
39+
$roleDef.Id = $null
40+
$roleDef.Name = "Virtual machine restarter"
41+
$roleDef.Actions.Add("Microsoft.ClassicCompute/virtualMachines/restart/action")
42+
$roleDef.Description = "Can monitor and restart virtual machines"
43+
44+
New-AzureRoleDefinition -Role $roleDef
45+
$addedRoleDef = Get-AzureRoleDefinition -Name "Virtual machine restarter"
46+
47+
Assert-AreEqual $roleDef.Actions $addedRoleDef.Actions
48+
Assert-AreEqual $roleDef.Description $addedRoleDef.Description
49+
Assert-AreEqual $roleDef.AssignableScopes $addedRoleDef.AssignableScopes
50+
Assert-AreEqual $true $roleDef.IsCustom
51+
}

src/ResourceManager/Resources/Commands.Resources.Test/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
77
<package id="Microsoft.Azure.Gallery" version="2.6.2-preview" targetFramework="net45" />
88
<package id="Microsoft.Azure.Graph.RBAC" version="1.7.0-preview" targetFramework="net45" />
9-
<package id="Microsoft.Azure.Management.Authorization" version="0.18.0-preview" targetFramework="net45" />
9+
<package id="Microsoft.Azure.Management.Authorization" version="0.18.2-preview" targetFramework="net45" />
1010
<package id="Microsoft.Azure.Management.Resources" version="2.18.0-preview" targetFramework="net45" />
1111
<package id="Microsoft.Azure.Test.Framework" version="1.0.5571.32271-prerelease" targetFramework="net45" />
1212
<package id="Microsoft.Azure.Test.HttpRecorder" version="1.0.5571.32271-prerelease" targetFramework="net45" />

src/ResourceManager/Resources/Commands.Resources/Commands.Resources.csproj

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@
6868
<SpecificVersion>False</SpecificVersion>
6969
<HintPath>..\..\..\packages\Microsoft.Azure.Graph.RBAC.1.7.0-preview\lib\net40\Microsoft.Azure.Graph.RBAC.dll</HintPath>
7070
</Reference>
71-
<Reference Include="Microsoft.Azure.Management.Authorization">
72-
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Authorization.0.18.0-preview\lib\net40\Microsoft.Azure.Management.Authorization.dll</HintPath>
71+
<Reference Include="Microsoft.Azure.Management.Authorization, Version=0.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
72+
<SpecificVersion>False</SpecificVersion>
73+
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Authorization.0.18.2-preview\lib\net40\Microsoft.Azure.Management.Authorization.dll</HintPath>
7374
</Reference>
7475
<Reference Include="Microsoft.Azure.ResourceManager">
7576
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Resources.2.18.0-preview\lib\net40\Microsoft.Azure.ResourceManager.dll</HintPath>
@@ -186,7 +187,7 @@
186187
<Compile Include="Models.Authorization\AuthorizationClient.cs" />
187188
<Compile Include="ProviderFeatures\RegisterAzureProviderFeatureCmdlet.cs" />
188189
<Compile Include="Providers\GetAzureProviderCmdlet.cs" />
189-
<Compile Include="Providers\GetAzureProviderOperationCmdlet.cs" />
190+
<Compile Include="Providers\GetAzureProviderOperationCmdlet.cs" />
190191
<Compile Include="Providers\RegisterAzureProviderCmdlet.cs" />
191192
<Compile Include="Providers\UnregisterAzureProviderCmdlet.cs" />
192193
<Compile Include="ResourceGroups\GetAzureResourceGroupLogCommand.cs" />
@@ -198,6 +199,7 @@
198199
<Compile Include="RoleAssignments\GetAzureRoleAssignmentCommand.cs" />
199200
<Compile Include="RoleAssignments\NewAzureRoleAssignmentCommand.cs" />
200201
<Compile Include="RoleDefinitions\GetAzureRoleDefinitionCommand.cs" />
202+
<Compile Include="RoleDefinitions\NewAzureRoleDefinitionCommand.cs" />
201203
<Compile Include="Templates\TestAzureResourceGroupTemplateCommand.cs" />
202204
<Compile Include="Templates\SaveAzureResourceGroupGalleryTemplateCommand.cs" />
203205
<Compile Include="Templates\GetAzureResourceGroupGalleryTemplateCommand.cs" />

src/ResourceManager/Resources/Commands.Resources/Models.ActiveDirectory/ParameterSet.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,9 @@ internal static class ParameterSet
6767
public const string ApplicationWithKeyCredential = "ApplicationWithKeyCredentialParameterSet";
6868

6969
public const string Empty = "EmptyParameterSet";
70+
71+
public const string InputFile = "InputFileParameterSet";
72+
73+
public const string RoleDefinition = "RoleDefinitionParameterSet";
7074
}
7175
}

src/ResourceManager/Resources/Commands.Resources/Models.Authorization/AuthorizationClient.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,5 +178,44 @@ public PSRoleDefinition GetRoleRoleDefinition(string name)
178178

179179
return role;
180180
}
181+
182+
public PSRoleDefinition CreateRoleDefinition(PSRoleDefinition roleDefinition)
183+
{
184+
AuthorizationClient.ValidateRoleDefinition(roleDefinition);
185+
186+
Guid newRoleDefinitionId = Guid.NewGuid();
187+
RoleDefinitionCreateOrUpdateParameters parameters = new RoleDefinitionCreateOrUpdateParameters()
188+
{
189+
RoleDefinition = new RoleDefinition()
190+
{
191+
Name = newRoleDefinitionId,
192+
Properties = new RoleDefinitionProperties()
193+
{
194+
AssignableScopes = roleDefinition.AssignableScopes,
195+
Description = roleDefinition.Description,
196+
Permissions = new List<Permission>()
197+
{
198+
new Permission()
199+
{
200+
Actions = roleDefinition.Actions,
201+
NotActions = roleDefinition.NotActions
202+
}
203+
},
204+
RoleName = roleDefinition.Name,
205+
Type = "CustomRole"
206+
}
207+
}
208+
};
209+
210+
return AuthorizationManagementClient.RoleDefinitions.CreateOrUpdate(newRoleDefinitionId, parameters).RoleDefinition.ToPSRoleDefinition();
211+
}
212+
213+
private static void ValidateRoleDefinition(PSRoleDefinition roleDefinition)
214+
{
215+
if (string.IsNullOrWhiteSpace(roleDefinition.Name))
216+
{
217+
throw new ArgumentException(ProjectResources.InvalidRoleDefinitionName);
218+
}
219+
}
181220
}
182221
}

src/ResourceManager/Resources/Commands.Resources/Models.Authorization/AuthorizationClientExtensions.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ public static PSRoleDefinition ToPSRoleDefinition(this RoleDefinition role)
3232
Name = role.Properties.RoleName,
3333
Actions = new List<string>(role.Properties.Permissions.SelectMany(r => r.Actions)),
3434
NotActions = new List<string>(role.Properties.Permissions.SelectMany(r => r.NotActions)),
35-
Id = role.Id
35+
Id = role.Id,
36+
AssignableScopes = role.Properties.AssignableScopes.ToList(),
37+
Description = role.Properties.Description,
38+
IsCustom = role.Properties.Type == "CustomRole" ? true : false
3639
};
3740
}
3841

src/ResourceManager/Resources/Commands.Resources/Models.Authorization/PSRoleDefinition.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,14 @@ public class PSRoleDefinition
2222

2323
public string Id { get; set; }
2424

25+
public bool IsCustom { get; set; }
26+
27+
public string Description { get; set; }
28+
2529
public List<string> Actions { get; set; }
2630

2731
public List<string> NotActions { get; set; }
32+
33+
public List<string> AssignableScopes { get; set; }
2834
}
2935
}

src/ResourceManager/Resources/Commands.Resources/Properties/Resources.Designer.cs

Lines changed: 12 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ResourceManager/Resources/Commands.Resources/Properties/Resources.resx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,4 +282,7 @@
282282
<data name="RegisterProviderFeatureMessage" xml:space="preserve">
283283
<value>Registering provider feature ...</value>
284284
</data>
285-
</root>
285+
<data name="InvalidRoleDefinitionName" xml:space="preserve">
286+
<value>RoleDefinitionName is invalid</value>
287+
</data>
288+
</root>

0 commit comments

Comments
 (0)