a8f1f92ef26d346d54725a17ff4d1f03147f8d70
[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.is;
24 import static org.junit.Assert.assertThat;
25
26 import java.io.File;
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;
38
39 /**
40  * Tests @{link AAIMicroServiceAuth}
41  */
42
43 public class MicroServiceAuthTest {
44
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";
47
48     static {
49         System.setProperty("CONFIG_HOME", System.getProperty("user.dir") + File.separator + "src/test/resources");
50     }
51
52     /**
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.
55      *
56      * @throws AAIAuthException
57      * @throws IOException
58      */
59     @Test(expected = AAIAuthException.class)
60     public void missingPolicyFile() throws AAIAuthException, IOException {
61         String defaultFile = AAIMicroServiceAuthCore.getDefaultAuthFileName();
62         try {
63             AAIMicroServiceAuthCore.setDefaultAuthFileName("invalid.default.file");
64             BabelAuthConfig babelServiceAuthConfig = new BabelAuthConfig();
65             babelServiceAuthConfig.setAuthPolicyFile("invalid.file.name");
66             new AAIMicroServiceAuth(babelServiceAuthConfig);
67         } finally {
68             AAIMicroServiceAuthCore.setDefaultAuthFileName(defaultFile);
69         }
70     }
71
72     /**
73      * Test loading of a temporary file created with the specified roles
74      *
75      * @throws AAIAuthException
76      * @throws IOException
77      * @throws JSONException
78      */
79     @Test
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));
85     }
86
87     /**
88      * Test that the default policy file is loaded when a non-existent file is passed to the authorisation clas.
89      *
90      * @throws AAIAuthException
91      */
92     @Test
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);
99     }
100
101     /**
102      * Test loading of the policy file relative to CONFIG_HOME
103      *
104      * @throws AAIAuthException
105      */
106     @Test
107     public void createAuth() throws AAIAuthException {
108         AAIMicroServiceAuth auth = createStandardAuth();
109         assertAdminUserAuthorisation(auth, VALID_ADMIN_USER);
110     }
111
112     @Test
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));
117     }
118
119     @Test
120     public void testValidateRequest() throws AAIAuthException {
121         AAIMicroServiceAuth auth = createStandardAuth();
122         assertThat(auth.validateRequest(null, new MockHttpServletRequest(), null, "app/v1/babel"), is(false));
123     }
124
125     private AAIMicroServiceAuth createStandardAuth() throws AAIAuthException {
126         BabelAuthConfig babelServiceAuthConfig = new BabelAuthConfig();
127         babelServiceAuthConfig.setAuthPolicyFile(authPolicyFile);
128         return new AAIMicroServiceAuth(babelServiceAuthConfig);
129     }
130
131     /**
132      * @param rolesJson
133      * @return
134      * @throws IOException
135      * @throws AAIAuthException
136      */
137     private AAIMicroServiceAuth createAuthService(JSONObject roles) throws IOException, AAIAuthException {
138         File file = File.createTempFile("auth-policy", "json");
139         file.deleteOnExit();
140         FileWriter fileWriter = new FileWriter(file);
141         fileWriter.write(roles.toString());
142         fileWriter.flush();
143         fileWriter.close();
144
145         BabelAuthConfig babelAuthConfig = new BabelAuthConfig();
146         babelAuthConfig.setAuthPolicyFile(file.getAbsolutePath());
147         return new AAIMicroServiceAuth(babelAuthConfig);
148     }
149
150     /**
151      * Assert authorisation results for an admin user based on the test policy file
152      *
153      * @param auth
154      * @param adminUser
155      * @throws AAIAuthException
156      */
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));
162     }
163
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;
171     }
172
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);
178         return methodsArray;
179     }
180
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);
186         return usersArray;
187     }
188
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);
195
196         JSONArray rolesArray = new JSONArray();
197         rolesArray.put(role);
198
199         JSONObject roles = new JSONObject();
200         roles.put("roles", rolesArray);
201         return roles;
202     }
203
204 }