Skip to content

Commit 2a4ab91

Browse files
authored
Merge pull request #5334 from markcowl/kvgraphfix
Revert KeyVault cmdlets to use older Graph library
2 parents a4b09ae + e8da0b4 commit 2a4ab91

File tree

7 files changed

+124
-10
lines changed

7 files changed

+124
-10
lines changed

src/ResourceManager/KeyVault/Commands.KeyVault.Test/Commands.KeyVault.Test.csproj

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -362,10 +362,4 @@
362362
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
363363
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
364364
<Import Project="..\..\..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets" Condition="Exists('..\..\..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets')" />
365-
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
366-
<PropertyGroup>
367-
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
368-
</PropertyGroup>
369-
<Error Condition="!Exists('..\..\..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets'))" />
370-
</Target>
371365
</Project>

src/ResourceManager/KeyVault/Commands.KeyVault/Models/KeyVaultManagementCmdletBase.cs

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
#if NETSTANDARD
1516
using Microsoft.Azure.Graph.RBAC.Version1_6.ActiveDirectory;
17+
#else
18+
using Microsoft.Azure.ActiveDirectory.GraphClient;
19+
#endif
1620
using Microsoft.Azure.Commands.Common.Authentication;
1721
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
1822
using Microsoft.Azure.Commands.Common.Authentication.Models;
@@ -67,7 +71,13 @@ public ActiveDirectoryClient ActiveDirectoryClient
6771
if (_activeDirectoryClient == null)
6872
{
6973
_dataServiceCredential = new DataServiceCredential(AzureSession.Instance.AuthenticationFactory, DefaultProfile.DefaultContext, AzureEnvironment.Endpoint.Graph);
74+
#if NETSTANDARD
7075
_activeDirectoryClient = new ActiveDirectoryClient(DefaultProfile.DefaultContext);
76+
#else
77+
_activeDirectoryClient = new ActiveDirectoryClient(new Uri(string.Format("{0}/{1}",
78+
DefaultProfile.DefaultContext.Environment.GetEndpoint(AzureEnvironment.Endpoint.Graph), _dataServiceCredential.TenantId)),
79+
() => Task.FromResult(_dataServiceCredential.GetToken()));
80+
#endif
7181
}
7282
return this._activeDirectoryClient;
7383
}
@@ -204,7 +214,13 @@ protected string GetCurrentUsersObjectId()
204214
string objectId = null;
205215
if (DefaultContext.Account.Type == AzureAccount.AccountType.User)
206216
{
207-
objectId = ActiveDirectoryClient.GetObjectId(new ADObjectFilterOptions()).ToString();
217+
#if NETSTANDARD
218+
objectId = ActiveDirectoryClient.GetObjectId(new ADObjectFilterOptions {UPN = DefaultContext.Account.Id}).ToString();
219+
#else
220+
var userFetcher = ActiveDirectoryClient.Me.ToUser();
221+
var user = userFetcher.ExecuteAsync().Result;
222+
objectId = user.ObjectId;
223+
#endif
208224
}
209225

