Added HTTPS and CADI/AAF Support for PDP-X
[policy/xacml-pdp.git] / main / src / test / java / org / onap / policy / pdpx / main / rest / TestXacmlPdpRestServer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pdpx.main.rest;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26
27 import java.io.IOException;
28 import java.lang.reflect.Constructor;
29 import java.lang.reflect.Modifier;
30 import java.security.SecureRandom;
31 import java.security.cert.X509Certificate;
32 import java.util.Properties;
33 import javax.net.ssl.SSLContext;
34 import javax.net.ssl.TrustManager;
35 import javax.net.ssl.X509TrustManager;
36 import javax.ws.rs.client.Client;
37 import javax.ws.rs.client.ClientBuilder;
38 import javax.ws.rs.client.Invocation;
39 import javax.ws.rs.client.WebTarget;
40 import javax.ws.rs.core.MediaType;
41 import org.glassfish.jersey.client.ClientConfig;
42 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
43 import org.junit.After;
44 import org.junit.Test;
45 import org.onap.policy.common.endpoints.report.HealthCheckReport;
46 import org.onap.policy.common.utils.network.NetworkUtil;
47 import org.onap.policy.pdpx.main.PolicyXacmlPdpException;
48 import org.onap.policy.pdpx.main.parameters.CommonTestData;
49 import org.onap.policy.pdpx.main.parameters.RestServerParameters;
50 import org.onap.policy.pdpx.main.rest.model.StatisticsReport;
51 import org.onap.policy.pdpx.main.startstop.Main;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55 /**
56  * Class to perform unit test of {@link XacmlPdpRestServer}.
57  *
58  */
59 public class TestXacmlPdpRestServer {
60
61     private static final Logger LOGGER = LoggerFactory.getLogger(TestXacmlPdpRestServer.class);
62     private static final String NOT_ALIVE = "not alive";
63     private static final String ALIVE = "alive";
64     private static final String SELF = "self";
65     private static final String NAME = "Policy Xacml PDP";
66     private static final String HEALTHCHECK_ENDPOINT = "healthcheck";
67     private static final String STATISTICS_ENDPOINT = "statistics";
68     private static String KEYSTORE = System.getProperty("user.dir") + "/src/test/resources/ssl/policy-keystore";
69     private Main main;
70     private XacmlPdpRestServer restServer;
71
72     /**
73      * Method for cleanup after each test.
74      */
75     @After
76     public void teardown() {
77         try {
78             if (NetworkUtil.isTcpPortOpen("localhost", 6969, 1, 1000L)) {
79                 if (main != null) {
80                     stopXacmlPdpService(main);
81                 }
82
83                 if (restServer != null) {
84                     restServer.stop();
85                 }
86             }
87         } catch (IOException | PolicyXacmlPdpException e) {
88             LOGGER.error("teardown failed", e);
89         } catch (InterruptedException ie) {
90             Thread.interrupted();
91             LOGGER.error("teardown failed", ie);
92         }
93     }
94
95     @Test
96     public void testHealthCheckSuccess() throws IOException, InterruptedException {
97         main = startXacmlPdpService(true);
98         final Invocation.Builder invocationBuilder = sendHttpRequest(HEALTHCHECK_ENDPOINT);
99         final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class);
100         validateHealthCheckReport(NAME, SELF, true, 200, ALIVE, report);
101     }
102
103     @Test
104     public void testHealthCheckFailure() throws InterruptedException, IOException {
105         final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false);
106         restServerParams.setName(CommonTestData.PDPX_GROUP_NAME);
107         restServer = new XacmlPdpRestServer(restServerParams);
108         restServer.start();
109         final Invocation.Builder invocationBuilder = sendHttpRequest(HEALTHCHECK_ENDPOINT);
110         final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class);
111         validateHealthCheckReport(NAME, SELF, false, 500, NOT_ALIVE, report);
112         assertTrue(restServer.isAlive());
113         assertTrue(restServer.toString().startsWith("XacmlPdpRestServer [servers="));
114     }
115
116     @Test
117     public void testHttpsHealthCheckSuccess() throws Exception {
118         main = startXacmlPdpService(false);
119         final Invocation.Builder invocationBuilder = sendHttpsRequest(HEALTHCHECK_ENDPOINT);
120         final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class);
121         validateHealthCheckReport(NAME, SELF, true, 200, ALIVE, report);
122     }
123
124     @Test
125     public void testStatistics_200() throws IOException, InterruptedException {
126         main = startXacmlPdpService(true);
127         Invocation.Builder invocationBuilder = sendHttpRequest(STATISTICS_ENDPOINT);
128         StatisticsReport report = invocationBuilder.get(StatisticsReport.class);
129         validateStatisticsReport(report, 0, 200);
130         updateXacmlPdpStatistics();
131         invocationBuilder = sendHttpRequest(STATISTICS_ENDPOINT);
132         report = invocationBuilder.get(StatisticsReport.class);
133         validateStatisticsReport(report, 1, 200);
134         XacmlPdpStatisticsManager.resetAllStatistics();
135     }
136
137     @Test
138     public void testStatistics_500() throws IOException, InterruptedException {
139         final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false);
140         restServerParams.setName(CommonTestData.PDPX_GROUP_NAME);
141         restServer = new XacmlPdpRestServer(restServerParams);
142         restServer.start();
143         final Invocation.Builder invocationBuilder = sendHttpRequest(STATISTICS_ENDPOINT);
144         final StatisticsReport report = invocationBuilder.get(StatisticsReport.class);
145         validateStatisticsReport(report, 0, 500);
146         XacmlPdpStatisticsManager.resetAllStatistics();
147     }
148
149     @Test
150     public void testHttpsStatistic() throws Exception {
151         main = startXacmlPdpService(false);
152         final Invocation.Builder invocationBuilder = sendHttpsRequest(STATISTICS_ENDPOINT);
153         final StatisticsReport report = invocationBuilder.get(StatisticsReport.class);
154         validateStatisticsReport(report, 0, 200);
155     }
156
157     @Test
158     public void testStatisticsConstructorIsPrivate() {
159         try {
160             final Constructor<XacmlPdpStatisticsManager> constructor =
161                     XacmlPdpStatisticsManager.class.getDeclaredConstructor();
162             assertTrue(Modifier.isPrivate(constructor.getModifiers()));
163             constructor.setAccessible(true);
164             constructor.newInstance();
165             fail("Expected an InstantiationException to be thrown");
166         } catch (final Exception exp) {
167             assertTrue(exp.getCause().toString().contains("Instantiation of the class is not allowed"));
168         }
169     }
170
171     private Main startXacmlPdpService(final boolean http) {
172         final String[] xacmlPdpConfigParameters = new String[2];
173         if (http) {
174             xacmlPdpConfigParameters[0] = "-c";
175             xacmlPdpConfigParameters[1] = "parameters/XacmlPdpConfigParameters.json";
176         } else {
177             final Properties systemProps = System.getProperties();
178             systemProps.put("javax.net.ssl.keyStore", KEYSTORE);
179             systemProps.put("javax.net.ssl.keyStorePassword", "Pol1cy_0nap");
180             System.setProperties(systemProps);
181             xacmlPdpConfigParameters[0] = "-c";
182             xacmlPdpConfigParameters[1] = "parameters/XacmlPdpConfigParameters_Https.json";
183         }
184         return new Main(xacmlPdpConfigParameters);
185     }
186
187     private void stopXacmlPdpService(final Main main) throws PolicyXacmlPdpException {
188         main.shutdown();
189     }
190
191     private Invocation.Builder sendHttpRequest(final String endpoint) throws IOException, InterruptedException {
192         final ClientConfig clientConfig = new ClientConfig();
193
194         final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
195         clientConfig.register(feature);
196
197         final Client client = ClientBuilder.newClient(clientConfig);
198         final WebTarget webTarget = client.target("http://localhost:6969/policy/pdpx/v1/" + endpoint);
199
200         final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
201
202         if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) {
203             throw new IllegalStateException("cannot connect to port 6969");
204         }
205         return invocationBuilder;
206     }
207
208     private Invocation.Builder sendHttpsRequest(final String endpoint) throws Exception {
209
210         final TrustManager[] noopTrustManager = new TrustManager[] {new X509TrustManager() {
211
212             @Override
213             public X509Certificate[] getAcceptedIssuers() {
214                 return new X509Certificate[0];
215             }
216
217             @Override
218             public void checkClientTrusted(final java.security.cert.X509Certificate[] certs, final String authType) {}
219
220             @Override
221             public void checkServerTrusted(final java.security.cert.X509Certificate[] certs, final String authType) {}
222         }};
223
224         final SSLContext sc = SSLContext.getInstance("TLSv1.2");
225         sc.init(null, noopTrustManager, new SecureRandom());
226         final ClientBuilder clientBuilder =
227                 ClientBuilder.newBuilder().sslContext(sc).hostnameVerifier((host, session) -> true);
228         final Client client = clientBuilder.build();
229         final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
230         client.register(feature);
231
232         final WebTarget webTarget = client.target("https://localhost:6969/policy/pdpx/v1/" + endpoint);
233
234         final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
235
236         if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) {
237             throw new IllegalStateException("cannot connect to port 6969");
238         }
239         return invocationBuilder;
240     }
241
242     private void updateXacmlPdpStatistics() {
243         XacmlPdpStatisticsManager.updateTotalPoliciesCount();
244         XacmlPdpStatisticsManager.updatePermitDecisionsCount();
245         XacmlPdpStatisticsManager.updateDenyDecisionsCount();
246         XacmlPdpStatisticsManager.updateIndeterminantDecisionsCount();
247         XacmlPdpStatisticsManager.updateNotApplicableDecisionsCount();
248     }
249
250     private void validateStatisticsReport(final StatisticsReport report, final int count, final int code) {
251         assertEquals(code, report.getCode());
252         assertEquals(count, report.getTotalPoliciesCount());
253         assertEquals(count, report.getPermitDecisionsCount());
254         assertEquals(count, report.getDenyDecisionsCount());
255         assertEquals(count, report.getIndeterminantDecisionsCount());
256         assertEquals(count, report.getNotApplicableDecisionsCount());
257     }
258
259     private void validateHealthCheckReport(final String name, final String url, final boolean healthy, final int code,
260             final String message, final HealthCheckReport report) {
261         assertEquals(name, report.getName());
262         assertEquals(url, report.getUrl());
263         assertEquals(healthy, report.isHealthy());
264         assertEquals(code, report.getCode());
265         assertEquals(message, report.getMessage());
266     }
267 }