Skip to content

Commit ff2e0f4

Browse files
kentdrbording
andauthored
Prepare RTM (#487)
* Remove preview from workflows * Bump NSB to v9, fix analyzer errors * Remove SolutionDir from project file * Remove Guard and use new language features * Fix overzealous reformatting disaster * Tweaks * Add license notice * Fix throw helper calls --------- Co-authored-by: Brandon Ording <bording@gmail.com>
1 parent e0b3568 commit ff2e0f4

15 files changed

Lines changed: 123 additions & 142 deletions

.github/workflows/ci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ jobs:
2929
uses: actions/setup-dotnet@v4.0.0
3030
with:
3131
dotnet-version: 8.0.x
32-
dotnet-quality: 'preview'
3332
- name: Build
3433
run: dotnet build src --configuration Release
3534
- name: Upload packages

.github/workflows/release.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ jobs:
1818
uses: actions/setup-dotnet@v4.0.0
1919
with:
2020
dotnet-version: 8.0.x
21-
dotnet-quality: 'preview'
2221
- name: Build
2322
run: dotnet build src --configuration Release
2423
- name: Sign NuGet packages

THIRD-PARTY-NOTICES.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
License notice for rhino-esb
2+
---------------------------------------
3+
4+
https://github.com/hibernating-rhinos/rhino-esb/blob/master/license.txt
5+
6+
Copyright (c) 2005 - 2009 Ayende Rahien (ayende@ayende.com)
7+
All rights reserved.
8+
9+
Redistribution and use in source and binary forms, with or without modification,
10+
are permitted provided that the following conditions are met:
11+
12+
* Redistributions of source code must retain the above copyright notice,
13+
this list of conditions and the following disclaimer.
14+
* Redistributions in binary form must reproduce the above copyright notice,
15+
this list of conditions and the following disclaimer in the documentation
16+
and/or other materials provided with the distribution.
17+
* Neither the name of Ayende Rahien nor the names of its
18+
contributors may be used to endorse or promote products derived from this
19+
software without specific prior written permission.
20+
21+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
25+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30+
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

src/AcceptanceTests/Infrastructure/DefaultServer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public async Task<EndpointConfiguration> GetConfiguration(RunDescriptor runDescr
2727
typesToInclude.AddRange(types);
2828

2929
var configuration = new EndpointConfiguration(endpointConfiguration.EndpointName);
30+
configuration.UseSerialization<SystemJsonSerializer>();
3031

3132
configuration.TypesToIncludeInScan(typesToInclude);
3233
configuration.EnableInstallers();

src/AcceptanceTests/NServiceBus.Encryption.MessageProperty.AcceptanceTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<ItemGroup>
1212
<PackageReference Include="GitHubActionsTestLogger" Version="2.3.3" />
1313
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
14-
<PackageReference Include="NServiceBus.AcceptanceTesting" Version="9.0.0-alpha.1" />
14+
<PackageReference Include="NServiceBus.AcceptanceTesting" Version="9.0.0" />
1515
<PackageReference Include="NUnit" Version="3.14.0" />
1616
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
1717
</ItemGroup>

src/MessageProperty/AesEncryptionService.cs

Lines changed: 54 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public AesEncryptionService(
5151
{
5252
encryptionKeyIdentifier, key
5353
}
54-
}, new List<byte[]>(0))
54+
}, [])
5555
{
5656
}
5757

@@ -62,7 +62,7 @@ public AesEncryptionService(
6262
/// <param name="keys">A dictionary of available encryption keys and their identifiers for encryption and decryption.</param>
6363
public AesEncryptionService(
6464
string encryptionKeyIdentifier,
65-
IDictionary<string, byte[]> keys) : this(encryptionKeyIdentifier, keys, new List<byte[]>(0))
65+
IDictionary<string, byte[]> keys) : this(encryptionKeyIdentifier, keys, [])
6666
{
6767
}
6868

