ddbc002a74283c6d03b04884ce12b275651fe23c
[aai/babel.git] / src / main / java / org / onap / aai / auth / AAIMicroServiceAuthCore.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2019 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
22 package org.onap.aai.auth;
23
24 import com.fasterxml.jackson.core.JsonProcessingException;
25 import com.fasterxml.jackson.databind.JsonNode;
26 import com.fasterxml.jackson.databind.ObjectMapper;
27 import java.io.File;
28 import java.io.FileNotFoundException;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.Date;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Map.Entry;
36 import java.util.Timer;
37 import java.util.TimerTask;
38 import java.util.concurrent.TimeUnit;
39 import org.onap.aai.babel.logging.ApplicationMsgs;
40 import org.onap.aai.babel.logging.LogHelper;
41
42 /** Authentication and authorization by user and role. */
43 public class AAIMicroServiceAuthCore {
44
45     private static LogHelper applicationLogger = LogHelper.INSTANCE;
46
47     private static final String CONFIG_HOME = System.getProperty("CONFIG_HOME");
48
49     public static final String FILESEP =
50             (System.getProperty("file.separator") == null) ? "/" : System.getProperty("file.separator");
51     public static final String APPCONFIG_DIR =
52             (CONFIG_HOME == null) ? System.getProperty("APP_HOME") + FILESEP + "appconfig" : CONFIG_HOME;
53
54     private static String appConfigAuthDir = APPCONFIG_DIR + FILESEP + "auth";
55     private static String defaultAuthFileName = appConfigAuthDir + FILESEP + "auth_policy.json";
56
57     private static boolean usersInitialized = false;
58     private static HashMap<String, AAIAuthUser> users;
59     private static boolean timerSet = false;
60     private static Timer timer = null;
61     private static String policyAuthFileName;
62
63     public enum HTTP_METHODS {
64         GET, PUT, DELETE, HEAD, POST
65     }
66
67     // Don't instantiate
68     private AAIMicroServiceAuthCore() {}
69
70     public static String getDefaultAuthFileName() {
71         return defaultAuthFileName;
72     }
73
74     public static void setDefaultAuthFileName(String defaultAuthFileName) {
75         AAIMicroServiceAuthCore.defaultAuthFileName = defaultAuthFileName;
76     }
77
78     public static synchronized void init(String authPolicyFile) throws AAIAuthException {
79         try {
80             policyAuthFileName = AAIMicroServiceAuthCore.getConfigFile(authPolicyFile);
81         } catch (IOException e) {
82             applicationLogger.debug("Exception while retrieving policy file.");
83             applicationLogger.error(ApplicationMsgs.PROCESS_REQUEST_ERROR, e);
84             throw new AAIAuthException(e.getMessage());
85         }
86         if (policyAuthFileName == null) {
87             throw new AAIAuthException("Auth policy file could not be found" + CONFIG_HOME + APPCONFIG_DIR);
88         }
89         AAIMicroServiceAuthCore.reloadUsers();
90
91         TimerTask task = new FileWatcher(new File(policyAuthFileName)) {
92             @Override
93             protected void onChange(File file) {
94                 // here we implement the onChange
95                 applicationLogger.debug("File " + file.getName() + " has been changed!");
96                 try {
97                     AAIMicroServiceAuthCore.reloadUsers();
98                 } catch (AAIAuthException e) {
99                     applicationLogger.error(ApplicationMsgs.PROCESS_REQUEST_ERROR, e);
100                 }
101                 applicationLogger.debug("File " + file.getName() + " has been reloaded!");
102             }
103         };
104
105         if (!timerSet) {
106             timerSet = true;
107             timer = new Timer();
108             long period = TimeUnit.SECONDS.toMillis(1);
109             timer.schedule(task, new Date(), period);
110             applicationLogger.debug("Config Watcher Interval = " + period);
111         }
112     }
113
114     public static void cleanup() {
115         timer.cancel();
116     }
117
118     public static String getConfigFile(String authPolicyFile) throws IOException {
119         File authFile = new File(authPolicyFile);
120         if (authFile.exists()) {
121             return authFile.getCanonicalPath();
122         }
123         authFile = new File(appConfigAuthDir + FILESEP + authPolicyFile);
124         if (authFile.exists()) {
125             return authFile.getCanonicalPath();
126         }
127         if (defaultAuthFileName != null) {
128             authFile = new File(defaultAuthFileName);
129             if (authFile.exists()) {
130                 return defaultAuthFileName;
131             }
132         }
133         return null;
134     }
135
136     public static synchronized void reloadUsers() throws AAIAuthException {
137         users = new HashMap<>();
138
139         ObjectMapper mapper = new ObjectMapper();
140         try {
141             applicationLogger.debug("Reading from " + policyAuthFileName);
142             JsonNode rootNode = mapper.readTree(new File(policyAuthFileName));
143             for (JsonNode roleNode : rootNode.path("roles")) {
144                 String roleName = roleNode.path("name").asText();
145                 AAIAuthRole r = new AAIAuthRole();
146                 installFunctionOnRole(roleNode.path("functions"), roleName, r);
147                 assignRoleToUsers(roleNode.path("users"), roleName, r);
148             }
149         } catch (FileNotFoundException e) {
150             throw new AAIAuthException("Auth policy file could not be found", e);
151         } catch (JsonProcessingException e) {
152             throw new AAIAuthException("Error processing Auth policy file ", e);
153         } catch (IOException e) {
154             throw new AAIAuthException("Error reading Auth policy file", e);
155         }
156
157         usersInitialized = true;
158     }
159
160     private static void installFunctionOnRole(JsonNode functionsNode, String roleName, AAIAuthRole r) {
161         for (JsonNode functionNode : functionsNode) {
162             String function = functionNode.path("name").asText();
163             JsonNode methodsNode = functionNode.path("methods");
164             boolean hasMethods = false;
165             for (JsonNode method_node : methodsNode) {
166                 String methodName = method_node.path("name").asText();
167                 hasMethods = true;
168                 String func = methodName + ":" + function;
169                 applicationLogger.debug("Installing function " + func + " on role " + roleName);
170                 r.addAllowedFunction(func);
171             }
172
173             if (!hasMethods) {
174                 for (HTTP_METHODS meth : HTTP_METHODS.values()) {
175                     String func = meth.toString() + ":" + function;
176                     applicationLogger.debug("Installing (all methods) " + func + " on role " + roleName);
177                     r.addAllowedFunction(func);
178                 }
179             }
180         }
181     }
182
183     private static void assignRoleToUsers(JsonNode usersNode, String roleName, AAIAuthRole r) {
184         for (JsonNode userNode : usersNode) {
185             String name = userNode.path("username").asText().toLowerCase();
186             AAIAuthUser user;
187             if (users.containsKey(name)) {
188                 user = users.get(name);
189             } else {
190                 user = new AAIAuthUser();
191             }
192             applicationLogger.debug("Assigning " + roleName + " to user " + name);
193             user.setUser(name);
194             user.addRole(roleName, r);
195             users.put(name, user);
196         }
197     }
198
199     public static class AAIAuthUser {
200         private String username;
201         private HashMap<String, AAIAuthRole> roles;
202
203         public AAIAuthUser() {
204             this.roles = new HashMap<>();
205         }
206
207         public String getUser() {
208             return this.username;
209         }
210
211         public Map<String, AAIAuthRole> getRoles() {
212             return this.roles;
213         }
214
215         public void addRole(String roleName, AAIAuthRole r) {
216             this.roles.put(roleName, r);
217         }
218
219         public boolean checkAllowed(String checkFunc) {
220             for (Entry<String, AAIAuthRole> role_entry : roles.entrySet()) {
221                 AAIAuthRole role = role_entry.getValue();
222                 if (role.hasAllowedFunction(checkFunc)) {
223                     return true;
224                 }
225             }
226             return false;
227         }
228
229         public void setUser(String myuser) {
230             this.username = myuser;
231         }
232     }
233
234     public static class AAIAuthRole {
235
236         private List<String> allowedFunctions;
237
238         public AAIAuthRole() {
239             this.allowedFunctions = new ArrayList<>();
240         }
241
242         public void addAllowedFunction(String func) {
243             this.allowedFunctions.add(func);
244         }
245
246         public void delAllowedFunction(String delFunc) {
247             if (this.allowedFunctions.contains(delFunc)) {
248                 this.allowedFunctions.remove(delFunc);
249             }
250         }
251
252         public boolean hasAllowedFunction(String afunc) {
253             return this.allowedFunctions.contains(afunc);
254         }
255     }
256
257     public static boolean authorize(String username, String authFunction) throws AAIAuthException {
258         if (!usersInitialized || users == null) {
259             throw new AAIAuthException("Auth module not initialized");
260         }
261         if (users.containsKey(username)) {
262             if (users.get(username).checkAllowed(authFunction)) {
263                 logAuthenticationResult(username, authFunction, "AUTH ACCEPTED");
264                 return true;
265             } else {
266                 logAuthenticationResult(username, authFunction, "AUTH FAILED");
267                 return false;
268             }
269         } else {
270             logAuthenticationResult(username, authFunction, "User not found");
271             return false;
272         }
273     }
274
275     private static void logAuthenticationResult(String username, String authFunction, String result) {
276         applicationLogger.debug(result + ": " + username + " on function " + authFunction);
277     }
278 }