Skip to content
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

Add support for sending X5C when using a service principal with certificate for authentication #1666

Merged
merged 5 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.5.1.0
spongemike2 marked this conversation as resolved.
Show resolved Hide resolved
2.5.1.1
11 changes: 9 additions & 2 deletions 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,12 +274,14 @@ public async Task<IMicrosoftAuthenticationResult> GetTokenForServicePrincipalAsy

try
{
AuthenticationResult result = await app.AcquireTokenForClient(scopes).ExecuteAsync();
Context.Trace.WriteLine($"Sending with X5C: '{sp.SendX5C}'.");
AuthenticationResult result = await app.AcquireTokenForClient(scopes).WithSendX5C(sp.SendX5C).ExecuteAsync();;

return new MsalResult(result);
}
catch (Exception ex)
{
Context.Trace.WriteLine($"Failed to acquire token for service principal '{sp.TenantId}/{sp.TenantId}'.");
Context.Trace.WriteLine($"Failed to acquire token for service principal '{sp.TenantId}/{sp.Id}'.");
spongemike2 marked this conversation as resolved.
Show resolved Hide resolved
Context.Trace.WriteException(ex);
throw;
}
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

This comment was marked as outdated.

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";
spongemike2 marked this conversation as resolved.
Show resolved Hide resolved

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