.NET: Fix SslConfigurationTest
[ignite.git] / modules / platforms / dotnet / Apache.Ignite.Core.Tests / Ssl / SslConfigurationTest.cs
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 namespace Apache.Ignite.Core.Tests.Ssl 
19 {
20     using System.Linq;
21     using Apache.Ignite.Core.Ssl;
22     using Apache.Ignite.Core.Common;
23     using NUnit.Framework;
24
25     /// <summary>
26     /// SSL configuration tests.
27     /// </summary>
28     [Category(TestUtils.CategoryIntensive)]
29     public class SslConfigurationTest
30     {
31         /** Test Password. */
32         private const string Password = "123456";
33
34         /** Key Store file. */
35         private const string KeyStoreFilePath = @"Config/KeyStore/server.jks";
36
37         /** Trust Store file. */
38         private const string TrustStoreFilePath = @"Config/KeyStore/trust.jks";
39
40         /// <summary>
41         /// Test teardown.
42         /// </summary>
43         [TearDown]
44         public void TearDown()
45         {
46             Ignition.StopAll(true);
47         }
48
49         /// <summary>
50         /// Returns SSL Context factory for tests.
51         /// </summary>
52         private static SslContextFactory GetSslContextFactory()
53         {
54             return new SslContextFactory(KeyStoreFilePath, Password, TrustStoreFilePath, Password);
55         }
56
57         /// <summary>
58         /// Tests Node Start with SslContextFactory
59         /// </summary>
60         [Test]
61         public void TestStart([Values(null, TrustStoreFilePath)] string trustStoreFilePath)
62         {
63             var factory = GetSslContextFactory();
64             factory.TrustStoreFilePath = trustStoreFilePath;
65
66             var cfg = new IgniteConfiguration(TestUtils.GetTestConfiguration())
67             {
68                 SslContextFactory = factory
69             };
70
71             var grid = Ignition.Start(cfg);
72
73             Assert.IsNotNull(grid);
74
75             Assert.AreEqual(1, grid.GetCluster().GetNodes().Count);
76
77             var cfgFactory = grid.GetConfiguration().SslContextFactory;
78
79             Assert.True(cfgFactory is SslContextFactory);
80             var sslContextFactory = (SslContextFactory)cfgFactory;
81             
82             Assert.AreEqual(Password, sslContextFactory.KeyStorePassword);
83             Assert.AreEqual(Password, sslContextFactory.TrustStorePassword);
84             Assert.AreEqual(KeyStoreFilePath, sslContextFactory.KeyStoreFilePath);
85             Assert.AreEqual(trustStoreFilePath, sslContextFactory.TrustStoreFilePath);
86         }
87
88         /// <summary>
89         /// Tests IgniteException when SSL configuration
90         /// </summary>
91         [Test]
92         public void TestConfigurationExceptions()
93         {
94             var cfg = new IgniteConfiguration(TestUtils.GetTestConfiguration())
95             {
96                 SslContextFactory = new SslContextFactory(@"WrongPath/server.jks", Password, 
97                                                           TrustStoreFilePath, Password)
98             };
99
100             var ex = Assert.Throws<IgniteException>(() => Ignition.Start(cfg));
101             Assert.True(ex.Message.StartsWith(@"Failed to initialize key store (key store file was not found): " + 
102                 @"[path=WrongPath/server.jks"));
103
104             cfg.SslContextFactory = new SslContextFactory(KeyStoreFilePath, Password,
105                                                           @"WrongPath/trust.jks", Password);
106
107             ex = Assert.Throws<IgniteException>(() => Ignition.Start(cfg));
108             Assert.True(ex.Message.StartsWith(@"Failed to initialize key store (key store file was not found): " + 
109                 @"[path=WrongPath/trust.jks"));
110
111             cfg.SslContextFactory = new SslContextFactory(KeyStoreFilePath, "654321",
112                                                           TrustStoreFilePath, Password);
113
114             ex = Assert.Throws<IgniteException>(() => Ignition.Start(cfg));
115             Assert.AreEqual(@"Failed to initialize key store (I/O error occurred): Config/KeyStore/server.jks",
116                 ex.Message);
117
118             cfg.SslContextFactory = new SslContextFactory(KeyStoreFilePath, Password,
119                                                           TrustStoreFilePath, "654321");
120
121             ex = Assert.Throws<IgniteException>(() => Ignition.Start(cfg));
122             Assert.AreEqual(@"Failed to initialize key store (I/O error occurred): Config/KeyStore/trust.jks",
123                 ex.Message);
124         }
125
126         /// <summary>
127         /// Tests Node Start with SslContextFactory from Spring xml.
128         /// </summary>
129         [Test]
130         public void TestStartWithConfigPath()
131         {
132             var cfg = new IgniteConfiguration(TestUtils.GetTestConfiguration())
133             {
134                 SpringConfigUrl = @"Config/ssl.xml",
135             };
136
137             var grid = Ignition.Start(cfg);
138
139             Assert.IsNotNull(grid);
140
141             Assert.AreEqual(1, grid.GetCluster().GetNodes().Count);
142
143             var factory = grid.GetConfiguration().SslContextFactory;
144
145             Assert.True(factory is SslContextFactory);
146             var sslContextFactory = (SslContextFactory)factory;
147
148             Assert.AreEqual(Password, sslContextFactory.KeyStorePassword);
149             Assert.AreEqual(Password, sslContextFactory.TrustStorePassword);
150             Assert.AreEqual(KeyStoreFilePath, sslContextFactory.KeyStoreFilePath);
151             Assert.AreEqual(TrustStoreFilePath, sslContextFactory.TrustStoreFilePath);
152         }
153
154         /// <summary>
155         /// Simple test with 2 SSL nodes.
156         /// </summary>
157         [Test]
158         public void TestTwoServers()
159         {
160             var cfg1 = new IgniteConfiguration(TestUtils.GetTestConfiguration())
161             {
162                 SpringConfigUrl = @"Config/ssl.xml"
163             };
164
165             var cfg2 = new IgniteConfiguration(TestUtils.GetTestConfiguration(name: "grid2"))
166             {
167                 SslContextFactory = GetSslContextFactory()
168             };
169
170             var grid1 = Ignition.Start(cfg1);
171
172             Assert.AreEqual("grid1", grid1.Name);
173             Assert.AreSame(grid1, Ignition.GetIgnite());
174             Assert.AreSame(grid1, Ignition.GetAll().Single());
175
176             var grid2 = Ignition.Start(cfg2);
177
178             Assert.AreEqual("grid2", grid2.Name);
179             Assert.Throws<IgniteException>(() => Ignition.GetIgnite());
180
181             Assert.AreSame(grid1, Ignition.GetIgnite("grid1"));
182             Assert.AreSame(grid1, Ignition.TryGetIgnite("grid1"));
183
184             Assert.AreSame(grid2, Ignition.GetIgnite("grid2"));
185             Assert.AreSame(grid2, Ignition.TryGetIgnite("grid2"));
186
187             Assert.AreEqual(new[] {grid1, grid2}, Ignition.GetAll().OrderBy(x => x.Name).ToArray());
188
189             Assert.AreEqual(2, grid1.GetCluster().GetNodes().Count);
190             Assert.AreEqual(2, grid2.GetCluster().GetNodes().Count);
191         }
192
193         /// <summary>
194         /// Simple test with 1 SSL node and 1 no-SSL node.
195         /// </summary>
196         [Test]
197         public void TestSslConfigurationMismatch()
198         {
199             var cfg = new IgniteConfiguration(TestUtils.GetTestConfiguration(name: "grid1"));
200
201             var sslCfg = new IgniteConfiguration(TestUtils.GetTestConfiguration(name: "grid2"))
202             {
203                 SslContextFactory = GetSslContextFactory()
204             };
205
206             Ignition.Start(cfg);
207
208             var ex = Assert.Throws<IgniteException>(() => Ignition.Start(sslCfg));
209             Assert.True(ex.Message.StartsWith(@"Unable to establish secure connection. " + 
210                                               @"Was remote cluster configured with SSL?"));
211
212             Ignition.StopAll(true);
213
214             Ignition.Start(sslCfg);
215
216             ex = Assert.Throws<IgniteException>(() => Ignition.Start(cfg));
217             Assert.True(ex.Message.StartsWith(@"Unable to establish secure connection. " +
218                                               @"Was remote cluster configured with SSL?"));
219         }
220
221         /// <summary>
222         /// Tests the client-server mode.
223         /// </summary>
224         [Test]
225         public void TestClientServer()
226         {
227             var factory = GetSslContextFactory();
228
229             var servCfg = new IgniteConfiguration(TestUtils.GetTestConfiguration(name: "grid1"))
230             {
231                 SslContextFactory = factory
232             };
233
234             var clientCfg = new IgniteConfiguration(TestUtils.GetTestConfiguration(name: "grid2"))
235             {
236                 SslContextFactory = factory,
237                 ClientMode = true
238             };
239
240             using (var serv = Ignition.Start(servCfg)) // start server-mode ignite first
241             {
242                 Assert.IsFalse(serv.GetCluster().GetLocalNode().IsClient);
243
244                 using (var grid = Ignition.Start(clientCfg))
245                 {
246                     Assert.IsTrue(grid.GetCluster().GetLocalNode().IsClient);
247
248                     Assert.AreEqual(2, grid.GetCluster().GetNodes().Count);
249                     Assert.AreEqual(2, serv.GetCluster().GetNodes().Count);
250
251                     Assert.AreEqual(1, grid.GetCluster().ForServers().GetNodes().Count);
252                     Assert.AreEqual(1, serv.GetCluster().ForServers().GetNodes().Count);
253                 }
254             }
255         }
256     }
257 }