e6f5aa6b6aecf5da48b05292fc6e329985afd3cb
[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
120
121     @Test
122     public void testValidateRequest() throws AAIAuthException {
123         AAIMicroServiceAuth auth = createStandardAuth();
124         assertThat(auth.validateRequest(null, new MockHttpServletRequest(), null, "app/v1/babel"), is(false));
125     }
126
127     private AAIMicroServiceAuth createStandardAuth() throws AAIAuthException {
128         BabelAuthConfig babelServiceAuthConfig = new BabelAuthConfig();
129         babelServiceAuthConfig.setAuthPolicyFile(authPolicyFile);
130         return new AAIMicroServiceAuth(babelServiceAuthConfig);
131     }
132
133     /**
134      * @param rolesJson
135      * @return
136      * @throws IOException
137      * @throws AAIAuthException
138      */
139     private AAIMicroServiceAuth createAuthService(JSONObject roles) throws IOException, AAIAuthException {
140         BabelAuthConfig babelAuthConfig = new BabelAuthConfig();
141         File file = File.createTempFile("auth-policy", "json");
142         file.deleteOnExit();
143         FileWriter fileWriter = new FileWriter(file);
144         fileWriter.write(roles.toString());
145         fileWriter.flush();
146         fileWriter.close();
147
148         babelAuthConfig.setAuthPolicyFile(file.getAbsolutePath());
149         return new AAIMicroServiceAuth(babelAuthConfig);
150     }
151
152     /**
153      * Assert authorisation results for an admin user based on the test policy file
154      *
155      * @param auth
156      * @param adminUser
157      * @throws AAIAuthException
158      */
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));
164     }
165
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;
173     }
174
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);
180         return methodsArray;
181     }
182
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);
188         return usersArray;
189     }
190
191     private JSONObject createRoleObject(String roleName, JSONArray usersArray, JSONArray functionsArray)
192             throws JSONException {
193         JSONObject roles = new JSONObject();
194
195         JSONObject role = new JSONObject();
196         role.put("name", roleName);
197         role.put("functions", functionsArray);
198         role.put("users", usersArray);
199
200         JSONArray rolesArray = new JSONArray();
201         rolesArray.put(role);
202         roles.put("roles", rolesArray);
203
204         return roles;
205     }
206
207 }