210226
return objectId;
@@ -225,9 +241,20 @@ private string GetObjectIdByUpn(string upn)
225241
string objId = null;
226242
if (!string.IsNullOrWhiteSpace(upn))
227243
{
228-
var user = ActiveDirectoryClient.FilterUsers(new ADObjectFilterOptions() { SPN = upn }).SingleOrDefault();
244+
#if NETSTANDARD
245+
var user = ActiveDirectoryClient.FilterUsers(new ADObjectFilterOptions() { UPN = upn }).SingleOrDefault();
246+
#else
247+
var user = ActiveDirectoryClient.Users.Where(u => u.UserPrincipalName.Equals(upn, StringComparison.OrdinalIgnoreCase))
248+
.ExecuteAsync().ConfigureAwait(false).GetAwaiter().GetResult().CurrentPage.SingleOrDefault();
249+
#endif
229250
if (user != null)
251+
{
252+
#if NETSTANDARD
230253
objId = user.Id.ToString();
254+
#else
255+
objId = user.ObjectId;
256+
#endif
257+
}
231258
}
232259
return objId;
233260
}
@@ -237,9 +264,15 @@ private string GetObjectIdBySpn(string spn)
237264
string objId = null;
238265
if (!string.IsNullOrWhiteSpace(spn))
239266
{
267+
#if NETSTANDARD
240268
var servicePrincipal = ActiveDirectoryClient.FilterServicePrincipals(new ADObjectFilterOptions() { SPN = spn }).SingleOrDefault();
241-
if (servicePrincipal != null)
242-
objId = servicePrincipal.Id.ToString();
269+
objId = servicePrincipal?.Id.ToString();
270+
#else
271+
var servicePrincipal = ActiveDirectoryClient.ServicePrincipals.Where(s =>
272+
s.ServicePrincipalNames.Any(n => n.Equals(spn, StringComparison.OrdinalIgnoreCase)))
273+
.ExecuteAsync().GetAwaiter().GetResult().CurrentPage.SingleOrDefault();
274+
objId = servicePrincipal?.ObjectId;
275+
#endif
243276
}
244277
return objId;
245278
}
@@ -250,23 +283,44 @@ private string GetObjectIdByEmail(string email)
250283
// In ADFS, Graph cannot handle this particular combination of filters.
251284
if (!DefaultProfile.DefaultContext.Environment.OnPremise && !string.IsNullOrWhiteSpace(email))
252285
{
286+
#if NETSTANDARD
253287
var users = ActiveDirectoryClient.FilterUsers(new ADObjectFilterOptions() { Mail = email });
254288
if (users != null)
255289
{
256290
ThrowIfMultipleObjectIds(users, email);
257291
var user = users.FirstOrDefault();
258292
objId = user?.Id.ToString();
259293
}
294+
#else
295+
var users = ActiveDirectoryClient.Users.Where(FilterByEmail(email)).ExecuteAsync().GetAwaiter().GetResult().CurrentPage;
296+
if (users != null)
297+
{
298+
ThrowIfMultipleObjectIds(users, email);
299+
var user = users.FirstOrDefault();
300+
objId = user?.ObjectId;
301+
}
302+
#endif
260303
}
261304
return objId;
262305
}
263306

