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));
120 public void testValidateRequest() throws AAIAuthException {
121 AAIMicroServiceAuth auth = createStandardAuth();
122 assertThat(auth.validateRequest(null, new MockHttpServletRequest(), null, "app/v1/babel"), is(false));
125 private AAIMicroServiceAuth createStandardAuth() throws AAIAuthException {
126 BabelAuthConfig babelServiceAuthConfig = new BabelAuthConfig();
127 babelServiceAuthConfig.setAuthPolicyFile(authPolicyFile);
128 return new AAIMicroServiceAuth(babelServiceAuthConfig);
134 * @throws IOException
135 * @throws AAIAuthException
137 private AAIMicroServiceAuth createAuthService(JSONObject roles) throws IOException, AAIAuthException {
138 File file = File.createTempFile("auth-policy", "json");
140 FileWriter fileWriter = new FileWriter(file);
141 fileWriter.write(roles.toString());
145 BabelAuthConfig babelAuthConfig = new BabelAuthConfig();
146 babelAuthConfig.setAuthPolicyFile(file.getAbsolutePath());
147 return new AAIMicroServiceAuth(babelAuthConfig);
151 * Assert authorisation results for an admin user based on the test policy file
155 * @throws AAIAuthException
157 private void assertAdminUserAuthorisation(AAIMicroServiceAuth auth, String adminUser) throws AAIAuthException {
158 assertThat(AAIMicroServiceAuthCore.authorize(adminUser, "GET:actions"), is(true));
159 assertThat(AAIMicroServiceAuthCore.authorize(adminUser, "POST:actions"), is(true));
160 assertThat(AAIMicroServiceAuthCore.authorize(adminUser, "PUT:actions"), is(true));
161 assertThat(AAIMicroServiceAuthCore.authorize(adminUser, "DELETE:actions"), is(true));
164 private JSONArray createFunctionObject(String functionName) throws JSONException {
165 JSONArray functionsArray = new JSONArray();
166 JSONObject func = new JSONObject();
167 func.put("name", functionName);
168 func.put("methods", createMethodObject("method"));
169 functionsArray.put(func);
170 return functionsArray;
173 private JSONArray createMethodObject(String methodName) throws JSONException {
174 JSONArray methodsArray = new JSONArray();
175 JSONObject method = new JSONObject();
176 method.put("name", methodName);
177 methodsArray.put(method);
181 private JSONArray createUserObject(String username) throws JSONException {
182 JSONArray usersArray = new JSONArray();
183 JSONObject user = new JSONObject();
184 user.put("username", username);
185 usersArray.put(user);
189 private JSONObject createRoleObject(String roleName, JSONArray usersArray, JSONArray functionsArray)
190 throws JSONException {
191 JSONObject role = new JSONObject();
192 role.put("name", roleName);
193 role.put("functions", functionsArray);
194 role.put("users", usersArray);
196 JSONArray rolesArray = new JSONArray();
197 rolesArray.put(role);
199 JSONObject roles = new JSONObject();
200 roles.put("roles", rolesArray);