@@ -77,9 +77,9 @@ public AesEncryptionService(
7777
IDictionary<string, byte[]> keys,
7878
IList<byte[]> decryptionKeys)
7979
{
80-
Guard.AgainstNullAndEmpty(nameof(encryptionKeyIdentifier), encryptionKeyIdentifier);
81-
Guard.AgainstNull(nameof(keys), keys);
82-
Guard.AgainstNull(nameof(decryptionKeys), decryptionKeys);
80+
ArgumentException.ThrowIfNullOrWhiteSpace(encryptionKeyIdentifier);
81+
ArgumentNullException.ThrowIfNull(keys);
82+
ArgumentNullException.ThrowIfNull(decryptionKeys);
8383

8484
this.encryptionKeyIdentifier = encryptionKeyIdentifier;
8585
this.decryptionKeys = decryptionKeys;
@@ -127,28 +127,26 @@ public EncryptedValue Encrypt(string value, IOutgoingLogicalMessageContext conte
127127

128128
AddKeyIdentifierHeader(context);
129129

130-
using (var aes = Aes.Create())
130+
using var aes = Aes.Create();
131+
132+
aes.Key = encryptionKey;
133+
aes.Mode = CipherMode.CBC;
134+
ConfigureIV(aes);
135+
136+
using var encryptor = aes.CreateEncryptor();
137+
using var memoryStream = new MemoryStream();
138+
using var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
139+
using var writer = new StreamWriter(cryptoStream);
140+
141+
writer.Write(value);
142+
writer.Flush();
143+
cryptoStream.Flush();
144+
cryptoStream.FlushFinalBlock();
145+
return new EncryptedValue
131146
{
132-
aes.Key = encryptionKey;
133-
aes.Mode = CipherMode.CBC;
134-
ConfigureIV(aes);
135-
136-
using (var encryptor = aes.CreateEncryptor())
137-
using (var memoryStream = new MemoryStream())
138-
using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
139-
using (var writer = new StreamWriter(cryptoStream))
140-
{
141-
writer.Write(value);
142-
writer.Flush();
143-
cryptoStream.Flush();
144-
cryptoStream.FlushFinalBlock();
145-
return new EncryptedValue
146-
{
147-
EncryptedBase64Value = Convert.ToBase64String(memoryStream.ToArray()),
148-
Base64Iv = Convert.ToBase64String(aes.IV)
149-
};
150-
}
151-
}
147+
EncryptedBase64Value = Convert.ToBase64String(memoryStream.ToArray()),
148+
Base64Iv = Convert.ToBase64String(aes.IV)
149+
};
152150
}
153151

154152
string DecryptUsingKeyIdentifier(EncryptedValue encryptedValue, string keyIdentifier)
@@ -191,20 +189,21 @@ string DecryptUsingAllKeys(EncryptedValue encryptedValue)
191189
static string Decrypt(EncryptedValue encryptedValue, byte[] key)
192190
{
193191
var iv = Convert.FromBase64String(encryptedValue.Base64Iv);
194-
using (var aes = Aes.Create())
195-
{
196-
var encrypted = Convert.FromBase64String(encryptedValue.EncryptedBase64Value);
197-
aes.IV = iv;
198-
aes.Mode = CipherMode.CBC;
199-
aes.Key = key;
200-
using (var decryptor = aes.CreateDecryptor())
201-
using (var memoryStream = new MemoryStream(encrypted))
202-
using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
203-
using (var reader = new StreamReader(cryptoStream))
204-
{
205-
return reader.ReadToEnd();
206-
}
207-
}
192+
193+
var encrypted = Convert.FromBase64String(encryptedValue.EncryptedBase64Value);
194+
195+
using var aes = Aes.Create();
196+
197+
aes.IV = iv;
198+
aes.Mode = CipherMode.CBC;
199+
aes.Key = key;
200+
201+
using var decryptor = aes.CreateDecryptor();
202+
using var memoryStream = new MemoryStream(encrypted);
203+
using var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
204+
using var reader = new StreamReader(cryptoStream);
205+
206+
return reader.ReadToEnd();
208207
}
209208

210209
static void VerifyExpiredKeys(IList<byte[]> keys)
@@ -233,48 +232,39 @@ static void VerifyEncryptionKey(byte[] key)
233232

