2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.babel;
23 import static org.hamcrest.CoreMatchers.is;
24 import static org.junit.Assert.assertThat;
27 import java.io.FileWriter;
28 import java.io.IOException;
29 import org.json.JSONArray;
30 import org.json.JSONException;
31 import org.json.JSONObject;
32 import org.junit.Test;
33 import org.onap.aai.auth.AAIAuthException;
34 import org.onap.aai.auth.AAIMicroServiceAuth;
35 import org.onap.aai.auth.AAIMicroServiceAuthCore;
36 import org.onap.aai.babel.config.BabelAuthConfig;
37 import org.springframework.mock.web.MockHttpServletRequest;
40 * Tests @{link AAIMicroServiceAuth}
43 public class MicroServiceAuthTest {
45 private static final String VALID_ADMIN_USER = "cn=common-name, ou=org-unit, o=org, l=location, st=state, c=us";
46 private static final String authPolicyFile = "auth_policy.json";
49 System.setProperty("CONFIG_HOME", System.getProperty("user.dir") + File.separator + "src/test/resources");
53 * Temporarily invalidate the default policy file and then try to initialise the authorisation class using the name
54 * of a policy file that does not exist.
56 * @throws AAIAuthException
59 @Test(expected = AAIAuthException.class)
60 public void missingPolicyFile() throws AAIAuthException, IOException {
61 String defaultFile = AAIMicroServiceAuthCore.getDefaultAuthFileName();
63 AAIMicroServiceAuthCore.setDefaultAuthFileName("invalid.default.file");
64 BabelAuthConfig babelServiceAuthConfig = new BabelAuthConfig();
65 babelServiceAuthConfig.setAuthPolicyFile("invalid.file.name");
66 new AAIMicroServiceAuth(babelServiceAuthConfig);
68 AAIMicroServiceAuthCore.setDefaultAuthFileName(defaultFile);
73 * Test loading of a temporary file created with the specified roles
75 * @throws AAIAuthException
77 * @throws JSONException
80 public void createLocalAuthFile() throws AAIAuthException, IOException, JSONException {
81 JSONObject roles = createRoleObject("role", createUserObject("user"), createFunctionObject("func"));
82 createAuthService(roles);
83 assertThat(AAIMicroServiceAuthCore.authorize("nosuchuser", "method:func"), is(false));
84 assertThat(AAIMicroServiceAuthCore.authorize("user", "method:func"), is(true));
88 * Test that the default policy file is loaded when a non-existent file is passed to the authorisation clas.
90 * @throws AAIAuthException
93 public void createAuthFromDefaultFile() throws AAIAuthException {
94 BabelAuthConfig babelServiceAuthConfig = new BabelAuthConfig();
95 babelServiceAuthConfig.setAuthPolicyFile("non-existent-file");
96 AAIMicroServiceAuth auth = new AAIMicroServiceAuth(babelServiceAuthConfig);
97 // The default policy will have been loaded
98 assertAdminUserAuthorisation(auth, VALID_ADMIN_USER);
102 * Test loading of the policy file relative to CONFIG_HOME
104 * @throws AAIAuthException
107 public void createAuth() throws AAIAuthException {
108 AAIMicroServiceAuth auth = createStandardAuth();
109 assertAdminUserAuthorisation(auth, VALID_ADMIN_USER);
113 public void testAuthUser() throws AAIAuthException {
114 createStandardAuth();
115 assertThat(AAIMicroServiceAuthCore.authorize(VALID_ADMIN_USER, "GET:actions"), is(true));
116 assertThat(AAIMicroServiceAuthCore.authorize(VALID_ADMIN_USER, "WRONG:action"), is(false));
122 public void testValidateRequest() throws AAIAuthException {
123 AAIMicroServiceAuth auth = createStandardAuth();
124 assertThat(auth.validateRequest(null, new MockHttpServletRequest(), null, "app/v1/babel"), is(false));
127 private AAIMicroServiceAuth createStandardAuth() throws AAIAuthException {
128 BabelAuthConfig babelServiceAuthConfig = new BabelAuthConfig();
129 babelServiceAuthConfig.setAuthPolicyFile(authPolicyFile);
130 return new AAIMicroServiceAuth(babelServiceAuthConfig);
136 * @throws IOException
137 * @throws AAIAuthException
139 private AAIMicroServiceAuth createAuthService(JSONObject roles) throws IOException, AAIAuthException {
140 BabelAuthConfig babelAuthConfig = new BabelAuthConfig();
141 File file = File.createTempFile("auth-policy", "json");
143 FileWriter fileWriter = new FileWriter(file);
144 fileWriter.write(roles.toString());
148 babelAuthConfig.setAuthPolicyFile(file.getAbsolutePath());
149 return new AAIMicroServiceAuth(babelAuthConfig);
153 * Assert authorisation results for an admin user based on the test policy file
157 * @throws AAIAuthException
159 private void assertAdminUserAuthorisation(AAIMicroServiceAuth auth, String adminUser) throws AAIAuthException {
160 assertThat(AAIMicroServiceAuthCore.authorize(adminUser, "GET:actions"), is(true));
161 assertThat(AAIMicroServiceAuthCore.authorize(adminUser, "POST:actions"), is(true));
162 assertThat(AAIMicroServiceAuthCore.authorize(adminUser, "PUT:actions"), is(true));
163 assertThat(AAIMicroServiceAuthCore.authorize(adminUser, "DELETE:actions"), is(true));
166 private JSONArray createFunctionObject(String functionName) throws JSONException {
167 JSONArray functionsArray = new JSONArray();
168 JSONObject func = new JSONObject();
169 func.put("name", functionName);
170 func.put("methods", createMethodObject("method"));
171 functionsArray.put(func);
172 return functionsArray;
175 private JSONArray createMethodObject(String methodName) throws JSONException {
176 JSONArray methodsArray = new JSONArray();
177 JSONObject method = new JSONObject();
178 method.put("name", methodName);
179 methodsArray.put(method);
183 private JSONArray createUserObject(String username) throws JSONException {
184 JSONArray usersArray = new JSONArray();
185 JSONObject user = new JSONObject();
186 user.put("username", username);
187 usersArray.put(user);
191 private JSONObject createRoleObject(String roleName, JSONArray usersArray, JSONArray functionsArray)
192 throws JSONException {
193 JSONObject roles = new JSONObject();
195 JSONObject role = new JSONObject();
196 role.put("name", roleName);
197 role.put("functions", functionsArray);
198 role.put("users", usersArray);
200 JSONArray rolesArray = new JSONArray();
201 rolesArray.put(role);
202 roles.put("roles", rolesArray);