307+
#if !NETSTANDARD
308+
private Expression<Func<IUser, bool>> FilterByEmail(string email)
309+
{
310+
return u => u.Mail.Equals(email, StringComparison.OrdinalIgnoreCase) ||
311+
u.OtherMails.Any(m => m.Equals(email, StringComparison.OrdinalIgnoreCase));
312+
}
313+
#endif
264314
private bool ValidateObjectId(string objId)
265315
{
266316
bool isValid = false;
267317
if (!string.IsNullOrWhiteSpace(objId))
268318
{
319+
#if NETSTANDARD
269320
var objectCollection = ActiveDirectoryClient.GetObjectsByObjectId(new List<string> { objId });
321+
#else
322+
var objectCollection = ActiveDirectoryClient.GetObjectsByObjectIdsAsync(new[] { objId }, new string[] { }).GetAwaiter().GetResult();
323+
#endif
270324
if (objectCollection.Any())
271325
{
272326
isValid = true;

src/ResourceManager/KeyVault/Commands.KeyVault/Models/ModelExtensions.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
#if NETSTANDARD
1516
using Microsoft.Azure.Graph.RBAC.Version1_6.ActiveDirectory;
17+
#else
18+
using Microsoft.Azure.ActiveDirectory.GraphClient;
19+
#endif
1620
using Microsoft.WindowsAzure.Commands.Utilities.Common;
1721
using System;
1822
using System.Collections.Generic;
@@ -97,6 +101,7 @@ public static string GetDisplayNameForADObject(string objectId, ActiveDirectoryC
97101

98102
try
99103
{
104+
#if NETSTANDARD
100105
var obj = adClient.GetObjectsByObjectId(new List<string> { objectId }).FirstOrDefault();
101106
if (obj != null)
102107
{
@@ -113,6 +118,25 @@ public static string GetDisplayNameForADObject(string objectId, ActiveDirectoryC
113118
upnOrSpn = servicePrincipal.ServicePrincipalNames.FirstOrDefault();
114119
}
115120
}
121+
#else
122+
var obj = adClient.GetObjectsByObjectIdsAsync(new[] { objectId }, new string[] { }).GetAwaiter().GetResult().FirstOrDefault();
123+
if (obj != null)
124+
{
125+
if (obj.ObjectType.Equals("user", StringComparison.InvariantCultureIgnoreCase))
126+
{
127+
var user = adClient.Users.GetByObjectId(objectId).ExecuteAsync().GetAwaiter().GetResult();
128+
displayName = user.DisplayName;
129+
upnOrSpn = user.UserPrincipalName;
130+
}
131+
else if (obj.ObjectType.Equals("serviceprincipal", StringComparison.InvariantCultureIgnoreCase))
132+
{
133+
var servicePrincipal = adClient.ServicePrincipals.GetByObjectId(objectId).ExecuteAsync().GetAwaiter().GetResult();
134+
displayName = servicePrincipal.AppDisplayName;
135+
upnOrSpn = servicePrincipal.ServicePrincipalNames.FirstOrDefault();
136+
}
137+
}
138+
139+
#endif
116140
}
117141
catch
118142
{

src/ResourceManager/KeyVault/Commands.KeyVault/Models/PSVault.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
#if NETSTANDARD
1516
using Microsoft.Azure.Graph.RBAC.Version1_6.ActiveDirectory;
17+
#else
18+
using Microsoft.Azure.ActiveDirectory.GraphClient;
19+
#endif
1620
using Microsoft.Azure.Commands.ResourceManager.Common.Tags;
1721
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
1822
using Microsoft.Azure.Management.KeyVault.Models;

src/ResourceManager/KeyVault/Commands.KeyVault/Models/PSVaultAccessPolicy.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
#if NETSTANDARD
1516
using Microsoft.Azure.Graph.RBAC.Version1_6.ActiveDirectory;
17+
#else
18+
using Microsoft.Azure.ActiveDirectory.GraphClient;
19+
#endif
1620
using System;
1721
using System.Collections.Generic;
1822
using KeyVaultManagement = Microsoft.Azure.Management.KeyVault;

src/ResourceManager/KeyVault/Commands.KeyVault/Models/VaultManagementClient.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
#if NETSTANDARD
1516
using Microsoft.Azure.Graph.RBAC.Version1_6.ActiveDirectory;
17+
#else
18+
using Microsoft.Azure.ActiveDirectory.GraphClient;
19+
#endif
1620
using Microsoft.Azure.Commands.Common.Authentication;
1721
using Microsoft.Azure.Commands.Common.Authentication.Models;
1822
using Microsoft.Azure.Commands.ResourceManager.Common.Tags;

src/ResourceManager/KeyVault/KeyVault.sln

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Commands.ServiceManagement.
2626
EndProject
2727
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Commands.Common.Authentication.ResourceManager", "..\Common\Commands.Common.Authentication.ResourceManager\Commands.Common.Authentication.ResourceManager.csproj", "{69C2EB6B-CD63-480A-89A0-C489706E9299}"
2828
EndProject
29+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Commands.Common.Authorization", "..\..\Common\Commands.Common.Authorization\Commands.Common.Authorization.csproj", "{24508E26-154D-47F1-80EE-439BF0710996}"
30+
EndProject
31+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Commands.Common.Network", "..\..\Common\Commands.Common.Network\Commands.Common.Network.csproj", "{1338F7AE-7111-4ED3-8916-2D0FECC876F4}"
32+
EndProject
33+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Commands.Common.Storage", "..\..\Common\Commands.Common.Storage\Commands.Common.Storage.csproj", "{65C3A86A-716D-4E7D-AB67-1DB00B3BF72D}"
34+
EndProject
35+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Commands.Resources", "..\Resources\Commands.Resources\Commands.Resources.csproj", "{E1F5201D-6067-430E-B303-4E367652991B}"
36+
EndProject
37+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Commands.Resources.Rest", "..\Resources\Commands.ResourceManager\Cmdlets\Commands.Resources.Rest.csproj", "{8058D403-06E3-4BED-8924-D166CE303961}"
38+
EndProject
2939
Global
3040
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3141
Debug|Any CPU = Debug|Any CPU
@@ -76,6 +86,26 @@ Global
7686
{69C2EB6B-CD63-480A-89A0-C489706E9299}.Debug|Any CPU.Build.0 = Debug|Any CPU
7787
{69C2EB6B-CD63-480A-89A0-C489706E9299}.Release|Any CPU.ActiveCfg = Release|Any CPU
7888
{69C2EB6B-CD63-480A-89A0-C489706E9299}.Release|Any CPU.Build.0 = Release|Any CPU
89+
{24508E26-154D-47F1-80EE-439BF0710996}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
90+
{24508E26-154D-47F1-80EE-439BF0710996}.Debug|Any CPU.Build.0 = Debug|Any CPU
91+
{24508E26-154D-47F1-80EE-439BF0710996}.Release|Any CPU.ActiveCfg = Release|Any CPU
92+
{24508E26-154D-47F1-80EE-439BF0710996}.Release|Any CPU.Build.0 = Release|Any CPU
93+
{1338F7AE-7111-4ED3-8916-2D0FECC876F4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
94+
{1338F7AE-7111-4ED3-8916-2D0FECC876F4}.Debug|Any CPU.Build.0 = Debug|Any CPU
95+
{1338F7AE-7111-4ED3-8916-2D0FECC876F4}.Release|Any CPU.ActiveCfg = Release|Any CPU
96+
{1338F7AE-7111-4ED3-8916-2D0FECC876F4}.Release|Any CPU.Build.0 = Release|Any CPU
97+
{65C3A86A-716D-4E7D-AB67-1DB00B3BF72D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
98+
{65C3A86A-716D-4E7D-AB67-1DB00B3BF72D}.Debug|Any CPU.Build.0 = Debug|Any CPU
99+
{65C3A86A-716D-4E7D-AB67-1DB00B3BF72D}.Release|Any CPU.ActiveCfg = Release|Any CPU
100+
{65C3A86A-716D-4E7D-AB67-1DB00B3BF72D}.Release|Any CPU.Build.0 = Release|Any CPU
101+
{E1F5201D-6067-430E-B303-4E367652991B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
102+
{E1F5201D-6067-430E-B303-4E367652991B}.Debug|Any CPU.Build.0 = Debug|Any CPU
103+
{E1F5201D-6067-430E-B303-4E367652991B}.Release|Any CPU.ActiveCfg = Release|Any CPU
104+
{E1F5201D-6067-430E-B303-4E367652991B}.Release|Any CPU.Build.0 = Release|Any CPU
105+
{8058D403-06E3-4BED-8924-D166CE303961}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
106+
{8058D403-06E3-4BED-8924-D166CE303961}.Debug|Any CPU.Build.0 = Debug|Any CPU
107+
{8058D403-06E3-4BED-8924-D166CE303961}.Release|Any CPU.ActiveCfg = Release|Any CPU
108+
{8058D403-06E3-4BED-8924-D166CE303961}.Release|Any CPU.Build.0 = Release|Any CPU
79109
EndGlobalSection
80110
GlobalSection(SolutionProperties) = preSolution
81111
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)