234233
static bool IsValidKey(byte[] key)
235234
{
236-
using (var aes = Aes.Create())
237-
{
238-
var bitLength = key.Length * 8;
235+
using var aes = Aes.Create();
239236

240-
var maxValidKeyBitLength = aes.LegalKeySizes.Max(keyLength => keyLength.MaxSize);
241-
if (bitLength < maxValidKeyBitLength)
242-
{
243-
Log.WarnFormat("Encryption key is {0} bits which is less than the maximum allowed {1} bits. Consider using a {2}-bit encryption key to obtain the maximum cipher strength", bitLength, maxValidKeyBitLength, maxValidKeyBitLength);
244-
}
237+
var bitLength = key.Length * 8;
245238

246-
return aes.ValidKeySize(bitLength);
239+
var maxValidKeyBitLength = aes.LegalKeySizes.Max(keyLength => keyLength.MaxSize);
240+
if (bitLength < maxValidKeyBitLength)
241+
{
242+
Log.WarnFormat("Encryption key is {0} bits which is less than the maximum allowed {1} bits. Consider using a {2}-bit encryption key to obtain the maximum cipher strength", bitLength, maxValidKeyBitLength, maxValidKeyBitLength);
247243
}
244+
245+
return aes.ValidKeySize(bitLength);
248246
}
249247

250248
/// <summary>
251249
/// Adds the key identifier of the currently used encryption key to the outgoing message's headers.
252250
/// </summary>
253-
protected internal virtual void AddKeyIdentifierHeader(IOutgoingLogicalMessageContext context)
254-
{
255-
context.Headers[EncryptionHeaders.EncryptionKeyIdentifier] = encryptionKeyIdentifier;
256-
}
251+
protected internal virtual void AddKeyIdentifierHeader(IOutgoingLogicalMessageContext context) => context.Headers[EncryptionHeaders.EncryptionKeyIdentifier] = encryptionKeyIdentifier;
257252

258253
/// <summary>
259254
/// Tries to locate an encryption key identifier from an incoming message.
260255
/// </summary>
261-
protected internal virtual bool TryGetKeyIdentifierHeader(out string keyIdentifier, IIncomingLogicalMessageContext context)
262-
{
263-
return context.Headers.TryGetValue(EncryptionHeaders.EncryptionKeyIdentifier, out keyIdentifier);
264-
}
256+
protected internal virtual bool TryGetKeyIdentifierHeader(out string keyIdentifier, IIncomingLogicalMessageContext context) => context.Headers.TryGetValue(EncryptionHeaders.EncryptionKeyIdentifier, out keyIdentifier);
265257

266258
/// <summary>
267259
/// Configures the initialization vector.
268260
/// </summary>
269-
protected internal virtual void ConfigureIV(Aes aes)
270-
{
271-
aes.GenerateIV();
272-
}
261+
protected internal virtual void ConfigureIV(Aes aes) => aes.GenerateIV();
273262

274263
readonly string encryptionKeyIdentifier;
275-
IList<byte[]> decryptionKeys; // Required, as we decrypt in the configured order.
276-
byte[] encryptionKey;
277-
IDictionary<string, byte[]> keys;
264+
readonly IList<byte[]> decryptionKeys; // Required, as we decrypt in the configured order.
265+
readonly byte[] encryptionKey;
266+
readonly IDictionary<string, byte[]> keys;
267+
278268
static readonly ILog Log = LogManager.GetLogger<AesEncryptionService>();
279269
}
280270
}

src/MessageProperty/EncryptedString.cs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public EncryptedString()
2121
/// </summary>
2222
public EncryptedString(SerializationInfo info, StreamingContext context)
2323
{
24-
Guard.AgainstNull(nameof(info), info);
24+
ArgumentNullException.ThrowIfNull(info);
2525
EncryptedValue = info.GetValue("EncryptedValue", typeof(EncryptedValue)) as EncryptedValue;
2626
}
2727

@@ -35,8 +35,8 @@ public EncryptedString(SerializationInfo info, StreamingContext context)
3535
/// </summary>
3636
public EncryptedValue EncryptedValue
3737
{
38-
get { return encryptedValue; }
39-
set { encryptedValue = value; }
38+
get => encryptedValue;
39+
set => encryptedValue = value;
4040
}
4141

4242
// we need to duplicate to make versions > 3.2.7 backwards compatible with 2.X
@@ -46,28 +46,22 @@ public EncryptedValue EncryptedValue
4646
/// </summary>
4747
public void GetObjectData(SerializationInfo info, StreamingContext context)
4848
{
49-
Guard.AgainstNull(nameof(info), info);
49+
ArgumentNullException.ThrowIfNull(info);
5050
info.AddValue("EncryptedValue", EncryptedValue);
5151
}
5252

5353
/// <summary>
5454
/// Gets the string value from the WireEncryptedString.
5555
/// </summary>
56-
public static implicit operator string(EncryptedString s)
57-
{
58-
return s?.Value;
59-
}
56+
public static implicit operator string(EncryptedString s) => s?.Value;
6057

6158
/// <summary>
6259
/// Creates a new WireEncryptedString from the given string.
6360
/// </summary>
64-
public static implicit operator EncryptedString(string s)
61+
public static implicit operator EncryptedString(string s) => new()
6562
{
66-
return new EncryptedString
67-
{
68-
Value = s
69-
};
70-
}
63+
Value = s
64+
};
7165

7266
EncryptedValue encryptedValue;
7367
}

