Incorporate the ECOMP SDC Artefact Generator code
[aai/babel.git] / src / test / java / org / onap / aai / babel / MicroServiceAuthTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 European Software Marketing Ltd.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.babel;
22
23 import static org.hamcrest.CoreMatchers.equalTo;
24 import static org.hamcrest.CoreMatchers.is;
25 import static org.junit.Assert.assertThat;
26
27 import java.io.File;
28 import java.io.FileWriter;
29 import java.io.IOException;
30 import org.json.JSONArray;
31 import org.json.JSONException;
32 import org.json.JSONObject;
33 import org.junit.Test;
34 import org.onap.aai.auth.AAIAuthException;
35 import org.onap.aai.auth.AAIMicroServiceAuth;
36 import org.onap.aai.auth.AAIMicroServiceAuthCore;
37 import org.onap.aai.babel.config.BabelAuthConfig;
38 import org.springframework.mock.web.MockHttpServletRequest;
39
40 /**
41  * Tests @{link AAIMicroServiceAuth}
42  */
43
44 public class MicroServiceAuthTest {
45
46     private static final String VALID_ADMIN_USER = "cn=common-name, ou=org-unit, o=org, l=location, st=state, c=us";
47     private static final String authPolicyFile = "auth_policy.json";
48
49     static {
50         System.setProperty("CONFIG_HOME", System.getProperty("user.dir") + File.separator + "src/test/resources");
51     }
52
53     /**
54      * Temporarily invalidate the default policy file and then try to initialise the authorisation class using the name
55      * of a policy file that does not exist.
56      *
57      * @throws AAIAuthException
58      * @throws IOException
59      */
60     @Test(expected = AAIAuthException.class)
61     public void missingPolicyFile() throws AAIAuthException, IOException {
62         String defaultFile = AAIMicroServiceAuthCore.getDefaultAuthFileName();
63         try {
64             AAIMicroServiceAuthCore.setDefaultAuthFileName("invalid.default.file");
65             BabelAuthConfig babelServiceAuthConfig = new BabelAuthConfig();
66             babelServiceAuthConfig.setAuthPolicyFile("invalid.file.name");
67             new AAIMicroServiceAuth(babelServiceAuthConfig);
68         } finally {
69             AAIMicroServiceAuthCore.setDefaultAuthFileName(defaultFile);
70         }
71     }
72
73     /**
74      * Test loading of a temporary file created with the specified roles
75      *
76      * @throws AAIAuthException
77      * @throws IOException
78      * @throws JSONException
79      */
80     @Test
81     public void createLocalAuthFile() throws AAIAuthException, IOException, JSONException {
82         JSONObject roles = createRoleObject("role", createUserObject("user"), createFunctionObject("func"));
83         AAIMicroServiceAuth auth = createAuthService(roles);
84         assertThat(auth.authorize("nosuchuser", "method:func"), is(false));
85         assertThat(auth.authorize("user", "method:func"), is(true));
86     }
87
88     /**
89      * Test that the default policy file is loaded when a non-existent file is passed to the authorisation clas.
90      *
91      * @throws AAIAuthException
92      */
93     @Test
94     public void createAuthFromDefaultFile() throws AAIAuthException {
95         BabelAuthConfig babelServiceAuthConfig = new BabelAuthConfig();
96         babelServiceAuthConfig.setAuthPolicyFile("non-existent-file");
97         AAIMicroServiceAuth auth = new AAIMicroServiceAuth(babelServiceAuthConfig);
98         // The default policy will have been loaded
99         assertAdminUserAuthorisation(auth, VALID_ADMIN_USER);
100     }
101
102     /**
103      * Test loading of the policy file relative to CONFIG_HOME
104      *
105      * @throws AAIAuthException
106      */
107     @Test
108     public void createAuth() throws AAIAuthException {
109         AAIMicroServiceAuth auth = createStandardAuth();
110         assertAdminUserAuthorisation(auth, VALID_ADMIN_USER);
111     }
112
113     @Test
114     public void testAuthUser() throws AAIAuthException {
115         AAIMicroServiceAuth auth = createStandardAuth();
116         assertThat(auth.authenticate(VALID_ADMIN_USER, "GET:actions"), is(equalTo("OK")));
117         assertThat(auth.authenticate(VALID_ADMIN_USER, "WRONG:action"), is(equalTo("AAI_9101")));
118     }
119
120
121
122     @Test
123     public void testValidateRequest() throws AAIAuthException {
124         AAIMicroServiceAuth auth = createStandardAuth();
125         assertThat(auth.validateRequest(null, new MockHttpServletRequest(), null, "app/v1/babel"), is(false));
126     }
127
128     private AAIMicroServiceAuth createStandardAuth() throws AAIAuthException {
129         BabelAuthConfig babelServiceAuthConfig = new BabelAuthConfig();
130         babelServiceAuthConfig.setAuthPolicyFile(authPolicyFile);
131         return new AAIMicroServiceAuth(babelServiceAuthConfig);
132     }
133
134     /**
135      * @param rolesJson
136      * @return
137      * @throws IOException
138      * @throws AAIAuthException
139      */
140     private AAIMicroServiceAuth createAuthService(JSONObject roles) throws IOException, AAIAuthException {
141         BabelAuthConfig babelAuthConfig = new BabelAuthConfig();
142         File file = File.createTempFile("auth-policy", "json");
143         file.deleteOnExit();
144         FileWriter fileWriter = new FileWriter(file);
145         fileWriter.write(roles.toString());
146         fileWriter.flush();
147         fileWriter.close();
148
149         babelAuthConfig.setAuthPolicyFile(file.getAbsolutePath());
150         return new AAIMicroServiceAuth(babelAuthConfig);
151     }
152
153     /**
154      * Assert authorisation results for an admin user based on the test policy file
155      *
156      * @param auth
157      * @param adminUser
158      * @throws AAIAuthException
159      */
160     private void assertAdminUserAuthorisation(AAIMicroServiceAuth auth, String adminUser) throws AAIAuthException {
161         assertThat(auth.authorize(adminUser, "GET:actions"), is(true));
162         assertThat(auth.authorize(adminUser, "POST:actions"), is(true));
163         assertThat(auth.authorize(adminUser, "PUT:actions"), is(true));
164         assertThat(auth.authorize(adminUser, "DELETE:actions"), is(true));
165     }
166
167     private JSONArray createFunctionObject(String functionName) throws JSONException {
168         JSONArray functionsArray = new JSONArray();
169         JSONObject func = new JSONObject();
170         func.put("name", functionName);
171         func.put("methods", createMethodObject("method"));
172         functionsArray.put(func);
173         return functionsArray;
174     }
175
176     private JSONArray createMethodObject(String methodName) throws JSONException {
177         JSONArray methodsArray = new JSONArray();
178         JSONObject method = new JSONObject();
179         method.put("name", methodName);
180         methodsArray.put(method);
181         return methodsArray;
182     }
183
184     private JSONArray createUserObject(String username) throws JSONException {
185         JSONArray usersArray = new JSONArray();
186         JSONObject user = new JSONObject();
187         user.put("username", username);
188         usersArray.put(user);
189         return usersArray;
190     }
191
192     private JSONObject createRoleObject(String roleName, JSONArray usersArray, JSONArray functionsArray)
193             throws JSONException {
194         JSONObject roles = new JSONObject();
195
196         JSONObject role = new JSONObject();
197         role.put("name", roleName);
198         role.put("functions", functionsArray);
199         role.put("users", usersArray);
200
201         JSONArray rolesArray = new JSONArray();
202         rolesArray.put(role);
203         roles.put("roles", rolesArray);
204
205         return roles;
206     }
207
208 }