Skip to content

Commit 3025a84

Browse files
Add workload identity as a MSAL option (#228)
* Add workload identity options * Bit clearer! * Add Federation token file * Update ConnectionSettings.cs * Add file exists check --------- Co-authored-by: MattB <mattb-msft@hotmail.com>
1 parent e7c5547 commit 3025a84

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

src/libraries/Authentication/Authentication.Msal/Model/AuthTypes.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public enum AuthTypes
1010
ClientSecret,
1111
UserManagedIdentity,
1212
SystemManagedIdentity,
13-
FederatedCredentials
13+
FederatedCredentials,
14+
WorkloadIdentity
1415
}
1516
}

src/libraries/Authentication/Authentication.Msal/Model/ConnectionSettings.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.Agents.Core;
66
using Microsoft.Extensions.Configuration;
77
using System;
8+
using System.IO;
89

910
namespace Microsoft.Agents.Authentication.Msal.Model
1011
{
@@ -69,6 +70,11 @@ public ConnectionSettings(IConfigurationSection msalConfigurationSection) : base
6970
/// </summary>
7071
public bool SendX5C { get; set; } = false;
7172

73+
/// <summary>
74+
/// Token path used for the workload identity, like the MSAL example for AKS, equal to AZURE_FEDERATED_TOKEN_FILE
75+
/// </summary>
76+
public string FederatedTokenFile { get; set; }
77+
7278
/// <summary>
7379
/// ClientId of the ManagedIdentity used with FederatedCredentials
7480
/// </summary>
@@ -147,6 +153,25 @@ public void ValidateConfiguration()
147153
throw new ArgumentNullException(nameof(Authority), "TenantId or Authority is required");
148154
}
149155
break;
156+
case AuthTypes.WorkloadIdentity:
157+
if (string.IsNullOrEmpty(ClientId))
158+
{
159+
throw new ArgumentNullException(nameof(ClientId), "ClientId is required");
160+
}
161+
if (string.IsNullOrEmpty(Authority) && string.IsNullOrEmpty(TenantId))
162+
{
163+
throw new ArgumentNullException(nameof(Authority), "TenantId or Authority is required");
164+
}
165+
if (string.IsNullOrEmpty(FederatedTokenFile))
166+
{
167+
throw new ArgumentNullException(nameof(FederatedTokenFile), "FederatedTokenFile option is required");
168+
169+
}
170+
if (!File.Exists(FederatedTokenFile))
171+
{
172+
throw new ArgumentNullException(nameof(FederatedTokenFile), "FederatedToken file is not present");
173+
}
174+
break;
150175
default:
151176
break;
152177
}

src/libraries/Authentication/Authentication.Msal/MsalAuth.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System;
1717
using System.Collections.Concurrent;
1818
using System.Collections.Generic;
19+
using System.IO;
1920
using System.Linq;
2021
using System.Threading.Tasks;
2122

@@ -37,6 +38,8 @@ public class MsalAuth : IAccessTokenProvider, IOBOExchange, IMSALProvider
3738
private readonly ConnectionSettings _connectionSettings;
3839
private readonly ILogger _logger;
3940
private readonly ICertificateProvider _certificateProvider;
41+
private DateTimeOffset _lastReadWorkloadIdentity;
42+
private string _lastJwtWorkLoadIdentity = null;
4043

4144
/// <summary>
4245
/// Creates a MSAL Authentication Instance.
@@ -236,6 +239,18 @@ async Task<String> FetchExternalTokenAsync()
236239
}
237240
cAppBuilder.WithClientAssertion((AssertionRequestOptions options) => FetchExternalTokenAsync());
238241
}
242+
else if (_connectionSettings.AuthType == AuthTypes.WorkloadIdentity)
243+
{
244+
cAppBuilder.WithClientAssertion(() =>
245+
{
246+
// read only once every 5 minutes, less heavy for I/O
247+
if (_lastJwtWorkLoadIdentity != null && DateTimeOffset.UtcNow.Subtract(_lastReadWorkloadIdentity) <= TimeSpan.FromMinutes(5))
248+
return _lastJwtWorkLoadIdentity;
249+
_lastReadWorkloadIdentity = DateTimeOffset.UtcNow;
250+
_lastJwtWorkLoadIdentity = File.ReadAllText(_connectionSettings.FederatedTokenFile);
251+
return _lastJwtWorkLoadIdentity;
252+
});
253+
}
239254
else
240255
{
241256
throw new System.NotImplementedException();

0 commit comments

Comments
 (0)