Skip to content

Commit b94baeb

Browse files
author
John Simons
committed
Merge branch 'hotfix-4.2.3'
2 parents 855115d + a9b78af commit b94baeb

12 files changed

+723
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
namespace NServiceBus.AcceptanceTests.Encryption
2+
{
3+
using System.Linq;
4+
using NServiceBus.Encryption;
5+
using System;
6+
using EndpointTemplates;
7+
using AcceptanceTesting;
8+
using NUnit.Framework;
9+
using ScenarioDescriptors;
10+
11+
public class When_using_encryption_with_custom_service : NServiceBusAcceptanceTest
12+
{
13+
[Test]
14+
public void Should_receive_decrypted_message()
15+
{
16+
Scenario.Define<Context>()
17+
.WithEndpoint<Endpoint>(b => b.Given((bus, context) => bus.SendLocal(new MessageWithSecretData
18+
{
19+
Secret = "betcha can't guess my secret"
20+
})))
21+
.Done(c => c.Done)
22+
.Repeat(r => r.For<AllSerializers>())
23+
.Should(c => Assert.AreEqual("betcha can't guess my secret", c.Secret))
24+
.Run();
25+
}
26+
27+
public class Context : ScenarioContext
28+
{
29+
public bool Done { get; set; }
30+
public string Secret { get; set; }
31+
}
32+
33+
public class Endpoint : EndpointConfigurationBuilder
34+
{
35+
public Endpoint()
36+
{
37+
EndpointSetup<DefaultServer>(c => c.Configurer.RegisterSingleton<IEncryptionService>(new MyEncryptionService()));
38+
}
39+
40+
public class Handler : IHandleMessages<MessageWithSecretData>
41+
{
42+
public Context Context { get; set; }
43+
44+
public void Handle(MessageWithSecretData message)
45+
{
46+
Context.Secret = message.Secret.Value;
47+
Context.Done = true;
48+
}
49+
}
50+
}
51+
52+
[Serializable]
53+
public class MessageWithSecretData : IMessage
54+
{
55+
public WireEncryptedString Secret { get; set; }
56+
}
57+
58+
59+
public class MyEncryptionService : IEncryptionService
60+
{
61+
public EncryptedValue Encrypt(string value)
62+
{
63+
return new EncryptedValue
64+
{
65+
EncryptedBase64Value = new string(value.Reverse().ToArray())
66+
};
67+
}
68+
69+
public string Decrypt(EncryptedValue encryptedValue)
70+
{
71+
return new string(encryptedValue.EncryptedBase64Value.Reverse().ToArray());
72+
}
73+
}
74+
}
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
namespace NServiceBus.AcceptanceTests.Encryption
2+
{
3+
using System;
4+
using Config;
5+
using Config.ConfigurationSource;
6+
using EndpointTemplates;
7+
using AcceptanceTesting;
8+
using NUnit.Framework;
9+
using ScenarioDescriptors;
10+
11+
public class When_using_encryption_with_multikey : NServiceBusAcceptanceTest
12+
{
13+
[Test]
14+
public void Should_receive_decrypted_message()
15+
{
16+
Scenario.Define<Context>()
17+
.WithEndpoint<Sender>(b => b.Given((bus, context) => bus.Send(new MessageWithSecretData
18+
{
19+
Secret = "betcha can't guess my secret",
20+
})))
21+
.WithEndpoint<Receiver>()
22+
.Done(c => c.Done)
23+
.Repeat(r => r.For<AllSerializers>())
24+
.Should(c => Assert.AreEqual("betcha can't guess my secret", c.Secret))
25+
.Run();
26+
}
27+
28+
public class Context : ScenarioContext
29+
{
30+
public bool Done { get; set; }
31+
32+
public string Secret { get; set; }
33+
}
34+
35+
public class Sender : EndpointConfigurationBuilder
36+
{
37+
public Sender()
38+
{
39+
EndpointSetup<DefaultServer>(c => c.RijndaelEncryptionService())
40+
.AddMapping<MessageWithSecretData>(typeof(Receiver));
41+
}
42+
43+
public class Handler : IHandleMessages<MessageWithSecretData>
44+
{
45+
public Context Context { get; set; }
46+
47+
public void Handle(MessageWithSecretData message)
48+
{
49+
Context.Secret = message.Secret.Value;
50+
Context.Done = true;
51+
}
52+
}
53+
54+
public class ConfigureEncryption : IProvideConfiguration<RijndaelEncryptionServiceConfig>
55+
{
56+
public RijndaelEncryptionServiceConfig GetConfiguration()
57+
{
58+
return new RijndaelEncryptionServiceConfig
59+
{
60+
Key = "gdDbqRpqdRbTs3mhdZh9qCaDaxJXl+e6"
61+
};
62+
}
63+
}
64+
}
65+
66+
public class Receiver : EndpointConfigurationBuilder
67+
{
68+
public Receiver()
69+
{
70+
EndpointSetup<DefaultServer>(c => c.RijndaelEncryptionService());
71+
}
72+
73+
public class Handler : IHandleMessages<MessageWithSecretData>
74+
{
75+
public Context Context { get; set; }
76+
77+
public void Handle(MessageWithSecretData message)
78+
{
79+
Context.Secret = message.Secret.Value;
80+
Context.Done = true;
81+
}
82+
}
83+
84+
public class ConfigureEncryption : IProvideConfiguration<RijndaelEncryptionServiceConfig>
85+
{
86+
public RijndaelEncryptionServiceConfig GetConfiguration()
87+
{
88+
return new RijndaelEncryptionServiceConfig
89+
{
90+
Key = "adDbqRpqdRbTs3mhdZh9qCaDaxJXl+e6",
91+
ExpiredKeys = new RijndaelExpiredKeyCollection
92+
{
93+
new RijndaelExpiredKey
94+
{
95+
Key = "gdDbqRpqdRbTs3mhdZh9qCaDaxJXl+e6"
96+
}
97+
}
98+
};
99+
}
100+
}
101+
}
102+
103+
[Serializable]
104+
public class MessageWithSecretData : IMessage
105+
{
106+
public WireEncryptedString Secret { get; set; }
107+
}
108+
109+
}
110+
}

src/NServiceBus.AcceptanceTests/NServiceBus.AcceptanceTests.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@
117117
<Compile Include="BusStartStop\When_bus_start_and_stops_with_a_pending_message.cs" />
118118
<Compile Include="Configuration\When_a_config_override_is_found.cs" />
119119
<Compile Include="Encryption\When_using_encryption.cs" />
120+
<Compile Include="Encryption\When_using_encryption_with_custom_service.cs" />
121+
<Compile Include="Encryption\When_using_encryption_with_multikey.cs" />
120122
<Compile Include="EndpointTemplates\BusExtensions.cs" />
121123
<Compile Include="Gateway\When_sending_a_message_via_the_gateway.cs" />
122124
<Compile Include="Gateway\When_doing_request_response_between_sites.cs" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
namespace NServiceBus.Core.Tests.Encryption
2+
{
3+
using System;
4+
using System.Configuration;
5+
using System.IO;
6+
using System.Linq;
7+
using NServiceBus.Config;
8+
using NUnit.Framework;
9+
10+
[TestFixture]
11+
public class ConfigureRijndaelEncryptionServiceTests
12+
{
13+
14+
[Test]
15+
public void Can_read_from_xml()
16+
{
17+
var xml =
18+
@"<?xml version='1.0' encoding='utf-8' standalone='yes'?>
19+
<configuration>
20+
<configSections>
21+
<section
22+
name='RijndaelEncryptionServiceConfig'
23+
type='NServiceBus.Config.RijndaelEncryptionServiceConfig, NServiceBus.Core'/>
24+
</configSections>
25+
<RijndaelEncryptionServiceConfig Key='key1'>
26+
<ExpiredKeys>
27+
<add Key='key2' />
28+
<add Key='key3' />
29+
</ExpiredKeys>
30+
</RijndaelEncryptionServiceConfig>
31+
</configuration>";
32+
33+
var section = ReadSectionFromText<RijndaelEncryptionServiceConfig>(xml);
34+
var keys = section.ExpiredKeys.Cast<RijndaelExpiredKey>()
35+
.Select(x=>x.Key)
36+
.ToList();
37+
Assert.AreEqual("key1", section.Key);
38+
Assert.AreEqual(2,keys.Count);
39+
Assert.Contains("key2",keys);
40+
Assert.Contains("key3",keys);
41+
}
42+
43+
static T ReadSectionFromText<T>(string s) where T: ConfigurationSection
44+
{
45+
var xml = s.Replace("'", "\"");
46+
var tempPath = Path.GetTempFileName();
47+
try
48+
{
49+
File.WriteAllText(tempPath, xml);
50+
51+
var fileMap = new ExeConfigurationFileMap
52+
{
53+
ExeConfigFilename = tempPath
54+
};
55+
56+
var configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
57+
return (T) configuration.GetSection(typeof(T).Name);
58+
}
59+
finally
60+
{
61+
if (File.Exists(tempPath))
62+
{
63+
File.Delete(tempPath);
64+
}
65+
}
66+
}
67+
68+
[Test]
69+
public void Should_throw_for_whitespace_keys_in_config()
70+
{
71+
var config = new RijndaelEncryptionServiceConfig
72+
{
73+
ExpiredKeys = new RijndaelExpiredKeyCollection
74+
{
75+
new RijndaelExpiredKey
76+
{
77+
Key = " "
78+
}
79+
}
80+
};
81+
var exception = Assert.Throws<Exception>(() => ConfigureRijndaelEncryptionService.ExtractExpiredKeysFromConfigSection(config));
82+
Assert.AreEqual("The RijndaelEncryptionServiceConfig has a 'ExpiredKeys' property defined however some keys have no data.", exception.Message);
83+
}
84+
85+
[Test]
86+
public void Should_throw_for_null_keys_in_config()
87+
{
88+
var config = new RijndaelEncryptionServiceConfig
89+
{
90+
ExpiredKeys = new RijndaelExpiredKeyCollection
91+
{
92+
new RijndaelExpiredKey()
93+
}
94+
};
95+
var exception = Assert.Throws<Exception>(() => ConfigureRijndaelEncryptionService.ExtractExpiredKeysFromConfigSection(config));
96+
Assert.AreEqual("The RijndaelEncryptionServiceConfig has a 'ExpiredKeys' property defined however some keys have no data.", exception.Message);
97+
}
98+
99+
[Test]
100+
public void Should_for_duplicate_between_key_and_keys_in_config()
101+
{
102+
var config = new RijndaelEncryptionServiceConfig
103+
{
104+
Key = "a",
105+
ExpiredKeys = new RijndaelExpiredKeyCollection
106+
{
107+
new RijndaelExpiredKey
108+
{
109+
Key = "a"
110+
}
111+
}
112+
};
113+
var exception = Assert.Throws<Exception>(() => ConfigureRijndaelEncryptionService.ExtractExpiredKeysFromConfigSection(config));
114+
Assert.AreEqual("The RijndaelEncryptionServiceConfig has a 'Key' that is also defined inside the 'ExpiredKeys'.", exception.Message);
115+
}
116+
117+
[Test]
118+
public void Duplicates_should_be_skipped()
119+
{
120+
var config = new RijndaelEncryptionServiceConfig
121+
{
122+
ExpiredKeys = new RijndaelExpiredKeyCollection
123+
{
124+
new RijndaelExpiredKey
125+
{
126+
Key = "a"
127+
},
128+
new RijndaelExpiredKey
129+
{
130+
Key = "a"
131+
}
132+
}
133+
};
134+
var keys = ConfigureRijndaelEncryptionService.ExtractExpiredKeysFromConfigSection(config);
135+
136+
Assert.That(new[]{"a"}, Is.EquivalentTo(keys));
137+
}
138+
}
139+
140+
}

0 commit comments

Comments
 (0)