diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/src/AeadAes/AeAesEncryptionProcessor.cs b/Microsoft.Azure.Cosmos.Encryption.Custom/src/AeadAes/AeAesEncryptionProcessor.cs index 0944714aac..c7eb3658d4 100644 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/src/AeadAes/AeAesEncryptionProcessor.cs +++ b/Microsoft.Azure.Cosmos.Encryption.Custom/src/AeadAes/AeAesEncryptionProcessor.cs @@ -13,6 +13,8 @@ namespace Microsoft.Azure.Cosmos.Encryption.Custom using Newtonsoft.Json; using Newtonsoft.Json.Linq; +#pragma warning disable IDE0057 // Use range operator +#pragma warning disable VSTHRD103 // Call async methods when in an async method internal static class AeAesEncryptionProcessor { public static async Task EncryptAsync( @@ -65,6 +67,7 @@ public static async Task EncryptAsync( encryptionOptions.PathsToEncrypt); itemJObj.Add(Constants.EncryptedInfo, JObject.FromObject(encryptionProperties)); + input.Dispose(); return EncryptionProcessor.BaseSerializer.ToStream(itemJObj); } @@ -113,4 +116,7 @@ internal static async Task DecryptContentAsync( return decryptionContext; } } + +#pragma warning restore IDE0057 // Use range operator +#pragma warning restore VSTHRD103 // Call async methods when in an async method } diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/src/AeadAes/AeadAes256CbcHmac256Algorithm.cs b/Microsoft.Azure.Cosmos.Encryption.Custom/src/AeadAes/AeadAes256CbcHmac256Algorithm.cs index 6ea90a33bf..dc946861ae 100644 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/src/AeadAes/AeadAes256CbcHmac256Algorithm.cs +++ b/Microsoft.Azure.Cosmos.Encryption.Custom/src/AeadAes/AeadAes256CbcHmac256Algorithm.cs @@ -10,6 +10,8 @@ namespace Microsoft.Azure.Cosmos.Encryption.Custom using System.IO; using System.Security.Cryptography; +#pragma warning disable SYSLIB0021 // Type or member is obsolete + /// /// This class implements authenticated encryption algorithm with associated data as described in /// http://tools.ietf.org/html/draft-mcgrew-aead-aes-cbc-hmac-sha2-05 - specifically this implements @@ -483,4 +485,6 @@ private static int GetCipherTextLength(int inputSize) return ((inputSize / BlockSizeInBytes) + 1) * BlockSizeInBytes; } } + +#pragma warning restore SYSLIB0021 // Type or member is obsolete } \ No newline at end of file diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/src/Common/CosmosJsonDotNetSerializer.cs b/Microsoft.Azure.Cosmos.Encryption.Custom/src/Common/CosmosJsonDotNetSerializer.cs index b00c004476..9ed8056360 100644 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/src/Common/CosmosJsonDotNetSerializer.cs +++ b/Microsoft.Azure.Cosmos.Encryption.Custom/src/Common/CosmosJsonDotNetSerializer.cs @@ -40,10 +40,14 @@ internal CosmosJsonDotNetSerializer(JsonSerializerSettings jsonSerializerSetting /// The object representing the deserialized stream public T FromStream(Stream stream) { +#if NET8_0_OR_GREATER + ArgumentNullException.ThrowIfNull(stream); +#else if (stream == null) { throw new ArgumentNullException(nameof(stream)); } +#endif if (typeof(Stream).IsAssignableFrom(typeof(T))) { diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/src/CosmosDataEncryptionKeyProvider.cs b/Microsoft.Azure.Cosmos.Encryption.Custom/src/CosmosDataEncryptionKeyProvider.cs index cd2fe84fbc..c6f1ffb66c 100644 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/src/CosmosDataEncryptionKeyProvider.cs +++ b/Microsoft.Azure.Cosmos.Encryption.Custom/src/CosmosDataEncryptionKeyProvider.cs @@ -148,10 +148,14 @@ public async Task InitializeAsync( throw new InvalidOperationException($"{nameof(CosmosDataEncryptionKeyProvider)} has already been initialized."); } +#if NET8_0_OR_GREATER + ArgumentNullException.ThrowIfNull(database); +#else if (database == null) { throw new ArgumentNullException(nameof(database)); } +#endif ContainerResponse containerResponse = await database.CreateContainerIfNotExistsAsync( containerId, diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/src/DataEncryptionKey.cs b/Microsoft.Azure.Cosmos.Encryption.Custom/src/DataEncryptionKey.cs index b764debfd5..810227479e 100644 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/src/DataEncryptionKey.cs +++ b/Microsoft.Azure.Cosmos.Encryption.Custom/src/DataEncryptionKey.cs @@ -102,10 +102,14 @@ public static DataEncryptionKey Create( byte[] rawKey, string encryptionAlgorithm) { +#if NET8_0_OR_GREATER + ArgumentNullException.ThrowIfNull(rawKey); +#else if (rawKey == null) { throw new ArgumentNullException(nameof(rawKey)); } +#endif #pragma warning disable CS0618 // Type or member is obsolete if (!string.Equals(encryptionAlgorithm, CosmosEncryptionAlgorithm.AEAes256CbcHmacSha256Randomized)) diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/src/DataEncryptionKeyContainerCore.cs b/Microsoft.Azure.Cosmos.Encryption.Custom/src/DataEncryptionKeyContainerCore.cs index 90cf907abc..1fcdd8c783 100644 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/src/DataEncryptionKeyContainerCore.cs +++ b/Microsoft.Azure.Cosmos.Encryption.Custom/src/DataEncryptionKeyContainerCore.cs @@ -67,10 +67,14 @@ public override async Task> CreateData throw new ArgumentException(string.Format("Unsupported Encryption Algorithm {0}", encryptionAlgorithm), nameof(encryptionAlgorithm)); } +#if NET8_0_OR_GREATER + ArgumentNullException.ThrowIfNull(encryptionKeyWrapMetadata); +#else if (encryptionKeyWrapMetadata == null) { throw new ArgumentNullException(nameof(encryptionKeyWrapMetadata)); } +#endif CosmosDiagnosticsContext diagnosticsContext = CosmosDiagnosticsContext.Create(requestOptions); @@ -155,10 +159,14 @@ public override async Task> RewrapData ItemRequestOptions requestOptions = null, CancellationToken cancellationToken = default) { +#if NET8_0_OR_GREATER + ArgumentNullException.ThrowIfNull(newWrapMetadata); +#else if (newWrapMetadata == null) { throw new ArgumentNullException(nameof(newWrapMetadata)); } +#endif CosmosDiagnosticsContext diagnosticsContext = CosmosDiagnosticsContext.Create(requestOptions); diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/src/DataEncryptionKeyContainerInlineCore.cs b/Microsoft.Azure.Cosmos.Encryption.Custom/src/DataEncryptionKeyContainerInlineCore.cs index d918d85d4a..bab5cadd62 100644 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/src/DataEncryptionKeyContainerInlineCore.cs +++ b/Microsoft.Azure.Cosmos.Encryption.Custom/src/DataEncryptionKeyContainerInlineCore.cs @@ -78,10 +78,14 @@ public override Task> RewrapDataEncryp throw new ArgumentNullException(nameof(id)); } +#if NET8_0_OR_GREATER + ArgumentNullException.ThrowIfNull(newWrapMetadata); +#else if (newWrapMetadata == null) { throw new ArgumentNullException(nameof(newWrapMetadata)); } +#endif return TaskHelper.RunInlineIfNeededAsync(() => this.dataEncryptionKeyContainerCore.RewrapDataEncryptionKeyAsync(id, newWrapMetadata, encryptionAlgorithm, requestOptions, cancellationToken)); diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/src/DataEncryptionKeyFeedIterator{T}.cs b/Microsoft.Azure.Cosmos.Encryption.Custom/src/DataEncryptionKeyFeedIterator{T}.cs index 18e4246fdc..d01422c81b 100644 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/src/DataEncryptionKeyFeedIterator{T}.cs +++ b/Microsoft.Azure.Cosmos.Encryption.Custom/src/DataEncryptionKeyFeedIterator{T}.cs @@ -13,7 +13,7 @@ namespace Microsoft.Azure.Cosmos.Encryption.Custom internal sealed class DataEncryptionKeyFeedIterator : FeedIterator { - private readonly FeedIterator feedIterator; + private readonly DataEncryptionKeyFeedIterator feedIterator; private readonly CosmosResponseFactory responseFactory; public DataEncryptionKeyFeedIterator( @@ -57,7 +57,7 @@ public override async Task> ReadNextAsync(CancellationToken canc if (responseMessage.IsSuccessStatusCode && responseMessage.Content != null) { - dataEncryptionKeyPropertiesList = this.ConvertResponseToDataEncryptionKeyPropertiesList( + dataEncryptionKeyPropertiesList = DataEncryptionKeyFeedIterator.ConvertResponseToDataEncryptionKeyPropertiesList( responseMessage.Content); return (responseMessage, dataEncryptionKeyPropertiesList); @@ -67,7 +67,7 @@ public override async Task> ReadNextAsync(CancellationToken canc } } - private List ConvertResponseToDataEncryptionKeyPropertiesList( + private static List ConvertResponseToDataEncryptionKeyPropertiesList( Stream content) { JObject contentJObj = EncryptionProcessor.BaseSerializer.FromStream(content); diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/src/DecryptableFeedResponse.cs b/Microsoft.Azure.Cosmos.Encryption.Custom/src/DecryptableFeedResponse.cs index c16497ac20..24c07c71e4 100644 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/src/DecryptableFeedResponse.cs +++ b/Microsoft.Azure.Cosmos.Encryption.Custom/src/DecryptableFeedResponse.cs @@ -45,10 +45,14 @@ internal static DecryptableFeedResponse CreateResponse( ResponseMessage responseMessage, IReadOnlyCollection resource) { +#if NET8_0_OR_GREATER + ArgumentNullException.ThrowIfNull(responseMessage); +#else if (responseMessage == null) { throw new ArgumentNullException(nameof(responseMessage)); } +#endif using (responseMessage) { diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/src/EncryptionContainer.cs b/Microsoft.Azure.Cosmos.Encryption.Custom/src/EncryptionContainer.cs index a5ad419a52..f769e1504a 100644 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/src/EncryptionContainer.cs +++ b/Microsoft.Azure.Cosmos.Encryption.Custom/src/EncryptionContainer.cs @@ -122,10 +122,14 @@ public override async Task CreateItemStreamAsync( ItemRequestOptions requestOptions = null, CancellationToken cancellationToken = default) { +#if NET8_0_OR_GREATER + ArgumentNullException.ThrowIfNull(streamPayload); +#else if (streamPayload == null) { throw new ArgumentNullException(nameof(streamPayload)); } +#endif CosmosDiagnosticsContext diagnosticsContext = CosmosDiagnosticsContext.Create(requestOptions); using (diagnosticsContext.CreateScope("CreateItemStream")) @@ -304,6 +308,10 @@ public override async Task> ReplaceItemAsync( ItemRequestOptions requestOptions = null, CancellationToken cancellationToken = default) { +#if NET8_0_OR_GREATER + ArgumentNullException.ThrowIfNull(id); + ArgumentNullException.ThrowIfNull(item); +#else if (id == null) { throw new ArgumentNullException(nameof(id)); @@ -313,6 +321,7 @@ public override async Task> ReplaceItemAsync( { throw new ArgumentNullException(nameof(item)); } +#endif if (requestOptions is not EncryptionItemRequestOptions encryptionItemRequestOptions || encryptionItemRequestOptions.EncryptionOptions == null) @@ -384,6 +393,10 @@ public override async Task ReplaceItemStreamAsync( ItemRequestOptions requestOptions = null, CancellationToken cancellationToken = default) { +#if NET8_0_OR_GREATER + ArgumentNullException.ThrowIfNull(id); + ArgumentNullException.ThrowIfNull(streamPayload); +#else if (id == null) { throw new ArgumentNullException(nameof(id)); @@ -393,6 +406,7 @@ public override async Task ReplaceItemStreamAsync( { throw new ArgumentNullException(nameof(streamPayload)); } +#endif CosmosDiagnosticsContext diagnosticsContext = CosmosDiagnosticsContext.Create(requestOptions); using (diagnosticsContext.CreateScope("ReplaceItemStream")) @@ -428,11 +442,6 @@ private async Task ReplaceItemHelperAsync( cancellationToken); } - if (partitionKey == null) - { - throw new NotSupportedException($"{nameof(partitionKey)} cannot be null for operations using {nameof(EncryptionContainer)}."); - } - streamPayload = await EncryptionProcessor.EncryptAsync( streamPayload, this.Encryptor, @@ -536,10 +545,14 @@ public override async Task UpsertItemStreamAsync( ItemRequestOptions requestOptions = null, CancellationToken cancellationToken = default) { +#if NET8_0_OR_GREATER + ArgumentNullException.ThrowIfNull(streamPayload); +#else if (streamPayload == null) { throw new ArgumentNullException(nameof(streamPayload)); } +#endif CosmosDiagnosticsContext diagnosticsContext = CosmosDiagnosticsContext.Create(requestOptions); using (diagnosticsContext.CreateScope("UpsertItemStream")) @@ -572,11 +585,6 @@ private async Task UpsertItemHelperAsync( cancellationToken); } - if (partitionKey == null) - { - throw new NotSupportedException($"{nameof(partitionKey)} cannot be null for operations using {nameof(EncryptionContainer)}."); - } - streamPayload = await EncryptionProcessor.EncryptAsync( streamPayload, this.Encryptor, diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/src/EncryptionExceptionFactory.cs b/Microsoft.Azure.Cosmos.Encryption.Custom/src/EncryptionExceptionFactory.cs index 55884f1093..3b50114cdb 100644 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/src/EncryptionExceptionFactory.cs +++ b/Microsoft.Azure.Cosmos.Encryption.Custom/src/EncryptionExceptionFactory.cs @@ -8,6 +8,7 @@ namespace Microsoft.Azure.Cosmos.Encryption.Custom internal static class EncryptionExceptionFactory { +#pragma warning disable CA2208 // Instantiate argument exceptions correctly internal static ArgumentException InvalidKeySize(string algorithmName, int actualKeylength, int expectedLength) { return new ArgumentException( @@ -28,6 +29,7 @@ internal static ArgumentException InvalidAlgorithmVersion(byte actual, byte expe $"Invalid encryption algorithm version; actual: {actual:X2}, expected: {expected:X2}.", "cipherText"); } +#pragma warning restore CA2208 // Instantiate argument exceptions correctly internal static ArgumentException InvalidAuthenticationTag() { diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/src/EncryptionFeedIterator{T}.cs b/Microsoft.Azure.Cosmos.Encryption.Custom/src/EncryptionFeedIterator{T}.cs index ee22f330ac..81f1516e1b 100644 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/src/EncryptionFeedIterator{T}.cs +++ b/Microsoft.Azure.Cosmos.Encryption.Custom/src/EncryptionFeedIterator{T}.cs @@ -11,7 +11,7 @@ namespace Microsoft.Azure.Cosmos.Encryption.Custom internal sealed class EncryptionFeedIterator : FeedIterator { - private readonly FeedIterator feedIterator; + private readonly EncryptionFeedIterator feedIterator; private readonly CosmosResponseFactory responseFactory; public EncryptionFeedIterator( @@ -31,8 +31,7 @@ public override async Task> ReadNextAsync(CancellationToken canc if (typeof(T) == typeof(DecryptableItem)) { IReadOnlyCollection resource; - EncryptionFeedIterator encryptionFeedIterator = this.feedIterator as EncryptionFeedIterator; - (responseMessage, resource) = await encryptionFeedIterator.ReadNextWithoutDecryptionAsync(cancellationToken); + (responseMessage, resource) = await this.feedIterator.ReadNextWithoutDecryptionAsync(cancellationToken); return DecryptableFeedResponse.CreateResponse( responseMessage, diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/src/EncryptionKeyWrapMetadata.cs b/Microsoft.Azure.Cosmos.Encryption.Custom/src/EncryptionKeyWrapMetadata.cs index e3c285531d..5855a011cf 100644 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/src/EncryptionKeyWrapMetadata.cs +++ b/Microsoft.Azure.Cosmos.Encryption.Custom/src/EncryptionKeyWrapMetadata.cs @@ -114,18 +114,5 @@ public bool Equals(EncryptionKeyWrapMetadata other) this.Value == other.Value && this.Name == other.Name; } - - internal string GetName(EncryptionKeyWrapMetadata encryptionKeyWrapMetadata) - { - /* A legacy DEK may not have a Name value in meta-data*/ - if (string.IsNullOrWhiteSpace(encryptionKeyWrapMetadata.Name)) - { - return encryptionKeyWrapMetadata.Value; - } - else - { - return encryptionKeyWrapMetadata.Name; - } - } } } \ No newline at end of file diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/src/EncryptionProcessor.cs b/Microsoft.Azure.Cosmos.Encryption.Custom/src/EncryptionProcessor.cs index e1e384d156..53d74390f8 100644 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/src/EncryptionProcessor.cs +++ b/Microsoft.Azure.Cosmos.Encryption.Custom/src/EncryptionProcessor.cs @@ -112,7 +112,11 @@ public static async Task EncryptAsync( } DecryptionContext decryptionContext = await DecryptInternalAsync(encryptor, diagnosticsContext, itemJObj, encryptionPropertiesJObj, cancellationToken); +#if NET8_0_OR_GREATER + await input.DisposeAsync(); +#else input.Dispose(); +#endif return (BaseSerializer.ToStream(itemJObj), decryptionContext); } @@ -181,6 +185,11 @@ private static void ValidateInputForEncrypt( Encryptor encryptor, EncryptionOptions encryptionOptions) { +#if NET8_0_OR_GREATER + ArgumentNullException.ThrowIfNull(input); + ArgumentNullException.ThrowIfNull(encryptor); + ArgumentNullException.ThrowIfNull(encryptionOptions); +#else if (input == null) { throw new ArgumentNullException(nameof(input)); @@ -195,7 +204,9 @@ private static void ValidateInputForEncrypt( { throw new ArgumentNullException(nameof(encryptionOptions)); } +#endif +#pragma warning disable CA2208 // Instantiate argument exceptions correctly if (string.IsNullOrWhiteSpace(encryptionOptions.DataEncryptionKeyId)) { throw new ArgumentNullException(nameof(encryptionOptions.DataEncryptionKeyId)); @@ -210,6 +221,7 @@ private static void ValidateInputForEncrypt( { throw new ArgumentNullException(nameof(encryptionOptions.PathsToEncrypt)); } +#pragma warning restore CA2208 // Instantiate argument exceptions correctly } private static JObject RetrieveItem( diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/src/MdeServices/MdeEncryptionAlgorithm.cs b/Microsoft.Azure.Cosmos.Encryption.Custom/src/MdeServices/MdeEncryptionAlgorithm.cs index 8bb3a09ed3..b35422d261 100644 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/src/MdeServices/MdeEncryptionAlgorithm.cs +++ b/Microsoft.Azure.Cosmos.Encryption.Custom/src/MdeServices/MdeEncryptionAlgorithm.cs @@ -34,6 +34,10 @@ public MdeEncryptionAlgorithm( TimeSpan? cacheTimeToLive, bool withRawKey = false) { +#if NET8_0_OR_GREATER + ArgumentNullException.ThrowIfNull(dekProperties); + ArgumentNullException.ThrowIfNull(encryptionKeyStoreProvider); +#else if (dekProperties == null) { throw new ArgumentNullException(nameof(dekProperties)); @@ -43,6 +47,7 @@ public MdeEncryptionAlgorithm( { throw new ArgumentNullException(nameof(encryptionKeyStoreProvider)); } +#endif KeyEncryptionKey keyEncryptionKey = KeyEncryptionKey.GetOrCreate( dekProperties.EncryptionKeyWrapMetadata.Name, diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/src/MdeServices/MdeKeyWrapProvider.cs b/Microsoft.Azure.Cosmos.Encryption.Custom/src/MdeServices/MdeKeyWrapProvider.cs index 6520ecd67e..297a4dac7e 100644 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/src/MdeServices/MdeKeyWrapProvider.cs +++ b/Microsoft.Azure.Cosmos.Encryption.Custom/src/MdeServices/MdeKeyWrapProvider.cs @@ -28,10 +28,14 @@ public override Task UnwrapKeyAsync( EncryptionKeyWrapMetadata metadata, CancellationToken cancellationToken) { +#if NET8_0_OR_GREATER + ArgumentNullException.ThrowIfNull(metadata); +#else if (metadata == null) { throw new ArgumentNullException(nameof(metadata)); } +#endif KeyEncryptionKey keyEncryptionKey = KeyEncryptionKey.GetOrCreate( metadata.Name, @@ -47,10 +51,14 @@ public override Task WrapKeyAsync( EncryptionKeyWrapMetadata metadata, CancellationToken cancellationToken) { +#if NET8_0_OR_GREATER + ArgumentNullException.ThrowIfNull(metadata); +#else if (metadata == null) { throw new ArgumentNullException(nameof(metadata)); } +#endif KeyEncryptionKey keyEncryptionKey = KeyEncryptionKey.GetOrCreate( metadata.Name, diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/src/Microsoft.Azure.Cosmos.Encryption.Custom.csproj b/Microsoft.Azure.Cosmos.Encryption.Custom/src/Microsoft.Azure.Cosmos.Encryption.Custom.csproj index ea968c3020..6f492824b6 100644 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/src/Microsoft.Azure.Cosmos.Encryption.Custom.csproj +++ b/Microsoft.Azure.Cosmos.Encryption.Custom/src/Microsoft.Azure.Cosmos.Encryption.Custom.csproj @@ -1,6 +1,6 @@ - netstandard2.0 + netstandard2.0;net8.0 Microsoft.Azure.Cosmos.Encryption.Custom Microsoft.Azure.Cosmos.Encryption.Custom $(LangVersion) diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/src/SecurityUtility.cs b/Microsoft.Azure.Cosmos.Encryption.Custom/src/SecurityUtility.cs index 632f1904d5..9a7ad82bbe 100644 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/src/SecurityUtility.cs +++ b/Microsoft.Azure.Cosmos.Encryption.Custom/src/SecurityUtility.cs @@ -42,7 +42,9 @@ internal static string GetSHA256Hash(byte[] input) using (SHA256 sha256 = SHA256.Create()) { +#pragma warning disable CA1850 // Prefer static 'HashData' method over 'ComputeHash' byte[] hashValue = sha256.ComputeHash(input); +#pragma warning restore CA1850 // Prefer static 'HashData' method over 'ComputeHash' return GetHexString(hashValue); } } @@ -54,8 +56,12 @@ internal static string GetSHA256Hash(byte[] input) internal static void GenerateRandomBytes(byte[] randomBytes) { // Generate random bytes cryptographically. +#if NET8_0_OR_GREATER + RandomNumberGenerator.Fill(randomBytes); +#else using RNGCryptoServiceProvider rngCsp = new (); rngCsp.GetBytes(randomBytes); +#endif } /// diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/src/Transformation/JObjectSqlSerializer.Stable.cs b/Microsoft.Azure.Cosmos.Encryption.Custom/src/Transformation/JObjectSqlSerializer.Stable.cs index f5b30910ab..12c8822b48 100644 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/src/Transformation/JObjectSqlSerializer.Stable.cs +++ b/Microsoft.Azure.Cosmos.Encryption.Custom/src/Transformation/JObjectSqlSerializer.Stable.cs @@ -21,7 +21,7 @@ internal class JObjectSqlSerializer private static readonly JsonSerializerSettings JsonSerializerSettings = EncryptionProcessor.JsonSerializerSettings; - internal (TypeMarker, byte[]) Serialize(JToken propertyValue) + internal virtual (TypeMarker, byte[]) Serialize(JToken propertyValue) { switch (propertyValue.Type) { diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/src/Transformation/MdeEncryptionProcessor.Preview.cs b/Microsoft.Azure.Cosmos.Encryption.Custom/src/Transformation/MdeEncryptionProcessor.Preview.cs index 5f14d006bf..e4447c3c5c 100644 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/src/Transformation/MdeEncryptionProcessor.Preview.cs +++ b/Microsoft.Azure.Cosmos.Encryption.Custom/src/Transformation/MdeEncryptionProcessor.Preview.cs @@ -36,7 +36,11 @@ public async Task EncryptAsync( foreach (string pathToEncrypt in encryptionOptions.PathsToEncrypt) { +#if NET8_0_OR_GREATER + string propertyName = pathToEncrypt[1..]; +#else string propertyName = pathToEncrypt.Substring(1); +#endif if (!itemJObj.TryGetValue(propertyName, out JToken propertyValue)) { continue; @@ -69,7 +73,11 @@ public async Task EncryptAsync( pathsEncrypted); itemJObj.Add(Constants.EncryptedInfo, JObject.FromObject(encryptionProperties)); +#if NET8_0_OR_GREATER + await input.DisposeAsync(); +#else input.Dispose(); +#endif return EncryptionProcessor.BaseSerializer.ToStream(itemJObj); } @@ -95,7 +103,12 @@ internal async Task DecryptObjectAsync( List pathsDecrypted = new (encryptionProperties.EncryptedPaths.Count()); foreach (string path in encryptionProperties.EncryptedPaths) { +#if NET8_0_OR_GREATER + string propertyName = path[1..]; +#else string propertyName = path.Substring(1); +#endif + if (!document.TryGetValue(propertyName, out JToken propertyValue)) { // malformed document, such record shouldn't be there at all diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/src/Transformation/MdeEncryptionProcessor.Stable.cs b/Microsoft.Azure.Cosmos.Encryption.Custom/src/Transformation/MdeEncryptionProcessor.Stable.cs index ab05b4f770..1f9b628617 100644 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/src/Transformation/MdeEncryptionProcessor.Stable.cs +++ b/Microsoft.Azure.Cosmos.Encryption.Custom/src/Transformation/MdeEncryptionProcessor.Stable.cs @@ -36,7 +36,11 @@ public async Task EncryptAsync( foreach (string pathToEncrypt in encryptionOptions.PathsToEncrypt) { +#if NET8_0_OR_GREATER + string propertyName = pathToEncrypt[1..]; +#else string propertyName = pathToEncrypt.Substring(1); +#endif if (!itemJObj.TryGetValue(propertyName, out JToken propertyValue)) { continue; @@ -69,7 +73,11 @@ public async Task EncryptAsync( pathsEncrypted); itemJObj.Add(Constants.EncryptedInfo, JObject.FromObject(encryptionProperties)); +#if NET8_0_OR_GREATER + await input.DisposeAsync(); +#else input.Dispose(); +#endif return EncryptionProcessor.BaseSerializer.ToStream(itemJObj); } @@ -94,7 +102,11 @@ internal async Task DecryptObjectAsync( List pathsDecrypted = new (encryptionProperties.EncryptedPaths.Count()); foreach (string path in encryptionProperties.EncryptedPaths) { +#if NET8_0_OR_GREATER + string propertyName = path[1..]; +#else string propertyName = path.Substring(1); +#endif if (!document.TryGetValue(propertyName, out JToken propertyValue)) { // malformed document, such record shouldn't be there at all diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/src/Transformation/MemoryTextReader.cs b/Microsoft.Azure.Cosmos.Encryption.Custom/src/Transformation/MemoryTextReader.cs index 71aed0fd14..c3f96c274a 100644 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/src/Transformation/MemoryTextReader.cs +++ b/Microsoft.Azure.Cosmos.Encryption.Custom/src/Transformation/MemoryTextReader.cs @@ -71,6 +71,12 @@ public override int Read() public override int Read(char[] buffer, int index, int count) { +#if NET8_0_OR_GREATER + ArgumentNullException.ThrowIfNull(buffer); + ArgumentOutOfRangeException.ThrowIfNegative(index); + ArgumentOutOfRangeException.ThrowIfNegative(count); + ArgumentOutOfRangeException.ThrowIfGreaterThan(count, buffer.Length - index); +#else if (buffer == null) { throw new ArgumentNullException(nameof(buffer)); @@ -90,6 +96,7 @@ public override int Read(char[] buffer, int index, int count) { throw new ArgumentOutOfRangeException(); } +#endif if (this.closed) { @@ -119,7 +126,11 @@ public override string ReadToEnd() } this.pos = this.length; +#if NET8_0_OR_GREATER + return new string(this.chars[this.pos..this.length].Span); +#else return new string(this.chars.Slice(this.pos, this.length - this.pos).ToArray()); +#endif } public override string ReadLine() @@ -135,7 +146,11 @@ public override string ReadLine() char ch = this.chars.Span[i]; if (ch == '\r' || ch == '\n') { +#if NET8_0_OR_GREATER + string result = new (this.chars[this.pos..i].Span); +#else string result = new (this.chars.Slice(this.pos, i - this.pos).ToArray()); +#endif this.pos = i + 1; if (ch == '\r' && this.pos < this.length && this.chars.Span[this.pos] == '\n') { @@ -150,7 +165,11 @@ public override string ReadLine() if (i > this.pos) { +#if NET8_0_OR_GREATER + string result = new (this.chars[this.pos..i].Span); +#else string result = new (this.chars.Slice(this.pos, i - this.pos).ToArray()); +#endif this.pos = i; return result; } diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/tests/EmulatorTests/LegacyEncryptionTests.cs b/Microsoft.Azure.Cosmos.Encryption.Custom/tests/EmulatorTests/LegacyEncryptionTests.cs index 30f6364a32..9ee43b8b07 100644 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/tests/EmulatorTests/LegacyEncryptionTests.cs +++ b/Microsoft.Azure.Cosmos.Encryption.Custom/tests/EmulatorTests/LegacyEncryptionTests.cs @@ -1491,7 +1491,7 @@ private static EncryptionItemRequestOptions GetRequestOptions( }; } - private static TransactionalBatchItemRequestOptions GetBatchItemRequestOptions( + private static EncryptionTransactionalBatchItemRequestOptions GetBatchItemRequestOptions( string dekId, List pathsToEncrypt, string ifMatchEtag = null) @@ -1812,13 +1812,11 @@ public override Stream ToStream(T input) MemoryStream streamPayload = new(); using (StreamWriter streamWriter = new(streamPayload, encoding: Encoding.UTF8, bufferSize: 1024, leaveOpen: true)) { - using (JsonWriter writer = new JsonTextWriter(streamWriter)) - { - writer.Formatting = Formatting.None; - this.serializer.Serialize(writer, input); - writer.Flush(); - streamWriter.Flush(); - } + using JsonTextWriter writer = new (streamWriter); + writer.Formatting = Formatting.None; + this.serializer.Serialize(writer, input); + writer.Flush(); + streamWriter.Flush(); } streamPayload.Position = 0; diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/tests/EmulatorTests/MdeCustomEncryptionTests.cs b/Microsoft.Azure.Cosmos.Encryption.Custom/tests/EmulatorTests/MdeCustomEncryptionTests.cs index 661b876907..14b1915abb 100644 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/tests/EmulatorTests/MdeCustomEncryptionTests.cs +++ b/Microsoft.Azure.Cosmos.Encryption.Custom/tests/EmulatorTests/MdeCustomEncryptionTests.cs @@ -1822,7 +1822,7 @@ private static EncryptionItemRequestOptions GetRequestOptions( } } - private static TransactionalBatchItemRequestOptions GetBatchItemRequestOptions( + private static EncryptionTransactionalBatchItemRequestOptions GetBatchItemRequestOptions( string dekId, List pathsToEncrypt, string ifMatchEtag = null) diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/tests/EmulatorTests/Microsoft.Azure.Cosmos.Encryption.Custom.EmulatorTests.csproj b/Microsoft.Azure.Cosmos.Encryption.Custom/tests/EmulatorTests/Microsoft.Azure.Cosmos.Encryption.Custom.EmulatorTests.csproj index c5dfa8db0c..915be6c3f8 100644 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/tests/EmulatorTests/Microsoft.Azure.Cosmos.Encryption.Custom.EmulatorTests.csproj +++ b/Microsoft.Azure.Cosmos.Encryption.Custom/tests/EmulatorTests/Microsoft.Azure.Cosmos.Encryption.Custom.EmulatorTests.csproj @@ -3,7 +3,7 @@ true true AnyCPU - net6.0 + net6.0;net8.0 false false Microsoft.Azure.Cosmos.Encryption.Custom.EmulatorTests diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/tests/Microsoft.Azure.Cosmos.Encryption.Custom.Performance.Tests/Microsoft.Azure.Cosmos.Encryption.Custom.Performance.Tests.csproj b/Microsoft.Azure.Cosmos.Encryption.Custom/tests/Microsoft.Azure.Cosmos.Encryption.Custom.Performance.Tests/Microsoft.Azure.Cosmos.Encryption.Custom.Performance.Tests.csproj index 4deb4d0edf..821014c014 100644 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/tests/Microsoft.Azure.Cosmos.Encryption.Custom.Performance.Tests/Microsoft.Azure.Cosmos.Encryption.Custom.Performance.Tests.csproj +++ b/Microsoft.Azure.Cosmos.Encryption.Custom/tests/Microsoft.Azure.Cosmos.Encryption.Custom.Performance.Tests/Microsoft.Azure.Cosmos.Encryption.Custom.Performance.Tests.csproj @@ -3,7 +3,7 @@ Microsoft.Azure.Cosmos.Encryption.Custom.Performance.Tests Exe - net6 + net8.0 enable enable diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/tests/Microsoft.Azure.Cosmos.Encryption.Custom.Tests/Microsoft.Azure.Cosmos.Encryption.Custom.Tests.csproj b/Microsoft.Azure.Cosmos.Encryption.Custom/tests/Microsoft.Azure.Cosmos.Encryption.Custom.Tests/Microsoft.Azure.Cosmos.Encryption.Custom.Tests.csproj index eee64aada9..7e1f7d48fe 100644 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/tests/Microsoft.Azure.Cosmos.Encryption.Custom.Tests/Microsoft.Azure.Cosmos.Encryption.Custom.Tests.csproj +++ b/Microsoft.Azure.Cosmos.Encryption.Custom/tests/Microsoft.Azure.Cosmos.Encryption.Custom.Tests/Microsoft.Azure.Cosmos.Encryption.Custom.Tests.csproj @@ -4,7 +4,7 @@ true true AnyCPU - net6.0 + net6.0;net8.0 false false Microsoft.Azure.Cosmos.Encryption.Tests diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Contracts/ContractEnforcement.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Contracts/ContractEnforcement.cs index c58109d65b..84bd8c63c1 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Contracts/ContractEnforcement.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Contracts/ContractEnforcement.cs @@ -14,7 +14,7 @@ public class ContractEnforcement { - private static readonly InvariantComparer invariantComparer = new InvariantComparer(); + private static readonly InvariantComparer invariantComparer = new (); private static Assembly GetAssemblyLocally(string name) { @@ -91,7 +91,10 @@ private static string GenerateNameWithClassAttributes(Type type) $"{nameof(type.IsValueType)}:{(type.IsValueType ? bool.TrueString : bool.FalseString)};" + $"{nameof(type.IsNested)}:{(type.IsNested ? bool.TrueString : bool.FalseString)};" + $"{nameof(type.IsGenericType)}:{(type.IsGenericType ? bool.TrueString : bool.FalseString)};" + +#pragma warning disable SYSLIB0050 // 'Type.IsSerializable' is obsolete: 'Formatter-based serialization is obsolete and should not be used. $"{nameof(type.IsSerializable)}:{(type.IsSerializable ? bool.TrueString : bool.FalseString)}"; +#pragma warning restore SYSLIB0050 // 'Type.IsSerializable' is obsolete: 'Formatter-based serialization is obsolete and should not be used. + } private static string GenerateNameWithMethodAttributes(MethodInfo methodInfo) @@ -241,7 +244,7 @@ public static void ValidatePreviewContractContainBreakingChanges( public static string GetCurrentContract(string dllName) { - TypeTree locally = new TypeTree(typeof(object)); + TypeTree locally = new (typeof(object)); Assembly assembly = ContractEnforcement.GetAssemblyLocally(dllName); Type[] exportedTypes = assembly.GetExportedTypes(); ContractEnforcement.BuildTypeTree(locally, exportedTypes); @@ -252,13 +255,13 @@ public static string GetCurrentContract(string dllName) public static string GetCurrentTelemetryContract(string dllName) { - List nonTelemetryModels = new List + List nonTelemetryModels = new() { "AzureVMMetadata", "Compute" }; - TypeTree locally = new TypeTree(typeof(object)); + TypeTree locally = new (typeof(object)); Assembly assembly = ContractEnforcement.GetAssemblyLocally(dllName); Type[] exportedTypes = assembly.GetTypes().Where(t => t!= null && @@ -328,7 +331,10 @@ public static void ValidateJsonAreSame(string baselineJson, string currentJson) private class InvariantComparer : IComparer { - public int Compare(string a, string b) => Comparer.DefaultInvariant.Compare(a, b); + public int Compare(string a, string b) + { + return Comparer.DefaultInvariant.Compare(a, b); + } } } }