Skip to content

Commit 74b2c87

Browse files
committed
Merge pull request Azure#82 from rakashya/UpdateRD
Power shell changes for Set-AzureRoleDefinition and Remove-AzureRoleDefinition
2 parents 293a28e + cc36c94 commit 74b2c87

File tree

24 files changed

+380
-31
lines changed

24 files changed

+380
-31
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
<HintPath>..\..\..\packages\Microsoft.Azure.Gallery.2.6.2-preview\lib\net40\Microsoft.Azure.Gallery.dll</HintPath>
6161
</Reference>
6262
<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>
63+
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Authorization.0.18.1-preview\lib\net40\Microsoft.Azure.Management.Authorization.dll</HintPath>
6464
</Reference>
6565
<Reference Include="Microsoft.Azure.Management.Batch">
6666
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Batch.1.3.0-preview\lib\net40\Microsoft.Azure.Management.Batch.dll</HintPath>

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.1-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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
</Reference>
6161
<Reference Include="Microsoft.Azure.Management.Authorization">
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.1-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>

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.1-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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
<HintPath>..\..\..\packages\Microsoft.Azure.Graph.RBAC.1.7.0-preview\lib\net40\Microsoft.Azure.Graph.RBAC.dll</HintPath>
6565
</Reference>
6666
<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>
67+
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Authorization.0.18.1-preview\lib\net40\Microsoft.Azure.Management.Authorization.dll</HintPath>
6868
</Reference>
6969
<Reference Include="Microsoft.Azure.ResourceManager">
7070
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Resources.2.18.0-preview\lib\net40\Microsoft.Azure.ResourceManager.dll</HintPath>
@@ -173,6 +173,7 @@
173173
<Compile Include="ScenarioTests\ResourceGroupTests.cs" />
174174
<Compile Include="ScenarioTests\ResourceTests.cs" />
175175
<Compile Include="ScenarioTests\RoleAssignmentTests.cs" />
176+
<Compile Include="ScenarioTests\RoleDefinitionTests.cs" />
176177
<Compile Include="Templates\TestAzureResourceGroupTemplateCommandTests.cs" />
177178
<Compile Include="Templates\SaveAzureResourceGroupGalleryTemplateCommandTests.cs" />
178179
<Compile Include="Templates\GetAzureResourceGroupGalleryTemplateCommandTests.cs" />
@@ -245,6 +246,7 @@
245246
<Content Include="ScenarioTests\AuthorizationTests.ps1">
246247
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
247248
</Content>
249+
<None Include="Resources\RoleDefinition.json" />
248250
<None Include="ScenarioTests\ActiveDirectoryTests.ps1">
249251
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
250252
</None>
@@ -266,6 +268,7 @@
266268
<None Include="ScenarioTests\RoleAssignmentTests.ps1">
267269
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
268270
</None>
271+
<None Include="ScenarioTests\RoleDefinitionTests.ps1" />
269272
<None Include="SessionRecords\Microsoft.Azure.Commands.Resources.Test.ScenarioTests.ActiveDirectoryTests\TestGetADUserWithMail.json">
270273
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
271274
</None>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"Id": "/providers/Microsoft.Authorization/roleDefinitions/2ABB0DE9-E2FA-4865-BDCE-772C5BBC501A",
3+
"Actions": [
4+
"Microsoft.Authorization/*/read",
5+
"Microsoft.Support/*"
6+
]
7+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 RdNegativeScenarios()
26+
{
27+
ResourcesController.NewInstance.RunPsTest("Test-RdNegativeScenarios");
28+
}
29+
30+
[Fact(Skip = "Not implemented")]
31+
[Trait(Category.AcceptanceType, Category.CheckIn)]
32+
public void RdPositiveScenarios()
33+
{
34+
ResourcesController.NewInstance.RunPsTest("Test-RDPositiveScenarios");
35+
}
36+
}
37+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
.SYNOPSIS
17+
Tests verify negative scenarios for RoleDefinitions
18+
#>
19+
function Test-RdNegativeScenarios
20+
{
21+
# Does not throw when getting a non-existing role assignment
22+
$rdName = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'
23+
$badRdNameResult = Get-AzureRoleDefinition -Name $rdName
24+
Assert-Null $badRdNameResult
25+
26+
$rdId = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'
27+
$badIdException = "Can not find role definition with id " + $rdId + "."
28+
29+
# Throws on trying to update the a role that does not exist
30+
Assert-Throws { Set-AzureRoleDefinition -InputFile .\Resources\RoleDefinition.json } $badIdException
31+
32+
# Get a null role definition
33+
$rdName = 'nonExisting role'
34+
$rdNull = Get-AzureRoleDefinition -Name $rdName
35+
36+
# Create a role definition
37+
# $rd = New-AzureRoleDefinition -InputFile .Resources\RoleDefinition.json
38+
39+
# Role Defintion not provided.
40+
$roleDefNotProvided = "Role definition not provided."
41+
Assert-Throws { Set-AzureRoleDefinition } $roleDefNotProvided
42+
Assert-Throws { Set-AzureRoleDefinition -InputFile "" } $roleDefNotProvided
43+
Assert-Throws { Set-AzureRoleDefinition -Role $rdNull } $roleDefNotProvided
44+
Assert-Throws { Set-AzureRoleDefinition -InputFile "" -Role $rdNull } $roleDefNotProvided
45+
46+
# Provide role definition using either InputFile or Role, not both.
47+
$doNotProvideBothParams = "Provide role definition using either InputFile or Role, not both."
48+
Assert-Throws { Set-AzureRoleDefinition -InputFile .\Resources\RoleDefinition.json -Role $rd } $doNotProvideBothParams
49+
50+
# Throws on trying to delete a role that does not exist
51+
Assert-Throws { Remove-AzureRoleDefinition -Id $rdId -Force} $badIdException
52+
}
53+
54+
<#
55+
.SYNOPSIS
56+
Tests verify positive scenarios for RoleDefinitions.
57+
#>
58+
function Test-RDPositiveScenarios
59+
{
60+
# Create a role definition with Name rdNamme.
61+
$rdName = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'
62+
# $rd = New-AzureRoleDefinition -InputFile .\Resources\RoleDefinition.json
63+
$rd = Get-AzureRoleDefinition -Name $rdName
64+
65+
# Update the role definition with name $rdName that was created in the step above.
66+
$newActions = {'Microsoft.Authorization/*/read'}
67+
$rd.Actions = $newActions
68+
$updatedRd = Set-AzureRoleDefinition -RoleDefinition $rd
69+
70+
Assert-AreEqual $rd.Name $updatedRd.Name
71+
Assert-AreEqual $newActions $updatedRd.Actions
72+
73+
# delete the role definition
74+
$deletedRd = Remove-AzureRoleDefinition -Id $rd.Id -Force
75+
Assert-AreEqual $rd.Name $deletedRd.Name
76+
77+
# try to read the deleted role definition
78+
$readRd = Get-AzureRoleDefinition -Name $rd.Name
79+
Assert-Null $readRd
80+
81+
# $rdReCreated = New-AzureRoleDefinition -Role $rd
82+
$rdReDeleted = Get-AzureRoleDefinition -Name $rd.Name | Remove-AzureRoleDefinition -Force
83+
}

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.1-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: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
<HintPath>..\..\..\packages\Microsoft.Azure.Graph.RBAC.1.7.0-preview\lib\net40\Microsoft.Azure.Graph.RBAC.dll</HintPath>
7070
</Reference>
7171
<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>
72+
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Authorization.0.18.1-preview\lib\net40\Microsoft.Azure.Management.Authorization.dll</HintPath>
7373
</Reference>
7474
<Reference Include="Microsoft.Azure.ResourceManager">
7575
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Resources.2.18.0-preview\lib\net40\Microsoft.Azure.ResourceManager.dll</HintPath>
@@ -186,7 +186,7 @@
186186
<Compile Include="Models.Authorization\AuthorizationClient.cs" />
187187
<Compile Include="ProviderFeatures\RegisterAzureProviderFeatureCmdlet.cs" />
188188
<Compile Include="Providers\GetAzureProviderCmdlet.cs" />
189-
<Compile Include="Providers\GetAzureProviderOperationCmdlet.cs" />
189+
<Compile Include="Providers\GetAzureProviderOperationCmdlet.cs" />
190190
<Compile Include="Providers\RegisterAzureProviderCmdlet.cs" />
191191
<Compile Include="Providers\UnregisterAzureProviderCmdlet.cs" />
192192
<Compile Include="ResourceGroups\GetAzureResourceGroupLogCommand.cs" />
@@ -198,6 +198,8 @@
198198
<Compile Include="RoleAssignments\GetAzureRoleAssignmentCommand.cs" />
199199
<Compile Include="RoleAssignments\NewAzureRoleAssignmentCommand.cs" />
200200
<Compile Include="RoleDefinitions\GetAzureRoleDefinitionCommand.cs" />
201+
<Compile Include="RoleDefinitions\RemoveAzureRoleDefinitionCommand.cs" />
202+
<Compile Include="RoleDefinitions\SetAzureRoleDefinitionCommand.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: 79 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15-
using Microsoft.Azure.Commands.Resources.Models.ActiveDirectory;
16-
using Microsoft.Azure.Management.Authorization;
17-
using Microsoft.Azure.Management.Authorization.Models;
18-
using Microsoft.Azure.Common.Authentication.Models;
1915
using System;
2016
using System.Collections.Generic;
2117
using System.Linq;
22-
using ProjectResources = Microsoft.Azure.Commands.Resources.Properties.Resources;
18+
using Microsoft.Azure.Commands.Resources.Models.ActiveDirectory;
2319
using Microsoft.Azure.Common.Authentication;
20+
using Microsoft.Azure.Common.Authentication.Models;
21+
using Microsoft.Azure.Management.Authorization;
22+
using Microsoft.Azure.Management.Authorization.Models;
23+
using ProjectResources = Microsoft.Azure.Commands.Resources.Properties.Resources;
2424

