2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 Amdocs
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.sa.auth;
23 import com.fasterxml.jackson.core.JsonProcessingException;
24 import com.fasterxml.jackson.databind.JsonNode;
25 import com.fasterxml.jackson.databind.ObjectMapper;
27 import java.io.FileNotFoundException;
28 import java.io.FileReader;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.List;
34 import java.util.Timer;
35 import org.json.simple.parser.JSONParser;
36 import org.json.simple.parser.ParseException;
37 import org.onap.aai.cl.api.Logger;
38 import org.onap.aai.cl.eelf.LoggerFactory;
39 import org.onap.aai.sa.searchdbabstraction.util.SearchDbConstants;
41 public class SearchDbServiceAuthCore {
43 private static Logger logger = LoggerFactory.getInstance().getLogger(SearchDbServiceAuthCore.class.getName());
45 private static String GlobalAuthFileName = SearchDbConstants.SDB_AUTH_CONFIG_FILENAME;
47 private enum HTTP_METHODS {
55 private SearchDbServiceAuthCore() {}
57 private static boolean usersInitialized = false;
58 private static HashMap<String, SearchDbAuthUser> users;
59 private static boolean timerSet = false;
60 private static Timer timer = null;
62 public synchronized static void init() {
65 SearchDbServiceAuthCore.getConfigFile();
66 SearchDbServiceAuthCore.reloadUsers();
70 public static void cleanup() {
74 public static String getConfigFile() {
75 if (GlobalAuthFileName == null) {
76 String nc = GlobalAuthFileName;
78 nc = "/home/aaiadmin/etc/aaipolicy.json";
80 GlobalAuthFileName = nc;
82 return GlobalAuthFileName;
85 public synchronized static void reloadUsers() {
86 users = new HashMap<>();
87 ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
88 JSONParser parser = new JSONParser();
90 parser.parse(new FileReader(GlobalAuthFileName));
91 JsonNode rootNode = mapper.readTree(new File(GlobalAuthFileName));
92 JsonNode rolesNode = rootNode.path("roles");
94 for (JsonNode roleNode : rolesNode) {
95 String roleName = roleNode.path("name").asText();
97 TabularAuthRole authRole = new TabularAuthRole();
98 JsonNode usersNode = roleNode.path("users");
99 JsonNode functionsNode = roleNode.path("functions");
100 for (JsonNode functionNode : functionsNode) {
101 String function = functionNode.path("name").asText();
102 JsonNode methodsNode = functionNode.path("methods");
103 boolean hasMethods = false;
104 for (JsonNode methodNode : methodsNode) {
105 String methodName = methodNode.path("name").asText();
107 String thisFunction = methodName + ":" + function;
109 authRole.addAllowedFunction(thisFunction);
112 if (hasMethods == false) {
113 // iterate the list from HTTP_METHODS
114 for (HTTP_METHODS meth : HTTP_METHODS.values()) {
115 String thisFunction = meth.toString() + ":" + function;
117 authRole.addAllowedFunction(thisFunction);
122 for (JsonNode userNode : usersNode) {
123 // make the user lower case
124 String username = userNode.path("username").asText().toLowerCase();
125 SearchDbAuthUser authUser = null;
126 if (users.containsKey(username)) {
127 authUser = users.get(username);
129 authUser = new SearchDbAuthUser();
133 authUser.setUser(username);
134 authUser.addRole(roleName, authRole);
135 users.put(username, authUser);
138 } catch (FileNotFoundException fnfe) {
139 logger.debug("Failed to load the policy file ");
141 } catch (ParseException e) {
142 logger.debug("Failed to Parse the policy file ");
144 } catch (JsonProcessingException e) {
145 logger.debug("JSON processing error while parsing policy file: " + e.getMessage());
147 } catch (IOException e) {
148 logger.debug("IO Exception while parsing policy file: " + e.getMessage());
151 usersInitialized = true;
155 public static class SearchDbAuthUser {
156 public SearchDbAuthUser() {
157 this.roles = new HashMap<>();
160 private String username;
161 private HashMap<String, TabularAuthRole> roles;
163 public String getUser() {
164 return this.username;
167 public HashMap<String, TabularAuthRole> getRoles() {
171 public void addRole(String roleName, TabularAuthRole authRole) {
172 this.roles.put(roleName, authRole);
175 public boolean checkAllowed(String checkFunc) {
176 for (Map.Entry<String, TabularAuthRole> roleEntry : this.roles.entrySet()) {
177 TabularAuthRole role = roleEntry.getValue();
178 if (role.hasAllowedFunction(checkFunc)) {
179 // break out as soon as we find it
183 // we would have got positive confirmation had it been there
187 public void setUser(String myuser) {
188 this.username = myuser;
193 public static class TabularAuthRole {
194 public TabularAuthRole() {
195 this.allowedFunctions = new ArrayList<>();
198 private List<String> allowedFunctions;
200 public void addAllowedFunction(String func) {
201 this.allowedFunctions.add(func);
204 public void delAllowedFunction(String delFunc) {
205 if (this.allowedFunctions.contains(delFunc)) {
206 this.allowedFunctions.remove(delFunc);
210 public boolean hasAllowedFunction(String afunc) {
211 if (this.allowedFunctions.contains(afunc)) {
219 public static HashMap<String, SearchDbAuthUser> getUsers(String key) {
220 if (!usersInitialized || (users == null)) {
226 public static boolean authorize(String username, String authFunction) {
228 if (!usersInitialized || (users == null)) {
231 if (users.containsKey(username)) {
232 if (users.get(username).checkAllowed(authFunction) == true) {