2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.babel;
25 import static org.hamcrest.CoreMatchers.equalTo;
26 import static org.hamcrest.CoreMatchers.is;
27 import static org.junit.Assert.assertThat;
30 import java.io.FileWriter;
31 import java.io.IOException;
32 import org.json.JSONArray;
33 import org.json.JSONException;
34 import org.json.JSONObject;
35 import org.junit.Test;
36 import org.onap.aai.auth.AAIAuthException;
37 import org.onap.aai.auth.AAIMicroServiceAuth;
38 import org.onap.aai.auth.AAIMicroServiceAuthCore;
39 import org.onap.aai.babel.config.BabelAuthConfig;
40 import org.springframework.mock.web.MockHttpServletRequest;
43 * Tests @{link AAIMicroServiceAuth}
46 public class MicroServiceAuthTest {
48 private static final String VALID_ADMIN_USER = "cn=common-name, ou=org-unit, o=org, l=location, st=state, c=us";
49 private static final String authPolicyFile = "auth_policy.json";
52 System.setProperty("CONFIG_HOME",
53 System.getProperty("user.dir") + File.separator + "src/test/resources");
57 * Temporarily invalidate the default policy file and then try to initialise the authorisation class using the name
58 * of a policy file that does not exist.
60 * @throws AAIAuthException
63 @Test(expected = AAIAuthException.class)
64 public void missingPolicyFile() throws AAIAuthException, IOException {
65 String defaultFile = AAIMicroServiceAuthCore.getDefaultAuthFileName();
67 AAIMicroServiceAuthCore.setDefaultAuthFileName("invalid.default.file");
68 BabelAuthConfig gapServiceAuthConfig = new BabelAuthConfig();
69 gapServiceAuthConfig.setAuthPolicyFile("invalid.file.name");
70 new AAIMicroServiceAuth(gapServiceAuthConfig);
72 AAIMicroServiceAuthCore.setDefaultAuthFileName(defaultFile);
77 * Test loading of a temporary file created with the specified roles
79 * @throws AAIAuthException
81 * @throws JSONException
84 public void createLocalAuthFile() throws AAIAuthException, IOException, JSONException {
85 JSONObject roles = createRoleObject("role", createUserObject("user"), createFunctionObject("func"));
86 AAIMicroServiceAuth auth = createAuthService(roles);
87 assertThat(auth.authorize("nosuchuser", "method:func"), is(false));
88 assertThat(auth.authorize("user", "method:func"), is(true));
92 * Test that the default policy file is loaded when a non-existent file is passed to the authorisation clas.
94 * @throws AAIAuthException
97 public void createAuthFromDefaultFile() throws AAIAuthException {
98 BabelAuthConfig gapServiceAuthConfig = new BabelAuthConfig();
99 gapServiceAuthConfig.setAuthPolicyFile("non-existent-file");
100 AAIMicroServiceAuth auth = new AAIMicroServiceAuth(gapServiceAuthConfig);
101 // The default policy will have been loaded
102 assertAdminUserAuthorisation(auth, VALID_ADMIN_USER);
106 * Test loading of the policy file relative to CONFIG_HOME
108 * @throws AAIAuthException
111 public void createAuth() throws AAIAuthException {
112 AAIMicroServiceAuth auth = createStandardAuth();
113 assertAdminUserAuthorisation(auth, VALID_ADMIN_USER);
117 public void testAuthUser() throws AAIAuthException {
118 AAIMicroServiceAuth auth = createStandardAuth();
119 assertThat(auth.authenticate(VALID_ADMIN_USER, "GET:actions"), is(equalTo("OK")));
120 assertThat(auth.authenticate(VALID_ADMIN_USER, "WRONG:action"), is(equalTo("AAI_9101")));
126 public void testValidateRequest() throws AAIAuthException {
127 AAIMicroServiceAuth auth = createStandardAuth();
128 assertThat(auth.validateRequest(null, new MockHttpServletRequest(), null, "app/v1/gap"), is(false));
131 private AAIMicroServiceAuth createStandardAuth() throws AAIAuthException {
132 BabelAuthConfig gapServiceAuthConfig = new BabelAuthConfig();
133 gapServiceAuthConfig.setAuthPolicyFile(authPolicyFile);
134 return new AAIMicroServiceAuth(gapServiceAuthConfig);
140 * @throws IOException
141 * @throws AAIAuthException
143 private AAIMicroServiceAuth createAuthService(JSONObject roles) throws IOException, AAIAuthException {
144 BabelAuthConfig babelAuthConfig = new BabelAuthConfig();
145 File file = File.createTempFile("auth-policy", "json");
147 FileWriter fileWriter = new FileWriter(file);
148 fileWriter.write(roles.toString());
152 babelAuthConfig.setAuthPolicyFile(file.getAbsolutePath());
153 return new AAIMicroServiceAuth(babelAuthConfig);
157 * Assert authorisation results for an admin user based on the test policy file
161 * @throws AAIAuthException
163 private void assertAdminUserAuthorisation(AAIMicroServiceAuth auth, String adminUser) throws AAIAuthException {
164 assertThat(auth.authorize(adminUser, "GET:actions"), is(true));
165 assertThat(auth.authorize(adminUser, "POST:actions"), is(true));
166 assertThat(auth.authorize(adminUser, "PUT:actions"), is(true));
167 assertThat(auth.authorize(adminUser, "DELETE:actions"), is(true));
170 private JSONArray createFunctionObject(String functionName) throws JSONException {
171 JSONArray functionsArray = new JSONArray();
172 JSONObject func = new JSONObject();
173 func.put("name", functionName);
174 func.put("methods", createMethodObject("method"));
175 functionsArray.put(func);
176 return functionsArray;
179 private JSONArray createMethodObject(String methodName) throws JSONException {
180 JSONArray methodsArray = new JSONArray();
181 JSONObject method = new JSONObject();
182 method.put("name", methodName);
183 methodsArray.put(method);
187 private JSONArray createUserObject(String username) throws JSONException {
188 JSONArray usersArray = new JSONArray();
189 JSONObject user = new JSONObject();
190 user.put("username", username);
191 usersArray.put(user);
195 private JSONObject createRoleObject(String roleName, JSONArray usersArray, JSONArray functionsArray)
196 throws JSONException {
197 JSONObject roles = new JSONObject();
199 JSONObject role = new JSONObject();
200 role.put("name", roleName);
201 role.put("functions", functionsArray);
202 role.put("users", usersArray);
204 JSONArray rolesArray = new JSONArray();
205 rolesArray.put(role);
206 roles.put("roles", rolesArray);