src/MessageProperty/EncryptionConfigurationExtensions.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ public static class EncryptionConfigurationExtensions
1313
/// <summary>
1414
/// Enable message property encryption using the given encryption service.
1515
/// </summary>
16-
/// <param name="configuration">The endpoint configurartion to extend.</param>
16+
/// <param name="configuration">The endpoint configuration to extend.</param>
1717
/// <param name="encryptionService">The encryption service used to encrypt and decrypt message properties.</param>
1818
public static void EnableMessagePropertyEncryption(this EndpointConfiguration configuration, IEncryptionService encryptionService)
1919
{
20-
Guard.AgainstNull(nameof(configuration), configuration);
21-
Guard.AgainstNull(nameof(encryptionService), encryptionService);
20+
ArgumentNullException.ThrowIfNull(configuration);
21+
ArgumentNullException.ThrowIfNull(encryptionService);
2222

2323
configuration.GetSettings().Set(EncryptionServiceConfigurationKey, encryptionService);
2424
configuration.EnableFeature<MessagePropertyEncryption>();
@@ -27,21 +27,18 @@ public static void EnableMessagePropertyEncryption(this EndpointConfiguration co
2727
/// <summary>
2828
/// Enable message property encryption using the given encryption service.
2929
/// </summary>
30-
/// <param name="configuration">The endpoint configurartion to extend.</param>
30+
/// <param name="configuration">The endpoint configuration to extend.</param>
3131
/// <param name="encryptionService">The encryption service used to encrypt and decrypt message properties.</param>
3232
/// <param name="encryptedPropertyConvention">The convention which defines which properties should be encrypted. By default, all properties of type <see cref="EncryptedString"/> will be encrypted.</param>
3333
public static void EnableMessagePropertyEncryption(this EndpointConfiguration configuration, IEncryptionService encryptionService, Func<PropertyInfo, bool> encryptedPropertyConvention)
3434
{
35-
Guard.AgainstNull(nameof(encryptedPropertyConvention), encryptedPropertyConvention);
35+
ArgumentNullException.ThrowIfNull(encryptedPropertyConvention);
3636

3737
configuration.EnableMessagePropertyEncryption(encryptionService);
3838
configuration.GetSettings().Set(new IsEncryptedPropertyConvention(encryptedPropertyConvention));
3939
}
4040

41-
internal static IEncryptionService GetEncryptionService(this IReadOnlySettings settings)
42-
{
43-
return settings.Get<IEncryptionService>(EncryptionServiceConfigurationKey);
44-
}
41+
internal static IEncryptionService GetEncryptionService(this IReadOnlySettings settings) => settings.Get<IEncryptionService>(EncryptionServiceConfigurationKey);
4542

4643
const string EncryptionServiceConfigurationKey = "NServiceBus.Encryption.MessageProperty.EncryptionService";
4744
}

src/MessageProperty/EncryptionInspector.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ static List<MemberInfo> GetFieldsAndProperties(object target)
155155

156156
IsEncryptedPropertyConvention encryptedPropertyConvention;
157157

158-
static List<MemberInfo> NoMembers = new List<MemberInfo>(0);
158+
static List<MemberInfo> NoMembers = [];
159159

160-
static List<Tuple<object, MemberInfo>> AlreadyVisited = new List<Tuple<object, MemberInfo>>(0);
160+
static List<Tuple<object, MemberInfo>> AlreadyVisited = [];
161161

162162
static ConcurrentDictionary<RuntimeTypeHandle, List<MemberInfo>> cache = new ConcurrentDictionary<RuntimeTypeHandle, List<MemberInfo>>();
163163
}

src/MessageProperty/IsEncrytedPropertyConvention.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,12 @@
66
/// <summary>
77
/// Message convention definitions.
88
/// </summary>
9-
class IsEncryptedPropertyConvention
9+
class IsEncryptedPropertyConvention(Func<PropertyInfo, bool> isEncryptedPropertyAction)
1010
{
11-
public IsEncryptedPropertyConvention(Func<PropertyInfo, bool> isEncryptedPropertyAction)
12-
{
13-
IsEncryptedPropertyAction = isEncryptedPropertyAction;
14-
}
15-
1611
public bool IsEncryptedProperty(PropertyInfo property)
1712
{
18-
Guard.AgainstNull(nameof(property), property);
13+
ArgumentNullException.ThrowIfNull(property);
14+
1915
try
2016
{
2117
//the message mutator will cache the whole message so we don't need to cache here
@@ -27,6 +23,6 @@ public bool IsEncryptedProperty(PropertyInfo property)
2723
}
2824
}
2925

30-
Func<PropertyInfo, bool> IsEncryptedPropertyAction;
26+
readonly Func<PropertyInfo, bool> IsEncryptedPropertyAction = isEncryptedPropertyAction;
3127
}
3228
}

0 commit comments

Comments
 (0)