Skip to content

Commit

Permalink
Add method for sending X5C
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael J. Lyons (XBOX) committed Jul 17, 2024
1 parent 85d9ee3 commit 62b9c3d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.5.1.0
2.5.1.1
15 changes: 14 additions & 1 deletion src/shared/Core/Authentication/MicrosoftAuthentication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ public class ServicePrincipalIdentity
/// If both <see cref="Certificate"/> and <see cref="ClientSecret"/> are set, the certificate will be used.
/// </remarks>
public string ClientSecret { get; set; }

/// <summary>
/// Whether the authentication should send X5C
/// </summary>
public bool SendX5C { get; set; }
}

public interface IMicrosoftAuthenticationResult
Expand Down Expand Up @@ -269,7 +274,15 @@ public async Task<IMicrosoftAuthenticationResult> GetTokenForServicePrincipalAsy

try
{
AuthenticationResult result = await app.AcquireTokenForClient(scopes).ExecuteAsync();
var tokenBuilder = app.AcquireTokenForClient(scopes);

if (sp.SendX5C)
{
tokenBuilder = tokenBuilder.WithSendX5C(true);
}

AuthenticationResult result = await tokenBuilder.ExecuteAsync();

return new MsalResult(result);
}
catch (Exception ex)
Expand Down
2 changes: 2 additions & 0 deletions src/shared/Microsoft.AzureRepos/AzureDevOpsConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public static class EnvironmentVariables
public const string ServicePrincipalId = "GCM_AZREPOS_SERVICE_PRINCIPAL";
public const string ServicePrincipalSecret = "GCM_AZREPOS_SP_SECRET";
public const string ServicePrincipalCertificateThumbprint = "GCM_AZREPOS_SP_CERT_THUMBPRINT";
public const string ServicePrincipalCertificateSendX5C = "GCM_AZREPOS_SP_CERT_SEND_X5C";
public const string ManagedIdentity = "GCM_AZREPOS_MANAGEDIDENTITY";
}

Expand All @@ -59,6 +60,7 @@ public static class Credential
public const string ServicePrincipal = "azreposServicePrincipal";
public const string ServicePrincipalSecret = "azreposServicePrincipalSecret";
public const string ServicePrincipalCertificateThumbprint = "azreposServicePrincipalCertificateThumbprint";
public const string ServicePrincipalCertificateSendX5C = "azreposServicePrincipalCertificateSendX5C";
public const string ManagedIdentity = "azreposManagedIdentity";
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/shared/Microsoft.AzureRepos/AzureReposHostProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,14 @@ private bool UseServicePrincipal(out ServicePrincipalIdentity sp)

if (hasCertThumbprint)
{
bool hasX5CSetting = _context.Settings.TryGetSetting(
AzureDevOpsConstants.EnvironmentVariables.ServicePrincipalCertificateSendX5C,
Constants.GitConfiguration.Credential.SectionName,
AzureDevOpsConstants.GitConfiguration.Credential.ServicePrincipalCertificateSendX5C,
out string certHasX5C);

sp.SendX5C = !hasX5CSetting || certHasX5C == "false";

X509Certificate2 cert = X509Utils.GetCertificateByThumbprint(certThumbprint);
if (cert is null)
{
Expand Down

0 comments on commit 62b9c3d

Please sign in to comment.