Skip to content

Commit 530e40c

Browse files
authored
Add ManagedIdentityCredential support in ConfigurableCredentialProvider (#56283)
* Add ManagedIdentityCredential support in ConfigurableCredentialProvider (#55502) * Add ConfigurableCredential support for ManagedIdentityCredential tests Implements #55502: Adds ManagedIdentityCredential to the ConfigurableCredential test infrastructure. Refactors base test class to use virtual factory methods, adds CC test subclass and creation tests. Probe-specific tests are skipped in CC pending chained credential support (#56233). * Add ManagedIdentityIdType/ManagedIdentityId config properties Introduce unified ManagedIdentityIdType (SystemAssigned|ClientId|ResourceId| ObjectId) and ManagedIdentityId config properties, taking priority over legacy per-type properties. Creation tests expanded to 18 with separate assertions for both identity type and ID value smuggled through to ManagedIdentityId. * Rename ManagedIdentityIdType config property to ManagedIdentityIdKind
1 parent e9ad88e commit 530e40c

File tree

5 files changed

+840
-336
lines changed

5 files changed

+840
-336
lines changed

sdk/identity/Azure.Identity/src/Credentials/DefaultAzureCredentialOptions.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

44
using System;
@@ -100,6 +100,21 @@ internal DefaultAzureCredentialOptions(CredentialSettings settings, IConfigurati
100100
ManagedIdentityResourceId = new ResourceIdentifier(managedIdentityResourceId);
101101
}
102102

103+
if (section[nameof(ManagedIdentityObjectId)] is string managedIdentityObjectId)
104+
{
105+
ManagedIdentityObjectId = managedIdentityObjectId;
106+
}
107+
108+
if (section[nameof(ManagedIdentityIdKind)] is string managedIdentityIdKind)
109+
{
110+
ManagedIdentityIdKind = managedIdentityIdKind;
111+
}
112+
113+
if (section[nameof(ManagedIdentityId)] is string managedIdentityId)
114+
{
115+
ManagedIdentityId = managedIdentityId;
116+
}
117+
103118
if (TimeSpan.TryParse(section[nameof(CredentialProcessTimeout)], out TimeSpan credentialProcessTimeout))
104119
{
105120
CredentialProcessTimeout = credentialProcessTimeout;
@@ -331,6 +346,23 @@ public string VisualStudioCodeTenantId
331346
/// </remarks>
332347
public ResourceIdentifier ManagedIdentityResourceId { get; set; }
333348

349+
/// <summary>
350+
/// Specifies the object ID of a user-assigned managed identity. If this value is configured, then
351+
/// <see cref="ManagedIdentityClientId"/> and <see cref="ManagedIdentityResourceId"/> should not be configured.
352+
/// </summary>
353+
internal string ManagedIdentityObjectId { get; set; }
354+
355+
/// <summary>
356+
/// Specifies the type of managed identity to use. Valid values are "SystemAssigned", "ClientId", "ResourceId", and "ObjectId".
357+
/// When set to a user-assigned type, <see cref="ManagedIdentityId"/> must also be specified.
358+
/// </summary>
359+
internal string ManagedIdentityIdKind { get; set; }
360+
361+
/// <summary>
362+
/// Specifies the ID of the managed identity when <see cref="ManagedIdentityIdKind"/> is set to "ClientId", "ResourceId", or "ObjectId".
363+
/// </summary>
364+
internal string ManagedIdentityId { get; set; }
365+
334366
/// <summary>
335367
/// Specifies timeout for credentials invoked via sub-process. e.g. Visual Studio, Azure CLI, Azure PowerShell.
336368
/// </summary>
@@ -444,6 +476,9 @@ internal string Subscription
444476
dacClone.WorkloadIdentityClientId = WorkloadIdentityClientId;
445477
dacClone.ManagedIdentityClientId = ManagedIdentityClientId;
446478
dacClone.ManagedIdentityResourceId = ManagedIdentityResourceId;
479+
dacClone.ManagedIdentityObjectId = ManagedIdentityObjectId;
480+
dacClone.ManagedIdentityIdKind = ManagedIdentityIdKind;
481+
dacClone.ManagedIdentityId = ManagedIdentityId;
447482
dacClone.CredentialProcessTimeout = CredentialProcessTimeout;
448483
dacClone.ExcludeEnvironmentCredential = ExcludeEnvironmentCredential;
449484
dacClone.ExcludeWorkloadIdentityCredential = ExcludeWorkloadIdentityCredential;

sdk/identity/Azure.Identity/src/DefaultAzureCredentialFactory.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,14 +296,30 @@ public virtual TokenCredential CreateManagedIdentityCredential(bool isProbeEnabl
296296
IsForceRefreshEnabled = options.IsForceRefreshEnabled,
297297
};
298298

299-
if (!string.IsNullOrEmpty(options.ManagedIdentityClientId))
299+
// ManagedIdentityIdKind/ManagedIdentityId (new config properties) take priority
300+
if (!string.IsNullOrEmpty(options.ManagedIdentityIdKind))
301+
{
302+
miOptions.ManagedIdentityId = options.ManagedIdentityIdKind switch
303+
{
304+
"SystemAssigned" => ManagedIdentityId.SystemAssigned,
305+
"ClientId" => ManagedIdentityId.FromUserAssignedClientId(options.ManagedIdentityId),
306+
"ResourceId" => ManagedIdentityId.FromUserAssignedResourceId(new ResourceIdentifier(options.ManagedIdentityId)),
307+
"ObjectId" => ManagedIdentityId.FromUserAssignedObjectId(options.ManagedIdentityId),
308+
_ => throw new ArgumentException($"Invalid {nameof(options.ManagedIdentityIdKind)} value: '{options.ManagedIdentityIdKind}'. Valid values are 'SystemAssigned', 'ClientId', 'ResourceId', 'ObjectId'."),
309+
};
310+
}
311+
else if (!string.IsNullOrEmpty(options.ManagedIdentityClientId))
300312
{
301313
miOptions.ManagedIdentityId = ManagedIdentityId.FromUserAssignedClientId(options.ManagedIdentityClientId);
302314
}
303315
else if (options.ManagedIdentityResourceId != null)
304316
{
305317
miOptions.ManagedIdentityId = ManagedIdentityId.FromUserAssignedResourceId(options.ManagedIdentityResourceId);
306318
}
319+
else if (!string.IsNullOrEmpty(options.ManagedIdentityObjectId))
320+
{
321+
miOptions.ManagedIdentityId = ManagedIdentityId.FromUserAssignedObjectId(options.ManagedIdentityObjectId);
322+
}
307323
else
308324
{
309325
miOptions.ManagedIdentityId = ManagedIdentityId.SystemAssigned;

0 commit comments

Comments
 (0)