Skip to content

Commit

Permalink
Treat warnings as errors (#3166)
Browse files Browse the repository at this point in the history
* Treat warnings as errors

Suppress SYSLIB0057

* Fix for tests

---------

Co-authored-by: Keegan Caruso <[email protected]>
Co-authored-by: jennyf19 <[email protected]>
  • Loading branch information
3 people authored Dec 26, 2024
1 parent 191c6ae commit bfc7b6b
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<LangVersion>13</LangVersion>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFrameworkIdentifier)' == 'NET6_0_OR_GREATER'">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,22 @@ public Task LoadIfNeededAsync(CredentialDescription credentialDescription, Crede

internal static X509Certificate2 LoadFromBase64Encoded(string certificateBase64, X509KeyStorageFlags x509KeyStorageFlags)
{
#pragma warning disable SYSLIB0057 // Type or member is obsolete
return new X509Certificate2(
Convert.FromBase64String(certificateBase64),
(string?)null,
x509KeyStorageFlags);
#pragma warning restore SYSLIB0057 // Type or member is obsolete
}

internal static X509Certificate2 LoadFromBase64Encoded(string certificateBase64, string password, X509KeyStorageFlags x509KeyStorageFlags)
{
#pragma warning disable SYSLIB0057 // Type or member is obsolete
return new X509Certificate2(
Convert.FromBase64String(certificateBase64),
password,
x509KeyStorageFlags);
#pragma warning restore SYSLIB0057 // Type or member is obsolete
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ private static X509Certificate2 LoadFromPath(
{
X509KeyStorageFlags x509KeyStorageFlags = CertificateLoaderHelper.DetermineX509KeyStorageFlag();

#pragma warning disable SYSLIB0057 // Type or member is obsolete
return new X509Certificate2(
certificateFileName,
password,
x509KeyStorageFlags);
#pragma warning restore SYSLIB0057 // Type or member is obsolete
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,12 @@ public async Task LoadIfNeededAsync(CredentialDescription credentialDescription,
// Return a certificate with only the public key if the private key is not exportable.
if (certificate.Policy?.Exportable != true)
{
#pragma warning disable SYSLIB0057 // Type or member is obsolete
return new X509Certificate2(
certificate.Cer,
(string?)null,
x509KeyStorageFlags);
#pragma warning restore SYSLIB0057 // Type or member is obsolete
}

// Parse the secret ID and version to retrieve the private key.
Expand Down
2 changes: 2 additions & 0 deletions tests/E2E Tests/TokenAcquirerTests/CertificateRotationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,9 @@ private X509Certificate2 CreateSelfSignedCertificateAddAddToCertStore(string cer
var cert = req.CreateSelfSigned(notBefore.HasValue ? notBefore.Value : DateTimeOffset.Now, expiry);

byte[] bytes = cert.Export(X509ContentType.Pfx, (string?)null);
#pragma warning disable SYSLIB0057 // Type or member is obsolete
X509Certificate2 certWithPrivateKey = new(bytes);
#pragma warning restore SYSLIB0057 // Type or member is obsolete

// Add it to the local cert store.
X509Store x509Store = new(StoreName.My, StoreLocation.CurrentUser);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ public void TestFromStoreWithThumbprint(string certificateThumbprint, StoreLocat
[Fact]
public void TestFromCertificate()
{
using X509Certificate2 certificate2 = new X509Certificate2(new byte[0]);
#pragma warning disable SYSLIB0057 // Type or member is obsolete
using X509Certificate2 certificate2 = new([]);
#pragma warning restore SYSLIB0057 // Type or member is obsolete
CertificateDescription certificateDescription =
CertificateDescription.FromCertificate(certificate2);
}
Expand Down
8 changes: 6 additions & 2 deletions tests/Microsoft.Identity.Web.Test/MsAuth10AtPopTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ public void MsAuth10AtPop_ThrowsWithNullPopKeyTest()
{
// Arrange
IConfidentialClientApplication app = CreateBuilder();
var clientCertificate = new X509Certificate2(new byte[0]);
#pragma warning disable SYSLIB0057 // Type or member is obsolete
using X509Certificate2 clientCertificate = new([]);
#pragma warning restore SYSLIB0057 // Type or member is obsolete
var jwkClaim = "jwk_claim";
var clientId = "client_id";

Expand All @@ -99,7 +101,9 @@ public void MsAuth10AtPop_ThrowsWithNullJwkClaimTest()
{
// Arrange
IConfidentialClientApplication app = CreateBuilder();
var clientCertificate = new X509Certificate2(new byte[0]);
#pragma warning disable SYSLIB0057 // Type or member is obsolete
using X509Certificate2 clientCertificate = new([]);
#pragma warning restore SYSLIB0057 // Type or member is obsolete
var popPublicKey = "pop_key";
var clientId = "client_id";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected override HttpContext CreateHttpContext()
[Fact]
public async Task TokenValidated_MissingScopesAndRoles_AuthenticationFailsAsync()
{
Assert.True(_tokenContext.Result.Succeeded);
Assert.True(_tokenContext.Result!.Succeeded);
await _jwtEvents.TokenValidated(_tokenContext);
Assert.False(_tokenContext.Result.Succeeded);
}
Expand All @@ -68,7 +68,7 @@ protected override HttpContext CreateHttpContext()
[Fact]
public async Task TokenValidated_WithScopesAndRoles_AuthenticationSucceedsAsync()
{
Assert.True(_tokenContext.Result.Succeeded);
Assert.True(_tokenContext.Result!.Succeeded);
await _jwtEvents.TokenValidated(_tokenContext);
Assert.True(_tokenContext.Result.Succeeded);
}
Expand Down

0 comments on commit bfc7b6b

Please sign in to comment.