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()
44 .getLogger(SearchDbServiceAuthCore.class.getName());
46 private static String GlobalAuthFileName = SearchDbConstants.SDB_AUTH_CONFIG_FILENAME;
48 private static enum HTTP_METHODS {
49 POST, GET, PUT, DELETE
55 private SearchDbServiceAuthCore() {
58 private static boolean usersInitialized = false;
59 private static HashMap<String, SearchDbAuthUser> users;
60 private static boolean timerSet = false;
61 private static Timer timer = null;
63 public synchronized static void init() {
66 SearchDbServiceAuthCore.getConfigFile();
67 SearchDbServiceAuthCore.reloadUsers();
71 public static void cleanup() {
75 public static String getConfigFile() {
76 if (GlobalAuthFileName == null) {
77 String nc = GlobalAuthFileName;
79 nc = "/home/aaiadmin/etc/aaipolicy.json";
81 GlobalAuthFileName = nc;
83 return GlobalAuthFileName;
86 public synchronized static void reloadUsers() {
87 users = new HashMap<String, SearchDbAuthUser>();
88 ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
89 JSONParser parser = new JSONParser();
91 Object obj = parser.parse(new FileReader(GlobalAuthFileName));
92 JsonNode rootNode = mapper.readTree(new File(GlobalAuthFileName));
93 JsonNode rolesNode = rootNode.path("roles");
95 for (JsonNode roleNode : rolesNode) {
96 String roleName = roleNode.path("name").asText();
98 TabularAuthRole authRole = new TabularAuthRole();
99 JsonNode usersNode = roleNode.path("users");
100 JsonNode functionsNode = roleNode.path("functions");
101 for (JsonNode functionNode : functionsNode) {
102 String function = functionNode.path("name").asText();
103 JsonNode methodsNode = functionNode.path("methods");
104 boolean hasMethods = false;
105 for (JsonNode methodNode : methodsNode) {
106 String methodName = methodNode.path("name").asText();
108 String thisFunction = methodName + ":" + function;
110 authRole.addAllowedFunction(thisFunction);
113 if (hasMethods == false) {
114 // iterate the list from HTTP_METHODS
115 for (HTTP_METHODS meth : HTTP_METHODS.values()) {
116 String thisFunction = meth.toString() + ":" + function;
118 authRole.addAllowedFunction(thisFunction);
123 for (JsonNode userNode : usersNode) {
124 // make the user lower case
125 String username = userNode.path("username").asText().toLowerCase();
126 SearchDbAuthUser authUser = null;
127 if (users.containsKey(username)) {
128 authUser = users.get(username);
130 authUser = new SearchDbAuthUser();
134 authUser.setUser(username);
135 authUser.addRole(roleName, authRole);
136 users.put(username, authUser);
139 } catch (FileNotFoundException fnfe) {
140 logger.debug("Failed to load the policy file ");
142 } catch (ParseException e) {
143 logger.debug("Failed to Parse the policy file ");
145 } catch (JsonProcessingException e) {
146 logger.debug("JSON processing error while parsing policy file: " + e.getMessage());
148 } catch (IOException e) {
149 logger.debug("IO Exception while parsing policy file: " + e.getMessage());
152 usersInitialized = true;
156 public static class SearchDbAuthUser {
157 public SearchDbAuthUser() {
158 this.roles = new HashMap<String, TabularAuthRole>();
161 private String username;
162 private HashMap<String, TabularAuthRole> roles;
164 public String getUser() {
165 return this.username;
168 public HashMap<String, TabularAuthRole> getRoles() {
172 public void addRole(String roleName, TabularAuthRole authRole) {
173 this.roles.put(roleName, authRole);
176 public boolean checkAllowed(String checkFunc) {
177 for (Map.Entry<String, TabularAuthRole> roleEntry : this.roles.entrySet()) {
178 TabularAuthRole role = roleEntry.getValue();
179 if (role.hasAllowedFunction(checkFunc)) {
180 // break out as soon as we find it
184 // we would have got positive confirmation had it been there
188 public void setUser(String myuser) {
189 this.username = myuser;
194 public static class TabularAuthRole {
195 public TabularAuthRole() {
196 this.allowedFunctions = new ArrayList<String>();
199 private List<String> allowedFunctions;
201 public void addAllowedFunction(String func) {
202 this.allowedFunctions.add(func);
205 public void delAllowedFunction(String delFunc) {
206 if (this.allowedFunctions.contains(delFunc)) {
207 this.allowedFunctions.remove(delFunc);
211 public boolean hasAllowedFunction(String afunc) {
212 if (this.allowedFunctions.contains(afunc)) {
220 public static HashMap<String, SearchDbAuthUser> getUsers(String key) {
221 if (!usersInitialized || (users == null)) {
227 public static boolean authorize(String username, String authFunction) {
229 if (!usersInitialized || (users == null)) {
232 if (users.containsKey(username)) {
233 if (users.get(username).checkAllowed(authFunction) == true) {