1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.openehealth.ipf.commons.audit;
18
19
20 import io.vertx.core.AsyncResult;
21 import io.vertx.core.Handler;
22 import io.vertx.core.Verticle;
23 import io.vertx.core.Vertx;
24 import io.vertx.ext.unit.Async;
25 import io.vertx.ext.unit.TestContext;
26 import io.vertx.ext.unit.junit.VertxUnitRunner;
27 import org.junit.*;
28 import org.junit.runner.RunWith;
29 import org.openehealth.ipf.commons.audit.codes.EventOutcomeIndicator;
30 import org.openehealth.ipf.commons.audit.event.ApplicationActivityBuilder;
31 import org.openehealth.ipf.commons.audit.protocol.VertxTLSSyslogSenderImpl;
32 import org.openehealth.ipf.commons.audit.protocol.VertxUDPSyslogSenderImpl;
33 import org.openehealth.ipf.commons.audit.utils.AuditUtils;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 import java.io.IOException;
38 import java.net.ServerSocket;
39 import java.util.Properties;
40 import java.util.concurrent.CountDownLatch;
41 import java.util.concurrent.TimeUnit;
42
43 import static org.openehealth.ipf.commons.audit.SyslogServerFactory.*;
44 import static org.openehealth.ipf.commons.audit.protocol.AuditTransmissionProtocol.*;
45
46
47
48
49
50 @Ignore
51 @RunWith(VertxUnitRunner.class)
52 public class AuditorIntegrationTest {
53
54 private Logger LOG = LoggerFactory.getLogger(AuditorIntegrationTest.class);
55
56 private static final String CLIENT_KEY_STORE =
57 AuditorIntegrationTest.class.getResource("/security/client.keystore").getPath();
58 private static final String CLIENT_KEY_STORE_PASS = "initinit";
59
60 private static final String EXPIRED_CLIENT_KEY_STORE =
61 AuditorIntegrationTest.class.getResource("/security/expired.keystore").getPath();
62
63 private static final String SERVER_KEY_STORE =
64 AuditorIntegrationTest.class.getResource("/security/server.keystore").getPath();
65 private static final String SERVER_KEY_STORE_PASS = "initinit";
66
67 private static final String TRUST_STORE =
68 AuditorIntegrationTest.class.getResource("/security/ca.keystore").getPath();
69 private static final String TRUST_STORE_PASS = "initinit";
70
71 private static final String LOCALHOST = "localhost";
72 private static final long WAIT_TIME = 10000L;
73
74 private Vertx vertx;
75 private int port;
76 private Properties p;
77 private String deploymentId;
78
79 private DefaultAuditContext auditContext;
80
81 @Before
82 public void setup(TestContext context) throws Exception {
83 p = System.getProperties();
84 port = freePort();
85 this.auditContext = new DefaultAuditContext();
86 auditContext.setAuditRepositoryPort(port);
87 auditContext.setAuditRepositoryHost(LOCALHOST);
88 auditContext.setAuditRepositoryTransport("TLS");
89 vertx = Vertx.vertx();
90 }
91
92 @After
93 public void tearDown(TestContext context) {
94 System.setProperties(p);
95 deploymentId = null;
96 vertx.close(undeployVertx(context));
97 }
98
99 @Test
100 public void testUDP(TestContext testContext) throws Exception {
101 auditContext.setAuditRepositoryTransport("UDP");
102 int count = 10;
103 Async async = testContext.async(count + 1);
104 deploy(testContext, createUDPServer(LOCALHOST, port, async));
105 while (async.count() > count) {
106 Thread.sleep(10);
107 }
108 for (int i = 0; i < count; i++) sendAudit();
109 async.awaitSuccess(WAIT_TIME);
110 }
111
112 @Test
113 public void testUDPVertx(TestContext testContext) throws Exception {
114 auditContext.setAuditTransmissionProtocol(new VertxUDPSyslogSenderImpl(vertx));
115 int count = 10;
116 Async async = testContext.async(count);
117 deploy(testContext, createUDPServer(LOCALHOST, port, async));
118 for (int i = 0; i < count; i++) sendAudit();
119 async.awaitSuccess(WAIT_TIME);
120 }
121
122 @Test
123 public void testTwoWayVertxTLS(TestContext testContext) throws Exception {
124 initTLSSystemProperties(null);
125 auditContext.setAuditTransmissionProtocol(new VertxTLSSyslogSenderImpl(vertx));
126 int count = 10;
127 Async async = testContext.async(count);
128 deploy(testContext, createTCPServerTwoWayTLS(port,
129 TRUST_STORE,
130 TRUST_STORE_PASS,
131 SERVER_KEY_STORE,
132 SERVER_KEY_STORE_PASS,
133 async));
134 for (int i = 0; i < count; i++) sendAudit();
135 async.awaitSuccess(WAIT_TIME);
136 }
137
138
139 @Test
140 public void testOneWayTLS(TestContext testContext) throws Exception {
141 initTLSSystemProperties(null);
142 Async async = testContext.async();
143 deploy(testContext, createTCPServerOneWayTLS(port,
144 SERVER_KEY_STORE,
145 SERVER_KEY_STORE_PASS,
146 async));
147 sendAudit();
148 async.awaitSuccess(WAIT_TIME);
149 }
150
151 @Test
152 public void testTwoWayTLS(TestContext testContext) throws Exception {
153 initTLSSystemProperties(null);
154 int count = 10;
155 Async async = testContext.async(count);
156 deploy(testContext, createTCPServerTwoWayTLS(port,
157 TRUST_STORE,
158 TRUST_STORE_PASS,
159 SERVER_KEY_STORE,
160 SERVER_KEY_STORE_PASS,
161 async));
162 for (int i = 0; i < count; i++) sendAudit();
163 async.awaitSuccess(WAIT_TIME);
164 }
165
166
167 @Test
168 public void testTwoWayTLSInterrupted(TestContext testContext) throws Exception {
169 initTLSSystemProperties(null);
170 int count = 5;
171 Async async = testContext.async(count * 2);
172 Verticle tcpServer = createTCPServerTwoWayTLS(port,
173 TRUST_STORE,
174 TRUST_STORE_PASS,
175 SERVER_KEY_STORE,
176 SERVER_KEY_STORE_PASS,
177 async);
178 deploy(testContext, tcpServer);
179 for (int i = 0; i < count; i++) sendAudit();
180 undeploy(testContext);
181 deploy(testContext, tcpServer);
182 for (int i = 0; i < count; i++) sendAudit();
183 async.awaitSuccess(WAIT_TIME);
184 }
185
186 @Ignore
187 public void testTwoWayVertxTLSInterrupted(TestContext testContext) throws Exception {
188 initTLSSystemProperties(null);
189 auditContext.setAuditTransmissionProtocol(new VertxTLSSyslogSenderImpl(vertx));
190 int count = 5;
191 Async async = testContext.async(count * 2);
192 Verticle tcpServer = createTCPServerTwoWayTLS(port,
193 TRUST_STORE,
194 TRUST_STORE_PASS,
195 SERVER_KEY_STORE,
196 SERVER_KEY_STORE_PASS,
197 async);
198 deploy(testContext, tcpServer);
199 for (int i = 0; i < count; i++) sendAudit();
200 undeploy(testContext);
201 deploy(testContext, tcpServer);
202 for (int i = 0; i < count; i++) sendAudit();
203 async.awaitSuccess(WAIT_TIME);
204 }
205
206 @Ignore
207 public void testTLSTwoWayTLSWrongClientCert(TestContext testContext) throws Exception {
208 initTLSSystemProperties(EXPIRED_CLIENT_KEY_STORE);
209 Async async = testContext.async();
210 deploy(testContext, createTCPServerTwoWayTLS(port,
211 TRUST_STORE,
212 TRUST_STORE_PASS,
213 SERVER_KEY_STORE,
214 SERVER_KEY_STORE_PASS,
215 async));
216 sendAudit();
217 try {
218 async.awaitSuccess(WAIT_TIME);
219 Assert.fail();
220 } catch (Exception e) {
221 LOG.info("Exception thrown :" + e.getMessage());
222 }
223 if (async.isSucceeded()) {
224 Assert.fail();
225 }
226 async.complete();
227 }
228
229 private void deploy(TestContext testContext, Verticle verticle) throws InterruptedException {
230 CountDownLatch latch = new CountDownLatch(1);
231 vertx.deployVerticle(verticle, deployHandler(testContext, latch));
232 latch.await(2000, TimeUnit.MILLISECONDS);
233 }
234
235 private Handler<AsyncResult<String>> deployHandler(TestContext testContext, CountDownLatch latch) {
236 return testContext.asyncAssertSuccess(id -> {
237 LOG.info("Server deployed with ID {}", id);
238 deploymentId = id;
239 latch.countDown();
240 });
241 }
242
243 private void undeploy(TestContext testContext) throws InterruptedException {
244 CountDownLatch latch = new CountDownLatch(1);
245 vertx.undeploy(deploymentId, undeployHandler(testContext, latch));
246 latch.await(2000, TimeUnit.MILLISECONDS);
247 }
248
249 private Handler<AsyncResult<Void>> undeployHandler(TestContext testContext, CountDownLatch latch) {
250 return testContext.asyncAssertSuccess(id -> {
251 LOG.info("Server undeployed with ID {}", deploymentId);
252 deploymentId = null;
253 latch.countDown();
254 });
255 }
256
257 private Handler<AsyncResult<Void>> undeployVertx(TestContext testContext) {
258 return testContext.asyncAssertSuccess(event -> {
259 LOG.info("Vertx stopped");
260 });
261 }
262
263 private void sendAudit() {
264 LOG.debug("Sending audit record");
265 auditContext.audit(
266 new ApplicationActivityBuilder.ApplicationStart(EventOutcomeIndicator.Success)
267 .setAuditSource(auditContext)
268 .setApplicationParticipant(
269 "appName",
270 null,
271 null,
272 AuditUtils.getLocalHostName())
273 .addApplicationStarterParticipant(System.getProperty("user.name"))
274 .getMessages());
275 }
276
277 private void initTLSSystemProperties(String clientKeyStore) {
278 System.setProperty(JAVAX_NET_SSL_KEYSTORE_PASSWORD, CLIENT_KEY_STORE_PASS);
279 System.setProperty(JAVAX_NET_SSL_KEYSTORE, clientKeyStore != null ? clientKeyStore : CLIENT_KEY_STORE);
280 System.setProperty(JAVAX_NET_SSL_TRUSTSTORE_PASSWORD, TRUST_STORE_PASS);
281 System.setProperty(JAVAX_NET_SSL_TRUSTSTORE, TRUST_STORE);
282 System.setProperty(JDK_TLS_CLIENT_PROTOCOLS, "TLSv1.2");
283 }
284
285 private int freePort() {
286 ServerSocket serverSocket = null;
287 try {
288 serverSocket = new ServerSocket(0);
289 return serverSocket.getLocalPort();
290 } catch (Exception e) {
291 LOG.error(e.getMessage());
292 return -1;
293 } finally {
294 if (serverSocket != null) {
295 try {
296 serverSocket.close();
297 } catch (IOException ignored) {
298 }
299 }
300 }
301 }
302
303 }