2525
namespace Microsoft.Azure.Commands.Resources.Models.Authorization
2626
{
@@ -92,14 +92,13 @@ public PSRoleAssignment CreateRoleAssignment(FilterRoleAssignmentsOptions parame
9292

9393
RoleAssignmentCreateParameters createParameters = new RoleAssignmentCreateParameters
9494
{
95-
Properties = new RoleAssignmentProperties {
96-
PrincipalId = principalId,
97-
RoleDefinitionId = roleDefinitionId
98-
}
95+
Properties = new RoleAssignmentProperties() { PrincipalId = principalId, RoleDefinitionId = roleDefinitionId }
9996
};
10097

10198
AuthorizationManagementClient.RoleAssignments.Create(parameters.Scope, roleAssignmentId, createParameters);
102-
return AuthorizationManagementClient.RoleAssignments.Get(parameters.Scope, roleAssignmentId).RoleAssignment.ToPSRoleAssignment(this, ActiveDirectoryClient);
99+
return
100+
AuthorizationManagementClient.RoleAssignments.Get(parameters.Scope, roleAssignmentId)
101+
.RoleAssignment.ToPSRoleAssignment(this, ActiveDirectoryClient);
103102
}
104103

105104
/// <summary>
@@ -167,6 +166,26 @@ public PSRoleAssignment RemoveRoleAssignment(FilterRoleAssignmentsOptions option
167166
return roleAssignment;
168167
}
169168

169+
/// <summary>
170+
/// Deletes a role definition based on the id.
171+
/// </summary>
172+
/// <param name="id">The role definition id.</param>
173+
/// <returns>The deleted role definition.</returns>
174+
public PSRoleDefinition RemoveRoleDefinition(string id)
175+
{
176+
PSRoleDefinition roleDefinition = this.GetRoleDefinition(id);
177+
if (roleDefinition != null)
178+
{
179+
AuthorizationManagementClient.RoleDefinitions.Delete(roleDefinition.Id);
180+
}
181+
else
182+
{
183+
throw new KeyNotFoundException(string.Format(ProjectResources.RoleDefinitionWithIdNotFound, id));
184+
}
185+
186+
return roleDefinition;
187+
}
188+
170189
public PSRoleDefinition GetRoleRoleDefinition(string name)
171190
{
172191
PSRoleDefinition role = FilterRoleDefinitions(name).FirstOrDefault();
@@ -178,5 +197,55 @@ public PSRoleDefinition GetRoleRoleDefinition(string name)
178197

179198
return role;
180199
}
200+
201+
/// <summary>
202+
/// Updates a role definiton.
203+
/// </summary>
204+
/// <param name="role">The role definition to update.</param>
205+
/// <returns>The updated role definition.</returns>
206+
public PSRoleDefinition UpdateRoleDefinition(PSRoleDefinition role)
207+
{
208+
PSRoleDefinition roleDefinition = this.GetRoleDefinition(role.Id);
209+
if (roleDefinition == null)
210+
{
211+
throw new KeyNotFoundException(string.Format(ProjectResources.RoleDefinitionWithIdNotFound, role.Id));
212+
}
213+
214+
roleDefinition.Name = role.Name ?? roleDefinition.Name;
215+
roleDefinition.Actions = role.Actions ?? roleDefinition.Actions;
216+
roleDefinition.NotActions = role.NotActions ?? roleDefinition.NotActions;
217+
218+
// TODO: confirm with ARM on what exception will be thrown when the last segment of the roleDefinition's ID is not a GUID.
219+
// This will be done after their API is designed.
220+
string[] scopes = roleDefinition.Id.Split('/');
221+
Guid roleDefinitionId = Guid.Parse(scopes.Last());
222+
223+
// TODO: update to include assignable scopes.
224+
return
225+
AuthorizationManagementClient.RoleDefinitions.CreateOrUpdate(
226+
roleDefinitionId,
227+
new RoleDefinitionCreateOrUpdateParameters()
228+
{
229+
RoleDefinition = new RoleDefinition()
230+
{
231+
Id = roleDefinition.Id,
232+
Name = roleDefinitionId,
233+
Properties =
234+
new RoleDefinitionProperties()
235+
{
236+
RoleName = roleDefinition.Name,
237+
Permissions =
238+
new List<Permission>()
239+
{
240+
new Permission()
241+
{
242+
Actions = roleDefinition.Actions,
243+
NotActions = roleDefinition.NotActions
244+
}
245+
}
246+
}
247+
}
248+
}).RoleDefinition.ToPSRoleDefinition();
249+
}
181250
}
182251
}

0 commit comments

Comments
 (0)