Skip to content

Commit f04992f

Browse files
authored
Add nullable to DataProtection (#22591)
* Add nullable to DataProtection Contributes to #5680
1 parent 9b63151 commit f04992f

21 files changed

+56
-44
lines changed

src/DataProtection/Abstractions/ref/Microsoft.AspNetCore.DataProtection.Abstractions.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.0;$(DefaultNetCoreTargetFramework)</TargetFrameworks>
55
<TargetFrameworks Condition="'$(DotNetBuildFromSource)' == 'true'">$(DefaultNetCoreTargetFramework)</TargetFrameworks>
6+
<Nullable>annotations</Nullable>
67
</PropertyGroup>
78
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
89
<Compile Include="Microsoft.AspNetCore.DataProtection.Abstractions.netstandard2.0.cs" />

src/DataProtection/Abstractions/src/DataProtectionCommonExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public static IDataProtector CreateProtector(this IDataProtectionProvider provid
8888
// because we don't want the code provider.CreateProtector() [parameterless] to inadvertently compile.
8989
// The actual signature for this method forces at least one purpose to be provided at the call site.
9090

91-
IDataProtector protector = provider.CreateProtector(purpose);
91+
IDataProtector? protector = provider.CreateProtector(purpose);
9292
if (subPurposes != null && subPurposes.Length > 0)
9393
{
9494
protector = protector?.CreateProtector((IEnumerable<string>)subPurposes);
@@ -111,7 +111,7 @@ public static IDataProtectionProvider GetDataProtectionProvider(this IServicePro
111111

112112
// We have our own implementation of GetRequiredService<T> since we don't want to
113113
// take a dependency on DependencyInjection.Interfaces.
114-
IDataProtectionProvider provider = (IDataProtectionProvider)services.GetService(typeof(IDataProtectionProvider));
114+
var provider = (IDataProtectionProvider?)services.GetService(typeof(IDataProtectionProvider));
115115
if (provider == null)
116116
{
117117
throw new InvalidOperationException(Resources.FormatDataProtectionExtensions_NoService(typeof(IDataProtectionProvider).FullName));

src/DataProtection/Abstractions/src/Error.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Microsoft.AspNetCore.DataProtection
99
{
1010
internal static class Error
1111
{
12-
public static CryptographicException CryptCommon_GenericError(Exception inner = null)
12+
public static CryptographicException CryptCommon_GenericError(Exception? inner = null)
1313
{
1414
return new CryptographicException(Resources.CryptCommon_GenericError, inner);
1515
}

src/DataProtection/Abstractions/src/Microsoft.AspNetCore.DataProtection.Abstractions.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<Description>ASP.NET Core data protection abstractions.
@@ -9,6 +9,7 @@ Microsoft.AspNetCore.DataProtection.IDataProtector</Description>
99
<IsAspNetCoreApp>true</IsAspNetCoreApp>
1010
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1111
<PackageTags>aspnetcore;dataprotection</PackageTags>
12+
<Nullable>enable</Nullable>
1213
</PropertyGroup>
1314

1415
<ItemGroup>

src/DataProtection/Cryptography.Internal/ref/Microsoft.AspNetCore.Cryptography.Internal.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.0;$(DefaultNetCoreTargetFramework)</TargetFrameworks>
55
<TargetFrameworks Condition="'$(DotNetBuildFromSource)' == 'true'">$(DefaultNetCoreTargetFramework)</TargetFrameworks>
6+
<Nullable>annotations</Nullable>
67
</PropertyGroup>
78
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
89
<Compile Include="Microsoft.AspNetCore.Cryptography.Internal.netstandard2.0.cs" />

src/DataProtection/Cryptography.Internal/src/CryptoUtil.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Diagnostics;
6+
using System.Diagnostics.CodeAnalysis;
67
using System.Runtime.CompilerServices;
78
using System.Runtime.ConstrainedExecution;
89
using System.Runtime.InteropServices;
@@ -16,7 +17,7 @@ internal unsafe static class CryptoUtil
1617
{
1718
// This isn't a typical Debug.Assert; the check is always performed, even in retail builds.
1819
[MethodImpl(MethodImplOptions.AggressiveInlining)]
19-
public static void Assert(bool condition, string message)
20+
public static void Assert([DoesNotReturnIf(false)] bool condition, string message)
2021
{
2122
if (!condition)
2223
{

src/DataProtection/Cryptography.Internal/src/Microsoft.AspNetCore.Cryptography.Internal.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<Description>Infrastructure for ASP.NET Core cryptographic packages. Applications and libraries should not reference this package directly.</Description>
@@ -8,6 +8,7 @@
88
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
99
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1010
<PackageTags>aspnetcore;dataprotection</PackageTags>
11+
<Nullable>annotations</Nullable>
1112
</PropertyGroup>
1213

1314
</Project>

src/DataProtection/Cryptography.KeyDerivation/ref/Microsoft.AspNetCore.Cryptography.KeyDerivation.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.0;$(DefaultNetCoreTargetFramework)</TargetFrameworks>
55
<TargetFrameworks Condition="'$(DotNetBuildFromSource)' == 'true'">$(DefaultNetCoreTargetFramework)</TargetFrameworks>
6+
<Nullable>annotations</Nullable>
67
</PropertyGroup>
78
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
89
<Compile Include="Microsoft.AspNetCore.Cryptography.KeyDerivation.netstandard2.0.cs" />

src/DataProtection/Cryptography.KeyDerivation/src/Microsoft.AspNetCore.Cryptography.KeyDerivation.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<Description>ASP.NET Core utilities for key derivation.</Description>
@@ -7,6 +7,8 @@
77
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
88
<GenerateDocumentationFile>true</GenerateDocumentationFile>
99
<PackageTags>aspnetcore;dataprotection</PackageTags>
10+
<Nullable>enable</Nullable>
11+
<Nullable Condition="'$(TargetFramework)' == 'netstandard2.0'">annotations</Nullable>
1012
</PropertyGroup>
1113

1214
<ItemGroup>

src/DataProtection/DataProtection/ref/Microsoft.AspNetCore.DataProtection.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.0;$(DefaultNetCoreTargetFramework)</TargetFrameworks>
55
<TargetFrameworks Condition="'$(DotNetBuildFromSource)' == 'true'">$(DefaultNetCoreTargetFramework)</TargetFrameworks>
6+
<Nullable>annotations</Nullable>
67
</PropertyGroup>
78
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
89
<Compile Include="Microsoft.AspNetCore.DataProtection.netstandard2.0.cs" />

src/DataProtection/DataProtection/src/Microsoft.AspNetCore.DataProtection.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
1010
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1111
<PackageTags>aspnetcore;dataprotection</PackageTags>
12+
<Nullable>annotations</Nullable>
1213
</PropertyGroup>
1314

1415
<ItemGroup>

src/DataProtection/EntityFrameworkCore/src/DataProtectionKey.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
namespace Microsoft.AspNetCore.DataProtection.EntityFrameworkCore
@@ -16,11 +16,11 @@ public class DataProtectionKey
1616
/// <summary>
1717
/// The friendly name of the <see cref="DataProtectionKey"/>.
1818
/// </summary>
19-
public string FriendlyName { get; set; }
19+
public string? FriendlyName { get; set; }
2020

2121
/// <summary>
2222
/// The XML representation of the <see cref="DataProtectionKey"/>.
2323
/// </summary>
24-
public string Xml { get; set; }
24+
public string? Xml { get; set; }
2525
}
2626
}

src/DataProtection/EntityFrameworkCore/src/EntityFrameworkCoreXmlRepository.cs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,25 @@ public EntityFrameworkCoreXmlRepository(IServiceProvider services, ILoggerFactor
4040
/// <inheritdoc />
4141
public virtual IReadOnlyCollection<XElement> GetAllElements()
4242
{
43-
using (var scope = _services.CreateScope())
43+
// forces complete enumeration
44+
return GetAllElementsCore().ToList().AsReadOnly();
45+
46+
IEnumerable<XElement> GetAllElementsCore()
4447
{
45-
var context = scope.ServiceProvider.GetRequiredService<TContext>();
48+
using (var scope = _services.CreateScope())
49+
{
50+
var context = scope.ServiceProvider.GetRequiredService<TContext>();
51+
52+
foreach (var key in context.DataProtectionKeys.AsNoTracking())
53+
{
54+
_logger.ReadingXmlFromKey(key.FriendlyName!, key.Xml);
4655

47-
// Put logger in a local such that `this` isn't captured.
48-
var logger = _logger;
49-
return context.DataProtectionKeys.AsNoTracking().Select(key => TryParseKeyXml(key.Xml, logger)).ToList().AsReadOnly();
56+
if (!string.IsNullOrEmpty(key.Xml))
57+
{
58+
yield return XElement.Parse(key.Xml);
59+
}
60+
}
61+
}
5062
}
5163
}
5264

@@ -67,18 +79,5 @@ public void StoreElement(XElement element, string friendlyName)
6779
context.SaveChanges();
6880
}
6981
}
70-
71-
private static XElement TryParseKeyXml(string xml, ILogger logger)
72-
{
73-
try
74-
{
75-
return XElement.Parse(xml);
76-
}
77-
catch (Exception e)
78-
{
79-
logger?.LogExceptionWhileParsingKeyXml(xml, e);
80-
return null;
81-
}
82-
}
8382
}
8483
}

src/DataProtection/EntityFrameworkCore/src/LoggingExtensions.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -7,23 +7,23 @@ namespace Microsoft.Extensions.Logging
77
{
88
internal static class LoggingExtensions
99
{
10-
private static readonly Action<ILogger, string, Exception> _anExceptionOccurredWhileParsingKeyXml;
11-
private static readonly Action<ILogger, string, string, Exception> _savingKeyToDbContext;
10+
private static readonly Action<ILogger, string?, string?, Exception?> _readingXmlFromKey;
11+
private static readonly Action<ILogger, string, string, Exception?> _savingKeyToDbContext;
1212

1313
static LoggingExtensions()
1414
{
15-
_anExceptionOccurredWhileParsingKeyXml = LoggerMessage.Define<string>(
16-
eventId: new EventId(1, "ExceptionOccurredWhileParsingKeyXml"),
17-
logLevel: LogLevel.Warning,
18-
formatString: "An exception occurred while parsing the key xml '{Xml}'.");
15+
_readingXmlFromKey = LoggerMessage.Define<string?, string?>(
16+
eventId: new EventId(1, "ReadKeyFromElement"),
17+
logLevel: LogLevel.Debug,
18+
formatString: "Reading data with key '{FriendlyName}', value '{Value}'.");
1919
_savingKeyToDbContext = LoggerMessage.Define<string, string>(
2020
eventId: new EventId(2, "SavingKeyToDbContext"),
2121
logLevel: LogLevel.Debug,
2222
formatString: "Saving key '{FriendlyName}' to '{DbContext}'.");
2323
}
2424

25-
public static void LogExceptionWhileParsingKeyXml(this ILogger logger, string keyXml, Exception exception)
26-
=> _anExceptionOccurredWhileParsingKeyXml(logger, keyXml, exception);
25+
public static void ReadingXmlFromKey(this ILogger logger, string? friendlyName, string? keyXml)
26+
=> _readingXmlFromKey(logger, friendlyName, keyXml, null);
2727

2828
public static void LogSavingKeyToDbContext(this ILogger logger, string friendlyName, string contextName)
2929
=> _savingKeyToDbContext(logger, friendlyName, contextName, null);

src/DataProtection/EntityFrameworkCore/src/Microsoft.AspNetCore.DataProtection.EntityFrameworkCore.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<Description>Support for storing keys using Entity Framework Core.</Description>
@@ -7,6 +7,7 @@
77
<GenerateDocumentationFile>true</GenerateDocumentationFile>
88
<PackageTags>aspnetcore;dataprotection;entityframeworkcore</PackageTags>
99
<IsPackable>true</IsPackable>
10+
<Nullable>enable</Nullable>
1011
</PropertyGroup>
1112

1213
<ItemGroup>

src/DataProtection/Extensions/ref/Microsoft.AspNetCore.DataProtection.Extensions.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.0;$(DefaultNetCoreTargetFramework)</TargetFrameworks>
55
<TargetFrameworks Condition="'$(DotNetBuildFromSource)' == 'true'">$(DefaultNetCoreTargetFramework)</TargetFrameworks>
6+
<Nullable>annotations</Nullable>
67
</PropertyGroup>
78
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
89
<Compile Include="Microsoft.AspNetCore.DataProtection.Extensions.netstandard2.0.cs" />

src/DataProtection/Extensions/src/DataProtectionProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ public static IDataProtectionProvider Create(
151151
}
152152

153153
internal static IDataProtectionProvider CreateProvider(
154-
DirectoryInfo keyDirectory,
154+
DirectoryInfo? keyDirectory,
155155
Action<IDataProtectionBuilder> setupAction,
156-
X509Certificate2 certificate)
156+
X509Certificate2? certificate)
157157
{
158158
// build the service collection
159159
var serviceCollection = new ServiceCollection();

src/DataProtection/Extensions/src/Microsoft.AspNetCore.DataProtection.Extensions.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<Description>Additional APIs for ASP.NET Core data protection.</Description>
@@ -7,6 +7,7 @@
77
<IsAspNetCoreApp>true</IsAspNetCoreApp>
88
<GenerateDocumentationFile>true</GenerateDocumentationFile>
99
<PackageTags>aspnetcore;dataprotection</PackageTags>
10+
<Nullable>enable</Nullable>
1011
</PropertyGroup>
1112

1213
<ItemGroup>

src/DataProtection/Extensions/src/TimeLimitedDataProtector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ internal sealed class TimeLimitedDataProtector : ITimeLimitedDataProtector
1717
private const string MyPurposeString = "Microsoft.AspNetCore.DataProtection.TimeLimitedDataProtector.v1";
1818

1919
private readonly IDataProtector _innerProtector;
20-
private IDataProtector _innerProtectorWithTimeLimitedPurpose; // created on-demand
20+
private IDataProtector? _innerProtectorWithTimeLimitedPurpose; // created on-demand
2121

2222
public TimeLimitedDataProtector(IDataProtector innerProtector)
2323
{

src/DataProtection/StackExchangeRedis/src/Microsoft.AspNetCore.DataProtection.StackExchangeRedis.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<Description>Support for storing data protection keys in Redis.</Description>
@@ -7,6 +7,7 @@
77
<GenerateDocumentationFile>true</GenerateDocumentationFile>
88
<PackageTags>aspnetcore;dataprotection;redis</PackageTags>
99
<IsPackable>true</IsPackable>
10+
<Nullable>enable</Nullable>
1011
</PropertyGroup>
1112

1213
<ItemGroup>

src/Shared/WebEncoders/WebEncoders.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
#nullable enable
5-
65
using System;
76
#if NETCOREAPP
87
using System.Buffers;

0 commit comments

Comments
 (0)