Skip to content

Commit 41bab3e

Browse files
isra-felYeming Liu
andauthored
enhance error handling (#12732)
Co-authored-by: Yeming Liu <[email protected]>
1 parent 172a22e commit 41bab3e

File tree

4 files changed

+36
-29
lines changed

4 files changed

+36
-29
lines changed

src/KeyVault/KeyVault/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Enhanced error handling in `Set-AzKeyVaultAccessPolicy` [#4007]
2122

2223
## Version 2.1.0
2324
* Added warning messages for planning to disable soft delete

src/KeyVault/KeyVault/Models/KeyVaultManagementCmdletBase.cs

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,27 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15-
// TODO: Remove IfDef
16-
#if NETSTANDARD
17-
using Microsoft.Azure.Graph.RBAC.Version1_6.ActiveDirectory;
18-
#else
19-
using Microsoft.Azure.ActiveDirectory.GraphClient;
20-
#endif
21-
using System;
22-
using System.Collections;
23-
using System.Collections.Generic;
24-
using System.Linq;
25-
using System.Linq.Expressions;
26-
using System.Threading.Tasks;
2715
using Microsoft.Azure.Commands.Common.Authentication;
2816
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
2917
using Microsoft.Azure.Commands.KeyVault.Models;
18+
using Microsoft.Azure.Commands.KeyVault.Properties;
3019
using Microsoft.Azure.Commands.ResourceManager.Common;
20+
using Microsoft.Azure.Commands.ResourceManager.Common.Paging;
3121
using Microsoft.Azure.Commands.ResourceManager.Common.Tags;
22+
using Microsoft.Azure.Graph.RBAC.Version1_6.ActiveDirectory;
3223
using Microsoft.Azure.Management.Internal.Resources;
3324
using Microsoft.Azure.Management.Internal.Resources.Models;
3425
using Microsoft.Azure.Management.Internal.Resources.Utilities;
3526
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
36-
using PSKeyVaultModels = Microsoft.Azure.Commands.KeyVault.Models;
37-
using PSKeyVaultProperties = Microsoft.Azure.Commands.KeyVault.Properties;
38-
using Microsoft.Rest.Azure;
27+
using System;
28+
using System.Collections;
29+
using System.Collections.Generic;
30+
using System.Linq;
31+
using CertPerms = Microsoft.Azure.Management.KeyVault.Models.CertificatePermissions;
3932
using KeyPerms = Microsoft.Azure.Management.KeyVault.Models.KeyPermissions;
33+
using PSKeyVaultProperties = Microsoft.Azure.Commands.KeyVault.Properties;
4034
using SecretPerms = Microsoft.Azure.Management.KeyVault.Models.SecretPermissions;
41-
using CertPerms = Microsoft.Azure.Management.KeyVault.Models.CertificatePermissions;
4235
using StoragePerms = Microsoft.Azure.Management.KeyVault.Models.StoragePermissions;
43-
using Microsoft.Azure.Management.KeyVault.Models;
44-
using Microsoft.Azure.Commands.ResourceManager.Common.Paging;
4536

4637
namespace Microsoft.Azure.Commands.KeyVault
4738
{
@@ -128,7 +119,7 @@ protected List<PSKeyVaultIdentityItem> FilterByTag(List<PSKeyVaultIdentityItem>
128119

129120
protected PSKeyVault FilterByTag(PSKeyVault keyVault, Hashtable tag)
130121
{
131-
return (PSKeyVault) FilterByTag(new List<PSKeyVaultIdentityItem> { keyVault }, tag).FirstOrDefault();
122+
return (PSKeyVault)FilterByTag(new List<PSKeyVaultIdentityItem> { keyVault }, tag).FirstOrDefault();
132123
}
133124

134125
protected List<PSKeyVaultIdentityItem> ListVaults(string resourceGroupName, Hashtable tag)
@@ -235,7 +226,7 @@ protected string GetCurrentUsersObjectId()
235226
{
236227
// TODO: Remove IfDef
237228
#if NETSTANDARD
238-
objectId = ActiveDirectoryClient.GetObjectId(new ADObjectFilterOptions {UPN = DefaultContext.Account.Id}).ToString();
229+
objectId = ActiveDirectoryClient.GetObjectId(new ADObjectFilterOptions { UPN = DefaultContext.Account.Id }).ToString();
239230
#else
240231
var userFetcher = ActiveDirectoryClient.Me.ToUser();
241232
var user = userFetcher.ExecuteAsync().Result;
@@ -335,13 +326,16 @@ private Expression<Func<IUser, bool>> FilterByEmail(string email)
335326
private bool ValidateObjectId(string objId)
336327
{
337328
if (string.IsNullOrWhiteSpace(objId)) return false;
338-
// TODO: Remove IfDef
339-
#if NETSTANDARD
340-
var objectCollection = ActiveDirectoryClient.GetObjectsByObjectId(new List<string> { objId });
341-
#else
342-
var objectCollection = ActiveDirectoryClient.GetObjectsByObjectIdsAsync(new[] { objId }, new string[] { }).GetAwaiter().GetResult();
343-
#endif
344-
return objectCollection.Any();
329+
try
330+
{
331+
var objectCollection = ActiveDirectoryClient.GetObjectsByObjectId(new List<string> { objId });
332+
return objectCollection.Any();
333+
}
334+
catch (Exception ex)
335+
{
336+
WriteWarning(Resources.ADGraphPermissionWarning);
337+
throw ex;
338+
}
345339
}
346340

347341
protected string GetObjectId(string objectId, string upn, string email, string spn)
@@ -410,7 +404,7 @@ protected bool IsValidObjectIdSyntax(string objectId)
410404
KeyPerms.Recover
411405
};
412406

413-
protected readonly string[] DefaultPermissionsToSecrets =
407+
protected readonly string[] DefaultPermissionsToSecrets =
414408
{
415409
SecretPerms.Get,
416410
SecretPerms.List,
@@ -440,7 +434,7 @@ protected bool IsValidObjectIdSyntax(string objectId)
440434
CertPerms.Restore
441435
};
442436

443-
protected readonly string[] DefaultPermissionsToStorage =
437+
protected readonly string[] DefaultPermissionsToStorage =
444438
{
445439
StoragePerms.Delete,
446440
StoragePerms.Deletesas,

src/KeyVault/KeyVault/Properties/Resources.Designer.cs

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

src/KeyVault/KeyVault/Properties/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,4 +498,7 @@ You can find the object ID using Azure Active Directory Module for Windows Power
498498
<data name="KeyOpsImportIsExclusive" xml:space="preserve">
499499
<value>The "import" operation is exclusive, it cannot be combined with any other value(s).</value>
500500
</data>
501+
<data name="ADGraphPermissionWarning" xml:space="preserve">
502+
<value>Please make sure you have sufficient permissions in AD Graph to get and list graph objects for validation to work. Otherwise skip witch `-BypassObjectIdValidation`.</value>
503+
</data>
501504
</root>

0 commit comments

Comments
 (0)