Upgrade Spring Boot from 1.5.x to 2.1.6
[aai/babel.git] / src / main / java / org / onap / aai / auth / AAIMicroServiceAuthCore.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright (c) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * Copyright (c) 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.nio.file.Path;
31 import java.nio.file.Paths;
32 import java.util.ArrayList;
33 import java.util.Date;
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map.Entry;
37 import java.util.Optional;
38 import java.util.Timer;
39 import java.util.TimerTask;
40 import java.util.concurrent.TimeUnit;
41 import org.onap.aai.babel.logging.ApplicationMsgs;
42 import org.onap.aai.babel.logging.LogHelper;
43
44 /** Authentication and authorization by user and role. */
45 public class AAIMicroServiceAuthCore {
46
47     private static LogHelper applicationLogger = LogHelper.INSTANCE;
48
49     /**
50      * The default policy file is expected to be located in either one of
51      * <ul>
52      * <li><code>$CONFIG_HOME/auth_policy.json</code></li>
53      * <li><code>$CONFIG_HOME/auth/auth_policy.json</code></li>
54      * <p>
55      * Note that if <code>CONFIG_HOME</code> is not set then assume it has a value of <code>$APP_HOME/appconfig</code>
56      */
57     private static String defaultAuthFileName = "auth_policy.json";
58
59     private static boolean usersInitialized = false;
60     private static HashMap<String, AAIAuthUser> users;
61     private static boolean timerSet = false;
62     private static Timer timer = null;
63     private static String policyAuthFileName;
64
65     public enum HTTP_METHODS {
66         GET, PUT, DELETE, HEAD, POST
67     }
68
69     // Don't instantiate
70     private AAIMicroServiceAuthCore() {}
71
72     public static String getDefaultAuthFileName() {
73         return defaultAuthFileName;
74     }
75
76     public static void setDefaultAuthFileName(String defaultAuthFileName) {
77         AAIMicroServiceAuthCore.defaultAuthFileName = defaultAuthFileName;
78     }
79
80     public static synchronized void init(String authPolicyFile) throws AAIAuthException {
81         try {
82             policyAuthFileName = AAIMicroServiceAuthCore.getConfigFile(authPolicyFile);
83         } catch (IOException e) {
84             applicationLogger.debug("Exception while retrieving policy file.");
85             applicationLogger.error(ApplicationMsgs.PROCESS_REQUEST_ERROR, e);
86             throw new AAIAuthException(e.getMessage());
87         }
88
89         if (policyAuthFileName == null) {
90             throw new AAIAuthException("Auth policy file could not be found");
91         }
92
93         AAIMicroServiceAuthCore.reloadUsers();
94
95         TimerTask task = new AuthFileWatcher(new File(policyAuthFileName));
96
97         if (!timerSet) {
98             timerSet = true;
99             timer = new Timer();
100             long period = TimeUnit.SECONDS.toMillis(1);
101             timer.schedule(task, new Date(), period);
102             applicationLogger.debug("Config Watcher Interval = " + period);
103         }
104     }
105
106     public static void cleanup() {
107         timer.cancel();
108     }
109
110     public static String getConfigFile(String authPolicyFile) throws IOException {
111         return locateConfigFile(authPolicyFile).orElse(locateConfigFile(defaultAuthFileName).orElse(null));
112     }
113
114     /**
115      * Locate the auth policy file by its name or path.
116      * <ul>
117      * <li>First try to use the absolute path to the file (if provided), or instead locate the path relative to the
118      * current (or user) dir.</li>
119      * <li>If this fails, try resolving the path relative to the configuration home location (either
120      * <code>$CONFIG_HOME</code> or <code>$APP_HOME/appconfig</code>).</li>
121      * <li>If this fails try resolving relative to the <code>auth</code> folder under configuration home.</li>
122      *
123      * @param authPolicyFile
124      *            filename or path
125      * @return the Optional canonical path to the located policy file
126      * @throws IOException
127      *             if the construction of the canonical pathname requires filesystem queries which cause I/O error(s)
128      */
129     private static Optional<String> locateConfigFile(String authPolicyFile) throws IOException {
130         if (authPolicyFile != null) {
131             List<Path> paths = new ArrayList<>();
132             paths.add(Paths.get("."));
133
134             String configHome = System.getProperty("CONFIG_HOME");
135             if (configHome == null) {
136                 configHome = System.getProperty("APP_HOME") + "/appconfig";
137             }
138
139             paths.add(Paths.get(configHome));
140             paths.add(Paths.get(configHome).resolve("auth"));
141
142             for (Path path : paths) {
143                 File authFile = path.resolve(authPolicyFile).toFile();
144                 if (authFile.exists()) {
145                     return Optional.of(authFile.getCanonicalPath());
146                 }
147             }
148         }
149
150         return Optional.empty();
151     }
152
153     public static synchronized void reloadUsers() throws AAIAuthException {
154         users = new HashMap<>();
155
156         ObjectMapper mapper = new ObjectMapper();
157         try {
158             applicationLogger.debug("Reading from " + policyAuthFileName);
159             JsonNode rootNode = mapper.readTree(new File(policyAuthFileName));
160             for (JsonNode roleNode : rootNode.path("roles")) {
161                 String roleName = roleNode.path("name").asText();
162                 AAIAuthRole r = new AAIAuthRole();
163                 installFunctionOnRole(roleNode.path("functions"), roleName, r);
164                 assignRoleToUsers(roleNode.path("users"), roleName, r);
165             }
166         } catch (FileNotFoundException e) {
167             throw new AAIAuthException("Auth policy file could not be found", e);
168         } catch (JsonProcessingException | NullPointerException e) {
169             throw new AAIAuthException("Error processing Auth policy file ", e);
170         } catch (IOException e) {
171             throw new AAIAuthException("Error reading Auth policy file", e);
172         }
173
174         usersInitialized = true;
175     }
176
177     private static void installFunctionOnRole(JsonNode functionsNode, String roleName, AAIAuthRole r) {
178         for (JsonNode functionNode : functionsNode) {
179             String function = functionNode.path("name").asText();
180             JsonNode methodsNode = functionNode.path("methods");
181             boolean hasMethods = false;
182             for (JsonNode method_node : methodsNode) {
183                 String methodName = method_node.path("name").asText();
184                 hasMethods = true;
185                 String func = methodName + ":" + function;
186                 applicationLogger.debug("Installing function " + func + " on role " + roleName);
187                 r.addAllowedFunction(func);
188             }
189
190             if (!hasMethods) {
191                 for (HTTP_METHODS meth : HTTP_METHODS.values()) {
192                     String func = meth.toString() + ":" + function;
193                     applicationLogger.debug("Installing (all methods) " + func + " on role " + roleName);
194                     r.addAllowedFunction(func);
195                 }
196             }
197         }
198     }
199
200     private static void assignRoleToUsers(JsonNode usersNode, String roleName, AAIAuthRole r) {
201         for (JsonNode userNode : usersNode) {
202             String name = userNode.path("username").asText().toLowerCase();
203             AAIAuthUser user;
204             if (users.containsKey(name)) {
205                 user = users.get(name);
206             } else {
207                 user = new AAIAuthUser();
208             }
209             applicationLogger.debug("Assigning " + roleName + " to user " + name);
210             user.addRole(roleName, r);
211             users.put(name, user);
212         }
213     }
214
215     public static class AAIAuthUser {
216         private HashMap<String, AAIAuthRole> roles;
217
218         public AAIAuthUser() {
219             this.roles = new HashMap<>();
220         }
221
222         public void addRole(String roleName, AAIAuthRole r) {
223             this.roles.put(roleName, r);
224         }
225
226         public boolean checkAllowed(String checkFunc) {
227             for (Entry<String, AAIAuthRole> role_entry : roles.entrySet()) {
228                 AAIAuthRole role = role_entry.getValue();
229                 if (role.hasAllowedFunction(checkFunc)) {
230                     return true;
231                 }
232             }
233             return false;
234         }
235     }
236
237     public static class AAIAuthRole {
238
239         private List<String> allowedFunctions;
240
241         public AAIAuthRole() {
242             this.allowedFunctions = new ArrayList<>();
243         }
244
245         public void addAllowedFunction(String func) {
246             this.allowedFunctions.add(func);
247         }
248
249         public boolean hasAllowedFunction(String afunc) {
250             return this.allowedFunctions.contains(afunc);
251         }
252     }
253
254     public static boolean authorize(String username, String authFunction) throws AAIAuthException {
255         if (!usersInitialized || users == null) {
256             throw new AAIAuthException("Auth module not initialized");
257         }
258         if (users.containsKey(username)) {
259             if (users.get(username).checkAllowed(authFunction)) {
260                 logAuthenticationResult(username, authFunction, "AUTH ACCEPTED");
261                 return true;
262             } else {
263                 logAuthenticationResult(username, authFunction, "AUTH FAILED");
264                 return false;
265             }
266         } else {
267             logAuthenticationResult(username, authFunction, "User not found");
268             return false;
269         }
270     }
271
272     private static void logAuthenticationResult(String username, String authFunction, String result) {
273         applicationLogger.debug(result + ": " + username + " on function " + authFunction);
274     }
275 }