Skip to content

Add workload identity as a MSAL option #228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public enum AuthTypes
ClientSecret,
UserManagedIdentity,
SystemManagedIdentity,
FederatedCredentials
FederatedCredentials,
WorkloadIdentity
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ public ConnectionSettings(IConfigurationSection msalConfigurationSection) : base
/// </summary>
public bool SendX5C { get; set; } = false;

/// <summary>
/// Token path used for the workload identity, like the MSAL example for AKS, equal to AZURE_FEDERATED_TOKEN_FILE
/// </summary>
public string FederatedTokenFile { get; set; }

/// <summary>
/// ClientId of the ManagedIdentity used with FederatedCredentials
/// </summary>
Expand Down Expand Up @@ -146,6 +151,20 @@ public void ValidateConfiguration()
throw new ArgumentNullException(nameof(Authority), "TenantId or Authority is required");
}
break;
case AuthTypes.WorkloadIdentity:
if (string.IsNullOrEmpty(ClientId))
{
throw new ArgumentNullException(nameof(ClientId), "ClientId is required");
}
if (string.IsNullOrEmpty(Authority) && string.IsNullOrEmpty(TenantId))
{
throw new ArgumentNullException(nameof(Authority), "TenantId or Authority is required");
}
if (string.IsNullOrEmpty(FederatedTokenFile))
{
throw new ArgumentNullException(nameof(FederatedTokenFile), "FederatedTokenFile is required");
}
break;
default:
break;
}
Expand Down
15 changes: 15 additions & 0 deletions src/libraries/Authentication/Authentication.Msal/MsalAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

Expand All @@ -35,6 +36,8 @@ public class MsalAuth : IAccessTokenProvider, IOBOExchange, IMSALProvider
private readonly ConnectionSettings _connectionSettings;
private readonly ILogger _logger;
private readonly ICertificateProvider _certificateProvider;
private DateTimeOffset _lastReadWorkloadIdentity;
private string _lastJwtWorkLoadIdentity = null;

/// <summary>
/// Creates a MSAL Authentication Instance.
Expand Down Expand Up @@ -225,6 +228,18 @@ async Task<String> FetchExternalTokenAsync()
}
cAppBuilder.WithClientAssertion((AssertionRequestOptions options) => FetchExternalTokenAsync());
}
else if (_connectionSettings.AuthType == AuthTypes.WorkloadIdentity)
{
cAppBuilder.WithClientAssertion(() =>
{
// read only once every 5 minutes, less heavy for I/O
if (_lastJwtWorkLoadIdentity != null && DateTimeOffset.UtcNow.Subtract(_lastReadWorkloadIdentity) <= TimeSpan.FromMinutes(5))
return _lastJwtWorkLoadIdentity;
_lastReadWorkloadIdentity = DateTimeOffset.UtcNow;
_lastJwtWorkLoadIdentity = File.ReadAllText(_connectionSettings.FederatedTokenFile);
return _lastJwtWorkLoadIdentity;
});
}
else
{
throw new System.NotImplementedException();
Expand Down