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.equalTo;
24 import static org.hamcrest.CoreMatchers.is;
25 import static org.junit.Assert.assertThat;
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;
41 * Tests @{link AAIMicroServiceAuth}
44 public class MicroServiceAuthTest {
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";
50 System.setProperty("CONFIG_HOME", System.getProperty("user.dir") + File.separator + "src/test/resources");
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.
57 * @throws AAIAuthException
60 @Test(expected = AAIAuthException.class)
61 public void missingPolicyFile() throws AAIAuthException, IOException {
62 String defaultFile = AAIMicroServiceAuthCore.getDefaultAuthFileName();
64 AAIMicroServiceAuthCore.setDefaultAuthFileName("invalid.default.file");
65 BabelAuthConfig babelServiceAuthConfig = new BabelAuthConfig();
66 babelServiceAuthConfig.setAuthPolicyFile("invalid.file.name");
67 new AAIMicroServiceAuth(babelServiceAuthConfig);
69 AAIMicroServiceAuthCore.setDefaultAuthFileName(defaultFile);
74 * Test loading of a temporary file created with the specified roles
76 * @throws AAIAuthException
78 * @throws JSONException
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));
89 * Test that the default policy file is loaded when a non-existent file is passed to the authorisation clas.
91 * @throws AAIAuthException
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);
103 * Test loading of the policy file relative to CONFIG_HOME
105 * @throws AAIAuthException
108 public void createAuth() throws AAIAuthException {
109 AAIMicroServiceAuth auth = createStandardAuth();
110 assertAdminUserAuthorisation(auth, VALID_ADMIN_USER);
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")));
123 public void testValidateRequest() throws AAIAuthException {
124 AAIMicroServiceAuth auth = createStandardAuth();
125 assertThat(auth.validateRequest(null, new MockHttpServletRequest(), null, "app/v1/babel"), is(false));
128 private AAIMicroServiceAuth createStandardAuth() throws AAIAuthException {
129 BabelAuthConfig babelServiceAuthConfig = new BabelAuthConfig();
130 babelServiceAuthConfig.setAuthPolicyFile(authPolicyFile);
131 return new AAIMicroServiceAuth(babelServiceAuthConfig);
137 * @throws IOException
138 * @throws AAIAuthException
140 private AAIMicroServiceAuth createAuthService(JSONObject roles) throws IOException, AAIAuthException {
141 BabelAuthConfig babelAuthConfig = new BabelAuthConfig();
142 File file = File.createTempFile("auth-policy", "json");
144 FileWriter fileWriter = new FileWriter(file);
145 fileWriter.write(roles.toString());
149 babelAuthConfig.setAuthPolicyFile(file.getAbsolutePath());
150 return new AAIMicroServiceAuth(babelAuthConfig);
154 * Assert authorisation results for an admin user based on the test policy file
158 * @throws AAIAuthException
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));
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;
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);
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);
192 private JSONObject createRoleObject(String roleName, JSONArray usersArray, JSONArray functionsArray)
193 throws JSONException {
194 JSONObject roles = new JSONObject();
196 JSONObject role = new JSONObject();
197 role.put("name", roleName);
198 role.put("functions", functionsArray);
199 role.put("users", usersArray);
201 JSONArray rolesArray = new JSONArray();
202 rolesArray.put(role);
203 roles.put("roles", rolesArray);