f24cbf1aef49856f6791cd98827a1e192199766c
[aai/babel.git] / src / test / java / org / onap / aai / babel / MicroServiceAuthTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.babel;
24
25 import static org.hamcrest.CoreMatchers.equalTo;
26 import static org.hamcrest.CoreMatchers.is;
27 import static org.junit.Assert.assertThat;
28
29 import java.io.File;
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;
41
42 /**
43  * Tests @{link AAIMicroServiceAuth}
44  */
45
46 public class MicroServiceAuthTest {
47
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";
50
51     static {
52         System.setProperty("CONFIG_HOME",
53                 System.getProperty("user.dir") + File.separator + "src/test/resources");
54     }
55
56     /**
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.
59      * 
60      * @throws AAIAuthException
61      * @throws IOException
62      */
63     @Test(expected = AAIAuthException.class)
64     public void missingPolicyFile() throws AAIAuthException, IOException {
65         String defaultFile = AAIMicroServiceAuthCore.getDefaultAuthFileName();
66         try {
67             AAIMicroServiceAuthCore.setDefaultAuthFileName("invalid.default.file");
68             BabelAuthConfig gapServiceAuthConfig = new BabelAuthConfig();
69             gapServiceAuthConfig.setAuthPolicyFile("invalid.file.name");
70             new AAIMicroServiceAuth(gapServiceAuthConfig);
71         } finally {
72             AAIMicroServiceAuthCore.setDefaultAuthFileName(defaultFile);
73         }
74     }
75
76     /**
77      * Test loading of a temporary file created with the specified roles
78      * 
79      * @throws AAIAuthException
80      * @throws IOException
81      * @throws JSONException
82      */
83     @Test
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));
89     }
90
91     /**
92      * Test that the default policy file is loaded when a non-existent file is passed to the authorisation clas.
93      * 
94      * @throws AAIAuthException
95      */
96     @Test
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);
103     }
104
105     /**
106      * Test loading of the policy file relative to CONFIG_HOME
107      * 
108      * @throws AAIAuthException
109      */
110     @Test
111     public void createAuth() throws AAIAuthException {
112         AAIMicroServiceAuth auth = createStandardAuth();
113         assertAdminUserAuthorisation(auth, VALID_ADMIN_USER);
114     }
115
116     @Test
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")));
121     }
122
123
124
125     @Test
126     public void testValidateRequest() throws AAIAuthException {
127         AAIMicroServiceAuth auth = createStandardAuth();
128         assertThat(auth.validateRequest(null, new MockHttpServletRequest(), null, "app/v1/gap"), is(false));
129     }
130
131     private AAIMicroServiceAuth createStandardAuth() throws AAIAuthException {
132         BabelAuthConfig gapServiceAuthConfig = new BabelAuthConfig();
133         gapServiceAuthConfig.setAuthPolicyFile(authPolicyFile);
134         return new AAIMicroServiceAuth(gapServiceAuthConfig);
135     }
136
137     /**
138      * @param rolesJson
139      * @return
140      * @throws IOException
141      * @throws AAIAuthException
142      */
143     private AAIMicroServiceAuth createAuthService(JSONObject roles) throws IOException, AAIAuthException {
144         BabelAuthConfig babelAuthConfig = new BabelAuthConfig();
145         File file = File.createTempFile("auth-policy", "json");
146         file.deleteOnExit();
147         FileWriter fileWriter = new FileWriter(file);
148         fileWriter.write(roles.toString());
149         fileWriter.flush();
150         fileWriter.close();
151
152         babelAuthConfig.setAuthPolicyFile(file.getAbsolutePath());
153         return new AAIMicroServiceAuth(babelAuthConfig);
154     }
155
156     /**
157      * Assert authorisation results for an admin user based on the test policy file
158      * 
159      * @param auth
160      * @param adminUser
161      * @throws AAIAuthException
162      */
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));
168     }
169
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;
177     }
178
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);
184         return methodsArray;
185     }
186
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);
192         return usersArray;
193     }
194
195     private JSONObject createRoleObject(String roleName, JSONArray usersArray, JSONArray functionsArray)
196             throws JSONException {
197         JSONObject roles = new JSONObject();
198
199         JSONObject role = new JSONObject();
200         role.put("name", roleName);
201         role.put("functions", functionsArray);
202         role.put("users", usersArray);
203
204         JSONArray rolesArray = new JSONArray();
205         rolesArray.put(role);
206         roles.put("roles", rolesArray);
207
208         return roles;
209     }
210
211 }